-
Notifications
You must be signed in to change notification settings - Fork 0
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
changes to tool as per review advice #12
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good! There are a few linting errors we should fix up from the github actions tool as well.
from azure.ai.translation.text import TextTranslationClient | ||
from azure.core.credentials import AzureKeyCredential |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's move these imports under the validate_environment and wrap them in a try/except so we can alert users if they need to install the library, for example:
langchain/libs/community/langchain_community/tools/azure_ai_services/content_safety.py
Lines 55 to 68 in e5fb363
try: | |
import azure.ai.contentsafety as sdk | |
from azure.core.credentials import AzureKeyCredential | |
values["content_safety_client"] = sdk.ContentSafetyClient( | |
endpoint=content_safety_endpoint, | |
credential=AzureKeyCredential(content_safety_key), | |
) | |
except ImportError: | |
raise ImportError( | |
"azure-ai-contentsafety is not installed. " | |
"Run `pip install azure-ai-contentsafety` to install." | |
) |
translate_key = os.getenv("AZURE_OPENAI_TRANSLATE_API_KEY") | ||
translate_endpoint = os.getenv("AZURE_OPENAI_TRANSLATE_ENDPOINT") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't related to OpenAI so we can drop that from the env var name
translate_key = os.getenv("AZURE_OPENAI_TRANSLATE_API_KEY") | |
translate_endpoint = os.getenv("AZURE_OPENAI_TRANSLATE_ENDPOINT") | |
translate_key = os.getenv("AZURE_TRANSLATE_API_KEY") | |
translate_endpoint = os.getenv("AZURE_TRANSLATE_ENDPOINT") |
) | ||
|
||
@classmethod | ||
def validate_environment(cls): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should accept the values
dict like shown here:
langchain/libs/community/langchain_community/tools/azure_ai_services/image_analysis.py
Line 39 in e5fb363
def validate_environment(cls, values: Dict) -> Any: |
if not translate_endpoint: | ||
raise ValueError("AZURE_TRANSLATE_ENDPOINT is missing in environment variables") | ||
|
||
return cls(translate_key=translate_key, translate_endpoint=translate_endpoint) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we're expected to create the client here and set it in the values dictionary:
values["translate_client"] = TextTranslationClient(
endpoint=translate_endpoint, credential=AzureKeyCredential(translate_key)
)
def __init__( | ||
self, | ||
*, | ||
translate_key: Optional[str] = None, | ||
translate_endpoint: Optional[str] = None, | ||
) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we double-check if we need this init?
def _run( | ||
self, | ||
query: str, | ||
to_language: Optional[str] = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This gets called internally, there won't be a chance for the user to pass a value to to_language
.
translate_test.ipynb
Outdated
"\n", | ||
"import os\n", | ||
"from dotenv import load_dotenv\n", | ||
"from libs.community.langchain_community.tools.azure_ai_services.azure_translate import AzureTranslateTool\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this got renamed from azure_translate to translate_tool?
translate_test.ipynb
Outdated
], | ||
"outputs": [ | ||
{ | ||
"ename": "ModuleNotFoundError", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's remove the ModuleNotFoundError output here
translate_test.ipynb
Outdated
"translate_key = os.getenv('AZURE_OPENAI_TRANSLATE_API_KEY')\n", | ||
"translate_endpoint = os.getenv('AZURE_OPENAI_TRANSLATE_ENDPOINT')\n", | ||
"\n", | ||
"assert translate_key is not None, \"AZURE_OPENAI_TRANSLATE_API_KEY is not set.\"\n", | ||
"assert translate_endpoint is not None, \"AZURE_OPENAI_TRANSLATE_ENDPOINT is not set.\"" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"translate_key = os.getenv('AZURE_OPENAI_TRANSLATE_API_KEY')\n", | |
"translate_endpoint = os.getenv('AZURE_OPENAI_TRANSLATE_ENDPOINT')\n", | |
"\n", | |
"assert translate_key is not None, \"AZURE_OPENAI_TRANSLATE_API_KEY is not set.\"\n", | |
"assert translate_endpoint is not None, \"AZURE_OPENAI_TRANSLATE_ENDPOINT is not set.\"" | |
"translate_key = os.getenv('AZURE_TRANSLATE_API_KEY')\n", | |
"translate_endpoint = os.getenv('AZURE_TRANSLATE_ENDPOINT')\n", | |
"\n", | |
"assert translate_key is not None, \"AZURE_TRANSLATE_API_KEY is not set.\"\n", | |
"assert translate_endpoint is not None, \"AZURE_TRANSLATE_ENDPOINT is not set.\"" |
translate_test.ipynb
Outdated
"outputs": [ | ||
{ | ||
"ename": "NameError", | ||
"evalue": "name 'AzureTranslateTool' is not defined", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like we need to clean up the notebook to show the working tool
I'm not sure what is causing these errors! |
You can run the following commands if you have poetry and ruff installed to lint your files how LangChain wants them, just replace them with your file paths. !poetry run ruff format "path/to/file"
!poetry run ruff check --fix "path/to/file"
!poetry run ruff check --select I --fix "path/to/file" |
""" | ||
|
||
translate_client: Any = None | ||
default_language: str = "en" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think to be consistent between translate tools (especially if we decide to merge them), lets add the following:
text_translation_key: str = ""
text_translation_endpoint: str = ""
region: str = ""
@classmethod | ||
def validate_environment(cls, values: Dict) -> Any: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing model_validator (from pydantic import model_validator)
@classmethod | |
def validate_environment(cls, values: Dict) -> Any: | |
@model_validator(mode="before") | |
@classmethod | |
def validate_environment(cls, values: Dict) -> Any: |
translate_key = os.getenv("AZURE_TRANSLATE_API_KEY") | ||
translate_endpoint = os.getenv("AZURE_TRANSLATE_ENDPOINT") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's leverage the helper function for env vars that the other tools use:
# Get environment variables
azure_translate_key = get_from_dict_or_env(
values, "text_translation_key", "AZURE_TRANSLATE_API_KEY"
)
azure_translate_endpoint = get_from_dict_or_env(
values, "text_translation_endpoint", "AZURE_TRANSLATE_ENDPOINT"
)
region = get_from_dict_or_env(
values, "region", "AZURE_REGION"
)
values["translate_client"] = TextTranslationClient( | ||
endpoint=translate_endpoint, credential=AzureKeyCredential(translate_key) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I needed region to call the client successfully
values["translate_client"] = TextTranslationClient( | |
endpoint=translate_endpoint, credential=AzureKeyCredential(translate_key) | |
) | |
values["translate_client"] = TextTranslationClient( | |
endpoint=translate_endpoint, credential=AzureKeyCredential(translate_key), region=region | |
) |
translate_test.ipynb
Outdated
"def translate_text(input_text, target_language='es'):\n", | ||
" try:\n", | ||
" # Run the translation\n", | ||
" translated_text = translator._run(input_text, target_language)\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't call the internal methods. I'd expect a user to invoke the tool directly or use an agent.
translate_tool = AzureTranslateTool(
text_translation_key=os.environ["AZURE_TRANSLATE_API_KEY"],
text_translation_endpoint=os.environ["AZURE_TRANSLATE_ENDPOINT"],
region="westus2",
)
answer = translate_tool.invoke({"query": "hola mundo"})
print(answer)
translate_tool = AzureTranslateTool(
text_translation_key=os.environ["AZURE_TRANSLATE_API_KEY"],
text_translation_endpoint=os.environ["AZURE_TRANSLATE_ENDPOINT"],
region="westus2",
)
tools = [translate_tool]
model = AzureChatOpenAI(
openai_api_version=os.environ["OPENAI_API_VERSION"],
azure_deployment=os.environ["COMPLETIONS_MODEL"],
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
api_key=os.environ["AZURE_OPENAI_API_KEY"],
)
agent = create_structured_chat_agent(model, tools, prompt)
agent_executor = AgentExecutor(
agent=agent, tools=tools, verbose=True, handle_parsing_errors=True
)
text = "hola mundo"
answer = agent_executor.invoke(
{"input": f"What does the text say in english? : {text}"}
)
print(answer)
…feature/translate_tool-v2
…e to text_translate.py Updated the tool description to specify the to_language parameter Changed error handling for package imports by adding specific messages for missing Azure packages. Updated the class docstring to include more descriptions Replaced logger.error with direct error raising
Re-PRing the translate_tool with changes made from first PR