有赞 LLM 统一调用库
Project description
YZ-OpenAI
有赞 LLM 统一调用库 - 支持 Chat 对话、Podcast TTS、ASR 语音识别
安装
pip install yz-openai
使用示例
1. Chat 对话
import asyncio
from yz_openai import YzOpenAI
async def main():
async with YzOpenAI(provider="volcengine", api_key="your-api-key") as client:
# 非流式调用
result = await client.chat.completion(
model="doubao-pro-32k",
messages=[{"role": "user", "content": "你好"}]
)
print(result.message.content)
# 流式调用
async for chunk in client.chat.completion(
model="doubao-pro-32k",
messages=[{"role": "user", "content": "写一篇文章"}],
stream=True
):
print(chunk["message"]["content"], end="", flush=True)
asyncio.run(main())
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)
await 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}")
await client.close()
asyncio.run(main())
异常处理
from yz_openai.base.exceptions import YzOpenAIException, YzOpenAIErrorCode
try:
result = await client.chat.completion(...)
except YzOpenAIException as e:
print(f"错误码: {e.code}")
print(f"错误信息: {e.message}")
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.3.tar.gz
(34.8 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
yz_openai-0.1.3-py3-none-any.whl
(38.1 kB
view details)
File details
Details for the file yz_openai-0.1.3.tar.gz.
File metadata
- Download URL: yz_openai-0.1.3.tar.gz
- Upload date:
- Size: 34.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b3b33af7fa44aceae3b1b12e18db9b0f72952a80129822554fc764465c07fd0d
|
|
| MD5 |
4a0634827fe9dae28d52da915828fbdf
|
|
| BLAKE2b-256 |
80495cb378c788767843df55763286096cfe2032e1fd44f742184d6dc0a73978
|
File details
Details for the file yz_openai-0.1.3-py3-none-any.whl.
File metadata
- Download URL: yz_openai-0.1.3-py3-none-any.whl
- Upload date:
- Size: 38.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
274dffb5884725a7d3a2ef582ceafce42293eed2d3b6d2316b3f1f5e4d63d174
|
|
| MD5 |
eff9a1da40dcd13d8c263d064977f273
|
|
| BLAKE2b-256 |
c894bc64c9e77e5abbba2c46926b856fe2363a8f6584621e95c1f64236bddbdd
|