mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-05-03 16:22:49 +00:00
Removing LangChain and Rebuilding Executor (#1322)
* rebuilding executor * removing langchain * Making all tests good * fixing types and adding ability for nor using system prompts * improving types * pleasing the types gods * pleasing the types gods * fixing parser, tools and executor * making sure all tests pass * final pass * fixing type * Updating Docs * preparing to cut new version
This commit is contained in:
@@ -1,8 +1,5 @@
|
||||
from typing import Any, ClassVar, Optional
|
||||
|
||||
from langchain.prompts import BasePromptTemplate, PromptTemplate
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from typing import Any, Optional
|
||||
from crewai.utilities import I18N
|
||||
|
||||
|
||||
@@ -14,27 +11,38 @@ class Prompts(BaseModel):
|
||||
system_template: Optional[str] = None
|
||||
prompt_template: Optional[str] = None
|
||||
response_template: Optional[str] = None
|
||||
SCRATCHPAD_SLICE: ClassVar[str] = "\n{agent_scratchpad}"
|
||||
use_system_prompt: Optional[bool] = False
|
||||
agent: Any
|
||||
|
||||
def task_execution(self) -> BasePromptTemplate:
|
||||
def task_execution(self) -> dict[str, str]:
|
||||
"""Generate a standard prompt for task execution."""
|
||||
slices = ["role_playing"]
|
||||
if len(self.tools) > 0:
|
||||
slices.append("tools")
|
||||
else:
|
||||
slices.append("no_tools")
|
||||
|
||||
system = self._build_prompt(slices)
|
||||
slices.append("task")
|
||||
|
||||
if not self.system_template and not self.prompt_template:
|
||||
return self._build_prompt(slices)
|
||||
if (
|
||||
not self.system_template
|
||||
and not self.prompt_template
|
||||
and self.use_system_prompt
|
||||
):
|
||||
return {
|
||||
"system": system,
|
||||
"user": self._build_prompt(["task"]),
|
||||
"prompt": self._build_prompt(slices),
|
||||
}
|
||||
else:
|
||||
return self._build_prompt(
|
||||
slices,
|
||||
self.system_template,
|
||||
self.prompt_template,
|
||||
self.response_template,
|
||||
)
|
||||
return {
|
||||
"prompt": self._build_prompt(
|
||||
slices,
|
||||
self.system_template,
|
||||
self.prompt_template,
|
||||
self.response_template,
|
||||
)
|
||||
}
|
||||
|
||||
def _build_prompt(
|
||||
self,
|
||||
@@ -42,12 +50,11 @@ class Prompts(BaseModel):
|
||||
system_template=None,
|
||||
prompt_template=None,
|
||||
response_template=None,
|
||||
) -> BasePromptTemplate:
|
||||
) -> str:
|
||||
"""Constructs a prompt string from specified components."""
|
||||
if not system_template and not prompt_template:
|
||||
prompt_parts = [self.i18n.slice(component) for component in components]
|
||||
prompt_parts.append(self.SCRATCHPAD_SLICE)
|
||||
prompt = PromptTemplate.from_template("".join(prompt_parts))
|
||||
prompt = "".join(prompt_parts)
|
||||
else:
|
||||
prompt_parts = [
|
||||
self.i18n.slice(component)
|
||||
@@ -56,9 +63,14 @@ class Prompts(BaseModel):
|
||||
]
|
||||
system = system_template.replace("{{ .System }}", "".join(prompt_parts))
|
||||
prompt = prompt_template.replace(
|
||||
"{{ .Prompt }}",
|
||||
"".join([self.i18n.slice("task"), self.SCRATCHPAD_SLICE]),
|
||||
"{{ .Prompt }}", "".join(self.i18n.slice("task"))
|
||||
)
|
||||
response = response_template.split("{{ .Response }}")[0]
|
||||
prompt = PromptTemplate.from_template(f"{system}\n{prompt}\n{response}")
|
||||
prompt = f"{system}\n{prompt}\n{response}"
|
||||
|
||||
prompt = (
|
||||
prompt.replace("{goal}", self.agent.goal)
|
||||
.replace("{role}", self.agent.role)
|
||||
.replace("{backstory}", self.agent.backstory)
|
||||
)
|
||||
return prompt
|
||||
|
||||
Reference in New Issue
Block a user