Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/ translate tool #7

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open

Feature/ translate tool #7

wants to merge 16 commits into from

Conversation

Leky1738
Copy link
Collaborator

initial text translate tool

@@ -14,24 +14,82 @@
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like some unintended files and changes got added to this PR.

@@ -0,0 +1,150 @@
from __future__ import annotations
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a stylistic convention in Python to name files using snake_case instead of camelCase.

azureTranslate.py --> azure_translate.py

edit: I'm also not sure we need to put this under its own azure_translator directory - it seems like the other Azure AI tools all live under /azure_ai_services/.

"azure-ai-translation-text package."
)

def __init__(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as the content safety PR:

should we follow the pattern of the other tools and define a validate_environment method instead of init?

https://github.com/langchain-ai/langchain/blob/master/libs/community/langchain_community/tools/azure_ai_services/text_analytics_for_health.py#L34

Comment on lines +76 to +77
logger.error("Input text for translation is empty.")
return None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is no input text, we should raise a ValueError so the user can catch the exception and act accordingly. It's much harder to programmatically act on a logging error.

endpoint=translate_endpoint, credential=AzureKeyCredential(translate_key)
)

def _translate_text(self, text: str, to_language: str) -> str:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this gets called internally, I don't believe the user has the ability to pass to_language. I think we can follow the way speech-to-text handles this by setting a default on the class: https://github.com/langchain-ai/langchain/blob/master/libs/community/langchain_community/tools/azure_ai_services/speech_to_text.py#L29

Comment on lines +85 to +87
logger.warning(
f"Translation successful: {response[0].translations[0].text}"
) # Use WARNING level for successful operations
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's sufficient to just return the translated text, no need to log

Comment on lines +89 to +91
else:
logger.error("Translation failed with an empty response")
return None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious, in what cases does the call to translate return an empty response? this seems unexpected for the API

Comment on lines +143 to +150
# Example test usage for the AzureTranslateTool
if __name__ == "__main__":
tool = AzureTranslateTool.from_env()
try:
translated_text = tool._run("good morning, How are you?", "es")
logger.info(f"Translated text: {translated_text}")
except RuntimeError as e:
logger.error(f"Error occurred: {e}")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine to have this for testing for now, but we'll need to remove it in the final PR

"AZURE_TRANSLATE_ENDPOINT is missing in environment variables"
)

logger.info(f"API Key: {translate_key[:4]}**** (masked)")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not log sensitive information, even if we redact some of it

raise RuntimeError(f"Error while running AzureTranslateTool: {e}")

@classmethod
def from_env(cls):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was this just added for temp testing? It seems we should read the env var values in the validate_environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create an AzureTextTranslationTool
2 participants