diff --git a/src/promptflow-tools/promptflow/tools/__init__.py b/src/promptflow-tools/promptflow/tools/__init__.py index 0f3f3274b3b..fe831864618 100644 --- a/src/promptflow-tools/promptflow/tools/__init__.py +++ b/src/promptflow-tools/promptflow/tools/__init__.py @@ -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 diff --git a/src/promptflow-tools/promptflow/tools/azure_detect.py b/src/promptflow-tools/promptflow/tools/azure_detect.py new file mode 100644 index 00000000000..b8691d173f9 --- /dev/null +++ b/src/promptflow-tools/promptflow/tools/azure_detect.py @@ -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) diff --git a/src/promptflow-tools/promptflow/tools/azure_translator.py b/src/promptflow-tools/promptflow/tools/azure_translator.py index 4938a6da7fd..884ed810a1b 100644 --- a/src/promptflow-tools/promptflow/tools/azure_translator.py +++ b/src/promptflow-tools/promptflow/tools/azure_translator.py @@ -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 @@ -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 @@ -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)