Chatgpt+python3.7

环境配置(请科学上网+全局代理运行代码!):


```python
##py 3.7
 pip install openai -i https://pypi.tuna.tsinghua.edu.cn/simple
 pip install urllib3==1.25.11 -i https://pypi.tuna.tsinghua.edu.cn/simple

代码测试:

```python
import openai
api_key = " " ##登录官网账户获取
openai.api_key = api_key
path_to_file = “test”
def askChatGPT(messages):
    MODEL = "gpt-3.5-turbo"
    response = openai.ChatCompletion.create(
        model=MODEL,
        messages = messages,
        temperature=1)
    return response['choices'][0]['message']['content']

messages = [{"role": "system","content":"你是一个司机。"}]


text = "如果前面有小学,车速应该限制到多少?"
d = {"role":"user","content":text}
messages.append(d)
text_respon = askChatGPT(messages)
d = {"role":"assistant","content":text_respon}
print('chatgpt:'+text+'\n')
messages.append(d)
print("messages",messages)
log_record = open("log_"+ path_to_file +".txt", mode = "a+", encoding = "utf-8")
print(text_respon, file = log_record)

你可能感兴趣的:(chatgpt,python,开发语言)