在 OpenAI API 中,如何以编程方式检查响应是否不完整?如果是这样,您可以添加另一个命令,例如“继续”或“扩展”,或者以编程方式完美地继续它。
\n根据我的经验,\n我知道如果响应不完整,API 将返回:
\n"finish_reason": "length"\nRun Code Online (Sandbox Code Playgroud)\n但如果响应超过 4000 个令牌,它就不起作用,因为您还需要将先前的响应(对话)传递给新的响应(对话)。如果响应是 4500,它将返回 4000 个令牌,但您无法获取剩余的 500 个令牌,因为每个对话的最大令牌是 4000 个令牌。如果我错了请纠正我。
\n这是我的代码,请注意,提示只是一个示例提示。实际上,我的提示也很长,因为我还无法微调 gpt 3.5,我需要根据我的提示来训练它。
\ndef chat_openai(prompt) -> dict:\n\n conversation = [{'role': 'user', 'content': prompt}]\n response, answer = None, ''\n for idx, api_key in enumerate(openai_api_keys):\n try:\n openai.api_key = api_key\n response = openai.ChatCompletion.create(model='gpt-3.5-turbo', messages=conversation, temperature=1)\n answer += response.choices[0].message.content\n conversation.append({'role': response.choices[0].message.role, 'content': answer})\n # Move successful API at the start of array\n if idx: openai_api_keys[0], openai_api_keys[idx] = openai_api_keys[idx], openai_api_keys[0]\n break\n except …Run Code Online (Sandbox Code Playgroud) python artificial-intelligence machine-learning openai-api chatgpt-api