mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-08 14:31:55 +00:00
Integration with eleven labs
This commit is contained in:
@@ -32,6 +32,7 @@ from langchain.tools.requests.tool import (
|
||||
RequestsPostTool,
|
||||
RequestsPutTool,
|
||||
)
|
||||
from langchain.tools.eleven_labs.text2speech import ElevenLabsText2SpeechTool
|
||||
from langchain.tools.scenexplain.tool import SceneXplainTool
|
||||
from langchain.tools.searx_search.tool import SearxSearchResults, SearxSearchRun
|
||||
from langchain.tools.shell.tool import ShellTool
|
||||
@@ -284,6 +285,9 @@ def _get_dataforseo_api_search(**kwargs: Any) -> BaseTool:
|
||||
def _get_dataforseo_api_search_json(**kwargs: Any) -> BaseTool:
|
||||
return DataForSeoAPISearchResults(api_wrapper=DataForSeoAPIWrapper(**kwargs))
|
||||
|
||||
def _get_eleven_labs_text2speech() -> BaseTool:
|
||||
return ElevenLabsText2SpeechTool()
|
||||
|
||||
|
||||
_EXTRA_LLM_TOOLS: Dict[
|
||||
str,
|
||||
@@ -340,6 +344,9 @@ _EXTRA_OPTIONAL_TOOLS: Dict[str, Tuple[Callable[[KwArg(Any)], BaseTool], List[st
|
||||
_get_dataforseo_api_search_json,
|
||||
["api_login", "api_password", "aiosession"],
|
||||
),
|
||||
"eleven_labs_text2speech": (
|
||||
_get_eleven_labs_text2speech, ["eleven_api_key"]
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
|
@@ -44,6 +44,7 @@ from langchain.tools.edenai import (
|
||||
EdenAiTextToSpeechTool,
|
||||
EdenaiTool,
|
||||
)
|
||||
from langchain.tools.eleven_labs.text2speech import ElevenLabsText2SpeechTool
|
||||
from langchain.tools.file_management import (
|
||||
CopyFileTool,
|
||||
DeleteFileTool,
|
||||
@@ -167,6 +168,7 @@ __all__ = [
|
||||
"EdenAiSpeechToTextTool",
|
||||
"EdenAiTextModerationTool",
|
||||
"EdenaiTool",
|
||||
"ElevenLabsText2SpeechTool",
|
||||
"ExtractHyperlinksTool",
|
||||
"ExtractTextTool",
|
||||
"FileSearchTool",
|
||||
|
8
libs/langchain/langchain/tools/eleven_labs/__init__.py
Normal file
8
libs/langchain/langchain/tools/eleven_labs/__init__.py
Normal file
@@ -0,0 +1,8 @@
|
||||
"""Eleven Labs Services Tools."""
|
||||
|
||||
from langchain.tools.eleven_labs.text2speech import (
|
||||
ElevenLabsText2SpeechTool
|
||||
)
|
||||
|
||||
|
||||
__all__ = [ElevenLabsText2SpeechTool]
|
86
libs/langchain/langchain/tools/eleven_labs/text2speech.py
Normal file
86
libs/langchain/langchain/tools/eleven_labs/text2speech.py
Normal file
@@ -0,0 +1,86 @@
|
||||
import tempfile
|
||||
from typing import Dict
|
||||
|
||||
from langchain.pydantic_v1 import root_validator
|
||||
from langchain.tools.base import BaseTool
|
||||
from langchain.utils import get_from_dict_or_env
|
||||
|
||||
|
||||
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
|
||||
"""
|
||||
|
||||
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 _text2speech(self, text: str) -> str:
|
||||
try:
|
||||
from elevenlabs import generate
|
||||
|
||||
except ImportError:
|
||||
raise ImportError(
|
||||
"elevenlabs is not installed. "
|
||||
"Run `pip install elevenlabs` to install."
|
||||
)
|
||||
|
||||
speech = generate(text=text, model='eleven_multilingual_v1')
|
||||
with tempfile.NamedTemporaryFile(
|
||||
mode="bx", suffix=".wav", delete=False
|
||||
) as f:
|
||||
f.write(speech)
|
||||
return f.name
|
||||
|
||||
def _run(self, query: str) -> str:
|
||||
"""Use the tool."""
|
||||
try:
|
||||
speech_file = self._text2speech(query)
|
||||
return speech_file
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"Error while running ElevenLabsText2SpeechTool: {e}")
|
||||
|
||||
def play(self, speech_file: str) -> None:
|
||||
"""Play the text as speech."""
|
||||
try:
|
||||
from elevenlabs import play
|
||||
|
||||
except ImportError:
|
||||
raise ImportError(
|
||||
"elevenlabs is not installed. "
|
||||
"Run `pip install elevenlabs` to install."
|
||||
)
|
||||
with open(speech_file, mode="rb") as f:
|
||||
speech = f.read()
|
||||
|
||||
play(speech)
|
||||
|
||||
def stream(self, query: str) -> None:
|
||||
"""Stream the text as speech."""
|
||||
|
||||
try:
|
||||
from elevenlabs import stream, generate
|
||||
|
||||
except ImportError:
|
||||
raise ImportError(
|
||||
"elevenlabs is not installed. "
|
||||
"Run `pip install elevenlabs` to install."
|
||||
)
|
||||
|
||||
speech_stream = generate(text=query, model='eleven_multilingual_v1', stream=True)
|
||||
stream(speech_stream)
|
||||
|
@@ -36,6 +36,7 @@ _EXPECTED = [
|
||||
"EdenAiTextModerationTool",
|
||||
"EdenAiTextToSpeechTool",
|
||||
"EdenaiTool",
|
||||
"ElevenLabsText2SpeechTool",
|
||||
"ExtractHyperlinksTool",
|
||||
"ExtractTextTool",
|
||||
"FileSearchTool",
|
||||
|
Reference in New Issue
Block a user