Chatgpt (python)

300 阅读1分钟

这段代码展示给予OpenAi的API异步聊天系统,代码分为2个部分:第一部分是主函数chat_async,它使用事件流并返回EventSourceResponse对象

async def chat_async(prompt: str, last_conversation: str = ""):
    async def event_stream():
        async for patch in openai.chat_async(prompt, last_conversation):
            data_dict = {
                "type": "ChatGPT API",
                "response": patch["appendText"]
            }
            yield {
                "event": "message",
                "data": json.dumps(data_dict, ensure_ascii=False)
            }
            await asyncio.sleep(0)

    return EventSourceResponse(event_stream())

这个主要是与OpenAI的API进行交互的异步方法,负责发送请求和处理响应数据

    async def chat_async(self, prompt: str, last_conversation: str):
        headers = {
            'Accept-Encoding': 'utf-8',
            'Authorization': 'Bearer ' + self.sk_token,
            'Content-Type': 'application/json'
        }
        data = {
            "model": "gpt-3.5-turbo",
            "messages": [
                {
                    "role": "assistant",
                    "content": last_conversation
                },
                {
                    "role": "user",
                    "content": prompt
                }
            ],
            "max_tokens": 1024,
            "stream": True
        }
        async with httpx.AsyncClient() as client:
            async with client.stream(method='post', url= 'https://api.openai.com/v1/chat/completions', headers=headers, json=data,
                                     timeout=None) as response:
                if response.status_code != 200:
                    print(response.text)
                    error = Error()
                    error.source = "ChatGPT API"
                    error.code = response.status_code
                    error.message = response.text
                    raise error
                async for line in response.aiter_lines():
                    if line.strip() == "" or line is None:
                        continue
                    if "data: " in line:
                        line = line[6:]
                    if line.replace('[DONE]', '').strip() == "":
                        break

                    // Replace accidentally escaped double quotes
                    line = line.replace('\\"', '"')
                    line = line.replace("\\'", "'")
                    line = line.replace("\\\\", "\\")
                    line = line.replace("\\x", "%")
                   // Try parse JSON
                    try:
                        line = json.loads(line)
                    except json.decoder.JSONDecodeError as e:
                        logging.warning(e)
                        print(line)
                        continue
                    if not self.__check_content(line):
                        if line["choices"] and line["choices"][0]["finish_reason"] != 'stop':
                            logging.warning("Field missing. Details: " + str(line))
                        continue

                    message = line["choices"][0]["delta"]["content"]
                    yield {
                        "appendText": parse.unquote(message)
                    }