
Google語音識別技術(shù)詳解與實踐應(yīng)用
接口轉(zhuǎn)發(fā)本質(zhì)上是一種代理,但更加適合個人開發(fā)者或小型公司。通過B地址配置指向api.openai.com,請求B地址的參數(shù)和內(nèi)容都會轉(zhuǎn)發(fā)到openAI。
接口轉(zhuǎn)發(fā)幾乎沒有明顯缺點,是一種高效的解決方案。
服務(wù)中轉(zhuǎn)是另一種解決方案,將接口部署到可以訪問api.openai.com的服務(wù)器,然后通過該服務(wù)器請求。
要通過curl調(diào)通openai的api,需要滿足以下前提條件:
127.0.0.1:1081
如果在國外,沒有網(wǎng)絡(luò)限制,可以直接使用以下命令:
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."
}
]
}'
在國內(nèi),需要通過代理進(jìn)行操作:
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."
}
]
}'
使用poetry管理依賴,添加支持socks5的httpx:
poetry add ‘httpx[socks]’
import os
from openai import OpenAI
os.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.nnImagine 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.nnWhen 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.nnJust 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.nnWith 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.nnYet, 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.nnBut 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.nnFrom 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.nnSo, 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)