-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
91 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import textwrap | ||
from dataclasses import dataclass, field | ||
from typing import Dict | ||
|
||
from langchain_core.output_parsers import StrOutputParser | ||
from langchain_core.prompts import ChatPromptTemplate | ||
from langchain_core.runnables import RunnableSerializable | ||
|
||
from ..core import BaseChatModelConverter | ||
|
||
_template = ChatPromptTemplate.from_template( | ||
textwrap.dedent( | ||
""" | ||
Translate the following text to {language}. | ||
Please maintain the original meaning and context as closely as possible. | ||
Input text: | ||
{input} | ||
Translated text: | ||
""" | ||
) | ||
) | ||
|
||
|
||
@dataclass | ||
class TranslationConverter(BaseChatModelConverter): | ||
language: str | ||
prompt: ChatPromptTemplate = field(default_factory=lambda: _template) | ||
_chain: RunnableSerializable[Dict, str] = field(init=False) | ||
|
||
def __post_init__(self) -> None: | ||
self._chain = self.prompt | self.chat_model | StrOutputParser() | ||
|
||
def _convert(self, prompt: str) -> str: | ||
return self._chain.invoke({"input": prompt, "language": self.language}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters