通过代理如何调通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 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.\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)
  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenAI API是一个强大的自然语言处理工具,可以帮助开发者使用Java编程语言进行开发。通过OpenAI API,开发者可以轻松地使用自然语言处理模型,以实现各种功能,例如文本生成、对话模型和语言翻译等。 使用OpenAI API进行Java开发的基本步骤如下: 1. 注册OpenAI账号并获取API密钥。 2. 在Java项目中添加OpenAI API的相关依赖项,例如OpenAI的Java SDK或者使用HTTP客户端库。 3. 使用API密钥进行身份验证,确保可以访问OpenAI API。 4. 构建请求,包括输入的文本和模型名称等参数。您可以根据项目的需求选择不同的模型,例如GPT-3或Davinci等。 5. 发起API请求并接收响应。您可以使用HTTP客户端库来发送请求,并处理返回的结果,以获取模型生成的文本或其他相关信息。 6. 根据业务需求对生成的文本进行处理和解析,以满足特定的应用场景。 在Java中使用OpenAI API的好处包括: 1. 强大的自然语言处理能力:OpenAI API基于深度学习技术,可以自动生成高质量的文本,帮助开发者快速实现各种自然语言处理功能。 2. 丰富的模型选择:OpenAI提供多种预训练模型,可以根据不同的应用场景选择最适合的模型。 3. 灵活性和可定制性:开发者可以通过指定模型参数和调整输入来控制生成的文本,以满足自己的特定需求。 4. 大规模语料库:OpenAI模型是在庞大的语料库上进行训练的,可以提供高质量的文本生成结果。 总结而言,OpenAI API提供了强大的自然语言处理功能,对于使用Java进行开发的开发者来说,可以轻松地利用这个工具来实现各种自然语言处理任务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值