当前位置:   article > 正文

【记录】OpenAI|Python调用GPT API的开发环境及代码(2024/06/21实测)

python调用gpt

本文默认读者已经有API Keys,并默认读者对Python环境较为熟悉,对相关内容不予介绍。
更新时间:2024/03/21

国内安装的时候偶尔会出现各种问题,
这篇记录是记录当下可用的一个方式。

近期更新(2024/06/21)

现在openai更新了,python高版本也支持了,直接运行这行指令试试吧:

pip install openai --upgrade
  • 1

安装(2024/03/21)

参考:python 安装openai的踩坑史

官网的介绍是pip install openai。不过它没说Python版本最好<=3.8。

若Python版本过高,到达了3.11,会出现以下报错:

ImportError: cannot import name 'OpenAI' from 'openai'
  • 1

因此请直接使用Python==3.8。

若有Anaconda

安装完整过程推荐:

conda create -n openai-demo python=3.8
conda activate openai-demo
pip install openai==1.14.2 # 或者conda install openai
  • 1
  • 2
  • 3

若openai装不上就换国内清华的源,或者关掉代理。

若只有Python环境

Python官网下一个Python3.8
然后在安装路径进行后续操作:

pip install openai==1.14.2
  • 1

经验之谈:
建议装VScode配合Python插件去管理并切换Python环境,
不建议直接莽改系统环境变量。

使用

获取样例

为了使我们的GPT API返回的结果更符合我们的需求,我们可以对GPT的角色进行自定义。而GPT自带非常多种类的自定义模板,其网址是:Prompt Example

选用你想要的角色即可,例如我需要内容提取,就选了一种内容提取的模板,然后复制了代码。
在这里插入图片描述
我复制的代码如下:

from openai import OpenAI
client = OpenAI()

response = client.chat.completions.create(
  model="gpt-3.5-turbo",
  messages=[
    {
      "role": "system",
      "content": "You will be provided with unstructured data, and your task is to parse it into CSV format."
    },
    {
      "role": "user",
      "content": "There are many fruits that were found on the recently discovered planet Goocrux. There are neoskizzles that grow there, which are purple and taste like candy. There are also loheckles, which are a grayish blue fruit and are very tart, a little bit like a lemon. Pounits are a bright green color and are more savory than sweet. There are also plenty of loopnovas which are a neon pink flavor and taste like cotton candy. Finally, there are fruits called glowls, which have a very sour and bitter taste which is acidic and caustic, and a pale orange tinge to them."
    }
  ],
  temperature=0.7,
  max_tokens=64,
  top_p=1
)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

这个时候的代码还是不能运行的,
还需要设置以下两个内容。

设置API Keys

API Key可以设置为系统环境变量,具体方式是新建一个名为OPENAI_API_KEY的环境变量,如下图:
在这里插入图片描述

也可以写在代码里面:

OpenAI(api_key='')
  • 1

我选择直接修改环境变量。

设置代理

参考:Nodejs和python 设置 openai 的API正向代理和反向代理入口链接连接方式,用于国内访问openAI接口

如果不设置代理,你将会看到任何请求都返回Connection error:
在这里插入图片描述

设置的方式是在Python中加四行代码:

# Set the proxy URL and port
proxy_url = 'http://127.0.0.1'
proxy_port = '10809'

# Set the http_proxy and https_proxy environment variables
os.environ['http_proxy'] = f'{proxy_url}:{proxy_port}'
os.environ['https_proxy'] = f'{proxy_url}:{proxy_port}'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

具体URL和端口由你使用的代理决定。

可运行的代码

设置完成后,完整代码如下:

from openai import OpenAI
import os

# Set the proxy URL and port
proxy_url = 'http://127.0.0.1'
proxy_port = '8080' # !!!please replace it with your own port

# Set the http_proxy and https_proxy environment variables
os.environ['http_proxy'] = f'{proxy_url}:{proxy_port}'
os.environ['https_proxy'] = f'{proxy_url}:{proxy_port}'

client = OpenAI()

response = client.chat.completions.create(
  model="gpt-3.5-turbo",
  messages=[
    {
      "role": "system",
      "content": "You will be provided with unstructured data, and your task is to parse it into CSV format."
    },
    {
      "role": "user",
      "content": "There are many fruits that were found on the recently discovered planet Goocrux. There are neoskizzles that grow there, which are purple and taste like candy. There are also loheckles, which are a grayish blue fruit and are very tart, a little bit like a lemon. Pounits are a bright green color and are more savory than sweet. There are also plenty of loopnovas which are a neon pink flavor and taste like cotton candy. Finally, there are fruits called glowls, which have a very sour and bitter taste which is acidic and caustic, and a pale orange tinge to them."
    }
  ],
  temperature=0.7,
  max_tokens=64,
  top_p=1
)

print(response.choices[0].message.content)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

代码运行结果:

在这里插入图片描述

本账号所有文章均为原创,欢迎转载,请注明文章出处:https://blog.csdn.net/qq_46106285/article/details/136923724。百度和各类采集站皆不可信,搜索请谨慎鉴别。技术类文章一般都有时效性,本人习惯不定期对自己的博文进行修正和更新,因此请访问出处以查看本文的最新版本。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号