- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用此代码在 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 field在 openai.Completion.create
中,将 openai.Completion.create
更改为 openai.ChatCompletion.create
关于python-3.x - Python API : No dialogue considered, 中的 ChatGPT 孤立问答,没有历史记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76027516/
我在我的项目中运行 pylint 进行错误检测,并偶然发现了这个警告。 我该如何解决这个警告? 最佳答案 假设您正在打开一个文件: file_handle = open("some_file.txt"
我们都知道 Jquery 有很多好处。如果您在企业中使用过Jquery,您的“学习”经历是什么?; 为了改进和利用 Jquery 的优势,需要考虑向后兼容性、与现有 javascript 的冲突、CS
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 4 年前。 Improve
我想用 div 模拟表格的行为。 我的布局结构分为三列: div#wrapper { width:800px; float:left; height:100%; margin-top:
给定一家软件公司,开发人员团队合作处理多个问题不同的项目。项目需要分配的特定技能开发商。出于我的目的,我想保持简单并将其限制为一个技能,即编程语言。所以有些项目需要Java,有些需要C 等 项目有固定
我正在使用两个函数 sprintf 和 snprintf 来处理“double”到字符串的转换,在其中一种情况下,正在运行的应用程序的语言环境与 Windows 的语言环境不同。因此,在这种情况下,s
我正在尝试实现一个 LocaleListener 来检测用户的首选语言(考虑 Accept-Language header )并将其存储在 session 中以避免每次请求都检查它。我开发了下面的代码
我正在尝试运行如下查询: MATCH (n:Type1)-[:relation1]->(:Type1)(:Type1)(:Type1)(:Type1)<-[:relation2]-(:Type2) W
嗨,我想知道在 JSP 中使用循环的行为 在foreach周围,集合的每个值都在内部 它显示四行还是三行?它是否每次都考虑相同的值(例如“1”)? 1 1 2 3 或 1 2 3
这里是 Rust 新手。当提供一个参数并在函数声明中将其保留为未使用时(例如,在学习 Rust 时...),编译器会警告该变量在范围内未使用的事实,并建议考虑在其前加下划线。这样做,警告就会消失。 w
同时关注 rustbyexample.com教程中,我输入了以下代码: impl fmt::Display for Structure { fn fmt(&self, f: &mut fmt:
以 void * 和其他指针类型作为参数的 C++ 多态函数:它是否被认为是有歧义的? 我担心,因为任何指针都可以转换为 void*,下面的 bar 的第二次调用是否会执行 void bar(void
所以我有一个程序可以在一行中从用户那里获取数字,例如: 2 1 2 3 4 第一个数字:2表示输出应该是2行2列的矩阵: 1 2 3 4 这就是我填充数组的方式: int dim; scanf("%d
我想在我的博客上做一些类似于 gmail 的“考虑包括” 建议的事情,但带有标签。 我正在考虑像这样存储标签集: 我想到了以下算法: //a blog post is published //it h
我想看看 stackoverflow 是否使用单独的插件来执行那些亮黄色的弹出窗口,上面写着“请考虑将此答案标记为已接受 或“请考虑添加评论以说明您为什么投了反对票” 这是为此使用了一个 jquery
我尝试构建 a pure CSS tree .我遇到了 block 之间水平线的问题(两个 block 在同一层)。我在以下 jsfiddles 中隔离了问题: https://jsfiddle.ne
我正在准备 Django 中的测试或测验。测验需要在一定的时间范围内完成。说 40 题 30 分钟。我总是可以在测试开始时启动时钟,然后在测验完成时计算时间。然而,在尝试过程中,可能会出现互联网连接中
在MSDN ,我找到了这句话: The result of a ?? operator is not considered to be a constant even if both its argu
我有一个项目,我启用了新的 Nullable reference type feature enable 现在让我们考虑这段代码 public class Foo { } var foo =
我正在编写一个应该可缓存的 Webpack 加载器。 documentation for this.cacheable说: A cacheable loader must have a determi
我是一名优秀的程序员,十分优秀!