调通openai的api
- 一、前提
- 二、通过curl调通openai的api
- 三、通过python调通openai的api
一、前提
- 会魔法上网
- 本地运行代理软件,知道端口号(如1081)。
127.0.0.1:1081
二、通过curl调通openai的api
- 如果在国外,没有qiang,直接:
curl https://api.openai.com/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer $OPENAI_API_KEY" -d '{"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."}]}'
- 在国内,走代理
curl -x socks5://127.0.0.1:1081 https://api.openai.com/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer xxx" -d '{"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."}]}'
三、通过python调通openai的api
poetry管理依赖。
- poetry add ‘httpx[socks]’
(1)加单引号的原因:防止zsh尝试解释或扩展方括号
(2)支持socks5
- 代码:
import os
from openai import OpenAIos.environ['http_proxy'] = 'socks5://127.0.0.1:1081'
os.environ['https_proxy'] = 'socks5://127.0.0.1:1081'client = OpenAI(api_key="xxx"
)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)
- 结果:
ChatCompletionMessage(content="In the realm of loops and cycles we traverse,\nA concept profound, like a poet’s verse,\nRecursion, a waltz of code’s elegant dance,\nA mesmerizing concept, a captivating trance.\n\nImagine a function that calls itself anew,\nUnraveling mysteries, like a tale that’s true,\nA self-contained magic, a loop in disguise,\nWith depths untold, where a solution lies.\n\nWhen at its core, a problem we face,\nToo complex to solve in a linear pace,\nRecursion emerges, with dazzling embrace,\nDividing the puzzle in a smaller space.\n\nJust like a mirror reflecting its own view,\nRecursion mirrors itself, a successor it brews,\nThrough fractal-like patterns, it gracefully repeats,\nFathoming nature’s design, from fibers to beets.\n\nWith every recursive step, a mystic unfold,\nA new layer exposed, stories yet to be told,\nLike Russian dolls, nested, each snug within,\nRecursion unravels intricate paths to begin.\n\nYet, beware of the dragon that lurks from within,\nFor an unchecked recursion may suck you in,\nA beast called infinite loop, a nightmare so deep,\nWhere time gets tangled, in an abyss it seeps.\n\nBut fear not, dear programmer, and heed my plea,\nFor recursion's power can be harnessed, you see,\nWith careful rules and base cases in place,\nThe beauty of recursion, you'll flawlessly embrace.\n\nFrom Fibonacci's spiral, to trees that enfold,\nRecursion paints masterpieces, stories untold,\nA symphony of iterations, a harmonious sight,\nAs recursive shadows dance in the programming light.\n\nSo, let us embrace this poetic technique,\nIn the realm of programming, courageous and sleek,\nFor recursion, the enchantress of code divine,\nWeaves elegance and power, forever to shine.", role='assistant', function_call=None, tool_calls=None)