V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
oyp
V2EX  ›  程序员

Node.js 调用 ChatGPT 并实现流式传输

  •  
  •   oyp · Jul 27, 2023 · 959 views
    This topic created in 1006 days ago, the information mentioned may be changed or developed.
    import {
        OpenAIApi,
        Configuration,
        CreateChatCompletionResponse
    } from 'openai'
    import process from 'process'
    
    const configuration = new Configuration({
        apiKey: 'sk-xxxxxxxxxxxx'
    })
    const openai = new OpenAIApi(configuration)
    openai.createChatCompletion({
        model: 'gpt-3.5-turbo',
        messages: [
            { role: 'user', content: '请模仿李白写一首诗' }
        ],
        stream: true
    }, { responseType: 'stream' }).then(value => {
        const data = value.data as ChatData
        data.on('data', chunk => {
            const match = chunk.toString().match(/^data: (.*)/)
            if (!match || match[1] == '[DONE]') return
            const res = JSON.parse(match[1])
            const { content } = res.choices[0].delta
            if (!content) return
            process.stdout.write(content)
        })
    })
    
    interface ChatData extends CreateChatCompletionResponse {
        /** 绑定事件 */
        on<K extends keyof EventType>(
            eventType: K,
            handle: (event: EventType[K]) => void
        ): void
    }
    
    interface EventType {
        'data': Buffer
    }
    
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5954 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 02:13 · PVG 10:13 · LAX 19:13 · JFK 22:13
    ♥ Do have faith in what you're doing.