程序员文章、书籍推荐和程序员创业信息与资源分享平台

网站首页 > 技术文章 正文

如何在Python中进行openAI接口调用

hfteth 2025-06-24 13:49:18 技术文章 1 ℃

## 一、环境配置

确保已经安装了Python后安装pip命令

```javascript

python -m ensurepip --default-pip

```

卸载

```javascript

python -m pip uninstall pip

```

查看是否安装成功

```javascript

pip --version

```

## 二、安装OpenAI SDK

进入项目文件夹下,安装项目依赖

代码环境:JavaScript

```javascript

# 下载 openai 包

pip install openai

# 查看openai版本

openai --version

# 或者

pip show openai

```

## 三、新建一个index.py文件

在文件夹新建index.py文件后,复制下方代码

```python

from openai import OpenAI

client = OpenAI(

api_key = "自己的API key", # 此处的key需要自己通过官方购买 或者通过其他渠道获取

base_url = "https://api.agicto.cn/v1" # 中转地址

)

chat_completion = client.chat.completions.create(

messages=[

{

"role": "user",

"content": "讲个笑话",

}

],

model="gpt-3.5-turbo", #此处可更换其它模型

)

print(chat_completion.choices[0].message.content)

```

**注意事项**

- 使用官方提供的中转地址参考openai 接口调用参考文档 :
https://platform.openai.com/docs/api-reference

- 官方的API会不定期清理,不稳定

- 建议使用第三方提供的key

## 运行效果

上方的key是从第三方渠道来的,比较稳定

运行时要注意:**命令行安装的包一定要和运营的环境版本是匹配的**

## 四、目前支持的模型列表

**Meta**

- Meta meta-llama/Llama-3-8b-chat-hf 8000

- Meta meta-llama/Llama-3-70b-chat-hf 8000

**most used model**

- claude-ai claude/claude-2.1

- claude-ai claude/claude-2.0

- claude-ai claude/claude-instant-1.2

- claude-ai claude/claude-3-opus-20240229

- claude-ai claude/claude-3-sonnet-20240229

- claude-ai claude/Coming soon Placeholder for future models

**Gemini Models**

- gemini google/gemini-pro

- gemini google/gemini-pro-vision Vision-focused variant

**Mistral.ai Models**

- mistral.ai mistral/open-mistral-7b

- mistral.ai mistral/open-mixtral-8x7b

- mistral.ai mistral/mistral-small-latest Latest version, small model

- mistral.ai mistral/mistral-medium-latest Latest version, medium model

- mistral.ai mistral/mistral-large-latest Latest version, large model

**all model**

- 01.AI 01-ai Yi Chat (34B) zero-one-ai/Yi-34B-Chat 4096

- DeepSeek Deepseek Coder Instruct (33B) deepseek-ai/deepseek-coder-33b-instruct 16384

- garage-bAInd Platypus2 Instruct (70B) garage-bAInd/Platypus2-70B-instruct 4096

- Google Gemma Instruct (2B) google/gemma-2b-it 8192

- Google Gemma Instruct (7B) google/gemma-7b-it 8192

- Gryphe MythoMax-L2 (13B) Gryphe/MythoMax-L2-13b 4096

- LM Sys Vicuna v1.5 (13B) lmsys/vicuna-13b-v1.5 4096

- LM Sys Vicuna v1.5 (7B) lmsys/vicuna-7b-v1.5 4096

- Meta Code Llama Instruct (13B) codellama/CodeLlama-13b-Instruct-hf 16384

- Meta Code Llama Instruct (34B) codellama/CodeLlama-34b-Instruct-hf 16384

- Meta Code Llama Instruct (70B) codellama/CodeLlama-70b-Instruct-hf 4096

- Meta Code Llama Instruct (7B) codellama/CodeLlama-7b-Instruct-hf 16384

- Meta LLaMA-2 Chat (70B) meta-llama/Llama-2-70b-chat-hf 4096

- Meta LLaMA-2 Chat (13B) meta-llama/Llama-2-13b-chat-hf 4096

- Meta LLaMA-2 Chat (7B) meta-llama/Llama-2-7b-chat-hf 4096

- mistralai Mistral (7B) Instruct mistralai/Mistral-7B-Instruct-v0.1 4096

- mistralai Mistral (7B) Instruct v0.2 mistralai/Mistral-7B-Instruct-v0.2 32768

- mistralai Mixtral-8x7B Instruct (46.7B) mistralai/Mixtral-8x7B-Instruct-v0.1 32768

- OpenChat OpenChat 3.5 (7B) openchat/openchat-3.5-1210 8192

- OpenOrca OpenOrca Mistral (7B) 8K Open-Orca/Mistral-7B-OpenOrca 8192

- Qwen Qwen 1.5 Chat (0.5B) Qwen/Qwen1.5-0.5B-Chat 32768

- Qwen Qwen 1.5 Chat (1.8B) Qwen/Qwen1.5-1.8B-Chat 32768

- Qwen Qwen 1.5 Chat (4B) Qwen/Qwen1.5-4B-Chat 32768

- Qwen Qwen 1.5 Chat (7B) Qwen/Qwen1.5-7B-Chat 32768

- Qwen Qwen 1.5 Chat (14B) Qwen/Qwen1.5-14B-Chat 32768

- Qwen Qwen 1.5 Chat (72B) Qwen/Qwen1.5-72B-Chat 4096

## 写在最后

在当今科技飞速发展的时代,AI(人工智能)已经成为了一个热门话题,并且在我们的生活中扮演着越来越重要的角色。从智能手机中的语音助手到自动化的工业生产,AI 的应用似乎无处不在。而我们作为开发人员,应该仔细思考如何在即将到来的AI浪潮中实现自己的价值。**而熟练使用AI工具以及基础接口**将是我们扎实的基础,在AI产品即将百花齐放的未来将为自己占据先机。

最近发表
标签列表