mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-21 03:51:42 +00:00
Thank you for contributing to LangChain! - [ ] **PR title**: "package: description" - Where "package" is whichever of langchain, community, core, etc. is being modified. Use "docs: ..." for purely docs changes, "infra: ..." for CI changes. - Example: "community: add foobar LLM" - [ ] **PR message**: ***Delete this entire checklist*** and replace with - **Description:** a description of the change - **Issue:** the issue # it fixes, if applicable - **Dependencies:** any dependencies required for this change - **Twitter handle:** if your PR gets announced, and you'd like a mention, we'll gladly shout you out! - [ ] **Add tests and docs**: If you're adding a new integration, please include 1. a test for the integration, preferably unit tests that do not rely on network access, 2. an example notebook showing its use. It lives in `docs/docs/integrations` directory. - [ ] **Lint and test**: Run `make format`, `make lint` and `make test` from the root of the package(s) you've modified. See contribution guidelines for more: https://python.langchain.com/docs/contributing/ Additional guidelines: - Make sure optional dependencies are imported within a function. - Please do not add dependencies to pyproject.toml files (even optional ones) unless they are required for unit tests. - Most PRs should not touch more than one package. - Changes should be backwards compatible. - If you are adding something to community, do not re-import it in langchain. If no one reviews your PR within a few days, please @-mention one of baskaryan, efriis, eyurtsev, ccurme, vbarda, hwchase17. --------- Co-authored-by: Chester Curme <chester.curme@gmail.com>
93 lines
3.0 KiB
Python
93 lines
3.0 KiB
Python
import tempfile
|
|
from enum import Enum
|
|
from typing import Any, Dict, Optional, Union
|
|
|
|
from langchain_core.callbacks import CallbackManagerForToolRun
|
|
from langchain_core.tools import BaseTool
|
|
from langchain_core.utils import get_from_dict_or_env
|
|
from pydantic import model_validator
|
|
|
|
|
|
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_v2"
|
|
MULTI_LINGUAL_FLASH = "eleven_flash_v2_5"
|
|
MONO_LINGUAL = "eleven_flash_v2"
|
|
|
|
|
|
class ElevenLabsText2SpeechTool(BaseTool): # type: ignore[override]
|
|
"""Tool that queries the Eleven Labs Text2Speech API.
|
|
|
|
In order to set this up, follow instructions at:
|
|
https://elevenlabs.io/docs
|
|
"""
|
|
|
|
model: Union[ElevenLabsModel, str] = ElevenLabsModel.MULTI_LINGUAL
|
|
voice: str = "JBFqnCBsd6RMkjVDRZzb"
|
|
|
|
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 more than 30 languages, including English, German, Polish, "
|
|
"Spanish, Italian, French, Portuguese, and Hindi. "
|
|
)
|
|
|
|
@model_validator(mode="before")
|
|
@classmethod
|
|
def validate_environment(cls, values: Dict) -> Any:
|
|
"""Validate that api key exists in environment."""
|
|
_ = get_from_dict_or_env(values, "elevenlabs_api_key", "ELEVENLABS_API_KEY")
|
|
|
|
return values
|
|
|
|
def _run(
|
|
self, query: str, run_manager: Optional[CallbackManagerForToolRun] = None
|
|
) -> str:
|
|
"""Use the tool."""
|
|
elevenlabs = _import_elevenlabs()
|
|
client = elevenlabs.client.ElevenLabs()
|
|
try:
|
|
speech = client.text_to_speech.convert(
|
|
text=query,
|
|
model_id=self.model,
|
|
voice_id=self.voice,
|
|
output_format="mp3_44100_128",
|
|
)
|
|
with tempfile.NamedTemporaryFile(
|
|
mode="bx", suffix=".mp3", 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()
|
|
client = elevenlabs.client.ElevenLabs()
|
|
speech_stream = client.text_to_speech.convert_as_stream(
|
|
text=query, model_id=self.model, voice_id=self.voice
|
|
)
|
|
elevenlabs.stream(speech_stream)
|