RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
OpenAITTS (#2493)
liuhuad545633a6ca0
diff --git a/conf/llm_factories.json b/conf/llm_factories.jsonindex 848ca3b8ac..6aece225ef 100644--- a/conf/llm_factories.json+++ b/conf/llm_factories.json@@ -77,6 +77,12 @@"tags": "LLM,CHAT,IMAGE2TEXT","max_tokens": 765,"model_type": "image2text"+ },+ {+ "llm_name": "tts-1",+ "tags": "TTS",+ "max_tokens": 2048,+ "model_type": "tts"}]},diff --git a/rag/llm/__init__.py b/rag/llm/__init__.pyindex 8868fce581..fbcea43b73 100644--- a/rag/llm/__init__.py+++ b/rag/llm/__init__.py@@ -138,5 +138,6 @@ Seq2txtModel = {TTSModel = {"Fish Audio": FishAudioTTS,- "Tongyi-Qianwen": QwenTTS+ "Tongyi-Qianwen": QwenTTS,+ "OpenAI":OpenAITTS}\ No newline at end of filediff --git a/rag/llm/tts_model.py b/rag/llm/tts_model.pyindex 8bf930caab..4ffbc521cf 100644--- a/rag/llm/tts_model.py+++ b/rag/llm/tts_model.py@@ -14,6 +14,7 @@# limitations under the License.#+import requestsfrom typing import Annotated, Literalfrom abc import ABCimport httpx@@ -23,6 +24,8 @@ from rag.utils import num_tokens_from_stringimport jsonimport reimport time++class ServeReferenceAudio(BaseModel):audio: bytestext: str@@ -52,7 +55,7 @@ class Base(ABC):def tts(self, audio):pass-+def normalize_text(self, text):return re.sub(r'(\*\*|##\d+\$\$|#)', '', text)@@ -78,13 +81,13 @@ class FishAudioTTS(Base):with httpx.Client() as client:try:with client.stream(- method="POST",- url=self.base_url,- content=ormsgpack.packb(- request, option=ormsgpack.OPT_SERIALIZE_PYDANTIC- ),- headers=self.headers,- timeout=None,+ method="POST",+ url=self.base_url,+ content=ormsgpack.packb(+ request, option=ormsgpack.OPT_SERIALIZE_PYDANTIC+ ),+ headers=self.headers,+ timeout=None,) as response:if response.status_code == HTTPStatus.OK:for chunk in response.iter_bytes():@@ -101,7 +104,7 @@ class FishAudioTTS(Base):class QwenTTS(Base):def __init__(self, key, model_name, base_url=""):import dashscope-+self.model_name = model_namedashscope.api_key = key@@ -109,11 +112,11 @@ class QwenTTS(Base):from dashscope.api_entities.dashscope_response import SpeechSynthesisResponsefrom dashscope.audio.tts import ResultCallback, SpeechSynthesizer, SpeechSynthesisResultfrom collections import deque-+class Callback(ResultCallback):def __init__(self) -> None:self.dque = deque()-+def _run(self):while True:if not self.dque:@@ -144,13 +147,40 @@ class QwenTTS(Base):text = self.normalize_text(text)callback = Callback()SpeechSynthesizer.call(model=self.model_name,- text=text,- callback=callback,- format="mp3")+ text=text,+ callback=callback,+ format="mp3")try:for data in callback._run():yield datayield num_tokens_from_string(text)-+except Exception as e:- raise RuntimeError(f"**ERROR**: {e}")\ No newline at end of file+ raise RuntimeError(f"**ERROR**: {e}")+++class OpenAITTS(Base):+ def __init__(self, key, model_name="tts-1", base_url="https://api.openai.com/v1"):+ self.api_key = key+ self.model_name = model_name+ self.base_url = base_url+ self.headers = {+ "Authorization": f"Bearer {self.api_key}",+ "Content-Type": "application/json"+ }++ def tts(self, text, voice="alloy"):+ text = self.normalize_text(text)+ payload = {+ "model": self.model_name,+ "voice": voice,+ "input": text+ }++ response = requests.post(f"{self.base_url}/audio/speech", headers=self.headers, json=payload, stream=True)++ if response.status_code != 200:+ raise Exception(f"**Error**: {response.status_code}, {response.text}")+ for chunk in response.iter_content(chunk_size=1024):+ if chunk:+ yield chunk