Skip to content

Commit

Permalink
support tracking via lunary (copy of commit from the old repo)
Browse files Browse the repository at this point in the history
  • Loading branch information
whimo committed Apr 22, 2024
1 parent b0f2804 commit ce7f040
Show file tree
Hide file tree
Showing 10 changed files with 1,083 additions and 592 deletions.
66 changes: 61 additions & 5 deletions motleycrew/agent/crewai/crewai.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import logging
from typing import Any, Optional, Sequence, Callable
from typing import Any, Optional, Sequence, Callable, Dict, List

from crewai import Agent
from langchain_core.runnables import RunnableConfig
from langchain.tools.render import render_text_description
from pydantic import Field

from motleycrew.agent.parent import MotleyAgentAbstractParent
from motleycrew.agent.shared import MotleyAgentParent
Expand All @@ -12,6 +13,59 @@
from motleycrew.common.utils import to_str
from motleycrew.common import LLMFramework
from motleycrew.common.llms import init_llm
from motleycrew.tracking import add_default_callbacks_to_langchain_config


class CrewAIAgentWithConfig(Agent):

def execute_task(
self,
task: Any,
context: Optional[str] = None,
tools: Optional[List[Any]] = None,
config: Optional[RunnableConfig] = None
) -> str:
"""Execute a task with the agent.
Args:
task: Task to execute.
context: Context to execute the task in.
tools: Tools to use for the task.
config: Runnable config
Returns:
Output of the agent
"""
task_prompt = task.prompt()

if context:
task_prompt = self.i18n.slice("task_with_context").format(
task=task_prompt, context=context
)

tools = self._parse_tools(tools or self.tools)
self.create_agent_executor(tools=tools)
self.agent_executor.tools = tools
self.agent_executor.task = task
self.agent_executor.tools_description = render_text_description(tools)
self.agent_executor.tools_names = self.__tools_names(tools)

result = self.agent_executor.invoke(
{
"input": task_prompt,
"tool_names": self.agent_executor.tools_names,
"tools": self.agent_executor.tools_description,
},
config=config
)["output"]

if self.max_rpm:
self._rpm_controller.stop_rpm_counter()

return result

@staticmethod
def __tools_names(tools) -> str:
return ", ".join([t.name for t in tools])


class CrewAIMotleyAgentParent(MotleyAgentParent):
Expand Down Expand Up @@ -59,8 +113,10 @@ def invoke(
raise ValueError(f"`task` must be a string or a Task, not {type(task)}")

langchain_tools = [tool.to_langchain_tool() for tool in self.tools.values()]
config = add_default_callbacks_to_langchain_config(config)

out = self.agent.execute_task(
task, to_str(task.message_history), tools=langchain_tools
task, to_str(task.message_history), tools=langchain_tools, config=config
)
task.outputs = [out]
# TODO: extract message history from agent, attach it to the task
Expand Down Expand Up @@ -92,7 +148,7 @@ def from_crewai_params(

def agent_factory(tools: dict[str, MotleyTool]):
langchain_tools = [t.to_langchain_tool() for t in tools.values()]
agent = Agent(
agent = CrewAIAgentWithConfig(
role=role,
goal=goal,
backstory=backstory,
Expand All @@ -114,7 +170,7 @@ def agent_factory(tools: dict[str, MotleyTool]):

@staticmethod
def from_agent(
agent: Agent,
agent: CrewAIAgentWithConfig,
delegation: bool | Sequence[MotleyAgentAbstractParent] = False,
tools: Sequence[MotleySupportedTool] | None = None,
verbose: bool = False,
Expand Down
2 changes: 2 additions & 0 deletions motleycrew/agent/langchain/langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from motleycrew.tasks import Task

from motleycrew.tool import MotleyTool
from motleycrew.tracking import add_default_callbacks_to_langchain_config
from motleycrew.common import MotleySupportedTool
from motleycrew.common import LLMFramework
from motleycrew.common.llms import init_llm
Expand Down Expand Up @@ -41,6 +42,7 @@ def invoke(
) -> Task:
self.materialize()

config = add_default_callbacks_to_langchain_config(config)
if isinstance(task, str):
# TODO: feed in context/task.message_history correctly
# TODO: attach the current task, if any, as a dependency of the new task
Expand Down
22 changes: 14 additions & 8 deletions motleycrew/agent/llama_index/llama_index_react.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
from typing import Sequence
from llama_index.core.agent import ReActAgent
from llama_index.core.llms import LLM
from llama_index.core.callbacks import CallbackManager

from motleycrew.agent.llama_index import LlamaIndexMotleyAgentParent
from motleycrew.agent.parent import MotleyAgentAbstractParent
from motleycrew.tool import MotleyTool
from motleycrew.common import MotleySupportedTool
from motleycrew.common import LLMFramework
from motleycrew.common.llms import init_llm
from motleycrew.tracking import get_default_callbacks_list


class ReActLlamaIndexMotleyAgent(LlamaIndexMotleyAgentParent):
def __init__(self,
goal: str,
name: str | None = None,
delegation: bool | Sequence[MotleyAgentAbstractParent] = False,
tools: Sequence[MotleySupportedTool] | None = None,
llm: LLM | None = None,
verbose: bool = False):
def __init__(
self,
goal: str,
name: str | None = None,
delegation: bool | Sequence[MotleyAgentAbstractParent] = False,
tools: Sequence[MotleySupportedTool] | None = None,
llm: LLM | None = None,
verbose: bool = False,
):
if llm is None:
llm = init_llm(llm_framework=LLMFramework.LLAMA_INDEX)

def agent_factory(tools: dict[str, MotleyTool]):
llama_index_tools = [t.to_llama_index_tool() for t in tools.values()]
# TODO: feed goal into the agent's prompt
callbacks = get_default_callbacks_list(LLMFramework.LLAMA_INDEX)
agent = ReActAgent.from_tools(
tools=llama_index_tools,
llm=llm,
verbose=verbose,
callback_manager=CallbackManager(callbacks),
)
return agent

Expand All @@ -37,5 +43,5 @@ def agent_factory(tools: dict[str, MotleyTool]):
agent_factory=agent_factory,
delegation=delegation,
tools=tools,
verbose=verbose
verbose=verbose,
)
14 changes: 14 additions & 0 deletions motleycrew/common/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,17 @@ class LLMFamily:
class LLMFramework:
LANGCHAIN = "langchain"
LLAMA_INDEX = "llama_index"


class LunaryRunType:
LLM = "llm"
AGENT = "agent"
TOOL = "tool"
CHAIN = "chain"
EMBED = "embed"


class LunaryEventName:
START = "start"
END = "end"
ERROR = "error"
6 changes: 6 additions & 0 deletions motleycrew/tasks/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ def __init__(

self.crew.add_task(self)

self.human_input = ''
self.tools_errors: int = 0

def increment_tools_errors(self):
self.tools_errors += 1

def prompt(self) -> str:
"""
For compatibility with crewai.Agent.execute_task
Expand Down
1 change: 1 addition & 0 deletions motleycrew/tracking/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .utils import add_default_callbacks_to_langchain_config, get_default_callbacks_list
Loading

0 comments on commit ce7f040

Please sign in to comment.