Skip to content

Commit

Permalink
[tool] restore azure_detect and translator class tool for previous co…
Browse files Browse the repository at this point in the history
…ntract tool meta working (microsoft#50)

previous contract tool meta is not updated as the same time of tool code
changes.
Will remove the file when new named source code is released to all
regions.
Probably around Aug 15.

[Task
2587486](https://msdata.visualstudio.com/Vienna/_workitems/edit/2587486):
Remove azure_detect.py and update previous contract tool meta on Aug 15

---------

Co-authored-by: Yao <[email protected]>
  • Loading branch information
chjinche and 16oeahr authored Aug 2, 2023
1 parent c787b11 commit 493cea8
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/promptflow-tools/promptflow/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from .aoai import AzureOpenAI # noqa: F401
from .azure_content_safety import AzureContentSafety # noqa: F401
from .azure_detect import AzureDetect # noqa: F401
from .azure_language_detector import get_language # noqa: F401
from .azure_form_recognizer import AzureFormRecognizer # noqa: F401
from .azure_translator import get_translation # noqa: F401
from .azure_translator import get_translation, AzureTranslator # noqa: F401
from .bing import Bing # noqa: F401
from .openai import OpenAI # noqa: F401
from .serpapi import SerpAPI # noqa: F401
64 changes: 64 additions & 0 deletions src/promptflow-tools/promptflow/tools/azure_detect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# TODO: previous contract tool meta is not updated as the same time of tool code changes.
# Will remove the file when new named source code is released to all regions.
# Probably around Aug 15.
import traceback

from promptflow.core.tool import ToolProvider, tool
from promptflow.connections import CustomConnection
from promptflow.core.tools_manager import register_builtins

debug = False


class AzureDetect(ToolProvider):
"""
Doc reference :
https://learn.microsoft.com/en-us/azure/cognitive-services/translator/text-sdk-overview?tabs=python
"""

def __init__(self, connection: CustomConnection):
super().__init__()
self.connection = connection

@tool
def get_language(self, input_text: str):
import uuid

import requests

traceId = str(uuid.uuid4())
try:
# If you encounter any issues with the base_url or path, make sure
# that you are using the latest endpoint:
# https://docs.microsoft.com/azure/cognitive-services/translator/reference/v3-0-detect
print(f"{traceId}: Detect language")
path = "/detect?api-version=3.0"
constructed_url = self.connection.api_endpoint + path
if debug:
print(f"{traceId} {constructed_url}")

headers = {
"Ocp-Apim-Subscription-Key": self.connection.api_key,
"Ocp-Apim-Subscription-Region": self.connection.api_region,
"Content-type": "application/json",
"X-ClientTraceId": traceId,
}
if debug:
print(f"{traceId} {headers}")

body = [{"text": input_text}]
request = requests.post(constructed_url, headers=headers, json=body)
response = request.json()
if debug:
print(f"{traceId} {response}")
# return the detected language IFF we support translation for that language.
if response[0]["isTranslationSupported"] is True:
return response[0]["language"]
else:
return ""
except Exception:
error_msg = traceback.format_exc()
return f"{traceId} Exception {error_msg}"


register_builtins(AzureDetect)
58 changes: 54 additions & 4 deletions src/promptflow-tools/promptflow/tools/azure_translator.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import traceback

from promptflow.connections import CustomConnection
from promptflow.core.tool import tool
from promptflow.core.tools_manager import register_builtin_method
from promptflow.core.tool import tool, ToolProvider
from promptflow.core.tools_manager import register_builtin_method, register_builtins

debug = False

Expand All @@ -17,8 +17,6 @@ def get_translation(connection: CustomConnection, input_text: str, source_langua

traceId = str(uuid.uuid4())
try:
import uuid

import requests

# If you encounter any issues with the base_url or path, make sure
Expand Down Expand Up @@ -55,3 +53,55 @@ def get_translation(connection: CustomConnection, input_text: str, source_langua


register_builtin_method(get_translation)


# TODO: previous contract tool meta is not updated as the same time of tool code changes.
# Will remove below codes when new function tool code is released to all regions.
# Probably around Aug 15.
class AzureTranslator(ToolProvider):
def __init__(self, connection: CustomConnection):
super().__init__()
self.connection = connection

@tool
def get_translation(self, input_text: str, source_language: str, target_language: str = "en"):
import uuid

traceId = str(uuid.uuid4())
try:
import requests

# If you encounter any issues with the base_url or path, make sure
# that you are using the latest endpoint:
# https://docs.microsoft.com/azure/cognitive-services/translator/reference/v3-0-translate
print(f"{traceId}: Translate from {source_language} to {target_language}")
path = "/translate?api-version=3.0"
params = f"&from={source_language}&to={target_language}"
constructed_url = self.connection.api_endpoint + path + params
if debug:
print(f"{traceId} {constructed_url}")

headers = {
"Ocp-Apim-Subscription-Key": self.connection.api_key,
"Ocp-Apim-Subscription-Region": self.connection.api_region,
"Content-type": "application/json",
"X-ClientTraceId": traceId,
}
if debug:
print(f"{traceId} {headers}")
# You can pass more than one object in body.
body = [{"text": input_text}]
request = requests.post(constructed_url, headers=headers, json=body)
response = request.json()
if debug:
print(f"{traceId} {response}")

translated_text = response[0]["translations"][0]["text"]
print(f"{traceId} Completed")
return translated_text
except Exception:
error_msg = traceback.format_exc()
return f"{traceId} Exception {error_msg}"


register_builtins(AzureTranslator)

0 comments on commit 493cea8

Please sign in to comment.