Skip to main content

有赞 LLM 统一调用库

Project description

YZ-OpenAI

有赞 LLM 统一调用库 - 支持 Chat 对话、Podcast TTS、ASR 语音识别

安装

pip install yz-openai

使用示例

1. Chat 对话(同步调用)

from yz_openai import YzOpenAI

# 使用上下文管理器
with YzOpenAI(provider="volcengine", api_key="your-api-key") as client:
    # 非流式调用
    result = client.chat.completion(
        model="doubao-pro-32k",
        messages=[{"role": "user", "content": "你好"}]
    )
    print(result["message"]["content"])

    # 流式调用
    for chunk in client.chat.completion(
        model="doubao-pro-32k",
        messages=[{"role": "user", "content": "写一篇文章"}],
        stream=True
    ):
        print(chunk["message"]["content"], end="", flush=True)

# 或手动管理客户端
client = YzOpenAI(provider="volcengine", api_key="your-api-key")
result = client.chat.completion(
    model="doubao-pro-32k",
    messages=[{"role": "user", "content": "你好"}]
)
print(result["message"]["content"])
client.close()

2. Podcast TTS - 生成播客

import asyncio
from yz_openai import YzOpenAI

async def main():
    client = YzOpenAI(
        provider="volcengine",
        app_id="your-app-id",
        access_key="your-access-key"
    )

    # 根据文档URL生成
    result = await client.podcast.generate({
        "action": 0,
        "input_url": "https://example.com/document.pdf",
        "speakers": [
            "zh_male_dayixiansheng_v2_saturn_bigtts",
            "zh_female_mizaitongxue_v2_saturn_bigtts"
        ],
        "audio_format": "mp3"
    })

    # 根据文本生成
    result = await client.podcast.generate({
        "action": 0,
        "input_text": "人工智能正在改变我们的生活...",
        "speakers": [
            "zh_male_dayixiansheng_v2_saturn_bigtts",
            "zh_female_mizaitongxue_v2_saturn_bigtts"
        ]
    })

    # 根据对话文本生成
    result = await client.podcast.generate({
        "action": 3,
        "nlp_texts": [
            {"speaker": "zh_male_dayixiansheng_v2_saturn_bigtts", "text": "你好"},
            {"speaker": "zh_female_mizaitongxue_v2_saturn_bigtts", "text": "你好"}
        ]
    })

    # 保存音频
    with open("podcast.mp3", "wb") as f:
        f.write(result.audio_data)

    client.close()

asyncio.run(main())

3. ASR 语音识别

import asyncio
from yz_openai import YzOpenAI

async def main():
    client = YzOpenAI(
        provider="volcengine",
        app_id="your-app-id",
        access_key="your-access-key"
    )

    # 使用URL
    result = await client.asr.transcribe({
        "file_url": "https://example.com/audio.mp3"
    })

    # 使用本地文件
    result = await client.asr.transcribe({
        "file_path": "/path/to/audio.mp3",
        "enable_itn": True,
        "enable_punc": True
    })

    print(f"识别文本: {result.text}")
    client.close()

asyncio.run(main())

异常处理

from yz_openai import YzOpenAI
from yz_openai.base.exceptions import YzOpenAIException, YzOpenAIErrorCode

# Chat 异常处理(同步)
try:
    client = YzOpenAI(provider="volcengine", api_key="your-api-key")
    result = client.chat.completion(
        model="doubao-pro-32k",
        messages=[{"role": "user", "content": "你好"}]
    )
except YzOpenAIException as e:
    print(f"错误码: {e.code}")
    print(f"错误信息: {e.message}")
finally:
    client.close()

# ASR/TTS/Podcast 异常处理(异步)
import asyncio

async def main():
    try:
        client = YzOpenAI(
            provider="volcengine",
            app_id="your-app-id",
            access_key="your-access-key"
        )
        result = await client.asr.transcribe({"file_url": "..."})
    except YzOpenAIException as e:
        print(f"错误码: {e.code}")
        print(f"错误信息: {e.message}")
    finally:
        client.close()

asyncio.run(main())

YZ-OpenAI - 让 LLM 调用更简单 🚀

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yz_openai-0.1.5.tar.gz (35.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

yz_openai-0.1.5-py3-none-any.whl (38.2 kB view details)

Uploaded Python 3

File details

Details for the file yz_openai-0.1.5.tar.gz.

File metadata

  • Download URL: yz_openai-0.1.5.tar.gz
  • Upload date:
  • Size: 35.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for yz_openai-0.1.5.tar.gz
Algorithm Hash digest
SHA256 15847044257830788d6a8dfeabadaea41e16b633477b902926cee80ad03392c6
MD5 e761aa3b8cab26ebc99cf204709f6b05
BLAKE2b-256 613720c1224663aad42a20c91cbde74eb64b2909870b0394f1031a4eee024d74

See more details on using hashes here.

File details

Details for the file yz_openai-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: yz_openai-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 38.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for yz_openai-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 0c7c8710b3e1cbce70296b14309856efccb3ac2e8e73ceca6b67e7c89d177829
MD5 29273b7c188843464e6faeecee3db7f2
BLAKE2b-256 d6761e8cd8aa0240fd1e5c09ddc3c0667f2c9aa4a60abdef58bfd257b4a132f6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page