ChatGPT4打造私人英语助手(源码献上)

网址 

https://platform.openai.com/account/api-keys

这个网站里点在“API Keys”页面上,单击“Create New API Key”按钮。

附上python使用代码: 

✨需要注意:

GPT3.5以及4均可以连接上下文。

方式:通过设置api接口中的system,assistant,user的content。

1. system中的content:可以给gpt设定任务,或者拟定预设。

2. assistance中的content:用来存放程序 (gpt) 的回答。

3.user中的content:用来存放用户 (也就是我们) 问出的问题。

import openai

# 初始化参数
openai.api_key = "sk-··················"
Q = "\nQ:"
A = "A:"
model = "gpt-4",
# "gpt-3.5-turbo"
messages = [
    {"role": "system",
     "content": "我现在需要你使用我提供给你的单词(一个或多个单词)形成几个相关联的句子,需要注意单词的形态在句子里的语法,使用句子使用中文,但是单词使用英文。例如:我提供的是ability,difficult,strenuous这几个单词,你给的句子格式应该是:“我们缺乏ability去面对那些非常difficult的事情,比如说那些非常耗时并且strenuous的事情。” 明白了回答ok"},
    {"role": "assistant", "content": "ok"}
]
while True:
    content = input(Q)
    # 将本次的问题提交到messages上下文字典
    messages.append({"role": "user", "content": content})
    # 发送gpt请求
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=0,
        max_tokens=1000,
        stream=True,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0,
    )
    # gpt生成回答
    ans = ''
    for r in response:
        if 'content' in r.choices[0].delta:
            ans += r.choices[0].delta['content']
    print(A, ans)
    # 将本次的gpt的回答提交到messages上下文字典
    messages.append({"role": "assistant", "content": ans.__str__()})

猜你喜欢

转载自blog.csdn.net/m0_56190554/article/details/130193320