Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat (packages/openai-compatible): Base for OpenAI-compatible providers. #3812

Merged
merged 6 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/beige-boats-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/openai-compatible': patch
---

feat (packages/openai-compatible): Base for OpenAI-compatible providers.
1 change: 1 addition & 0 deletions examples/ai-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@ai-sdk/groq": "1.0.3",
"@ai-sdk/mistral": "1.0.3",
"@ai-sdk/openai": "1.0.4",
"@ai-sdk/openai-compatible": "0.0.0",
"@ai-sdk/xai": "1.0.3",
"@opentelemetry/sdk-node": "0.54.2",
"@opentelemetry/auto-instrumentations-node": "0.47.0",
Expand Down
27 changes: 27 additions & 0 deletions examples/ai-core/src/embed-many/openai-compatible-togetherai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'dotenv/config';
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { embedMany } from 'ai';

async function main() {
const togetherai = createOpenAICompatible({
baseURL: 'https://api.together.xyz/v1',
name: 'togetherai',
headers: {
Authorization: `Bearer ${process.env.TOGETHER_AI_API_KEY}`,
},
});
const model = togetherai.textEmbeddingModel('BAAI/bge-large-en-v1.5');
const { embeddings, usage } = await embedMany({
model,
values: [
'sunny day at the beach',
'rainy afternoon in the city',
'snowy night in the mountains',
],
});

console.log(embeddings);
console.log(usage);
}

main().catch(console.error);
23 changes: 23 additions & 0 deletions examples/ai-core/src/embed/openai-compatible-togetherai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'dotenv/config';
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { embed } from 'ai';

async function main() {
const togetherai = createOpenAICompatible({
baseURL: 'https://api.together.xyz/v1',
name: 'togetherai',
headers: {
Authorization: `Bearer ${process.env.TOGETHER_AI_API_KEY}`,
},
});
const model = togetherai.textEmbeddingModel('BAAI/bge-large-en-v1.5');
const { embedding, usage } = await embed({
model,
value: 'sunny day at the beach',
});

console.log(embedding);
console.log(usage);
}

main().catch(console.error);
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'dotenv/config';
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { generateObject } from 'ai';
import { z } from 'zod';

async function main() {
const togetherai = createOpenAICompatible({
baseURL: 'https://api.together.xyz/v1',
name: 'togetherai',
headers: {
Authorization: `Bearer ${process.env.TOGETHER_AI_API_KEY}`,
},
});
const model = togetherai.chatModel('mistralai/Mistral-7B-Instruct-v0.1');
const result = await generateObject({
model,
schema: z.object({
recipe: z.object({
name: z.string(),
ingredients: z.array(
z.object({
name: z.string(),
amount: z.string(),
}),
),
steps: z.array(z.string()),
}),
}),
prompt: 'Generate a lasagna recipe.',
});

console.log(JSON.stringify(result.object.recipe, null, 2));
console.log();
console.log('Token usage:', result.usage);
console.log('Finish reason:', result.finishReason);
}

main().catch(console.error);
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'dotenv/config';
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { generateText } from 'ai';
import fs from 'node:fs';

async function main() {
const openai = createOpenAICompatible({
baseURL: 'https://api.openai.com/v1',
name: 'openai',
headers: {
Authorization: `Bearer ${process.env.TOGETHER_AI_API_KEY}`,
},
});
const model = openai.chatModel('gpt-4o-mini');
const result = await generateText({
model,
messages: [
{
role: 'user',
content: [
{ type: 'text', text: 'Describe the image in detail.' },
{ type: 'image', image: fs.readFileSync('./data/comic-cat.png') },
],
},
],
});

console.log(result.text);
}

main().catch(console.error);
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import 'dotenv/config';
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { generateText, tool } from 'ai';
import { z } from 'zod';
import { weatherTool } from '../tools/weather-tool';

async function main() {
const togetherai = createOpenAICompatible({
baseURL: 'https://api.together.xyz/v1',
name: 'togetherai',
headers: {
Authorization: `Bearer ${process.env.TOGETHER_AI_API_KEY}`,
},
});
const model = togetherai.chatModel(
'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo',
);
const result = await generateText({
model,
maxTokens: 512,
tools: {
weather: weatherTool,
cityAttractions: tool({
parameters: z.object({ city: z.string() }),
}),
},
prompt:
'What is the weather in San Francisco and what attractions should I visit?',
});

// typed tool calls:
for (const toolCall of result.toolCalls) {
switch (toolCall.toolName) {
case 'cityAttractions': {
toolCall.args.city; // string
break;
}

case 'weather': {
toolCall.args.location; // string
break;
}
}
}

// typed tool results for tools with execute method:
for (const toolResult of result.toolResults) {
switch (toolResult.toolName) {
// NOT AVAILABLE (NO EXECUTE METHOD)
// case 'cityAttractions': {
// toolResult.args.city; // string
// toolResult.result;
// break;
// }

case 'weather': {
toolResult.args.location; // string
toolResult.result.location; // string
toolResult.result.temperature; // number
break;
}
}
}

console.log('Text:', result.text);
console.log('Tool Calls:', JSON.stringify(result.toolCalls, null, 2));
console.log('Tool Results:', JSON.stringify(result.toolResults, null, 2));
}

main().catch(console.error);
25 changes: 25 additions & 0 deletions examples/ai-core/src/generate-text/openai-compatible-togetherai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'dotenv/config';
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { generateText } from 'ai';

async function main() {
const togetherai = createOpenAICompatible({
baseURL: 'https://api.together.xyz/v1',
name: 'togetherai',
headers: {
Authorization: `Bearer ${process.env.TOGETHER_AI_API_KEY}`,
},
});
const model = togetherai.chatModel('meta-llama/Llama-3-70b-chat-hf');
const result = await generateText({
model,
prompt: 'Invent a new holiday and describe its traditions.',
});

console.log(result.text);
console.log();
console.log('Token usage:', result.usage);
console.log('Finish reason:', result.finishReason);
}

main().catch(console.error);
41 changes: 41 additions & 0 deletions examples/ai-core/src/stream-object/openai-compatible-togetherai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'dotenv/config';
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { streamObject } from 'ai';
import { z } from 'zod';

async function main() {
const togetherai = createOpenAICompatible({
baseURL: 'https://api.together.xyz/v1',
name: 'togetherai',
headers: {
Authorization: `Bearer ${process.env.TOGETHER_AI_API_KEY}`,
},
});
const model = togetherai.chatModel('mistralai/Mistral-7B-Instruct-v0.1');
const result = streamObject({
model,
schema: z.object({
characters: z.array(
z.object({
name: z.string(),
class: z
.string()
.describe('Character class, e.g. warrior, mage, or thief.'),
description: z.string(),
}),
),
}),
prompt:
'Generate 3 character descriptions for a fantasy role playing game.',
});

for await (const partialObject of result.partialObjectStream) {
console.clear();
console.log(partialObject);
}

console.log();
console.log('Token usage:', await result.usage);
}

main().catch(console.error);
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import 'dotenv/config';
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { streamText, CoreMessage, ToolCallPart, ToolResultPart } from 'ai';
import { weatherTool } from '../tools/weather-tool';

const messages: CoreMessage[] = [];

async function main() {
let toolResponseAvailable = false;

const togetherai = createOpenAICompatible({
baseURL: 'https://api.together.xyz/v1',
name: 'togetherai',
headers: {
Authorization: `Bearer ${process.env.TOGETHER_AI_API_KEY}`,
},
});
const model = togetherai.chatModel(
'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo',
);
const result = streamText({
model,
maxTokens: 512,
tools: {
weather: weatherTool,
},
toolChoice: 'required',
prompt:
'What is the weather in San Francisco and what attractions should I visit?',
});

let fullResponse = '';
const toolCalls: ToolCallPart[] = [];
const toolResponses: ToolResultPart[] = [];

for await (const delta of result.fullStream) {
switch (delta.type) {
case 'text-delta': {
fullResponse += delta.textDelta;
process.stdout.write(delta.textDelta);
break;
}

case 'tool-call': {
toolCalls.push(delta);

process.stdout.write(
`\nTool call: '${delta.toolName}' ${JSON.stringify(delta.args)}`,
);
break;
}

case 'tool-result': {
toolResponses.push(delta);

process.stdout.write(
`\nTool response: '${delta.toolName}' ${JSON.stringify(
delta.result,
)}`,
);
break;
}
}
}
process.stdout.write('\n\n');

messages.push({
role: 'assistant',
content: [{ type: 'text', text: fullResponse }, ...toolCalls],
});

if (toolResponses.length > 0) {
messages.push({ role: 'tool', content: toolResponses });
}

toolResponseAvailable = toolCalls.length > 0;
console.log('Messages:', messages[0].content);
}

main().catch(console.error);
32 changes: 32 additions & 0 deletions examples/ai-core/src/stream-text/openai-compatible-togetherai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'dotenv/config';
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { streamText } from 'ai';

async function main() {
const togetherai = createOpenAICompatible({
baseURL: 'https://api.together.xyz/v1',
name: 'togetherai',
headers: {
Authorization: `Bearer ${process.env.TOGETHER_AI_API_KEY}`,
},
});
const model = togetherai.chatModel(
'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo',
);
const result = streamText({
model,
prompt:
'List the top 5 San Francisco news from the past week.' +
'You must include the date of each article.',
});

for await (const textPart of result.textStream) {
process.stdout.write(textPart);
}

console.log();
console.log('Token usage:', await result.usage);
console.log('Finish reason:', await result.finishReason);
}

main().catch(console.error);
1 change: 1 addition & 0 deletions packages/openai-compatible/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @ai-sdk/openai-compatible
Loading
Loading