Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
# Text Translation Agent

This code sample helps create agents that handle multilingual text processing, including dynamic language detection and bidirectional translation using Azure AI Translator service.
This code sample helps create agents that handle multilingual text processing, including dynamic language detection and bidirectional translation using Azure AI Translator service.

**IMPORTANT NOTE:** Starter templates, instructions, code samples and resources in this msft-agent-samples file (“samples”) are designed to assist in accelerating development of agents for specific scenarios. It is important that you review all provided resources and carefully test Agent behavior in the context of your use case: ([Learn More](https://learn.microsoft.com/en-us/legal/cognitive-services/agents/transparency-note?context=%2Fazure%2Fai-services%2Fagents%2Fcontext%2Fcontext)).

Certain Agent offerings may be subject to legal and regulatory requirements, may require licenses, or may not be suitable for all industries, scenarios, or use cases. By using any sample, you are acknowledging that Agents or other output created using that sample are solely your responsibility, and that you will comply with all applicable laws, regulations, and relevant safety standards, terms of service, and codes of conduct.

## Use Cases

1. **Text Translation**: Translate text between multiple languages for seamless communication.
2. **Business Communication**: Enable businesses to localize content for global audiences.
3. **Educational Content**: Translate learning materials to make them accessible to non-native speakers.

## Architecture Overview

The system consists of:

- An AI Agent created with Azure AI Agent Service using `gpt-4o` as the base model.
- Azure AI Translator integrated for language detection and translation.
- Azure AI Services resource for accessing language translation feature.
- A Bicep template to automate provisioning of Azure resources.

```text

+----------------+
| User Query |
| (Any Language) |
Expand All @@ -39,6 +43,7 @@ The system consists of:
## Setup Instructions

### Prerequisites

- Python 3.8+
- Azure CLI
- Azure subscription
Expand All @@ -55,55 +60,67 @@ The system consists of:
- For more info about Azure AI Translator service, see [Text translation Overview](https://learn.microsoft.com/en-us/azure/ai-services/translator/text-translation/overview)

### Steps
1. **Clone the Repository**

2. **Set Config Variables**
1. Clone the Repository in the folder of your choice

4. **Run the Agent Script**
```bash
python template.py
git clone https://github.com/azure-ai-foundry/foundry-samples
```

## ⚙️ Configuration Guide
2. Populate the config variables in config.yaml

| Parameter Name | Description |
|----------------------------------------|-----------------------------------------------------------------------------------|
| `project_endpoint` | Endpoint for the project this agent is using |
| `model_name` | Name of the model deployment to be used for the Agent |
| `translator_resource_connection_name` | Name of the translator resource connection added in the agent project |
| `connection_id` | Connection ID for the translator resource connection added in the agent project |

3. Install the requirements

It is recommended to use a virtual environment (via venv or similar) to isolate installation requirements of this sample from other samples.

```bash
pip install -r requirements.txt
```

4. Run the template script

```bash
python template.py
```

## Sample Data Instructions

You can try the sample user messages and test the interaction by invoking the agent with prompts as listed in the next section.

## Example Agent Interaction
### User:
Translate this Chinese poem to English: 白日依山尽,黄河入海流

### Agent:
At the end of the day, the Yellow River flows into the sea.
```text
>User: Translate this Chinese poem to English: 白日依山尽,黄河入海流

>Agent: At the end of the day, the Yellow River flows into the sea.

### User:
translate this text to Chinese, Spanish, and Korean: Translator Agent handles multilingual text processing, including dynamic language detection and bidirectional translation using Translator service. The translator agent is triggered when the user message has the “translation intent”.
>User: translate this text to Chinese, Spanish, and Korean: Translator Agent handles multilingual text processing, including dynamic language detection and bidirectional translation using Translator service. The translator agent is triggered when the user message has the “translation intent”.

### Agent:
Here are the translations:
> Agent: Here are the translations:

Chinese: Translator Agent 处理多语言文本,包括使用 Translator服务的动态语言检测和双向翻译。当用户消息具有 “translation intent” 时,将触发翻译代理。
Spanish: Translator Agent se encarga del procesamiento de texto multilingüe, incluida la detección dinámica de idiomas y la traducción bidireccional mediante el servicio Translator. El agente traductor se activa cuando el mensaje del usuario tiene la "intención de traducción".
Korean: Translator Agent는 Translator 서비스를 사용하여 동적 언어 감지 및 양방향 번역을 포함한 다국어 텍스트 처리를 처리합니다. 변환기 에이전트는 사용자 메시지에 "번역 의도"가 있을 때 트리거됩니다.
Chinese: Translator Agent 处理多语言文本,包括使用 Translator服务的动态语言检测和双向翻译。当用户消息具有 “translation intent” 时,将触发翻译代理。
Spanish: Translator Agent se encarga del procesamiento de texto multilingüe, incluida la detección dinámica de idiomas y la traducción bidireccional mediante el servicio Translator. El agente traductor se activa cuando el mensaje del usuario tiene la "intención de traducción".
Korean: Translator Agent는 Translator 서비스를 사용하여 동적 언어 감지 및 양방향 번역을 포함한 다국어 텍스트 처리를 처리합니다. 변환기 에이전트는 사용자 메시지에 "번역 의도"가 있을 때 트리거됩니다.

### User:
localize this text to Japanese and Germany: The agent can detect the source language for the input text unless the source language is defined in the user message.
> User: localize this text to Japanese and Germany: The agent can detect the source language for the input text unless the source language is defined in the user message.

### Agent:
Here are the localized texts:
> Agent: Here are the localized texts:

Japanese: エージェントは、入力テキストのソース言語を検出できます。ただし、ソース言語がユーザー・メッセージで定義されていない場合に限ります。
German: Der Agent kann die Ausgangssprache für den Eingabetext erkennen, es sei denn, die Ausgangssprache ist in der Benutzermeldung definiert.
Japanese: エージェントは、入力テキストのソース言語を検出できます。ただし、ソース言語がユーザー・メッセージで定義されていない場合に限ります。
German: Der Agent kann die Ausgangssprache für den Eingabetext erkennen, es sei denn, die Ausgangssprache ist in der Benutzermeldung definiert.

## Customization Tips

- Modify the system instructions in `template.py` to your specif needs (e.g., special handling on some terminologies).
- Extend the agent with other useful tools or APIs (using OpenAPI spec).

## License

This project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for details.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ model_name:
# The connection name of the above resource when added in the Azure AI Foundry portal to your project
translator_resource_connection_name:

# the region when the translator resource/AI service resource was created.
translator_resource_region:

# Go to the page of above connection in the Azure AI Foundry portal, then construct the connection ID by append "/connections/<language_resource_connection_name>" to the wsid parameter value in the page URL.
# For example, if the connection page URL contains "wsid=/subscriptions/<subscription_id>/resourceGroups/<resource_group_name>/providers/Microsoft.CognitiveServices/accounts/<account_name>/projects/<project_name>",
# Then the connection ID is /subscriptions/<subscription_id>/resourceGroups/<resource_group_name>/providers/Microsoft.CognitiveServices/accounts/<account_name>/projects/<project_name>/connections/<language_resource_connection_name>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
artifacts-keyring==0.4.0
azure-ai-agents==1.0.0a20250506003
#azure-ai-agents==1.0.0a20250506003
azure-ai-agents==1.0.0
azure-core==1.34.0
azure-identity==1.22.0
azure-storage-blob==12.26.0b1
Expand All @@ -24,3 +25,4 @@ requests==2.32.3
six==1.17.0
typing_extensions==4.13.2
urllib3==2.4.0
PyYAML==6.0.2
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
# Create the agent
agent = agents_client.create_agent(
model=model_name,
name="My_Translator_Agent",
instructions="You are a translation agent. The user can use any language to interface with you and please answer in the same language as the customer. " +
name="ms-translator",
instructions="You are a translation agent. The user can use any language to interface with you and please answer in the same language as the user used. " +
"Any translation asks must be performed via a call to the translator_api. In case of failure to call the translator_api indicate what was the failure",
tools=translator_api_tool.definitions
)
Expand Down Expand Up @@ -73,6 +73,6 @@
last_text = msg.text_messages[-1]
print(f"{msg.role}: {last_text.text.value}")

# Delete the Agent when done
# Delete the Agent when done
agents_client.delete_agent(agent.id)
print("Deleted agent")
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
"description": "Translates text into one or more languages.",
"operationId": "Translator_Translate",
"parameters": [
{
"name": "ocp-apim-subscription-region",
"in": "header",
"description": "the local region of the endpoint",
"required": true,
"schema": {
"type": "string",
"default": "${translator_resource_region}"
}
},
{
"name": "api-version",
"in": "query",
Expand Down
Loading