Files
FunASR/examples/openai_api/README.md
T
游雁 25e9063132 feat(openai-api): production-ready server with full test coverage
- Proper error handling, logging, health endpoint
- Verified end-to-end with OpenAI Python SDK
- Models: sensevoice, paraformer, paraformer-en, fun-asr-nano
- Response formats: json, verbose_json (with segments/timestamps)
- Complete README with curl/SDK/LangChain examples
2026-05-24 02:05:54 +08:00

2.9 KiB

FunASR OpenAI-Compatible API Server

Drop-in replacement for OpenAI's /v1/audio/transcriptions endpoint. Works with any agent framework that supports OpenAI audio API.

Quick Start

pip install funasr fastapi uvicorn python-multipart
python server.py --model sensevoice --device cuda --port 8000

Server starts in ~20s (model loading). Health check: GET /health

Usage with OpenAI SDK (Python)

from openai import OpenAI

client = OpenAI(base_url="http://localhost:8000/v1", api_key="not-needed")

# Basic transcription
result = client.audio.transcriptions.create(
    model="sensevoice",  # or "paraformer", "paraformer-en", "fun-asr-nano"
    file=open("meeting.wav", "rb"),
)
print(result.text)

# With timestamps/segments
result = client.audio.transcriptions.create(
    model="sensevoice",
    file=open("meeting.wav", "rb"),
    response_format="verbose_json",
)
# Returns: text, segments (with start/end/speaker), duration

Usage with curl

curl http://localhost:8000/v1/audio/transcriptions \
  -F file=@audio.wav \
  -F model=sensevoice

# With verbose output
curl http://localhost:8000/v1/audio/transcriptions \
  -F file=@audio.wav \
  -F model=sensevoice \
  -F response_format=verbose_json

Available Models

Model Speed (GPU) Speed (CPU) Languages Features
sensevoice 170x realtime 17x realtime zh/en/ja/ko/yue Emotion detection
paraformer 120x realtime 15x realtime zh/en Punctuation
paraformer-en 120x realtime 15x realtime en English only
fun-asr-nano 17x realtime 3.6x realtime 31 languages LLM-based, timestamps

API Endpoints

Endpoint Method Description
/v1/audio/transcriptions POST Transcribe audio (OpenAI-compatible)
/v1/models GET List available models
/health GET Health check + loaded models
/docs GET Interactive API documentation (Swagger)

Agent Framework Integration

Works with: LangChain, LlamaIndex, AutoGen, CrewAI, Semantic Kernel, Dify, or any framework using OpenAI audio API.

LangChain Example

from openai import OpenAI

client = OpenAI(base_url="http://localhost:8000/v1", api_key="x")

def transcribe_for_agent(audio_path: str) -> str:
    """Tool function for LangChain agent."""
    result = client.audio.transcriptions.create(
        model="sensevoice", file=open(audio_path, "rb")
    )
    return result.text

Docker Deployment

# Build
docker build -t funasr-api .

# Run with GPU
docker run --gpus all -p 8000:8000 funasr-api

Configuration

Arg Default Description
--host 0.0.0.0 Bind address
--port 8000 Port
--device cuda Device (cuda/cpu/mps)
--model sensevoice Pre-load model at startup