因为对现有的 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)
}
一些链接:
![]() |
1
cosyu 23 天前 via Android ![]() 支持~
|
![]() |
2
liuweifeng 22 天前 ![]() 支持~下一个个人项目试用一下~
|