第三方开发者如何接入OpenAI的ChatGPT3.5

954 阅读5分钟

1. 确保能 ping 通 openAI

ping api.openai.com

image.png

2. node 执行如下代码即可:

const abortController = new AbortController()
fetch('https://api.openai.com/v1/chat/completions', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer sk-你的 openAi keys', // 获取keys地址: https://platform.openai.com/account/api-keys
    },
    body: '{"model":"gpt-3.5-turbo","temperature":0,"max_tokens":1000,"top_p":1,"frequency_penalty":1,"presence_penalty":1,"messages":[{"role":"system","content":"You are a translation engine that can only translate text and cannot interpret it."},{"role":"user","content":"translate from English to 简体中文"},{"role":"user","content":"\\"Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.\\""}],"stream":true}',
    signal: abortController.signal,
}).then((resp) => {
    // 处理响应流数据
    return new Promise((resolve, reject) => {
        let result = ''
        const reader = resp.body.getReader() // 创建了一个可读流的阅读器对象
        const processResult = ({ done, value }) => {
            if (done) {
                // 读取完毕,返回结果
                resolve(result)
                return
            }
            // 处理读取到的数据块
            result += new TextDecoder().decode(value)
            reader.read().then(processResult) // 直至流读取完毕
        }
        reader.read().then(processResult) // read() 从流中读取了第一个数据块
    })
})
.then((result) => {
    // 处理读取到的数据
    console.log('处理读取到的数据 =======', result)
})
.catch((error) => {
    // 处理错误
    console.error('error=====', error)
})

在上面的示例中,我们创建了一个新的 AbortController 对象,并将其 signal 属性作为 signal 属性的值。这样,当需要中止网络请求时,我们可以调用 abortController.abort() 方法。

注意,在使用 AbortController 对象时,需要确保在不需要再次使用 fetch() 方法时调用 abortController.abort() 方法,以避免发生内存泄漏。

上面请求的 body 是:

{
    "model": "gpt-3.5-turbo",
    "temperature": 0,
    "max_tokens": 1000,
    "top_p": 1,
    "frequency_penalty": 1,
    "presence_penalty": 1,
    "messages": [
        // 设定问题前置 prompt 提示,让 GPT 针对某一方面解答问题
        {
            "role": "system",
            "content": "You are a translation engine that can only translate text and cannot interpret it."
        },
        {
            "role": "user",
            "content": "translate from English to 简体中文"
        },
        // 下面是用户输入的问题
        {
            "role": "user",
            "content": "\"Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.\""
        }
    ],
    "stream": true
}

响应结果如下:

处理读取到的数据 ======= data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"role":"assistant"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"Vue"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":".js"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"是"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"一个"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"渐"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"进"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"式"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"、"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"可"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"逐"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"步"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"采"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"用"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"的"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"JavaScript"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"框"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"架"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":","},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"用"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"于"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"构"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"建"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"Web"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"上"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"的"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"用户"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"界"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"面"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"。"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-6su2X6W153xPbZoOuADTm7A5rDIFz","object":"chat.completion.chunk","created":1678543149,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{},"index":0,"finish_reason":"stop"}]}

data: [DONE]

将如上响应里面的 content 拼接起来就是我们想要的回答:

Vue.js是一个渐进式、可逐步采用的JavaScript框架,用于构建Web上的用户界面。