文本是《工具配置(共27篇)》专题的第 27 篇。阅读本文前,建议先阅读前面的文章:
- 1.沉浸式翻译插件深度解析:从基础配置到高级定制与跨平台应用
- 2.沉浸式翻译:智能双语翻译工具,提升阅读体验与翻译精准度
- 3.ChatBox 配置指南:多平台AI对话工具,支持API Key与本地模型配置
- 4.Continue 插件安装与配置指南:JetBrains IDE 的 AI 辅助利器
- 5.Cursor 安装与配置全攻略:AI 驱动的智能编程助手
- 6.CherryStudio:跨平台AI模型管理与对话工具安装与配置全攻略
- 7.Dify:开源低代码 AI 应用平台 | 一站式构建与部署
- 8.AingDesk | 开源跨平台 AI 桌面客户端 · Windows / macOS / Docker 一站式部署
- 9.VS Code 与 Cline 插件安装及 AI 对话扩展使用指南
- 10.Zed 轻量级现代代码编辑器:性能、协作与 AI 集成
- 11.DeepChat 免费桌面智能助手|多模型接入·多模态交互·隐私安全
- 12.Void AI编辑器完全指南:免费开源Cursor替代品安装配置教程 | AI编程工具
- 13.探索前沿AI对话:LibreChat部署的深度洞察与最佳实践
- 14.Sider 配置AI模型指南
- 15.Cursor AI代码编辑器完整使用指南 – 下载安装配置教程2025
- 16.Trae AI 安装与使用教程 | 最强 AI 编程助手配置指南
- 17.2025最新IntelliJ IDEA 安装与使用全指南:版本选择、插件配置与AI助手集成
- 18.Glarity浏览器插件完整指南:免费开源AI网页摘要与翻译助手
- 19.Claude Code CLI 安装与配置完整教程 | 支持 Windows 与 macOS 的 AI 编程助手
- 20.91协商写作平台
- 21.Claude-Python示例代码
- 22.OpenAI-image-Python示例代码
- 23.Gemini-Python示例代码
- 24.OpenAI-Java示例代码
- 25.Rerank-python代码配置
- 26.Python分析文件代码示例
1. 资源准备
- API Key:此项配置填写在一步API官网创建API令牌,一键直达API令牌创建页面
- 创建API令牌步骤请参考API Key的获取和使用
- API Host:此项配置填写https://yibuapi.com/v1
- 查看支持的模型请参在这里复制模型在线查询
2. 代码展示
1、使用Openai库
安装官方的 Python 库
pip install openai
1、Chat(聊天)
from openai import OpenAI
client = OpenAI(
base_url='https://yibuapi.com/v1',
# sk-xxx替换为自己的key
api_key='sk-xxx'
)
completion = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
]
)
print(completion.choices[0].message)
2、Chat(图片识别)
from openai import OpenAI
client = OpenAI(
base_url='https://yibuapi.com/v1',
# sk-xxx替换为自己的key
api_key='sk-xxx'
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What's in this image?"},
{
"type": "image_url",
"image_url": {
"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
}
},
],
}
],
max_tokens=300,
)
print(response.choices[0])
3、Embeddings(向量)
from openai import OpenAI
client = OpenAI(
base_url='https://yibuapi.com/v1',
# sk-xxx替换为自己的key
api_key='sk-xxx'
)
client.embeddings.create(
model="text-embedding-ada-002",
input="The food was delicious and the waiter...",
encoding_format="float"
)
4、Image(图片生成)
from openai import OpenAI
client = OpenAI(
base_url='https://yibuapi.com/v1',
# sk-xxx替换为自己的key
api_key='sk-xxx'
)
client.images.generate(
model="dall-e-3",
prompt="A cute baby sea otter",
n=1,
size="1024x1024"
)
5、Audio(音频)
会在代码目录生成一个文件speech.mp3
from pathlib import Path
from openai import OpenAI
import warnings
# Ignore DeprecationWarning
warnings.filterwarnings("ignore", category=DeprecationWarning)
client = OpenAI(
base_url='https://yibuapi.com/v1',
# sk-xxx替换为自己的key
api_key='sk-xxx'
)
speech_file_path = Path(__file__).parent / "speech.mp3"
response = client.audio.speech.create(
model="tts-1",
voice="alloy",
input="The quick brown fox jumped over the lazy dog."
)
response.stream_to_file(speech_file_path)
2、使用Request库
安装requests的 Python 库
pip install requests
1、Chat(聊天)
import requests
import json
url = "https://yibuapi.com/v1/chat/completions"
payload = json.dumps({
"messages": [
{
"role": "system",
"content": "你是一个大语言模型机器人"
},
{
"role": "user",
"content": "你好"
}
],
"stream": False,
"model": "gpt-3.5-turbo",
"temperature": 0.5,
"presence_penalty": 0,
"frequency_penalty": 0,
"top_p": 1
})
headers = {
"Content-Type": "application/json",
# sk-xxx替换为自己的key
'Authorization': 'Bearer sk-xxx',
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
2、Chat(图片识别)
import requests
import json
url = "https://yibuapi.com/v1/chat/completions"
payload = json.dumps({
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "这是什么"
},
{
"type": "image_url",
"image_url": {
"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
}
}
]
}
]
})
headers = {
"Content-Type": "application/json",
# sk-xxx替换为自己的key
'Authorization': 'Bearer sk-xxx',
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
2、Embeddings(向量)
import requests
import json
url = "https://yibuapi.com/v1/embeddings"
payload = json.dumps({
"input": "The food was delicious and the waiter...",
"model": "text-embedding-ada-002",
"encoding_format": "float"
})
headers = {
"Content-Type": "application/json",
# sk-xxx替换为自己的key
'Authorization': 'Bearer sk-xxx',
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
3、Image(图片生成)
import requests
import json
url = "https://yibuapi.com/v1/images/generations"
payload = json.dumps({
"model": "dall-e-3",
"prompt": "A cute baby sea otter",
"n": 1,
"size": "1024x1024"
})
headers = {
"Content-Type": "application/json",
# sk-xxx替换为自己的key
'Authorization': 'Bearer sk-xxx',
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
4、Audio(音频)
import requests
import json
url = "https://yibuapi.com/v1/audio/speech"
payload = json.dumps({
"model": "tts-1",
"input": "The quick brown fox jumped over the lazy dog.",
"voice": "alloy"
})
headers = {
"Content-Type": "application/json",
# sk-xxx替换为自己的key
'Authorization': 'Bearer sk-xxx',
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)