small improvements for i18n

This commit is contained in:
João Moura
2024-05-02 04:58:29 -03:00
parent 2c17ff3e9f
commit cbe53d9daf
4 changed files with 13 additions and 8 deletions

View File

@@ -308,7 +308,7 @@ class Crew(BaseModel):
def _run_hierarchical_process(self) -> str:
"""Creates and assigns a manager agent to make sure the crew completes the tasks."""
i18n = I18N(language=self.language, language_file=self.language_file)
i18n = I18N(prompt_file=self.prompt_file)
try:
self.manager_agent.allow_delegation = (
True # Forcing Allow delegation to the manager

View File

@@ -88,7 +88,9 @@ class Telemetry:
self._add_attribute(span, "python_version", platform.python_version())
self._add_attribute(span, "crew_id", str(crew.id))
self._add_attribute(span, "crew_process", crew.process)
self._add_attribute(span, "crew_language", crew.prompt_file)
self._add_attribute(
span, "crew_language", crew.prompt_file if crew.i18n else "None"
)
self._add_attribute(span, "crew_memory", crew.memory)
self._add_attribute(span, "crew_number_of_tasks", len(crew.tasks))
self._add_attribute(span, "crew_number_of_agents", len(crew.agents))

View File

@@ -2,7 +2,7 @@ import json
import os
from typing import Dict, Optional
from pydantic import BaseModel, Field, PrivateAttr, ValidationError, model_validator
from pydantic import BaseModel, Field, PrivateAttr, model_validator
class I18N(BaseModel):
@@ -26,9 +26,9 @@ class I18N(BaseModel):
with open(prompts_path, "r") as f:
self._prompts = json.load(f)
except FileNotFoundError:
raise ValidationError(f"Prompt file '{self.prompt_file}' not found.")
raise Exception(f"Prompt file '{self.prompt_file}' not found.")
except json.JSONDecodeError:
raise ValidationError(f"Error decoding JSON from the prompts file.")
raise Exception(f"Error decoding JSON from the prompts file.")
if not self._prompts:
self._prompts = {}
@@ -48,4 +48,4 @@ class I18N(BaseModel):
try:
return self._prompts[kind][key]
except:
raise ValidationError(f"Prompt for '{kind}':'{key}' not found.")
raise Exception(f"Prompt for '{kind}':'{key}' not found.")

View File

@@ -6,7 +6,7 @@ from crewai.utilities.i18n import I18N
def test_load_prompts():
i18n = I18N()
i18n.load_prompts()
assert i18n._translations is not None
assert i18n._prompts is not None
def test_slice():
@@ -35,7 +35,10 @@ def test_retrieve_not_found():
def test_prompt_file():
i18n = I18N(prompt_file="tests/utilities/en.json")
import os
path = os.path.join(os.path.dirname(__file__), "prompts.json")
i18n = I18N(prompt_file=path)
i18n.load_prompts()
assert isinstance(i18n.retrieve("slices", "role_playing"), str)
assert i18n.retrieve("slices", "role_playing") == "Lorem ipsum dolor sit amet"