The AI21 API Client is a TypeScript library that provides a convenient interface for interacting with the AI21 API. It abstracts away the low-level details of making API requests and handling responses, allowing developers to focus on building their applications.
This client supports both Node.js and browser environments:
- Node.js: Works out of the box with Node.js >=18.0.0
- Browser: Requires explicit opt-in by setting
dangerouslyAllowBrowser: true
in the client options
// Browser usage example
const client = new AI21({
apiKey: process.env.AI21_API_KEY, // or pass it in directly
dangerouslyAllowBrowser: true // Required for browser environments
});
⚠️ Security Notice: Using this client in the browser could expose your API key to end users. Only enabledangerouslyAllowBrowser
if you understand the security implications and have implemented appropriate security measures.
You can install the AI21 API Client using npm or yarn:
npm install ai21
or
yarn add ai21
If you want to quickly get a glance how to use the AI21 Typescript SDK and jump straight to business, you can check out the examples. Take a look at our models and see them in action! Several examples and demonstrations have been put together to show our models' functionality and capabilities.
Feel free to dive in, experiment, and adapt these examples to suit your needs. We believe they'll help you get up and running quickly.
The full documentation for the REST API can be found on docs.ai21.com.
To use the AI21 API Client, you'll need to have an API key. You can obtain an API key by signing up for an account on the AI21 website.
The AI21
class provides a chat
property that gives you access to the Chat API. You can use this to generate text, complete prompts, and more.
Here's an example of how to use the AI21
class to interact with the API:
import { AI21 } from 'ai21';
const client = new AI21({
apiKey: process.env.AI21_API_KEY, // or pass it in directly
});
const response = await client.chat.completions.create({
model: 'jamba-1.5-mini',
messages: [{ role: 'user', content: 'Hello, how are you? tell me a 100 line story about a cat named "Fluffy"' }],
});
console.log(response);
The client supports streaming responses for real-time processing. Here are examples using different approaches:
const streamResponse = await client.chat.completions.create({
model: 'jamba-1.5-mini',
messages: [{ role: 'user', content: 'Write a story about a space cat' }],
stream: true,
});
for await (const chunk of streamResponse) {
console.log(chunk.choices[0]?.delta?.content || '');
}
The AI21
class provides a files
property that gives you access to the Files API. You can use it to upload, retrieve, update, list, and delete files.
import { AI21 } from 'ai21';
const client = new AI21({
apiKey: process.env.AI21_API_KEY, // or pass it in directly
});
const fileUploadResponse = await client.files.create({
file: './articles/article1.pdf',
labels: ['science', 'biology'],
path: 'virtual-path/to/science-articles',
});
const file = await client.files.get(fileUploadResponse.fileId);
The AI21
class provides a conversationalRag
property that gives you access to the Conversational RAG API. You can use it to ask questions that are answered based on the files you uploaded.
import { AI21 } from 'ai21';
const client = new AI21({
apiKey: process.env.AI21_API_KEY, // or pass it in directly
});
const convRagResponse = await client.conversationalRag.create({
messages: [{ role: 'user', content: 'This question presumes that the answer can be found within the uploaded files.' }],
});
The AI21
class accepts several configuration options, which you can pass in when creating a new instance:
baseURL
: The base URL for the API endpoint (default:https://api.ai21.com/studio/v1
)apiKey
: Your AI21 API KeymaxRetries
: The maximum number of retries for failed requests (default:3
)timeout
: The request timeout in secondsdangerouslyAllowBrowser
: Set totrue
to allow the client to be used in a browser environment.
For detailed information about the available methods and their parameters, please refer to the API reference documentation.
If you encounter any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request on the GitHub repository.
This library is licensed under the Apache-2.0 License.