1、 申请 API
前置条件1:jie 码平台
https://sms-activate.org/cn

见上面的截图。
截止:2024-01-09 chatgpt 注册只需要 google 账号就可以,不再需要接码。
但是,API 申请必须需要接码平台。
chatgpt API 平台地址:
https://platform.openai.com/docs/overview
申请 API 地址:
https://platform.openai.com/api-keys

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

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 模型,并发送一个特定的请求来生成内容。
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)

4、小结
先不做结论,本篇仅是 Demo,跑通为上策,后面实践了多了再给大家反馈。
推荐阅读
更短时间更快习得更多干货!
和全球 近2000+ Elastic 爱好者一起精进!
比同事抢先一步学习进阶干货!