mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-05-03 16:22:49 +00:00
@@ -1,36 +1,15 @@
|
||||
import json
|
||||
import os
|
||||
from typing import ClassVar, Dict, Optional
|
||||
from typing import ClassVar
|
||||
|
||||
from langchain.prompts import PromptTemplate
|
||||
from pydantic import BaseModel, Field, PrivateAttr, ValidationError, model_validator
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from .i18n import I18N
|
||||
|
||||
|
||||
class Prompts(BaseModel):
|
||||
"""Manages and generates prompts for a generic agent with support for different languages."""
|
||||
|
||||
_prompts: Optional[Dict[str, str]] = PrivateAttr()
|
||||
language: Optional[str] = Field(
|
||||
default="en",
|
||||
description="Language of the prompts.",
|
||||
)
|
||||
|
||||
@model_validator(mode="after")
|
||||
def load_prompts(self) -> "Prompts":
|
||||
"""Load prompts from a JSON file based on the specified language."""
|
||||
try:
|
||||
dir_path = os.path.dirname(os.path.realpath(__file__))
|
||||
prompts_path = os.path.join(dir_path, f"prompts/{self.language}.json")
|
||||
|
||||
with open(prompts_path, "r") as f:
|
||||
self._prompts = json.load(f)["slices"]
|
||||
except FileNotFoundError:
|
||||
raise ValidationError(
|
||||
f"Prompt file for language '{self.language}' not found."
|
||||
)
|
||||
except json.JSONDecodeError:
|
||||
raise ValidationError(f"Error decoding JSON from the prompts file.")
|
||||
return self
|
||||
i18n: I18N = Field(default=I18N())
|
||||
|
||||
SCRATCHPAD_SLICE: ClassVar[str] = "\n{agent_scratchpad}"
|
||||
|
||||
@@ -48,10 +27,6 @@ class Prompts(BaseModel):
|
||||
|
||||
def _build_prompt(self, components: [str]) -> str:
|
||||
"""Constructs a prompt string from specified components."""
|
||||
prompt_parts = [
|
||||
self._prompts[component]
|
||||
for component in components
|
||||
if component in self._prompts
|
||||
]
|
||||
prompt_parts = [self.i18n.slice(component) for component in components]
|
||||
prompt_parts.append(self.SCRATCHPAD_SLICE)
|
||||
return PromptTemplate.from_template("".join(prompt_parts))
|
||||
|
||||
Reference in New Issue
Block a user