Aily CLI
Project description
aily-py
安装
pip install aily-py
快速开始
复制example/.env.sample到项目根目录下,并重命名为.env,配置相关必填参数
from aily import Aily
from loguru import logger
from aily.tools import speech_to_text, text_to_speech
def record_end_handler(pcm_data):
# 语音转文字
text = speech_to_text(pcm_data)
logger.debug("转换后的文字为: {0}".format(text))
# 调用LLM
aily.send_message(text)
def invoke_end_handler(data):
logger.debug("回答:{0}".format(data))
# 文字转语音
speech_data = text_to_speech(data)
# 播放语音
aily.play(speech_data)
aily = Aily(".env")
aily.on_record_end.subscribe(lambda i: record_end_handler(i))
aily.on_invoke_end.subscribe(lambda i: invoke_end_handler(i))
aily.run()
Aily类
类初始化
-
配置文件形式初始化(推荐,便于与提供aily_config_tool工具配合使用)
aily = Aily(".env")
-
直接初始化
aily = Aily()
环境变量配置说明
.env配置文件各参数说明
-
PORT:串口设备路径(必填) -
BAUDRATE: 波特率(必填) -
SERIAL_TIMEOUT: 串口读取超时时间 -
CONVERSATION_MODE: 对话模式(single、multi、manual) -
LLM_URL: 大模型服务地址(必填) -
LLM_KEY: 大模型key(必填) -
LLM_MODEL: 使用的模型(必填) -
LLM_TEMPERATURE: 温度 -
LLM_PRE_PROMPT: 前置提示 -
LLM_MAX_TOKEN_LENGTH: 最大token长度 -
LLM_VISION_URL: 视觉模型服务地址 -
LLM_VISION_KEY: 视觉模型key -
LLM_VISION_MODEL: 视觉模型 -
STT_MODEL: 语音转文字使用的模型(必填), 可选值为:baidu,azure,whisper -
STT_KEY: 语音转文字使用的key -
STT_URL: 语音转文字服务地址 -
STT_SECRET_KEY: 语音转文字使用的secret_key(baidu需要) -
STT_LANG: 语音转文字使用的语言,如'zh-CN',根据选择的模型填写 -
STT_LOCATION: 选择的模型为azure时需要填写,默认为eastasia -
TTS_MODEL: 文字转语音使用的模型(必填), 可选值为:baidu,edge,tts-1,azure -
TTS_URL: 文字转语音服务的地址 -
TTS_KEY: 文字转语音服务的key -
TTS_ROLE: 文字转语音角色,根据模型提供的选择 -
TTS_LANG: 语言类型,如'zh-CN' -
TTS_LOCATION: 选择的模型为azure时需要填写,默认为eastasia -
USE_AILY_TTS: 是否使用aily的tts模块 -
TTS_PORT: aily tts模块的串口设备路径 -
TTS_BAUDRATE: aily tts模块的波特率 -
DB_NAME: 数据库名称(使用的是sqlite来保存对话记录) -
WAIT_WORDS_LOOP_PLAY: 等待音是否循环播放
Aily对象可订阅的事件
wakeup: 唤醒事件on_record_begin: 语音录制开始事件on_record_end: 语音录制结束事件on_play_begin: 语音播放开始事件on_play_end: 语音播放结束事件on_recognition: 离线指令识别on_direction: 音源方向识别on_invoke_begin: LLM调用开始事件on_invoke_end: LLM调用结束事件on_function_call: 功能调用事件on_custom_invoke: 自定义模型调用事件on_custom_invoke_vision: 自定义视觉模型调用事件on_invalid_msg: 无效语音事件(语音转文字后的结果为空,则发起此事件)on_conversation_records: 对话记录获取事件
Aily对象属性
配置文件方式初始化,相关参数配置可省略
port:配置串口设备路径baudrate:配置波特率serial_timeout:配置串口读取超时时间conversation_mode:配置对话模式,值为:single/multi/manual,默认为singlellm_server:配置大模型服务地址llm_key:配置大模型keyllm_model:配置使用的模型llm_temperature:配置温度llm_pre_prompt:配置系统提示词llm_max_token_length:配置最大token长度llm_vision_server:配置视觉模型服务地址llm_vision_key:配置视觉模型keyllm_vision_model:配置视觉模型use_aily_tts:配置是否使用aily tts模块,值为:True/False,默认为Falsetts_port:配置aily tts串口tts_baudrate:配置aily tts波特率custom_invoke:是否自定义模型调用,值为:True/False,默认为Falsecustom_invoke_vision:是否自定义视觉模型调用,值为:True/False,默认为Falseauto_play_wait_words: 配置是否自动播放等待音,值为:True/False,默认为Trueloop_play_wait_words: 配置等待音是否循环播放,值为:True/False,默认为Falseconversation_expired_at: 配置对话记录过期时间,单位为秒,默认为300
Aily对象方法
init(): 执行初始化run(): 启动服务send_message(message): 发送消息到LLMplay(data): 播放ttsplay_mp3(data): 播放mp3,如等待音,提示音等invoke_reply(message): 自定义模型调用后的结果反馈reply_message(message): function call函数处理后的结果反馈register_tools(tools): 注册工具choice_tool(tool_name): 选择工具
工具调用
语音转文字
speech_to_text
- 使用方法
from aily.tools import speech_to_text
from aily.models import STTBaiduOptions, ...
speech_to_text(pcm_data, model='baidu', options=STTBaiduOptions)
- 说明
- pcm_data:为pcm文件内容
- model:选择的转换模型,默认提供的可选有
baidu、azure、whisper - options:模型参数,模型参数的选择根据模型的选择有些差异
- baidu:
https://cloud.baidu.com/doc/SPEECH/s/0lbxfnc9b - azure:
https://github.com/Uberi/speech_recognition - whisper:
https://platform.openai.com/docs/guides/speech-to-text/quickstart
- baidu:
文字转语音
text_to_speech
- 使用方法
from aily.tools import text_to_speech
from aily.models import TTSBaiduOptions, ...
text_to_speech(text, model='baidu', options=TTSBaiduOptions)
- 说明
- text: 需要转换为语音的文本
- model:选择的转换模型,默认提供的可选有
baidu、edge、tts-1、azure - options: 模型参数,模型参数的选择根据模型的选择有些差异
- baidu:
https://cloud.baidu.com/doc/SPEECH/s/plbxhh4be - edge:
https://github.com/rany2/edge-tts - tts-1:
https://platform.openai.com/docs/guides/text-to-speech/overview - azure:
https://learn.microsoft.com/zh-cn/azure/ai-services/speech-service/get-started-text-to-speech?tabs=windows%2Cterminal&pivots=programming-language-python
- baidu:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
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
File details
Details for the file aily_py-2.0.1.tar.gz.
File metadata
- Download URL: aily_py-2.0.1.tar.gz
- Upload date:
- Size: 26.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c36b518e12e9d62ff8a6ac2612452877842dc1d3a58a14d94ba151920cd9d0ed
|
|
| MD5 |
4e4731fcb1e380d7c9e908e1fbfe4047
|
|
| BLAKE2b-256 |
c05d5637a99075ae237a42d1797b258e3ed9cc1880eb5d4958a3b7d17d958b5c
|
File details
Details for the file aily_py-2.0.1-py3-none-any.whl.
File metadata
- Download URL: aily_py-2.0.1-py3-none-any.whl
- Upload date:
- Size: 33.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
431243d0f026b0af4b19e8115e79d03c7996148ffcea5801b0fdb8ac9aa867a1
|
|
| MD5 |
e63d8b147c8c23451e102c53e3d845fb
|
|
| BLAKE2b-256 |
39e162c2f0c7a632c20f28718e91a6949c89d820da72ac746ca5cfd24ba53927
|