因为对现有的 AI SDK 都不太满意,就自己写了一个——具备 Vercel AI SDK Core 的大部分功能,但体积更小且根据功能分包。
使用例:
pnpm add @xsai/generate-text # 2.7KB minified (1.3KB gzip)
import { generateText } from '@xsai/generate-text'
import { env } from 'node:process'
const { text } = await generateText({
apiKey: env.OPENAI_API_KEY!,
baseURL: 'https://api.openai.com/v1/',
messages: [
{
content: 'You\'re a helpful assistant.',
role: 'system'
},
{
content: 'Why is the sky blue?',
role: 'user'
}
],
model: 'gpt-4o',
})
或者流式传输 + 工具调用:
pnpm add @xsai/stream-text @xsai/tool # 6KB minified (2.5KB gzip)
import { streamText } from '@xsai/stream-text'
import { tool } from '@xsai/tool'
import { env } from 'node:process'
import { description, object, pipe, string } from 'valibot'
const weather = await tool({
description: 'Get the weather in a location',
execute: ({ location }) => JSON.stringify({
location,
temperature: 72 + Math.floor(Math.random() * 21) - 10,
}),
name: 'weather',
parameters: object({
location: pipe(
string(),
description('The location to get the weather for'),
),
}),
})
const { textStream } = await streamText({
apiKey: env.OPENAI_API_KEY!,
baseURL: 'https://api.openai.com/v1/',
messages: [
{
content: 'You are a helpful assistant.',
role: 'system',
},
{
content: 'What is the weather in San Francisco?',
role: 'user',
},
],
model: 'gpt-4o',
tools: [weather],
})
for await (const textPart of textStream) {
console.log(textPart)
}
或者流式传输对象(并安装整包):
pnpm add xsai # 16KB minified (5.7KB gzip)
import { type } from 'arktype'
import { streamObject } from 'xsai'
import { env } from 'node:process'
const { partialObjectStream } = await streamObject({
apiKey: env.OPENAI_API_KEY!,
baseURL: 'https://api.openai.com/v1/',
messages: [
{
content: 'Extract the event information.',
role: 'system'
},
{
content: 'Alice and Bob are going to a science fair on Friday.',
role: 'user'
}
],
model: 'gpt-4o',
schema: type({
name: 'string',
date: 'string',
participants: 'string[]',
}),
})
for await (const partialObject of partialObjectStream) {
console.log(partialObject)
}
一些链接:
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.