mirror of
https://github.com/hwchase17/langchain
synced 2024-11-18 09:25:54 +00:00
ed58eeb9c5
Moved the following modules to new package langchain-community in a backwards compatible fashion: ``` mv langchain/langchain/adapters community/langchain_community mv langchain/langchain/callbacks community/langchain_community/callbacks mv langchain/langchain/chat_loaders community/langchain_community mv langchain/langchain/chat_models community/langchain_community mv langchain/langchain/document_loaders community/langchain_community mv langchain/langchain/docstore community/langchain_community mv langchain/langchain/document_transformers community/langchain_community mv langchain/langchain/embeddings community/langchain_community mv langchain/langchain/graphs community/langchain_community mv langchain/langchain/llms community/langchain_community mv langchain/langchain/memory/chat_message_histories community/langchain_community mv langchain/langchain/retrievers community/langchain_community mv langchain/langchain/storage community/langchain_community mv langchain/langchain/tools community/langchain_community mv langchain/langchain/utilities community/langchain_community mv langchain/langchain/vectorstores community/langchain_community mv langchain/langchain/agents/agent_toolkits community/langchain_community mv langchain/langchain/cache.py community/langchain_community mv langchain/langchain/adapters community/langchain_community mv langchain/langchain/callbacks community/langchain_community/callbacks mv langchain/langchain/chat_loaders community/langchain_community mv langchain/langchain/chat_models community/langchain_community mv langchain/langchain/document_loaders community/langchain_community mv langchain/langchain/docstore community/langchain_community mv langchain/langchain/document_transformers community/langchain_community mv langchain/langchain/embeddings community/langchain_community mv langchain/langchain/graphs community/langchain_community mv langchain/langchain/llms community/langchain_community mv langchain/langchain/memory/chat_message_histories community/langchain_community mv langchain/langchain/retrievers community/langchain_community mv langchain/langchain/storage community/langchain_community mv langchain/langchain/tools community/langchain_community mv langchain/langchain/utilities community/langchain_community mv langchain/langchain/vectorstores community/langchain_community mv langchain/langchain/agents/agent_toolkits community/langchain_community mv langchain/langchain/cache.py community/langchain_community ``` Moved the following to core ``` mv langchain/langchain/utils/json_schema.py core/langchain_core/utils mv langchain/langchain/utils/html.py core/langchain_core/utils mv langchain/langchain/utils/strings.py core/langchain_core/utils cat langchain/langchain/utils/env.py >> core/langchain_core/utils/env.py rm langchain/langchain/utils/env.py ``` See .scripts/community_split/script_integrations.sh for all changes
81 lines
2.6 KiB
Python
81 lines
2.6 KiB
Python
import tempfile
|
|
from enum import Enum
|
|
from typing import Any, Dict, Optional, Union
|
|
|
|
from langchain_core.callbacks import CallbackManagerForToolRun
|
|
from langchain_core.pydantic_v1 import root_validator
|
|
from langchain_core.tools import BaseTool
|
|
from langchain_core.utils import get_from_dict_or_env
|
|
|
|
|
|
def _import_elevenlabs() -> Any:
|
|
try:
|
|
import elevenlabs
|
|
except ImportError as e:
|
|
raise ImportError(
|
|
"Cannot import elevenlabs, please install `pip install elevenlabs`."
|
|
) from e
|
|
return elevenlabs
|
|
|
|
|
|
class ElevenLabsModel(str, Enum):
|
|
"""Models available for Eleven Labs Text2Speech."""
|
|
|
|
MULTI_LINGUAL = "eleven_multilingual_v1"
|
|
MONO_LINGUAL = "eleven_monolingual_v1"
|
|
|
|
|
|
class ElevenLabsText2SpeechTool(BaseTool):
|
|
"""Tool that queries the Eleven Labs Text2Speech API.
|
|
|
|
In order to set this up, follow instructions at:
|
|
https://docs.elevenlabs.io/welcome/introduction
|
|
"""
|
|
|
|
model: Union[ElevenLabsModel, str] = ElevenLabsModel.MULTI_LINGUAL
|
|
|
|
name: str = "eleven_labs_text2speech"
|
|
description: str = (
|
|
"A wrapper around Eleven Labs Text2Speech. "
|
|
"Useful for when you need to convert text to speech. "
|
|
"It supports multiple languages, including English, German, Polish, "
|
|
"Spanish, Italian, French, Portuguese, and Hindi. "
|
|
)
|
|
|
|
@root_validator(pre=True)
|
|
def validate_environment(cls, values: Dict) -> Dict:
|
|
"""Validate that api key exists in environment."""
|
|
_ = get_from_dict_or_env(values, "eleven_api_key", "ELEVEN_API_KEY")
|
|
|
|
return values
|
|
|
|
def _run(
|
|
self, query: str, run_manager: Optional[CallbackManagerForToolRun] = None
|
|
) -> str:
|
|
"""Use the tool."""
|
|
elevenlabs = _import_elevenlabs()
|
|
try:
|
|
speech = elevenlabs.generate(text=query, model=self.model)
|
|
with tempfile.NamedTemporaryFile(
|
|
mode="bx", suffix=".wav", delete=False
|
|
) as f:
|
|
f.write(speech)
|
|
return f.name
|
|
except Exception as e:
|
|
raise RuntimeError(f"Error while running ElevenLabsText2SpeechTool: {e}")
|
|
|
|
def play(self, speech_file: str) -> None:
|
|
"""Play the text as speech."""
|
|
elevenlabs = _import_elevenlabs()
|
|
with open(speech_file, mode="rb") as f:
|
|
speech = f.read()
|
|
|
|
elevenlabs.play(speech)
|
|
|
|
def stream_speech(self, query: str) -> None:
|
|
"""Stream the text as speech as it is generated.
|
|
Play the text in your speakers."""
|
|
elevenlabs = _import_elevenlabs()
|
|
speech_stream = elevenlabs.generate(text=query, model=self.model, stream=True)
|
|
elevenlabs.stream(speech_stream)
|