V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
M9l7chlLGv
V2EX  ›  分享创造

xsAI - 包体积小于 6KB 的 Vercel AI SDK 平替

  •  2
     
  •   M9l7chlLGv · 24 天前 · 1193 次点击

    xsai@0.1.1

    因为对现有的 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)
    }
    

    一些链接:

    2 条回复    2025-03-08 21:55:04 +08:00
    cosyu
        1
    cosyu  
       24 天前 via Android   ❤️ 1
    支持~
    liuweifeng
        2
    liuweifeng  
       23 天前   ❤️ 1
    支持~下一个个人项目试用一下~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   4076 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 20ms · UTC 04:09 · PVG 12:09 · LAX 21:09 · JFK 00:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.