当前位置:   article > 正文

通过python代码向chatGPT提问,并接受返回结果打印_python调用chatgpt接口回答网页上问题

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/368690
推荐阅读
相关标签
  

闽ICP备14008679号