通过python代码向chatGPT提问,并接受返回结果打印

由于科研需求

过程

在这里插入图片描述
在这里插入图片描述

结果

步骤:
1 申请API
https://platform.openai.com/docs/api-reference/introduction
在这里插入图片描述

2 得到一个密钥

3 放到上面的python 代码

# -*- coding:utf-8 -*-f
import requests

import openai

openai.api_key = "sk-( 放自己的!!!!!!!)m6C"

prompt = "how are you?"
response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=[
                {"role": "user", "content": prompt},
            ]
        )
answer = response.choices[0].message.content.strip()
print("答:", answer)

4代码报错
openai.error.APIConnectionError: Error communicating with OpenAI: HTTPSConnectionPool
5 debug ,只需要更改版本即可,原理点击这个

如果有人出现了和我同样的问题,解决方案是卸载urllib3然后重装至1.25.11版本再重新运行一遍就可以。

在终端或命令提示符中卸载urllib3
pip uninstall urllib3

然后,您可以通过使用指定版本号的pip install命令来安装所需的版本:
pip install urllib3==1.25.11

成功返回结果
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41517071/article/details/129782471