通过代理如何调通openai的api

2024-01-21 09:04
文章标签 代理 api openai 调通

本文主要是介绍通过代理如何调通openai的api,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

调通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)

这篇关于通过代理如何调通openai的api的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/629010

相关文章

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

高效+灵活,万博智云全球发布AWS无代理跨云容灾方案!

摘要 近日,万博智云推出了基于AWS的无代理跨云容灾解决方案,并与拉丁美洲,中东,亚洲的合作伙伴面向全球开展了联合发布。这一方案以AWS应用环境为基础,将HyperBDR平台的高效、灵活和成本效益优势与无代理功能相结合,为全球企业带来实现了更便捷、经济的数据保护。 一、全球联合发布 9月2日,万博智云CEO Michael Wong在线上平台发布AWS无代理跨云容灾解决方案的阐述视频,介绍了

在JS中的设计模式的单例模式、策略模式、代理模式、原型模式浅讲

1. 单例模式(Singleton Pattern) 确保一个类只有一个实例,并提供一个全局访问点。 示例代码: class Singleton {constructor() {if (Singleton.instance) {return Singleton.instance;}Singleton.instance = this;this.data = [];}addData(value)

【LabVIEW学习篇 - 21】:DLL与API的调用

文章目录 DLL与API调用DLLAPIDLL的调用 DLL与API调用 LabVIEW虽然已经足够强大,但不同的语言在不同领域都有着自己的优势,为了强强联合,LabVIEW提供了强大的外部程序接口能力,包括DLL、CIN(C语言接口)、ActiveX、.NET、MATLAB等等。通过DLL可以使用户很方便地调用C、C++、C#、VB等编程语言写的程序以及windows自带的大

如何更优雅地对接第三方API

如何更优雅地对接第三方API 本文所有示例完整代码地址:https://github.com/yu-linfeng/BlogRepositories/tree/master/repositories/third 我们在日常开发过程中,有不少场景会对接第三方的API,例如第三方账号登录,第三方服务等等。第三方服务会提供API或者SDK,我依稀记得早些年Maven还没那么广泛使用,通常要对接第三方

proxy代理解决vue中跨域问题

vue.config.js module.exports = {...// webpack-dev-server 相关配置devServer: {host: '0.0.0.0',port: port,open: true,proxy: {'/api': {target: `https://vfadmin.insistence.tech/prod-api`,changeOrigin: true,p

Java基础回顾系列-第五天-高级编程之API类库

Java基础回顾系列-第五天-高级编程之API类库 Java基础类库StringBufferStringBuilderStringCharSequence接口AutoCloseable接口RuntimeSystemCleaner对象克隆 数字操作类Math数学计算类Random随机数生成类BigInteger/BigDecimal大数字操作类 日期操作类DateSimpleDateForma

Restful API 原理以及实现

先说说API 再说啥是RESRFUL API之前,咱先说说啥是API吧。API大家应该都知道吧,简称接口嘛。随着现在移动互联网的火爆,手机软件,也就是APP几乎快爆棚了。几乎任何一个网站或者应用都会出一款iOS或者Android APP,相比网页版的体验,APP确实各方面性能要好很多。 那么现在问题来了。比如QQ空间网站,如果我想获取一个用户发的说说列表。 QQ空间网站里面需要这个功能。

京东物流查询|开发者调用API接口实现

快递聚合查询的优势 1、高效整合多种快递信息。2、实时动态更新。3、自动化管理流程。 聚合国内外1500家快递公司的物流信息查询服务,使用API接口查询京东物流的便捷步骤,首先选择专业的数据平台的快递API接口:物流快递查询API接口-单号查询API - 探数数据 以下示例是参考的示例代码: import requestsurl = "http://api.tanshuapi.com/a

WordPress开发中常用的工具或api文档

http://php.net/ http://httpd.apache.org/ https://wordpress.org/ https://cn.wordpress.org/ https://core.svn.wordpress.org/ zh-cn:开发者文档: https://codex.wordpress.org/zh-cn:%E5%BC%80%E5%8F%91%E8%80%