
Google语音识别技术详解与实践应用
在当前的网络环境下,访问api.openai.com等外部API接口可能会遇到各种挑战。本文将探讨curl无法访问api.openai.com的问题,并提供一系列解决方案,帮助开发者和企业有效解决这一问题。同时,文章将详细介绍如何通过curl和python调通openai的api,以及相关的FAQ解答。
代理服务是一种常见的解决方案,特别适用于大型场景。通过在能够访问api.openai.com的服务器上开启代理服务,个人或公司可以通过代理地址和端口发送请求。
接口转发本质上是一种代理,但更加适合个人开发者或小型公司。通过B地址配置指向api.openai.com,请求B地址的参数和内容都会转发到openAI。
接口转发几乎没有明显缺点,是一种高效的解决方案。
服务中转是另一种解决方案,将接口部署到可以访问api.openai.com的服务器,然后通过该服务器请求。
要通过curl调通openai的api,需要满足以下前提条件:
127.0.0.1:1081
如果在国外,没有网络限制,可以直接使用以下命令:
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."
}
]
}'
使用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)