File size: 1,258 Bytes
98346d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from langchain_openai import ChatOpenAI
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
from src.utils.llm import get_fast_llm



# Prompt template for translation
TRANSLATION_PROMPT = PromptTemplate(
    input_variables=["text", "source_lang", "target_lang"],
    template=(
                """You are an expert translator.
                    Translate the following text from {source_lang} to {target_lang}.
                    Preserve the original tone, style, and meaning as accurately as possible.

Text to translate:
{text}
Translation:"""
))

#Functions to translate text
def translate_text(text: str, source_lang: str, target_lang: str) -> str:
    """
    Translates text from source language to target language.
    Args:
        text (str): The text to translate.
        source_lang (str): The language of the input text.
        target_lang (str): The language to translate the text into.
    Returns:   
        str: The translated text.
    """
    llm = get_fast_llm()
    chain = TRANSLATION_PROMPT | llm | StrOutputParser()
    result = chain.invoke({
        "text": text,
        "source_lang": source_lang,
        "target_lang": target_lang
    })
    
    return result