forked from microsoft/promptflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tool] restore azure_detect and translator class tool for previous co…
…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
Showing
3 changed files
with
120 additions
and
5 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 |
---|---|---|
@@ -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 |
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,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) |
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