mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-10 00:28:31 +00:00
first stab at early concepts
This commit is contained in:
2
crewai/__init__.py
Normal file
2
crewai/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from .crew import Crew
|
||||
from .agent import Agent
|
||||
48
crewai/agent.py
Normal file
48
crewai/agent.py
Normal file
@@ -0,0 +1,48 @@
|
||||
"""Generic agent."""
|
||||
|
||||
from typing import List, Any
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from langchain.tools import Tool
|
||||
from langchain.agents import AgentExecutor
|
||||
from langchain.chat_models import ChatOpenAI as OpenAI
|
||||
from langchain.tools.render import render_text_description
|
||||
from langchain.agents.format_scratchpad import format_log_to_str
|
||||
from langchain.agents.output_parsers import ReActSingleInputOutputParser
|
||||
|
||||
from .prompts import AGENT_EXECUTION_PROMPT
|
||||
|
||||
class Agent(BaseModel):
|
||||
role: str = Field(description="Role of the agent")
|
||||
goal: str = Field(description="Objective of the agent")
|
||||
backstory: str = Field(description="Backstory of the agent")
|
||||
tools: List[Tool] = Field(description="Tools at agents disposal")
|
||||
llm: str = Field(description="LLM of the agent", default=OpenAI(
|
||||
temperature=0.7,
|
||||
model="gpt-4",
|
||||
verbose=True
|
||||
))
|
||||
|
||||
def execute(self, task: str) -> str:
|
||||
prompt = AGENT_EXECUTION_PROMPT.partial(
|
||||
tools=render_text_description(self.tools),
|
||||
tool_names=self.__tools_names(),
|
||||
backstory=self.backstory,
|
||||
role=self.role,
|
||||
goal=self.goal,
|
||||
)
|
||||
return self.__run(task, prompt, self.tools)
|
||||
|
||||
def __run(self, input: str, prompt: str, tools: List[Tool]) -> str:
|
||||
chat_with_bind = self.llm.bind(stop=["\nObservation"])
|
||||
agent = {
|
||||
"input": lambda x: x["input"],
|
||||
"agent_scratchpad": lambda x: format_log_to_str(x['intermediate_steps'])
|
||||
} | prompt | chat_with_bind | ReActSingleInputOutputParser()
|
||||
|
||||
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True, handle_parsing_errors=True)
|
||||
return agent_executor.invoke({"input": input})['output']
|
||||
|
||||
def __tools_names(self) -> str:
|
||||
return ", ".join([t.name for t in self.tools])
|
||||
|
||||
7
crewai/crew.py
Normal file
7
crewai/crew.py
Normal file
@@ -0,0 +1,7 @@
|
||||
"""Crew of agents."""
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
class Crew(BaseModel):
|
||||
description: str = Field(description="Description and of the crew being created.")
|
||||
goal: str = Field(description="Objective of the crew being created.")
|
||||
39
crewai/prompts.py
Normal file
39
crewai/prompts.py
Normal file
@@ -0,0 +1,39 @@
|
||||
"""Prompts for generic agent."""
|
||||
|
||||
from langchain.prompts import PromptTemplate
|
||||
|
||||
AGENT_EXECUTION_PROMPT = PromptTemplate.from_template(
|
||||
"""You are {role}.
|
||||
{backstory}
|
||||
|
||||
Your main goal is: {goal}
|
||||
|
||||
TOOLS:
|
||||
------
|
||||
|
||||
You have access to the following tools:
|
||||
|
||||
{tools}
|
||||
|
||||
To use a tool, please use the following format:
|
||||
|
||||
```
|
||||
Thought: Do I need to use a tool? Yes
|
||||
Action: the action to take, should be one of [{tool_names}]
|
||||
Action Input: the input to the action
|
||||
Observation: the result of the action
|
||||
```
|
||||
|
||||
When you have a response for your task, or if you do not need to use a tool, you MUST use the format:
|
||||
|
||||
```
|
||||
Thought: Do I need to use a tool? No
|
||||
Final Answer: [your response here]
|
||||
```
|
||||
|
||||
Begin!
|
||||
|
||||
Current Task: {input}
|
||||
{agent_scratchpad}
|
||||
"""
|
||||
)
|
||||
Reference in New Issue
Block a user