forked from crewAIInc/crewAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
84 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,50 @@ | ||
"""Prompts for generic agent.""" | ||
import json | ||
import os | ||
from typing import ClassVar, Dict, Optional | ||
|
||
from langchain.prompts import PromptTemplate | ||
from pydantic import BaseModel, Field, PrivateAttr, model_validator | ||
|
||
from pydantic import BaseModel, Field, PrivateAttr, model_validator, ValidationError | ||
|
||
class Prompts(BaseModel): | ||
"""Prompts for generic agent.""" | ||
"""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 crewai prompts.", | ||
description="Language of the prompts.", | ||
) | ||
|
||
@model_validator(mode="after") | ||
def load_prompts(self) -> "Prompts": | ||
"""Load prompts from file.""" | ||
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"] | ||
"""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 | ||
|
||
SCRATCHPAD_SLICE: ClassVar[str] = "\n{agent_scratchpad}" | ||
|
||
def task_execution_with_memory(self) -> str: | ||
return PromptTemplate.from_template( | ||
self._prompts["role_playing"] | ||
+ self._prompts["tools"] | ||
+ self._prompts["memory"] | ||
+ self._prompts["task"] | ||
+ self.SCRATCHPAD_SLICE | ||
) | ||
"""Generate a prompt for task execution with memory components.""" | ||
return self._build_prompt(["role_playing", "tools", "memory", "task"]) | ||
|
||
def task_execution_without_tools(self) -> str: | ||
return PromptTemplate.from_template( | ||
self._prompts["role_playing"] | ||
+ self._prompts["task"] | ||
+ self.SCRATCHPAD_SLICE | ||
) | ||
"""Generate a prompt for task execution without tools components.""" | ||
return self._build_prompt(["role_playing", "task"]) | ||
|
||
def task_execution(self) -> str: | ||
return PromptTemplate.from_template( | ||
self._prompts["role_playing"] | ||
+ self._prompts["tools"] | ||
+ self._prompts["task"] | ||
+ self.SCRATCHPAD_SLICE | ||
) | ||
"""Generate a standard prompt for task execution.""" | ||
return self._build_prompt(["role_playing", "tools", "task"]) | ||
|
||
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.append(self.SCRATCHPAD_SLICE) | ||
return PromptTemplate.from_template("".join(prompt_parts)) |