Openai Python 代码调用 Demo 级实现全步骤

1、 申请 API

前置条件1:jie 码平台

https://sms-activate.org/cn

cf9eb9eb042a84f144038abd0100df18.png

见上面的截图。

截止:2024-01-09 chatgpt 注册只需要 google 账号就可以,不再需要接码。

但是,API 申请必须需要接码平台。

  • chatgpt API 平台地址:

https://platform.openai.com/docs/overview

  • 申请 API 地址:

https://platform.openai.com/api-keys

b1bcf9225ba649173346b225382fb972.png

前置条件2:设置环境变量 (windows)

19fc7ad9d46dfd94ff7e112a08bff7b6.png

2、Demo级别代码

注意一下:咱们必须得加 dai li。

import json
import os


from openai import OpenAI


# 目前需要设置代理才可以访问 api
os.environ["HTTP_PROXY"] = "http://127.0.0.1:33210"
os.environ["HTTPS_PROXY"] = "http://127.0.0.1:33210"
os.environ["ALL_PROXY"] = "socks5://127.0.0.1:33211"


api_key=os.environ.get("OPENAI_API_KEY")


### 咱们得验证一下是否正确,不正确不行。
print(api_key)  


client = OpenAI(
   api_key=os.environ.get("OPENAI_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)

代码解读如下:

  • import json 和 import os:这两行导入 Python 的 json 和 os 模块。json 模块用于处理 JSON 数据,os 模块用于与操作系统交互,如环境变量。

  • from openai import OpenAI:从 openai 包导入 OpenAI 类。这是用于与 OpenAI API 交互的主要类。

  • 设置代理服务器:这些行设置了环境变量,以便所有 HTTP 和 HTTPS 请求通过本地代理服务器。这可能是因为直接访问 OpenAI API 在某些地区受限,需要daili。

  • api_key=os.environ.get("OPENAI_API_KEY"):从环境变量中获取 OpenAI API 密钥。

  • 打印 API 密钥:这是为了验证是否正确获取了 API 密钥。

  • 创建一个 OpenAI 客户端实例:使用获取到的 API 密钥实例化 OpenAI 类。

  • 使用 GPT-3.5 模型创建一个自动完成任务:这里使用 client.chat.completions.create 方法发送请求到 OpenAI 的聊天模型。这个请求包括模型名称("gpt-3.5-turbo")和一系列消息。

  • 这些消息定义了模型的角色("system" 和 "user")和内容。在这个例子中,系统角色定义了模型是一个擅长用诗意方式解释复杂编程概念的助手,用户角色请求模型用诗来解释编程中的递归概念。

总结来说,这段代码展示了如何通过代理设置访问 OpenAI 的 GPT-3.5 模型,并发送一个特定的请求来生成内容。

0e3898ee35538b87737e0217ec08a413.jpeg

3、执行结果

ChatCompletionMessage(content=“In the realm of code, where logic reigns supreme,\nLies a concept profound, a recursive dream.\nLike a hall of mirrors, reflections intertwined,\nRecursion in programming, a marvel of the mind.\n\nJust as a Russian doll, nested layers concealed,\nRecursion unwraps, the answer revealed.\nA function calls itself, a loop of mystic grace,\nAs it dives deeper, traversing time and space.\n\nLike a fractal’s dance, repeating patterns unfold,\nRecursion, the storyteller, in code’s stronghold.\nAs it encounters a problem, it crafts a grand plan,\nBreaking it down, recursively expand.\n\nAn exquisite embrace, with elegance and skill,\nRecursion mirrors nature, a symphony to thrill.\nIt tackles tasks large, by solving small parts,\nRepeating the process, until completion imparts.\n\nYet, beware the infinite, in recursive embrace,\nFor a runaway loop leads to a perilous chase.\nLike a bird’s endless flight into boundless sky,\nRecursion unchecked, can make a program sigh.\n\nBut with base cases secure, boundaries defined,\nRecursion dances gracefully, cleverly aligned.\nComplex problems decrypted, solutions unfurled,\nA recursive enchantment that transcends our world.\n\nSo dive into recursion, fear not its complex face,\nDiscover the magic, embrace its grace.\nFor in its looping realms, where wonder resides,\nRecursion in programming, unlocks truths, it guides.”, role=‘assistant’, function_call=None, tool_calls=None)
45f73c274ff83b96f95ffd1ce872872c.png

4、小结

先不做结论,本篇仅是 Demo,跑通为上策,后面实践了多了再给大家反馈。

推荐阅读

  1. 全网首发!从 0 到 1 Elasticsearch 8.X 通关视频

  2. 重磅 | 死磕 Elasticsearch 8.X 方法论认知清单

  3. 如何系统的学习 Elasticsearch ?

  4. 2023,做点事

8f9972383db350806ace89edcc1a5bce.jpeg

更短时间更快习得更多干货!

和全球 近2000+ Elastic 爱好者一起精进!

162aaff63516f1217447def4a9215832.gif

比同事抢先一步学习进阶干货!

  • 20
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

铭毅天下

和你一起,死磕Elastic!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值