OpenAI compatible ProvidersTogether.ai
Together.ai Provider
Together.ai is a platform for running and testing LLMs. It offers an OpenAI compatible API that you can use with the AI SDK.
Setup
The Together.ai provider is available via the @ai-sdk/openai
module as it is compatible with the OpenAI API.
You can install it with
pnpm add @ai-sdk/openai
Provider Instance
To use Together.ai, you can create a custom provider instance with the createOpenAI
function from @ai-sdk/openai
:
import { createOpenAI } from '@ai-sdk/openai';
const togetherai = createOpenAI({ name: 'togetherai', apiKey: process.env.TOGETHER_AI_API_KEY ?? '', baseURL: 'https://api.together.xyz/v1/',});
Language Models
You can create Together.ai models using a provider instance.
The first argument is the model id, e.g. google/gemma-2-9b-it
.
const model = togetherai('google/gemma-2-9b-it');
Example
You can use Together.ai language models to generate text with the generateText
function:
import { createOpenAI } from '@ai-sdk/openai'import { generateText } from 'ai'
const togetherai = createOpenAI({ name: 'togetherai', apiKey: process.env.TOGETHER_AI_API_KEY ?? '', baseURL: 'https://api.together.xyz/v1/'})
const { text } = await generateText({ model: togetherai('google/gemma-2-9b-it') prompt: 'Write a vegetarian lasagna recipe for 4 people.'})
Together.ai language models can also be used in the streamText
function.