当前位置:首页>文章>使用指南>Python配置openAI使用音视频图片对话

Python配置openAI使用音视频图片对话

文本是《工具配置(共27篇)》专题的第 27 篇。阅读本文前,建议先阅读前面的文章:

1. 资源准备

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)
使用指南

Python分析文件代码示例

2025-9-25 10:54:16

设计模式

享元模式详解:Python实现与应用场景全面解析

2025-9-8 9:58:56

搜索