This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 736
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
## init |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |