找回密码
 会员注册
查看: 37|回复: 0

AI大模型OpenAI接口调用①(安装openai软件包查看openai软件包版本PyCharm中开发Python程序调用OpenAI接口)

[复制链接]

2万

主题

0

回帖

7万

积分

超级版主

积分
73812
发表于 2024-9-3 00:44:46 | 显示全部楼层 |阅读模式
文章目录一、安装PythonSDK二、安装OpenAIPythonSDK1、安装openai软件包2、查看openai软件包版本3、openai接口参考文档三、PyCharm中开发Python程序调用OpenAI接口1、PyCharm创建Python项目2、APIKey配置用法3、GPT3模型和GPT4模型4、Python代码示例博客源码下载地址:https://download.csdn.net/download/han1202012/89478137一、安装PythonSDK参考【开发环境】Windows中安装Python各个版本(下载Python各版本SDK|安装Python)博客,在Windows上安装PythonSDK;安装了PythonSDK之后,才能使用Python的包管理工具pip,通过pipinstall命令,可以方便地将各种Python包安装到本地的Python环境中;安装Python包命令如下:pipinstallpackage_name1package_name就是要安装的Python包的名称;二、安装OpenAIPythonSDK1、安装openai软件包安装OpenAI的PythonSDK,执行pipinstallopenai1命令,安装OpenAISDK;OpenAIPythonSDK安装过程输入结果如下:C:\Users\octop>pipinstallopenaiCollectingopenaiDownloadingopenai-1.35.3-py3-none-any.whl(327kB)|████████████████████████████████|327kB218kB/sCollectingsniffioDownloadingsniffio-1.3.1-py3-none-any.whl(10kB)Collectinganyio=3.5.0Downloadinganyio-3.7.1-py3-none-any.whl(80kB)|████████████████████████████████|80kB2.6MB/sCollectingcached-property;python_version=1.7.0Downloadingdistro-1.9.0-py3-none-any.whl(20kB)Requirementalreadysatisfied:typing-extensions=4.7ind:\001_develop\022_python\python37_64\lib\site-packages(fromopenai)(4.7.1)Collectingpydantic=1.9.0Downloadingpydantic-2.5.3-py3-none-any.whl(381kB)|████████████████████████████████|381kB2.2MB/sCollectinghttpx=0.23.0Downloadinghttpx-0.24.1-py3-none-any.whl(75kB)|████████████████████████████████|75kB5.5MB/sCollectingtqdm>4Downloadingtqdm-4.66.4-py3-none-any.whl(78kB)|████████████████████████████████|78kB2.6MB/sCollectingexceptiongroup;python_version=2.8Downloadingidna-3.7-py3-none-any.whl(66kB)|████████████████████████████████|66kB4.5MB/sCollectingpydantic-core==2.14.6Downloadingpydantic_core-2.14.6-cp37-none-win_amd64.whl(1.9MB)|████████████████████████████████|1.9MB3.3MB/sCollectingannotated-types>=0.4.0Downloadingannotated_types-0.5.0-py3-none-any.whl(11kB)Requirementalreadysatisfied:importlib-metadata;python_version=="3.7"ind:\001_develop\022_python\python37_64\lib\site-packages(frompydantic=1.9.0->openai)(6.7.0)CollectingcertifiDownloadingcertifi-2024.6.2-py3-none-any.whl(164kB)|████████████████████████████████|164kB504kB/sCollectinghttpcore=0.15.0Downloadinghttpcore-0.17.3-py3-none-any.whl(74kB)|████████████████████████████████|74kB5.5MB/sRequirementalreadysatisfied:colorama;platform_system=="Windows"ind:\001_develop\022_python\python37_64\lib\site-packages(fromtqdm>4->openai)(0.4.4)Requirementalreadysatisfied:zipp>=0.5ind:\001_develop\022_python\python37_64\lib\site-packages(fromimportlib-metadata;python_version=="3.7"->pydantic=1.9.0->openai)(3.15.0)Collectingh11=0.13Downloadingh11-0.14.0-py3-none-any.whl(58kB)|████████████████████████████████|58kB2.8MB/sInstallingcollectedpackages:sniffio,exceptiongroup,idna,anyio,cached-property,distro,pydantic-core,annotated-types,pydantic,certifi,h11,httpcore,httpx,tqdm,openaiSuccessfullyinstalledannotated-types-0.5.0anyio-3.7.1cached-property-1.5.2certifi-2024.6.2distro-1.9.0exceptiongroup-1.2.1h11-0.14.0httpcore-0.17.3httpx-0.24.1idna-3.7openai-1.35.3pydantic-2.5.3pydantic-core-2.14.6sniffio-1.3.1tqdm-4.66.4WARNING:Youareusingpipversion20.1.1;however,version24.0isavailable.Youshouldconsiderupgradingviathe'd:\001_develop\022_python\python37_64\python.exe-mpipinstall--upgradepip'command.C:\Users\octop>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051开始安装:安装完成:2、查看openai软件包版本执行pipshowopenai1命令,查看安装的openai软件包版本;C:\Users\octop>pipshowopenaiNamepenaiVersion:1.35.3Summary:TheofficialPythonlibraryfortheopenaiAPIHome-page:NoneAuthor:NoneAuthor-email:OpenAILicense:NoneLocation:d:\001_develop\022_python\python37_64\lib\site-packagesRequires:pydantic,tqdm,sniffio,cached-property,httpx,anyio,distro,typing-extensionsRequired-by:C:\Users\octop>123456789101112133、openai接口参考文档openai接口调用参考文档:https://platform.openai.com/docs/api-reference三、PyCharm中开发Python程序调用OpenAI接口博客源码下载地址:https://download.csdn.net/download/han1202012/894781371、PyCharm创建Python项目进入PyCharm,选择"File/NewProject"选项,2、APIKey配置用法建议直接从某宝买的OpenAI的中转APIKey,会给你一个API_KEY和中转地址,有了这两个可以直接替换到代码中使用;#设置OPENAI_API_KEY环境变量os.environ["OPENAI_API_KEY"]="sk-6o3KJuuocEXpb1Ug39D0A4913a844fCaBa892eDe9814Df8a"#设置OPENAI_BASE_URL环境变量os.environ["OPENAI_BASE_URL"]="https://api.xiaoai.plus/v1"1234代码处,os.environ["OPENAI_API_KEY"]配置的是APIKey,os.environ["OPENAI_BASE_URL"]配置的是OpenAI接口的中转地址;也可以直接配置到Windows系统的环境变量中;APIKey使用注意事项:使用国内的OpenAI中转接口,直接使用即可,不要连XX,否则会报错;使用OpenAI的直连接口,必须挂上XX,否则也会报错;OpenAI的直连API会不定期清理,这个用起来很麻烦;3、GPT3模型和GPT4模型申请的APIKey只能访问指定的模型,如3.5或4.0版本,申请的3.5版本的APIKey无法使用4.0版本的模型;不同的模型收费不同,GPT4模型的费用要高于GPT3模型;可用的GPT3模型:gpt-3.5-turbogpt-3.5-turbo-0301gpt-3.5-turbo-0613gpt-3.5-turbo-0125gpt-3.5-turbo-16kgpt-3.5-turbo-1106text-embedding-3-smallgpt-3.5-turbo-16k-0613text-embedding-3-large可用的GPT4模型:gpt-4-1106-previewgpt-4gpt-4-0613gpt-4-32k-0613gpt-4-vision-previewgpt-4-0125-previewgpt-4-32k-0314dall-e-3gpt-4-32kgpt-4-0314gpt-4ogpt-4-turbo-2024-04-09上述模型可直接在创建OpenAI对话时配置,client.chat.completions.create的第一个参数,就是指定一个模型,这里指定的是gpt-3.5-turbo模型;client.chat.completions.create(#指定模型为"gpt-3.5-turbo"model="gpt-3.5-turbo",1234、Python代码示例我直接把购买的APIKey贴出来了,希望能多用一会儿;代码示例:importosfromopenaiimportOpenAI#下面的API是从淘宝上买的OpenAI国内中转API#使用下面的APIKey的时候务必断开梯子#淘宝上搜索APIKey,可以搜出一大堆的中转或直联的OpenAI接口#设置OPENAI_API_KEY环境变量os.environ["OPENAI_API_KEY"]="sk-6o3KJuuocEXpb1Ug39D0A4913a844fCaBa892eDe9814Df8a"#设置OPENAI_BASE_URL环境变量os.environ["OPENAI_BASE_URL"]="https://api.xiaoai.plus/v1"defopenai_chat():client=OpenAI(#下面两个参数的默认值来自环境变量,可以不加api_key=os.environ.get("OPENAI_API_KEY"),base_url=os.environ.get("OPENAI_BASE_URL"),)#创建一个聊天完成请求completion=client.chat.completions.create(#指定模型为"gpt-3.5-turbo"model="gpt-3.5-turbo",#定义对话消息列表messages=[#系统角色的消息,用于设置对话的起始状态{"role":"system","content":"Youareahelpfulassistant."},#用户角色的消息,用于指示用户的输入{"role":"user","content":"请写一首七言绝句,描述夕阳"}])print(completion)#响应print(completion.choices[0].message)#回答if__name__=='__main__'penai_chat()12345678910111213141516171819202122232425262728293031323334353637执行结果如下:\001_Develop\022_Python\Python39\python.exeD:/002_Project/011_Python/OpenAI/main.pyChatCompletion(id='chatcmpl-9dXgRX2Q47HiQoPASfk8xcCVj3C7v',choices=[Choice(finish_reason='stop',index=0,logprobs=None,message=ChatCompletionMessage(content='夕阳西下映山川,霞光映照满天边。\n金黄色彩映水面,画出美丽夕阳颜。',role='assistant',function_call=None,tool_calls=None))],created=1719211299,model='gpt-35-turbo',object='chat.completion',service_tier=None,system_fingerprint='fp_811936bd4f',usage=CompletionUsage(completion_tokens=47,prompt_tokens=34,total_tokens=81))ChatCompletionMessage(content='夕阳西下映山川,霞光映照满天边。\n金黄色彩映水面,画出美丽夕阳颜。',role='assistant',function_call=None,tool_calls=None)Processfinishedwithexitcode012345与ChatGPT输出结果进行对比:
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2025-1-13 17:33 , Processed in 2.579728 second(s), 25 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表