Created
December 6, 2024 11:17
-
-
Save bemijonathan/d3bec09b610075136a00ac97f52a4a98 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseAgent(): | |
def __init__(self, name, role, provider_config, tools): | |
self.name = name | |
self.role = role | |
self.memory = [] | |
self.provider = provider_config | |
self.tools = tools | |
async def use_tool(self, tool_name, params: Dict[str, Any]) | |
"""Execute a tool with given parameters""" | |
tool = self.tools.get(tool_name) | |
return result | |
async def get_completion(self, prompt): | |
"""Get completion from the AI provider""" | |
return completion results | |
async def get_completion_with_history(self, messages: List[Dict[str, str]]) -> str: | |
"""Get completion with conversation history""" | |
return await self.provider.generate_with_history(messages) | |
async def reflect(self, context: Dict): | |
""" | |
Perform self-reflection on the current context. | |
Returns structured thoughts about the situation. | |
""" | |
# Add available tools to context | |
context["available_tools"] = list(self.tools.keys()) | |
thought_process = ThoughtProcess( | |
observation=await self._observe(context), | |
thought=await self._think(context), | |
reasoning=await self._reason(context), | |
plan=await self._plan(context), | |
criticism=await self._self_criticize(context) | |
) | |
self.memory.append(thought_process) | |
return thought_process | |
async def execute_plan(self, plan: List[str], context: Dict) -> List[ToolResult]: | |
"""Execute a plan using available tools loops through the steps """ | |
results = [] | |
for step in plan: | |
tool_result = await self._execute_plan_step(step, context) | |
results.append(tool_result) | |
if not tool_result.success: | |
break | |
return results | |
async def summarize_experience(self) -> str: | |
"""Provide a summary of the agent's experience and learning.""" | |
if not self.memory: | |
return "No experience recorded yet." | |
return await self._generate_experience_summary() | |
async def collaborate(self, other_agent: 'BaseAgent', context: Dict) -> Dict: | |
""" | |
Collaborate with another agent. | |
Returns the results of the collaboration. | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment