---
title: "使用 Audio 模型"
url: "/docs/completion/audio"
canonical_url: "https://docs.litellm.ai/docs/completion/audio"
type: "docs"
last_updated: "2026-10-08"
related:
  - "/docs/completion/vision"
  - "/docs/completion/document_understanding"
---
# 使用 Audio 模型

> Index of all LiteLLM docs: https://docs.litellm.ai/llms.txt


如何向 `/chat/completions` 端點傳送 / 接收 audio

## 模型的 Audio 輸出 {#audio-output-from-a-model}

將提示詞轉換為類人音訊回應的範例

**LiteLLM Python SDK**

```python
import os 
import base64
from litellm import completion

os.environ["OPENAI_API_KEY"] = "your-api-key"

# openai call
completion = await litellm.acompletion(
    model="gpt-4o-audio-preview",
    modalities=["text", "audio"],
    audio={"voice": "alloy", "format": "wav"},
    messages=[{"role": "user", "content": "Is a golden retriever a good family dog?"}],
)

wav_bytes = base64.b64decode(completion.choices[0].message.audio.data)
with open("dog.wav", "wb") as f:
    f.write(wav_bytes)
```

**LiteLLM Proxy Server**

1. 在 config.yaml 上定義 audio 模型

```yaml
model_list:
  - model_name: gpt-4o-audio-preview # OpenAI gpt-4o-audio-preview
    litellm_params:
      model: openai/gpt-4o-audio-preview
      api_key: os.environ/OPENAI_API_KEY 

```

2. 執行 proxy server

```bash
litellm --config config.yaml
```

3. 使用 OpenAI Python SDK 進行測試

```python
import base64
from openai import OpenAI

client = OpenAI(
    api_key="LITELLM_PROXY_KEY", # sk-<your-litellm-api-key>
    base_url="LITELLM_PROXY_BASE" # http://0.0.0.0:4000
)

completion = client.chat.completions.create(
    model="gpt-4o-audio-preview",
    modalities=["text", "audio"],
    audio={"voice": "alloy", "format": "wav"},
    messages=[
        {
            "role": "user",
            "content": "Is a golden retriever a good family dog?"
        }
    ]
)

print(completion.choices[0])

wav_bytes = base64.b64decode(completion.choices[0].message.audio.data)
with open("dog.wav", "wb") as f:
    f.write(wav_bytes)

```

## 模型的 Audio 輸入 {#audio-input-to-a-model}

**LiteLLM Python SDK**

```python
import base64
import requests

url = "https://openaiassets.blob.core.windows.net/$web/API/docs/audio/alloy.wav"
response = requests.get(url)
response.raise_for_status()
wav_data = response.content
encoded_string = base64.b64encode(wav_data).decode("utf-8")

completion = litellm.completion(
    model="gpt-4o-audio-preview",
    modalities=["text", "audio"],
    audio={"voice": "alloy", "format": "wav"},
    messages=[
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "What is in this recording?"},
                {
                    "type": "input_audio",
                    "input_audio": {"data": encoded_string, "format": "wav"},
                },
            ],
        },
    ],
)

print(completion.choices[0].message)
```

**LiteLLM Proxy Server**

1. 在 config.yaml 上定義 audio 模型

```yaml
model_list:
  - model_name: gpt-4o-audio-preview # OpenAI gpt-4o-audio-preview
    litellm_params:
      model: openai/gpt-4o-audio-preview
      api_key: os.environ/OPENAI_API_KEY 

```

2. 執行 proxy server

```bash
litellm --config config.yaml
```

3. 使用 OpenAI Python SDK 進行測試

```python
import base64
from openai import OpenAI

client = OpenAI(
    api_key="LITELLM_PROXY_KEY", # sk-<your-litellm-api-key>
    base_url="LITELLM_PROXY_BASE" # http://0.0.0.0:4000
)

# Fetch the audio file and convert it to a base64 encoded string
url = "https://openaiassets.blob.core.windows.net/$web/API/docs/audio/alloy.wav"
response = requests.get(url)
response.raise_for_status()
wav_data = response.content
encoded_string = base64.b64encode(wav_data).decode('utf-8')

completion = client.chat.completions.create(
    model="gpt-4o-audio-preview",
    modalities=["text", "audio"],
    audio={"voice": "alloy", "format": "wav"},
    messages=[
        {
            "role": "user",
            "content": [
                { 
                    "type": "text",
                    "text": "What is in this recording?"
                },
                {
                    "type": "input_audio",
                    "input_audio": {
                        "data": encoded_string,
                        "format": "wav"
                    }
                }
            ]
        },
    ]
)

print(completion.choices[0].message)
```

## 檢查模型是否支援 `audio_input` 與 `audio_output` {#checking-if-a-model-supports-audio_input-and-audio_output}

**LiteLLM Python SDK**

使用 `litellm.supports_audio_output(model="")` -> 若模型可產生 audio 輸出，則回傳 `True`

使用 `litellm.supports_audio_input(model="")` -> 若模型可接受 audio 輸入，則回傳 `True`

```python
assert litellm.supports_audio_output(model="gpt-audio") == True
assert litellm.supports_audio_input(model="gpt-audio") == True

assert litellm.supports_audio_output(model="gpt-5.6-luna") == False
assert litellm.supports_audio_input(model="gpt-5.6-luna") == False
```

**LiteLLM Proxy Server**

1. 在 config.yaml 上定義音訊模型

```yaml
model_list:
  - model_name: gpt-audio # OpenAI gpt-audio
    litellm_params:
      model: openai/gpt-audio
      api_key: os.environ/OPENAI_API_KEY
  - model_name: llava-hf          # Custom OpenAI compatible model
    litellm_params:
      model: openai/llava-hf/llava-v1.6-vicuna-7b-hf
      api_base: http://localhost:8000
      api_key: fake-key
    model_info:
      supports_audio_output: True        # set supports_audio_output to True so /model/info returns this attribute as True
      supports_audio_input: True         # set supports_audio_input to True so /model/info returns this attribute as True
```

2. 執行 proxy server

```bash
litellm --config config.yaml
```

3. 呼叫 `/model/info` 以檢查您的模型是否支援音訊輸入與輸出。這些旗標會回傳在每個部署的 `model_info` 下，來源可能是 `litellm.model_cost`，或是您在 `config.yaml` 中設定的內容。`/model_group/info` 不會回傳 `supports_audio_input` 或 `supports_audio_output`

```shell
curl -X 'GET' \
  'http://localhost:4000/model/info' \
  -H 'accept: application/json' \
  -H "x-api-key: $LITELLM_API_KEY"
```

預期回應（截斷）

```json
{
  "data": [
    {
      "model_name": "gpt-audio",
      "model_info": {
        "max_input_tokens": 128000,
        "max_output_tokens": 16384,
        "mode": "chat",
        "supports_audio_input": true,
        "supports_audio_output": true
      }
    },
    {
      "model_name": "llava-hf",
      "model_info": {
        "max_input_tokens": null,
        "max_output_tokens": null,
        "mode": null,
        "supports_audio_input": true,
        "supports_audio_output": true
      }
    }
  ]
}
```

## 含 Audio 的回應格式 {#response-format-with-audio}

以下是當您向模型傳送 audio 輸入時，可能會從 `message` 端點收到的 `/chat/completions` JSON 資料結構範例。

```json
{
  "index": 0,
  "message": {
    "role": "assistant",
    "content": null,
    "refusal": null,
    "audio": {
      "id": "audio_abc123",
      "expires_at": 1729018505,
      "data": "<bytes omitted>",
      "transcript": "Yes, golden retrievers are known to be ..."
    }
  },
  "finish_reason": "stop"
}
```
- `audio` 如果請求了 audio 輸出模式，則此物件包含來自模型的 audio 回應資料
    - `audio.id` audio 回應的唯一識別碼
    - `audio.expires_at` 此 audio 回應在伺服器上對於多輪對話不再可用的 Unix 時間戳記（以秒為單位）。
    - `audio.data` 由模型產生的 Base64 編碼 audio 位元組，格式如請求中所指定。
    - `audio.transcript` 由模型產生的 audio 逐字稿。

## Related pages

- [使用 Vision 模型](https://docs.litellm.ai/docs/completion/vision.md)
- [使用 PDF 輸入](https://docs.litellm.ai/docs/completion/document_understanding.md)
