Have you ever wondered how to build a multilingual application that can effortlessly translate text from English to other languages? Imagine creating your very own translation tool, leveraging the power of LangChain to handle the heavy lifting. In this article, we will learn how to build a basic application using LangChain to translate text from English to another language. Even though it’s a simple example, it provides a foundational understanding of some key LangChain concepts and workflows. Let’s build an LLM Application with LCEL.
By the end of this article, we will have a better understanding of the following points:
Here are the steps to build an LLM Application with LCEL:
Install the necessary libraries for LangChain and FastAPI:
!pip install langchain
!pip install -qU langchain-openai
!pip install fastapi
!pip install uvicorn
!pip install langserve[all]
In your Jupyter Notebook, import the necessary modules and input your OpenAI API key:
import getpass
import os
os.environ["OPENAI_API_KEY"] = getpass.getpass('Enter your OpenAI API Key:')
Next, instantiate the GPT-4 model for the translation task:
from langchain_openai import ChatOpenAI
model = ChatOpenAI(model="gpt-4")
We will now define a system message to specify the translation task (English to Japanese) and a human message to input the text to be translated.
from langchain_core.messages import HumanMessage, SystemMessage
messages = [
SystemMessage(content="Translate the following from English into Japanese"),
HumanMessage(content="I love programming in Python!"),
]
# Invoke the model with the messages
response = model.invoke(messages)
response.content
The output of the model is more than just a string — it includes metadata. If we want to extract just the text of the translation, we can use an output parser:
from langchain_core.output_parsers import StrOutputParser
parser = StrOutputParser()
parsed_result = parser.invoke(response)
parsed_result
Now let’s chain the model and the output parser together using the | operator:
The use of | to chain the model and parser allows for a more streamlined process, where the output of the model is directly processed by the parser, resulting in the final output (translated_text) being extracted directly from the model’s response. This approach enhances code readability and efficiency in handling data transformations.
chain = model | parser
translated_text = chain.invoke(messages)
translated_text
To make the translation dynamic, we can create a prompt template. This way, we can input any English text for translation into Japanese.
from langchain_core.prompts import ChatPromptTemplate
system_template = "Translate the following text into Japanese:"
prompt_template = ChatPromptTemplate.from_messages([
('system', system_template),
('user', '{text}')
])
# Generate a structured message
result = prompt_template.invoke({"text": "I love programming in Python!"})
result.to_messages()
We can now chain the prompt template, the language model, and the output parser to make the translation seamless:
chain = prompt_template | model | parser
final_translation = chain.invoke({"text": "I love programming in Python!"})
final_translation
To enable debugging and tracing with LangSmith, make sure your environment variables are set correctly:
os.environ["LANGCHAIN_TRACING_V2"] = "true"
os.environ["LANGCHAIN_API_KEY"] = getpass.getpass('Enter your LangSmith API Key: ')
LangSmith will help trace the workflow as your chain becomes more complex, showing each step in the process.
To deploy your English-to-Japanese translation app as a REST API using LangServe, create a new Python file (e.g., serve.py or Untitled7.py):
from fastapi import FastAPI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI
from langserve import add_routes
import os
from langchain_openai import ChatOpenAI
# Set the OpenAI API key
os.environ["OPENAI_API_KEY"] = "Put your api here"
# Create the model instance
model = ChatOpenAI()
# Set up the components
system_template = "Translate the following text into Japanese:"Langsmithapi
prompt_template = ChatPromptTemplate.from_messages([
('system', system_template),
('user', '{text}')
])
model = ChatOpenAI()
parser = StrOutputParser()
# Chain components
chain = prompt_template | model | parser
# FastAPI setup
app = FastAPI(title="LangChain English to Japanese Translation API", version="1.0")
add_routes(app, chain, path="/chain")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="localhost", port=8000)
To run the server, execute the following command in the terminal:
python Untitled7.py
Your translation app will now be running at http://localhost:8000. You can test the API using the /chain/playground endpoint to interact with the translation API.
You can interact with the API using LangServe’s RemoteRunnable:
from langserve import RemoteRunnable
remote_chain = RemoteRunnable("http://localhost:8000/chain/")
translated_text = remote_chain.invoke({"text": "I love programming in Python!"})
print(translated_text)
In this tutorial, we built an English-to-Japanese translation app using LangChain (LLM Application with LCEL). We created a flexible and scalable translation API by chaining components like prompt templates, language models, and output parsers. You can now modify it to translate into other languages or expand its functionality to include language detection or more complex workflows.
If you are looking for a Generative AI course online, then explore: GenAI Pinnacle Program
Ans. LangChain is a framework that simplifies the process of working with language models (LLMs) by chaining various components such as prompt templates, language models, and output parsers. In this app, LangChain is used to build a translation workflow, from inputting text to translating it into another language.
Ans. The SystemMessage defines the task for the language model (e.g., “Translate the following from English into Japanese”), while the HumanMessage contains the actual text you want to translate.
Ans. A Prompt Template allows you to dynamically create a structured prompt for the LLM by defining placeholders (e.g., text to be translated) in the template. This makes the translation process flexible, as you can input different texts and reuse the same structure.
Ans. LCEL enables you to seamlessly chain components. In this app, components such as the prompt template, the language model, and the output parser are chained using the | operator. This simplifies the workflow by connecting different steps in the translation process.
Ans. LangSmith is a tool for debugging and tracing your LangChain workflows. As your app becomes more complex, LangSmith helps track each step and provides insights into performance and data flow, aiding in troubleshooting and optimization.