Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在使用chatgpt api时,支持发送上下文 #42

Merged
merged 4 commits into from
Mar 4, 2023

Conversation

jtc1246
Copy link
Contributor

@jtc1246 jtc1246 commented Mar 2, 2023

No description provided.

@yihong0618
Copy link
Owner

will take a look tomorrow.

@yihong0618
Copy link
Owner

感谢 PR 哈,有些小问题你可以先改一下。

  1. 需要把你 # added by jtc 这样的注释去掉
  2. 你搜索下 black 用 black 格式化下代码(这个我稍后会加 CI)
  3. if(len(self.history)>=5): l=len(self.history) 这块我没太看懂为啥,我明天有时间会研究一下
  4. 感觉可以不写入文件的?

@jtc1246
Copy link
Contributor Author

jtc1246 commented Mar 2, 2023

l=len(self.history)这行代码确实没用。if(len(self.history)>=5)是为了最多只发送5条历史对话,否则可能超过4096token,也会浪费钱。写入文件是没有必要的,只是我为了方便查看。

ms.append({"role": "user", "content": h[0]})
ms.append({"role": "assistant", "content": h[1]})
ms.append({"role": "user", "content": f"{query}"})
completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=ms)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's recommend to define the model as constant, it will move to the config.json later

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And how about this style

ms = []
if len(self.history) >= 5:
    for i in range(-5, 0):
        ms.append({"role": "user", "content": self.history[i][0]})
        ms.append({"role": "assistant", "content": self.history[i][1]})
else:
    for h in self.history:
        ms.append({"role": "user", "content": h[0]})
        ms.append({"role": "assistant", "content": h[1]})
ms.append({"role": "user", "content": f"{query}"})
completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=ms)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if self.history is a list, you can use self.history[-5:]

ms = []
for h in self.history[-5:]:
    ms.append({"role": "user", "content": h[0]})
    ms.append({"role": "assistant", "content": h[1]})
ms.append({"role": "user", "content": f"{query}"})
completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=ms)

@yihong0618
Copy link
Owner

@pjq 这个需要一些修改,但我可能会先合了自己改。
@jtc1246 没问题哈?

@yihong0618 yihong0618 merged commit 7249bc8 into yihong0618:main Mar 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants