mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-11 00:58:30 +00:00
fixed mixin (#831)
* fixed mixin * WIP: fixing types * type fixes on mixin
This commit is contained in:
@@ -15,7 +15,8 @@ from pydantic import (
|
|||||||
from pydantic_core import PydanticCustomError
|
from pydantic_core import PydanticCustomError
|
||||||
|
|
||||||
from crewai.utilities import I18N, RPMController, Logger
|
from crewai.utilities import I18N, RPMController, Logger
|
||||||
from crewai.agents import CacheHandler, ToolsHandler
|
from crewai.agents.cache.cache_handler import CacheHandler
|
||||||
|
from crewai.agents.tools_handler import ToolsHandler
|
||||||
from crewai.utilities.token_counter_callback import TokenProcess
|
from crewai.utilities.token_counter_callback import TokenProcess
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,65 +1,109 @@
|
|||||||
import time
|
import time
|
||||||
|
from typing import TYPE_CHECKING, Optional
|
||||||
|
|
||||||
from crewai.memory.entity.entity_memory_item import EntityMemoryItem
|
from crewai.memory.entity.entity_memory_item import EntityMemoryItem
|
||||||
from crewai.memory.long_term.long_term_memory_item import LongTermMemoryItem
|
from crewai.memory.long_term.long_term_memory_item import LongTermMemoryItem
|
||||||
from crewai.memory.short_term.short_term_memory_item import ShortTermMemoryItem
|
from crewai.memory.short_term.short_term_memory_item import ShortTermMemoryItem
|
||||||
from crewai.utilities.converter import ConverterError
|
from crewai.utilities.converter import ConverterError
|
||||||
from crewai.utilities.evaluators.task_evaluator import TaskEvaluator
|
from crewai.utilities.evaluators.task_evaluator import TaskEvaluator
|
||||||
|
from crewai.utilities import I18N
|
||||||
|
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from crewai.crew import Crew
|
||||||
|
from crewai.task import Task
|
||||||
|
from crewai.agents.agent_builder.base_agent import BaseAgent
|
||||||
|
|
||||||
|
|
||||||
class CrewAgentExecutorMixin:
|
class CrewAgentExecutorMixin:
|
||||||
|
crew: Optional["Crew"]
|
||||||
|
crew_agent: Optional["BaseAgent"]
|
||||||
|
task: Optional["Task"]
|
||||||
|
iterations: int
|
||||||
|
force_answer_max_iterations: int
|
||||||
|
have_forced_answer: bool
|
||||||
|
_i18n: I18N
|
||||||
|
|
||||||
def _should_force_answer(self) -> bool:
|
def _should_force_answer(self) -> bool:
|
||||||
|
"""Determine if a forced answer is required based on iteration count."""
|
||||||
return (
|
return (
|
||||||
self.iterations == self.force_answer_max_iterations
|
self.iterations == self.force_answer_max_iterations
|
||||||
) and not self.have_forced_answer
|
) and not self.have_forced_answer
|
||||||
|
|
||||||
def _create_short_term_memory(self, output) -> None:
|
def _create_short_term_memory(self, output) -> None:
|
||||||
|
"""Create and save a short-term memory item if conditions are met."""
|
||||||
|
if (
|
||||||
|
self.crew
|
||||||
|
and self.crew_agent
|
||||||
|
and self.task
|
||||||
|
and "Action: Delegate work to coworker" not in output.log
|
||||||
|
):
|
||||||
|
try:
|
||||||
|
memory = ShortTermMemoryItem(
|
||||||
|
data=output.log,
|
||||||
|
agent=self.crew_agent.role,
|
||||||
|
metadata={
|
||||||
|
"observation": self.task.description,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if (
|
||||||
|
hasattr(self.crew, "_short_term_memory")
|
||||||
|
and self.crew._short_term_memory
|
||||||
|
):
|
||||||
|
self.crew._short_term_memory.save(memory)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Failed to add to short term memory: {e}")
|
||||||
|
pass
|
||||||
|
|
||||||
|
def _create_long_term_memory(self, output) -> None:
|
||||||
|
"""Create and save long-term and entity memory items based on evaluation."""
|
||||||
if (
|
if (
|
||||||
self.crew
|
self.crew
|
||||||
and self.crew.memory
|
and self.crew.memory
|
||||||
and "Action: Delegate work to coworker" not in output.log
|
and self.crew._long_term_memory
|
||||||
|
and self.crew._entity_memory
|
||||||
|
and self.task
|
||||||
|
and self.crew_agent
|
||||||
):
|
):
|
||||||
memory = ShortTermMemoryItem(
|
try:
|
||||||
data=output.log,
|
ltm_agent = TaskEvaluator(self.crew_agent)
|
||||||
agent=self.crew_agent.role,
|
evaluation = ltm_agent.evaluate(self.task, output.log)
|
||||||
metadata={
|
|
||||||
"observation": self.task.description,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
self.crew._short_term_memory.save(memory)
|
|
||||||
|
|
||||||
def _create_long_term_memory(self, output) -> None:
|
if isinstance(evaluation, ConverterError):
|
||||||
if self.crew and self.crew.memory:
|
return
|
||||||
ltm_agent = TaskEvaluator(self.crew_agent)
|
|
||||||
evaluation = ltm_agent.evaluate(self.task, output.log)
|
|
||||||
|
|
||||||
if isinstance(evaluation, ConverterError):
|
long_term_memory = LongTermMemoryItem(
|
||||||
return
|
task=self.task.description,
|
||||||
|
agent=self.crew_agent.role,
|
||||||
long_term_memory = LongTermMemoryItem(
|
quality=evaluation.quality,
|
||||||
task=self.task.description,
|
datetime=str(time.time()),
|
||||||
agent=self.crew_agent.role,
|
expected_output=self.task.expected_output,
|
||||||
quality=evaluation.quality,
|
metadata={
|
||||||
datetime=str(time.time()),
|
"suggestions": evaluation.suggestions,
|
||||||
expected_output=self.task.expected_output,
|
"quality": evaluation.quality,
|
||||||
metadata={
|
},
|
||||||
"suggestions": evaluation.suggestions,
|
|
||||||
"quality": evaluation.quality,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
self.crew._long_term_memory.save(long_term_memory)
|
|
||||||
|
|
||||||
for entity in evaluation.entities:
|
|
||||||
entity_memory = EntityMemoryItem(
|
|
||||||
name=entity.name,
|
|
||||||
type=entity.type,
|
|
||||||
description=entity.description,
|
|
||||||
relationships="\n".join([f"- {r}" for r in entity.relationships]),
|
|
||||||
)
|
)
|
||||||
self.crew._entity_memory.save(entity_memory)
|
self.crew._long_term_memory.save(long_term_memory)
|
||||||
|
|
||||||
|
for entity in evaluation.entities:
|
||||||
|
entity_memory = EntityMemoryItem(
|
||||||
|
name=entity.name,
|
||||||
|
type=entity.type,
|
||||||
|
description=entity.description,
|
||||||
|
relationships="\n".join(
|
||||||
|
[f"- {r}" for r in entity.relationships]
|
||||||
|
),
|
||||||
|
)
|
||||||
|
self.crew._entity_memory.save(entity_memory)
|
||||||
|
except AttributeError as e:
|
||||||
|
print(f"Missing attributes for long term memory: {e}")
|
||||||
|
pass
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Failed to add to long term memory: {e}")
|
||||||
|
pass
|
||||||
|
|
||||||
def _ask_human_input(self, final_answer: dict) -> str:
|
def _ask_human_input(self, final_answer: dict) -> str:
|
||||||
"""Get human input."""
|
"""Prompt human input for final decision making."""
|
||||||
return input(
|
return input(
|
||||||
self._i18n.slice("getting_input").format(final_answer=final_answer)
|
self._i18n.slice("getting_input").format(final_answer=final_answer)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,6 +1,14 @@
|
|||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
from typing import Any, Dict, Iterator, List, Optional, Tuple, Union
|
from typing import (
|
||||||
|
Any,
|
||||||
|
Dict,
|
||||||
|
Iterator,
|
||||||
|
List,
|
||||||
|
Optional,
|
||||||
|
Tuple,
|
||||||
|
Union,
|
||||||
|
)
|
||||||
|
|
||||||
from langchain.agents import AgentExecutor
|
from langchain.agents import AgentExecutor
|
||||||
from langchain.agents.agent import ExceptionTool
|
from langchain.agents.agent import ExceptionTool
|
||||||
@@ -11,13 +19,15 @@ from langchain_core.exceptions import OutputParserException
|
|||||||
from langchain_core.tools import BaseTool
|
from langchain_core.tools import BaseTool
|
||||||
from langchain_core.utils.input import get_color_mapping
|
from langchain_core.utils.input import get_color_mapping
|
||||||
from pydantic import InstanceOf
|
from pydantic import InstanceOf
|
||||||
from crewai.agents.agent_builder.base_agent_executor_mixin import CrewAgentExecutorMixin
|
from crewai.agents.agent_builder.base_agent_executor_mixin import (
|
||||||
|
CrewAgentExecutorMixin,
|
||||||
|
)
|
||||||
|
|
||||||
from crewai.agents.tools_handler import ToolsHandler
|
from crewai.agents.tools_handler import ToolsHandler
|
||||||
from crewai.tools.tool_usage import ToolUsage, ToolUsageErrorException
|
from crewai.tools.tool_usage import ToolUsage, ToolUsageErrorException
|
||||||
from crewai.utilities import I18N
|
|
||||||
from crewai.utilities.constants import TRAINING_DATA_FILE
|
from crewai.utilities.constants import TRAINING_DATA_FILE
|
||||||
from crewai.utilities.training_handler import CrewTrainingHandler
|
from crewai.utilities.training_handler import CrewTrainingHandler
|
||||||
|
from crewai.utilities import I18N
|
||||||
|
|
||||||
|
|
||||||
class CrewAgentExecutor(AgentExecutor, CrewAgentExecutorMixin):
|
class CrewAgentExecutor(AgentExecutor, CrewAgentExecutorMixin):
|
||||||
|
|||||||
Reference in New Issue
Block a user