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

这个怎么实现连续对话 #8

Closed
sixawnstar opened this issue Feb 17, 2023 · 17 comments
Closed

这个怎么实现连续对话 #8

sixawnstar opened this issue Feb 17, 2023 · 17 comments

Comments

@sixawnstar
Copy link

当前未找到在什么地方添加连续对话选项

@Grt1228
Copy link
Owner

Grt1228 commented Feb 18, 2023

看官网的介绍好像是没有的。也可能我没看到

@coder-ldh
Copy link

看官网的介绍好像是没有的。也可能我没看到

好像是把上一个对话返回的id传进去,我看有人这样操作的

@Grt1228
Copy link
Owner

Grt1228 commented Feb 20, 2023

@coder-ldh @sixawnstar
刚刚翻了下官方文档找到了:https://platform.openai.com/docs/guides/completion/prompt-design

大概就是需要把之前的对话也传入,我测试了下大概是这个样子:

curl https://api.openai.com/v1/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
  "model": "text-davinci-003",
//看这里是一连串的对话,其实我是对话了三次,前面的问题全部被拼接起来了
  "prompt": "\nHuman: Hello,你是谁?\n\nAI: 你好!我是一个AI助理。我可以帮助您完成任务,回答问题和提供智能服务。请问有什么我可以帮您的吗?\nHuman: 帮我翻译一下:我爱你中国\n\nAI: 我爱你中国。\nHuman: 翻译成英文\nAI:\nAI: I love you, China.\n\nHuman: 翻译成韩文\nAI: 나는 너의 중국을 사랑해요.",
  "temperature": 0.9,
  "max_tokens": 150,
  "top_p": 1,
  "frequency_penalty": 0,
  "presence_penalty": 0.6,
  "stop": [" Human:", " AI:"]
}'

本质上还是:

public static void main(String[] args) {
        //配置api keys
        OpenAiClient openAiClient = new OpenAiClient("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",60,60,60);
        CompletionResponse completions = openAiClient.completions("我想申请转专业,从计算机专业转到会计学专业,帮我完成一份两百字左右的申请书");
        Arrays.stream(completions.getChoices()).forEach(System.out::println);
    }

保存之前的对话内容,使用下面的builder方法传入之前的对话内容即可,大致代码如下:

//对话测试
    @Test
    public void completionsV3() {
        String question = "Human: 帮我把下面的文本翻译成英文;我爱你中国\n";
        Completion q = Completion.builder()
                .prompt(question)
                .stop(Arrays.asList(" Human:", " Bot:"))

                .echo(true)
                .build();
        CompletionResponse completions = v2.completions(q);
        String text = completions.getChoices()[0].getText();

        q.setPrompt(text + "\n" + "再翻译成韩文\n");
        completions = v2.completions(q);
        text = completions.getChoices()[0].getText();

        q.setPrompt(text + "\n" + "再翻译成日文\n");
        completions = v2.completions(q);
        text = completions.getChoices()[0].getText();
        System.out.println(text);
    }
Human: 帮我把下面的文本翻译成英文;我爱你中国

Bot: I love you China.
再翻译成韩文

나는 너 중국을 사랑해.
再翻译成日文

私はあなた中国を愛しています。

只是

@Grt1228 Grt1228 pinned this issue Feb 20, 2023
@Grt1228 Grt1228 closed this as completed Feb 22, 2023
@807592732
Copy link

这样不太实用把。如果字数过多呢?

@Toifyx
Copy link

Toifyx commented Mar 2, 2023

目前官方支持 gpt-3.5-turbo 模型 对话了

@a1667834841
Copy link

可以使用 chat啦
Chat completions

@Grt1228
Copy link
Owner

Grt1228 commented Mar 4, 2023

@Toifyx @a1667834841 @807592732 1.0.3版本 已经支持 gpt-3.5-turbo 模型 对话了。可以更新版本试试

@Grt1228 Grt1228 unpinned this issue Mar 7, 2023
@Peins
Copy link

Peins commented Mar 8, 2023

就算是gpt-3.5-turbo 模型,还是得把前文消息传入,才能实现上下文,费钱,还限制字数了

@JsonSong89
Copy link

那如果聊天回合多了之后,传输的数据量岂不是一直会阶梯增长?

@Peins
Copy link

Peins commented Mar 16, 2023

那如果聊天回合多了之后,传输的数据量岂不是一直会阶梯增长?

会增长,但是有token限制,最多传多少吧

@wr345861927
Copy link

已经试了,触发了最大字数限制了
This model's maximum context length is 4097 tokens. However, you requested 4891 tokens (2843 in the messages, 2048 in the completion). Please reduce the length of the messages or completion.

@qujinqiang
Copy link

所以,解决方案是什么?

@atl1028
Copy link

atl1028 commented Apr 19, 2023

session_id

@SnowyLchen
Copy link

session_id

请问下如何使用sessionId实现

@XuanRanDev
Copy link

session_id

session_id如何实现呢?有无demo

@Azrael-CP
Copy link

session_id怎么实现的呀,求告知

@littlesky007
Copy link

根据token,截取掉前面的对话

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

No branches or pull requests