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

changes to tool as per review advice #12

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

Conversation

Leky1738
Copy link
Collaborator

@Leky1738 Leky1738 commented Oct 3, 2024

Re-PRing the translate_tool with changes made from first PR

Copy link
Collaborator

@kristapratico kristapratico left a 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.

Comment on lines 7 to 8
from azure.ai.translation.text import TextTranslationClient
from azure.core.credentials import AzureKeyCredential
Copy link
Collaborator

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:

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."
)

Comment on lines 43 to 44
translate_key = os.getenv("AZURE_OPENAI_TRANSLATE_API_KEY")
translate_endpoint = os.getenv("AZURE_OPENAI_TRANSLATE_ENDPOINT")
Copy link
Collaborator

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

Suggested change
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):
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 this should accept the values dict like shown here:

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)
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 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)
)

Comment on lines 54 to 59
def __init__(
self,
*,
translate_key: Optional[str] = None,
translate_endpoint: Optional[str] = None,
) -> None:
Copy link
Collaborator

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,
Copy link
Collaborator

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.

"\n",
"import os\n",
"from dotenv import load_dotenv\n",
"from libs.community.langchain_community.tools.azure_ai_services.azure_translate import AzureTranslateTool\n",
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 this got renamed from azure_translate to translate_tool?

],
"outputs": [
{
"ename": "ModuleNotFoundError",
Copy link
Collaborator

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

Comment on lines 45 to 49
"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.\""
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
"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.\""

"outputs": [
{
"ename": "NameError",
"evalue": "name 'AzureTranslateTool' is not defined",
Copy link
Collaborator

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

@Leky1738
Copy link
Collaborator Author

I'm not sure what is causing these errors!

@Sheepsta300
Copy link
Owner

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"
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 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 = ""

Comment on lines 34 to 35
@classmethod
def validate_environment(cls, values: Dict) -> Any:
Copy link
Collaborator

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)

Suggested change
@classmethod
def validate_environment(cls, values: Dict) -> Any:
@model_validator(mode="before")
@classmethod
def validate_environment(cls, values: Dict) -> Any:

Comment on lines 50 to 51
translate_key = os.getenv("AZURE_TRANSLATE_API_KEY")
translate_endpoint = os.getenv("AZURE_TRANSLATE_ENDPOINT")
Copy link
Collaborator

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"
)

Comment on lines 63 to 65
values["translate_client"] = TextTranslationClient(
endpoint=translate_endpoint, credential=AzureKeyCredential(translate_key)
)
Copy link
Collaborator

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

Suggested change
values["translate_client"] = TextTranslationClient(
endpoint=translate_endpoint, credential=AzureKeyCredential(translate_key)
)
values["translate_client"] = TextTranslationClient(
endpoint=translate_endpoint, credential=AzureKeyCredential(translate_key), region=region
)

"def translate_text(input_text, target_language='es'):\n",
" try:\n",
" # Run the translation\n",
" translated_text = translator._run(input_text, target_language)\n",
Copy link
Collaborator

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)

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.

3 participants