togethercomputer/together-typescript
TypeScript
Captured source
source ↗togethercomputer/together-typescript
Description: The official Together AI TypeScript library.
Language: TypeScript
License: Apache-2.0
Stars: 66
Forks: 9
Open issues: 6
Created: 2024-05-10T05:10:08Z
Pushed: 2026-06-10T13:22:43Z
Default branch: main
Fork: no
Archived: no
README:
Together TypeScript API Library
This library provides convenient access to the Together REST API from server-side TypeScript or JavaScript.
The REST API documentation can be found on docs.together.ai. The full API of this library can be found in [api.md](api.md).
It is generated with Stainless.
Installation
npm install together-ai
Usage
The full API of this library can be found in [api.md](api.md).
import Together from 'together-ai';
const client = new Together({
apiKey: process.env['TOGETHER_API_KEY'], // This is the default and can be omitted
});
const chatCompletion = await client.chat.completions.create({
messages: [{ role: 'user', content: 'Say this is a test!' }],
model: 'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo',
});
console.log(chatCompletion.choices);Streaming responses
We provide support for streaming responses using Server Sent Events (SSE).
import Together from 'together-ai';
const client = new Together();
const stream = await client.chat.completions.create({
messages: [{ role: 'user', content: 'Say this is a test!' }],
model: 'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo',
stream: true,
});
for await (const chatCompletionChunk of stream) {
console.log(chatCompletionChunk.choices);
}If you need to cancel a stream, you can break from the loop or call stream.controller.abort().
Request & Response types
This library includes TypeScript definitions for all request params and response fields. You may import and use them like so:
import Together from 'together-ai';
const client = new Together({
apiKey: process.env['TOGETHER_API_KEY'], // This is the default and can be omitted
});
const params: Together.Chat.CompletionCreateParams = {
messages: [{ role: 'user', content: 'Say this is a test' }],
model: 'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo',
};
const chatCompletion: Together.Chat.ChatCompletion = await client.chat.completions.create(params);Documentation for each method, request param, and response field are available in docstrings and will appear on hover in most modern editors.
Handling errors
When the library is unable to connect to the API, or if the API returns a non-success status code (i.e., 4xx or 5xx response), a subclass of APIError will be thrown:
const chatCompletion = await client.chat.completions
.create({
messages: [{ role: 'user', content: 'Say this is a test' }],
model: 'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo',
})
.catch(async (err) => {
if (err instanceof Together.APIError) {
console.log(err.status); // 400
console.log(err.name); // BadRequestError
console.log(err.headers); // {server: 'nginx', ...}
} else {
throw err;
}
});Error codes are as follows:
| Status Code | Error Type | | ----------- | -------------------------- | | 400 | BadRequestError | | 401 | AuthenticationError | | 403 | PermissionDeniedError | | 404 | NotFoundError | | 422 | UnprocessableEntityError | | 429 | RateLimitError | | >=500 | InternalServerError | | N/A | APIConnectionError |
Retries
Certain errors will be automatically retried 2 times by default, with a short exponential backoff. Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict, 429 Rate Limit, and >=501 Internal errors will all be retried by default.
You can use the maxRetries option to configure or disable this:
// Configure the default for all requests:
const client = new Together({
maxRetries: 0, // default is 2
});
// Or, configure per-request:
await client.chat.completions.create({ messages: [{ role: 'user', content: 'Say this is a test' }], model: 'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo' }, {
maxRetries: 5,
});Timeouts
Requests time out after 1 minute by default. You can configure this with a timeout option:
// Configure the default for all requests:
const client = new Together({
timeout: 20 * 1000, // 20 seconds (default is 1 minute)
});
// Override per-request:
await client.chat.completions.create({ messages: [{ role: 'user', content: 'Say this is a test' }], model: 'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo' }, {
timeout: 5 * 1000,
});On timeout, an APIConnectionTimeoutError is thrown.
Note that requests which time out will be [retried twice by default](#retries).
Advanced Usage
Accessing raw Response data (e.g., headers)
The "raw" Response returned by fetch() can be accessed through the .asResponse() method on the APIPromise type that all methods return. This method returns as soon as the headers for a successful response are received and does not consume the response body, so you are free to write custom parsing or streaming logic.
You can also use the .withResponse() method to get the raw Response along with the parsed data. Unlike .asResponse() this method consumes the body, returning once it is parsed.
const client = new Together();
const response = await client.chat.completions
.create({
messages: [{ role: 'user', content: 'Say this is a test' }],
model: 'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo',
})
.asResponse();
console.log(response.headers.get('X-My-Header'));
console.log(response.statusText); // access the underlying Response object
const { data: chatCompletion, response: raw } = await client.chat.completions
.create({
messages: [{ role: 'user', content: 'Say this is a test' }],
model: 'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo',
})
.withResponse();
console.log(raw.headers.get('X-My-Header'));
console.log(chatCompletion.choices);Logging
> [!IMPORTANT] > All log messages are intended for debugging only. The format and content of log messages > may change between releases.
Log levels
The log level can be configured in two ways:
1. Via the TOGETHER_LOG environment variable 2. Using the logLevel client option (overrides the environment variable if set)
import Together from 'together-ai';
const client = new Together({
logLevel: 'debug', // Show all log messages
});Available log levels, from most to least verbose:
'debug'- Show debug…
Excerpt shown — open the source for the full document.