Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
Adding azure translate tools
Browse files Browse the repository at this point in the history
  • Loading branch information
ajhofmann committed Aug 11, 2023
1 parent 88a3f42 commit fde170a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
32 changes: 32 additions & 0 deletions llama_hub/tools/azure_translate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Azure Translate Tool

This tool connects to a Azure account and allows an Agent to perform text translation into a variet of different languages

You will need to set up an api key and translate instance using Azure, learn more here: https://learn.microsoft.com/en-us/azure/ai-services/translator/translator-overview

For a full list of supported languages see here: https://learn.microsoft.com/en-us/azure/ai-services/translator/language-support

## Usage

Here's an example usage of the AzureTranslateToolSpec.

```python
from llama_index.agent import OpenAIAgent
from llama_hub.tools.azure_translate.base import AzureTranslateToolSpec

translate_tool = AzureTranslateToolSpec(
api_key='your-key',
region='eastus'
)

agent = OpenAIAgent.from_tools(
translate_tool.to_tool_list(),
verbose=True,
)
print(agent.chat('Say "hello world" in 5 different languages'))
```

`translate`: Translate text to a target language

This loader is designed to be used as a way to load data as a Tool in a Agent. See [here](https://github.com/emptycrown/llama-hub/tree/main) for examples.

1 change: 1 addition & 0 deletions llama_hub/tools/azure_translate/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## init
41 changes: 41 additions & 0 deletions llama_hub/tools/azure_translate/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Azure Translate tool spec."""

from llama_index.tools.tool_spec.base import BaseToolSpec
from typing import Optional, List
import requests

ENDPOINT_BASE_URL = "https://api.cognitive.microsofttranslator.com/translate"

class AzureTranslateToolSpec(BaseToolSpec):
"""Azure Translate tool spec."""

spec_functions = ["translate"]

def __init__(self, api_key: str, region: str) -> None:
"""Initialize with parameters."""
self.headers = {
'Ocp-Apim-Subscription-Key': api_key,
'Ocp-Apim-Subscription-Region': region,
'Content-type': 'application/json',
}

def translate(self, text: str, language: str):
"""
Use this tool to translate text from one language to another.
The source language will be automatically detected. You need to specify the target language
using a two character language code.
args:
language (str): Target translation language. One of af, sq, am, ar, hy, as, az, bn, ba, eu, bs, bg, ca, hr, cs, da, dv, nl, en, et, fo, fj, fi, fr, gl, ka, de, el, gu, ht, he, hi, hu, is, id, iu, ga, it, ja, kn, kk, km, ko, ku, ky, lo, lv, lt, mk, mg, ms, ml, mt, mi, mr, my, ne, nb, or, ps, fa, pl, pt, pa, ro, ru, sm, sk, sl, so, es, sw, sv, ty, ta, tt, te, th, bo, ti, to, tr, tk, uk, ur, ug, uz, vi, cy, zu
"""

request = requests.post(
ENDPOINT_BASE_URL,
params={
'api-version': '3.0',
'to': language
},
headers=self.headers,
json=[{ 'text': text }]
)
response = request.json()
return response

0 comments on commit fde170a

Please sign in to comment.