Skip to main content

Build a multilingual translation agent

In this tutorial, we will build a Multilingual Translation Agent by combining two specialized Agents: a translator agent for fast and efficient translations between languages and a research agent that provides additional context to improve translation quality. We'll then integrate these into a Team Agent for enhanced performance. By the end, you'll have a powerful agent capable of delivering accurate, context-rich translations efficiently.

Agents are intelligent entities leveraging Large Language Models (LLMs) to break down complex queries, use appropriate tools (e.g., translation, summarization), and deliver integrated results. In aiXplain, agents utilize Models, Utilities and Pipelines, with Team Agents handling multi-step tasks collaboratively.

Integrating Groq

Groq is an innovative AI hardware company that specializes in high-performance computing for machine learning workloads. Their Tensor Streaming Processor (TSP) architecture offers:

Ultra-low latency: Fast inference times for real-time applications. High throughput: Ability to process large amounts of data efficiently. Deterministic performance: Predictable and consistent results, essential for critical applications. By leveraging Groq’s hardware acceleration, we can enhance the speed and efficiency of AI models, resulting in faster and more responsive, capable agents.

Step 1: Create the Multilingual Agent

Create the Translation Tool

First, we'll create tools for translation using the AWS multilingual translation model along with the Speech Recognition Multi-Lingual (Whisper large on Groq) from our marketplace.

from aixplain.modules.agent import ModelTool, PipelineTool
from aixplain.factories import AgentFactory
from aixplain.factories import TeamAgentFactory

translation_tool = ModelTool(model="66aa869f6eb56342c26057e1") #AWS Multilingual Translation (Text)
speech_recognition_tool = ModelTool(model="66311fda6eb563279c574b71") #Groq Whiper Multilingual Speech Recognition (Audio)

Create the Translator Agent

Now, we'll create the translator agent, equipping it with the translation tool and utilizing the Groq Gemma 2 9B model as the LLM for enhanced speed and efficiency.

translator_agent = AgentFactory.create(
name="Speech Recognition and Translation Agent",
description=(
"This agent takes in audio files and extracts text from it from multiple languages"
"This agent is responsible for translating the source text from input_language to target_language."
"It generates multiple translation hypotheses, identifies ambiguities, and refines the translation based on feedback and additional examples."
),
tools = [translation_tool, speech_recognition_tool],
llm_id="675b2b96f12784b4c4018d22" #Groq Gemma 2 9B
)

Step 2: Create the Research Agent

The research agent assists the translator agent by providing additional context and examples to enhance translation quality.

Create the Scraper Tool

We'll create a scraper tool using the scraper utility to search for additional context and translation examples.

scraper_tool = ModelTool(model="66f423426eb563fa213a3531")

Create the Research Agent

Now, we'll create the research agent and equip it with the scraper tool and the Groq Gemma 7B model.

research_agent = AgentFactory.create(
name="Research Agent",
description=(
"This agent searches for additional context and translation examples from input_language to target_language"
"to help the Translator resolve ambiguities and refine the translation."
),
tools = [scraper_tool],
llm_id="660ea9ba4edcc355738532c8"
)

Step 3: Combine Agents into a Team Agent

Now that we have both the translator and research agents, we'll combine them into a team agent. This allows them to collaborate and leverage each other's capabilities.

from aixplain.factories import TeamAgentFactory

team = TeamAgentFactory.create(
name="Team of Agents for Multi Lingual Text and Audio Translation",
description="""You are a highly skilled translator tasked with providing accurate translations. Your goal is to translate the given text into the specified target language, using additional context to ensure the most precise and culturally appropriate translation possible.

To ensure the most accurate translation, you will be provided with additional context. This context may include information about the subject matter, the intended audience, cultural nuances, or specific terminology relevant to the text. Use this context to inform your translation choices and capture the intended meaning as closely as possible.

Instructions for translation:
1. Carefully read the text to be translated and the additional context provided.
2. Consider any cultural nuances, idiomatic expressions, or specialized terminology mentioned in the additional context.
3. Translate the text into the target language, ensuring that you capture not just the literal meaning, but also the tone, style, and cultural appropriateness based on the provided context.
4. Double-check your translation for accuracy and fluency in the target language.
5. Provide ONLY the final translation as your response, without any explanations, notes, or the original text.

Remember, your output MUST contain ONLY the translated text, without any additional commentary or the original text.""",

agents=[
translator_agent,
research_agent
],
llm_id="6646261c6eb563165658bbb1"
)

Step 4: Run the Agent

Let's test our agent by performing some translations.

English to French Translation

agent_response = team.run(
"""
Translate the text from English to French.

Paris announced the launch of its Green Deal, aligning with the latest EU regulations and aiming for 2050 carbon neutrality.
"""
)

import json
print(json.dumps(agent_response["data"], indent=4))
Show output

French to German

agent_response = team.run(
"""
Translate the text from French to German.

Paris a annoncé le lancement de son Green Deal, s\'alignant sur les dernières réglementations de l\'UE et visant la neutralité carbone d\'ici 2050.
"""
)

agent_response['data']['output']
Show output

German to English

agent_response = team.run(
"""
Translate the text from German to English.

Paris hat die Einführung seines Green Deals angekündigt, der sich an den neuesten EU-Vorschriften orientiert und bis 2050 Kohlenstoffneutralität anstrebt.
"""
)

agent_response['data']['output']
Show output

Arabic Audio to English

agent_response = team.run("Translate this audio file to English: ", content=["Discover aiXplain.mp3"])

agent_response['data']['output']
Show output

Hindi Audio to English

agent_response = team.run("Translate this audio file to English: ", content=["Discover aiXplain (1).mp3"])

agent_response['data']['output']
Show output

Step 5: Deploy the Agent

Once you are happy with your agent, deploy it to access the agent endpoints!

team.deploy()

Congratulations! You've successfully built a multilingual translation agent using Groq models in aiXplain. By leveraging Groq's hardware acceleration, your agents can perform tasks faster and more efficiently than traditional models.