python程序中调用openai接口

调用openai接口

  • 1. openai例子(国内访问)
  • 2. 解决思路
  • 3. 搭建nginx
    • 3.1 创建OpenSSL创建证书
    • 3.2 nginx配置
    • 3.3 验证效果
  • 4. python调用
  • 5. SSL: certificate_verify_failed错误

1. openai例子(国内访问)

from openai import OpenAI

API_KEY='sk-api-key'
client = OpenAI(api_key=API_KEY)

completion = client.chat.completions.create(
  model="gpt-3.5-turbo",
  messages=[
    {
   "role": "system", "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."},
    {
   "role": "user", "content": "Compose a poem that explains the concept of recursion in programming."}
  ]
)

print(completion.choices[0].message)

错误结果:

httpx.ConnectError: [Errno 101] Network is unreachable

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/cv/PycharmProjects/open-ai-test/pythonProject/main.py", line 16, in <module>
    completion = client.chat.completions.create(
  File "/home/cv/miniconda3/envs/ai/lib/python3.9/site-packages/openai/_utils/_utils.py", line 303, in wrapper
    return func(*args, **kwargs)
  File "/home/cv/miniconda3/envs/ai/lib/python3.9/site-packages/openai/resources/chat/completions.py", line 598, in create
    return self._post(
  File "/home/cv/miniconda3/envs/ai/lib/python3.9/site-packages/openai/_base_client.py", line 1088, in post
    return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
  File "/home/cv/miniconda3/envs/ai/lib/python3.9/site-packages/openai/_base_client.py", line 853, in request
    return self._request(
  File "/home/cv/miniconda3/envs/ai/lib/python3.9/site-packages/openai/_base_client.py", line 896, in _request
    return self

你可能感兴趣的:(gpt,openai,gpt,人工智能,机器学习,python,transformer)