gpt4 book ai didi

python-3.x - Python API : No dialogue considered, 中的 ChatGPT 孤立问答,没有历史记录

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

我正在使用此代码在 Python 中连接到 ChatGPT。我得到的是一个终端,类似于网络版本。然而,问题是 ChatGPT 似乎并没有从对话中学习。例如,当我问“巴黎的埃菲尔铁塔有多高?”时,它会正确回答。但当我接着问“1955 年有多高?”时,ChatGPT 无法理解上下文,也不知道我们仍在谈论埃菲尔铁塔。此外,我在输出部分对话时遇到问题。”

import openai

class ChatGPT:
def __init__(self, api_key,rolle):
# Set the OpenAI API key
openai.api_key=api_key
# Initialize the dialog list and create the first element with the system role and the passed role
self.dialog=[{"role":"system","content":rolle}]

def fragen(self, frage):
# Create a new dictionary with the role "user" and the content of the question
neue_frage = {"role":"user","content":frage}
# Add the new dictionary to the dialog list
self.dialog.append(neue_frage)
# Perform an OpenAI API call and retrieve the response
ergebnis = openai.Completion.create(
engine="gpt-3.5-turbo",
prompt=self.dialog,
max_tokens=1024,
n=1,
stop=None,
temperature=0.5,
)
# Extract the response from the API response
antwort = ergebnis.choices[0].text
# Create a new dictionary with the role "assistant" and the content of the answer
neue_antwort = {"role":"assistant","content":antwort}
# Add the new dictionary to the dialog list
self.dialog.append(neue_antwort)
# Update the dialog list to use the last two added dictionaries as input for the next request
self.dialog[-1]["prompt"] = True
# Return the answer and updated dialog list
return antwort, self.dialog

# Load the API key from a file
with open('api.key', 'r') as api_key:
API_KEY=api_key.read()

# Create a ChatGPT instance
chat_gpt=ChatGPT(API_KEY,"be a code terminal")

# Loop to receive questions from the user and receive responses from ChatGPT
while (frage := input('\n> ')) != 'X':
antwort, dialog=chat_gpt.fragen(frage)
# Print the response
print(antwort)
# Print the dialog list
print(dialog[-2:]) # Output the last two elements (user question and assistant response) of the dialog list
enter code here

最佳答案

看起来您发送了 history of conversation

尝试添加 user fieldopenai.Completion.create 中,将 openai.Completion.create 更改为 openai.ChatCompletion.create

关于python-3.x - Python API : No dialogue considered, 中的 ChatGPT 孤立问答,没有历史记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76027516/

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