Update crewai/prompts.py
Browse files- crewai/prompts.py +49 -76
crewai/prompts.py
CHANGED
@@ -1,84 +1,57 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
from
|
4 |
-
from typing import ClassVar
|
5 |
|
6 |
from langchain.prompts import PromptTemplate
|
7 |
-
from pydantic import BaseModel
|
8 |
|
9 |
|
10 |
class Prompts(BaseModel):
|
11 |
-
"""
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
SCRATCHPAD_SLICE: ClassVar[str] = "\n{agent_scratchpad}"
|
21 |
|
22 |
-
|
23 |
-
"""
|
24 |
-
|
25 |
-
|
26 |
-
)
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
)
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
{tools}
|
45 |
-
|
46 |
-
To use a tool, please use the exact following format:
|
47 |
-
|
48 |
-
```
|
49 |
-
Thought: Do I need to use a tool? Yes
|
50 |
-
Action: the action to take, should be one of [{tool_names}], just the name.
|
51 |
-
Action Input: the input to the action
|
52 |
-
Observation: the result of the action
|
53 |
-
```
|
54 |
-
|
55 |
-
When you have a response for your task, or if you do not need to use a tool, you MUST use the format:
|
56 |
-
|
57 |
-
```
|
58 |
-
Thought: Do I need to use a tool? No
|
59 |
-
Final Answer: [your response here]
|
60 |
-
```"""
|
61 |
-
)
|
62 |
-
|
63 |
-
VOTING_SLICE: ClassVar[str] = dedent(
|
64 |
-
"""\
|
65 |
-
You are working on a crew with your co-workers and need to decide who will execute the task.
|
66 |
-
|
67 |
-
These are your format instructions:
|
68 |
-
{format_instructions}
|
69 |
-
|
70 |
-
These are your co-workers and their roles:
|
71 |
-
{coworkers}"""
|
72 |
-
)
|
73 |
-
|
74 |
-
TASK_EXECUTION_WITH_MEMORY_PROMPT: ClassVar[str] = PromptTemplate.from_template(
|
75 |
-
ROLE_PLAYING_SLICE + TOOLS_SLICE + MEMORY_SLICE + TASK_SLICE + SCRATCHPAD_SLICE
|
76 |
-
)
|
77 |
-
|
78 |
-
TASK_EXECUTION_PROMPT: ClassVar[str] = PromptTemplate.from_template(
|
79 |
-
ROLE_PLAYING_SLICE + TOOLS_SLICE + TASK_SLICE + SCRATCHPAD_SLICE
|
80 |
-
)
|
81 |
-
|
82 |
-
CONSENSUNS_VOTING_PROMPT: ClassVar[str] = PromptTemplate.from_template(
|
83 |
-
ROLE_PLAYING_SLICE + VOTING_SLICE + TASK_SLICE + SCRATCHPAD_SLICE
|
84 |
-
)
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
from typing import ClassVar, Dict, Optional
|
|
|
4 |
|
5 |
from langchain.prompts import PromptTemplate
|
6 |
+
from pydantic import BaseModel, Field, PrivateAttr, ValidationError, model_validator
|
7 |
|
8 |
|
9 |
class Prompts(BaseModel):
|
10 |
+
"""Manages and generates prompts for a generic agent with support for different languages."""
|
11 |
+
|
12 |
+
_prompts: Optional[Dict[str, str]] = PrivateAttr()
|
13 |
+
language: Optional[str] = Field(
|
14 |
+
default="en",
|
15 |
+
description="Language of the prompts.",
|
16 |
+
)
|
17 |
+
|
18 |
+
@model_validator(mode="after")
|
19 |
+
def load_prompts(self) -> "Prompts":
|
20 |
+
"""Load prompts from a JSON file based on the specified language."""
|
21 |
+
try:
|
22 |
+
dir_path = os.path.dirname(os.path.realpath(__file__))
|
23 |
+
prompts_path = os.path.join(dir_path, f"prompts/{self.language}.json")
|
24 |
+
|
25 |
+
with open(prompts_path, "r") as f:
|
26 |
+
self._prompts = json.load(f)["slices"]
|
27 |
+
except FileNotFoundError:
|
28 |
+
raise ValidationError(
|
29 |
+
f"Prompt file for language '{self.language}' not found."
|
30 |
+
)
|
31 |
+
except json.JSONDecodeError:
|
32 |
+
raise ValidationError(f"Error decoding JSON from the prompts file.")
|
33 |
+
return self
|
34 |
|
35 |
SCRATCHPAD_SLICE: ClassVar[str] = "\n{agent_scratchpad}"
|
36 |
|
37 |
+
def task_execution_with_memory(self) -> str:
|
38 |
+
"""Generate a prompt for task execution with memory components."""
|
39 |
+
return self._build_prompt(["role_playing", "tools", "memory", "task"])
|
40 |
+
|
41 |
+
def task_execution_without_tools(self) -> str:
|
42 |
+
"""Generate a prompt for task execution without tools components."""
|
43 |
+
return self._build_prompt(["role_playing", "task"])
|
44 |
+
|
45 |
+
def task_execution(self) -> str:
|
46 |
+
"""Generate a standard prompt for task execution."""
|
47 |
+
return self._build_prompt(["role_playing", "tools", "task"])
|
48 |
+
|
49 |
+
def _build_prompt(self, components: [str]) -> str:
|
50 |
+
"""Constructs a prompt string from specified components."""
|
51 |
+
prompt_parts = [
|
52 |
+
self._prompts[component]
|
53 |
+
for component in components
|
54 |
+
if component in self._prompts
|
55 |
+
]
|
56 |
+
prompt_parts.append(self.SCRATCHPAD_SLICE)
|
57 |
+
return PromptTemplate.from_template("".join(prompt_parts))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|