先安装openai
pip install openai
默认安装最新版 网上大部分使用旧版,对于新版(1.0.0以上)不适用
# 这个可以
from openai import OpenAI
import os
# 代理地址不设置可能会有Connection Error等问题
# Set the proxy URL and port
proxy_url = 'http://用户名:@127.0.0.1' # 代理地址
proxy_port = '7890' # !!!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()
# client = OpenAI(
# base_url="对应的base_url",
# api_key="对应的api_key"
# )
# client = OpenAI(
# base_url="对应的base_url",
# api_key="对应的api_key"
# )
client = OpenAI(
base_url="对应的base_url",
api_key="对应的api_key"
)
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)
# print(response.choices[0].message.content)
正常输出如下所示: