gpt4 book ai didi

openai-api - 如何记住来自不同 ChatCompletion 实例的消息

转载 作者:行者123 更新时间:2023-12-02 22:47:28 25 4
gpt4 key购买 nike

我正在尝试使用 chatgpt api 运行一些命令,但它并没有运行所有内容,因此我创建了另一个 ChatCompletion 实例。如何让 chatgpt 记住我在先前实例中发送的提示?这是我的代码:

data = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": "Generate a title"},
{"role": "user", "content": "Generate 30 chapters"},
]
)

data2 = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": "Generate chapter 1"},
])

最佳答案

为了能够使模型保持在上下文中,您必须将以前的对话与当前提示一起发送。

[注意:对话历史记录必须小于 token 限制才能预期模型的正确行为,为避免这种情况,您可以在第 n 个响应后总结对话或使用文本嵌入]

这里是一个示例代码:

import openai
import time

# Set your OpenAI API key
openai.api_key = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxx"

# Define the engine and the query
engine = "gpt-3.5-turbo"
query = "Generate a title"

# Start a new conversation with an empty list of messages
messages = []

# Call the Chat Completion API to generate a title
data = openai.ChatCompletion.create(
engine=engine,
query=query,
messages=messages
)

# Print the generated title
title = data["message"]
print(title)

# Update the messages with the title
messages.append({"role": "assistant", "content": title})

# Define a function to generate chapters
def generate_chapters(n):
# Loop for n times
for i in range(n):
# Generate a query for each chapter
query = f"Generate chapter {i+1}"
# Call the Chat Completion API to generate a chapter
data = openai.ChatCompletion.create(
engine=engine,
query=query,
messages=messages
)
# Print the generated chapter
chapter = data["message"]
print(chapter)
# Update the messages with the chapter
messages.append({"role": "assistant", "content": chapter})
# Wait for 10 seconds before generating the next chapter
time.sleep(10)

# Generate 30 chapters
generate_chapters(30)


关于openai-api - 如何记住来自不同 ChatCompletion 实例的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76813435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com