From e11c7d1fd85f1a2120c02656470b09089da13fc9 Mon Sep 17 00:00:00 2001 From: Brandon Hancock Date: Wed, 26 Mar 2025 14:51:53 -0400 Subject: [PATCH] WIP --- docs/concepts/flows.mdx | 2 + examples/lite_agent_example.py | 82 +++++++++++++++++----------------- src/crewai/lite_agent.py | 15 ++----- 3 files changed, 47 insertions(+), 52 deletions(-) diff --git a/docs/concepts/flows.mdx b/docs/concepts/flows.mdx index 8ab99ec01..62d00550c 100644 --- a/docs/concepts/flows.mdx +++ b/docs/concepts/flows.mdx @@ -48,6 +48,8 @@ class ExampleFlow(Flow): ], ) + #TODO: NEED TO ADD AN EXAMPLE AGENT IN HERE AS WELL. + random_city = response["choices"][0]["message"]["content"] # Store the city in our state self.state["city"] = random_city diff --git a/examples/lite_agent_example.py b/examples/lite_agent_example.py index 1d0642f90..d8a15780d 100644 --- a/examples/lite_agent_example.py +++ b/examples/lite_agent_example.py @@ -25,7 +25,7 @@ class WebSearchTool(BaseTool): """Search the web for information about a topic.""" # This is a mock implementation if "tokyo" in query.lower(): - return "Tokyo's population in 2023 was approximately 14 million people in the city proper, and 37 million in the greater metropolitan area." + return "Tokyo's population in 2023 was approximately 21 million people in the city proper, and 37 million in the greater metropolitan area." elif "climate change" in query.lower() and "coral" in query.lower(): return "Climate change severely impacts coral reefs through: 1) Ocean warming causing coral bleaching, 2) Ocean acidification reducing calcification, 3) Sea level rise affecting light availability, 4) Increased storm frequency damaging reef structures. Sources: NOAA Coral Reef Conservation Program, Global Coral Reef Alliance." else: @@ -80,8 +80,8 @@ async def main(): # print(f"Raw response: {result.raw}") # Example 2: Query with structured output - # print("\n=== Example 2: Structured Output ===") - # structured_query = """ + print("\n=== Example 2: Structured Output ===") + structured_query = """ # Research the impact of climate change on coral reefs. # YOU MUST format your response as a valid JSON object with the following structure: @@ -94,48 +94,48 @@ async def main(): # Include at least 3 key points and 2 sources. Wrap your JSON in ```json and ``` tags. # """ - # result = await agent.kickoff_async(structured_query) + result = await agent.kickoff_async(structured_query) - # if result.pydantic: - # # Cast to the specific type for better IDE support - # research_result = cast(ResearchResult, result.pydantic) - # print(f"Main findings: {research_result.main_findings}") - # print("\nKey points:") - # for i, point in enumerate(research_result.key_points, 1): - # print(f"{i}. {point}") - # print("\nSources:") - # for i, source in enumerate(research_result.sources, 1): - # print(f"{i}. {source}") - # else: - # print(f"Raw response: {result.raw}") - # print( - # "\nNote: Structured output was not generated. The LLM may need more explicit instructions to format the response as JSON." - # ) - # print("Usage metrics:") - # print(result.usage_metrics) + if result.pydantic: + # Cast to the specific type for better IDE support + research_result = cast(ResearchResult, result.pydantic) + print(f"Main findings: {research_result.main_findings}") + print("\nKey points:") + for i, point in enumerate(research_result.key_points, 1): + print(f"{i}. {point}") + print("\nSources:") + for i, source in enumerate(research_result.sources, 1): + print(f"{i}. {source}") + else: + print(f"Raw response: {result.raw}") + print( + "\nNote: Structured output was not generated. The LLM may need more explicit instructions to format the response as JSON." + ) + print("Usage metrics:") + print(result.usage_metrics) - # Example 3: Multi-turn conversation - print("\n=== Example 3: Multi-turn Conversation ===") - messages = [ - {"role": "user", "content": "I'm planning a trip to Japan."}, - { - "role": "assistant", - "content": "That sounds exciting! Japan is a beautiful country with rich culture, delicious food, and stunning landscapes. What would you like to know about Japan to help with your trip planning?", - }, - { - "role": "user", - "content": "What are the best times to visit Tokyo and Kyoto?", - }, - ] + # # Example 3: Multi-turn conversation + # print("\n=== Example 3: Multi-turn Conversation ===") + # messages = [ + # {"role": "user", "content": "I'm planning a trip to Japan."}, + # { + # "role": "assistant", + # "content": "That sounds exciting! Japan is a beautiful country with rich culture, delicious food, and stunning landscapes. What would you like to know about Japan to help with your trip planning?", + # }, + # { + # "role": "user", + # "content": "What are the best times to visit Tokyo and Kyoto?", + # }, + # ] - result = await agent.kickoff_async(messages) - print(f"Response: {result.raw}") + # result = await agent.kickoff_async(messages) + # print(f"Response: {result.raw}") - # Print usage metrics if available - if result.usage_metrics: - print("\nUsage metrics:") - for key, value in result.usage_metrics.items(): - print(f"{key}: {value}") + # # Print usage metrics if available + # if result.usage_metrics: + # print("\nUsage metrics:") + # for key, value in result.usage_metrics.items(): + # print(f"{key}: {value}") if __name__ == "__main__": diff --git a/src/crewai/lite_agent.py b/src/crewai/lite_agent.py index c3f8d2217..683a3c887 100644 --- a/src/crewai/lite_agent.py +++ b/src/crewai/lite_agent.py @@ -14,12 +14,10 @@ from crewai.agents.parser import ( AgentFinish, OutputParserException, ) -from crewai.agents.tools_handler import ToolsHandler from crewai.llm import LLM from crewai.tools.base_tool import BaseTool from crewai.tools.structured_tool import CrewStructuredTool from crewai.tools.tool_usage import ToolUsage, ToolUsageErrorException -from crewai.types.usage_metrics import UsageMetrics from crewai.utilities import I18N from crewai.utilities.agent_utils import ( enforce_rpm_limit, @@ -230,8 +228,6 @@ class LiteAgent(BaseModel): response_format=schema ) - print("BASE PROMPT:", base_prompt) - return base_prompt def _format_messages( @@ -310,7 +306,7 @@ class LiteAgent(BaseModel): ) # Execute the agent using invoke loop - agent_finish = await self._invoke() + agent_finish = await self._invoke_loop() formatted_result: Optional[BaseModel] = None if self.response_format: @@ -350,16 +346,13 @@ class LiteAgent(BaseModel): raise e raise e - async def _invoke(self) -> AgentFinish: + async def _invoke_loop(self) -> AgentFinish: """ Run the agent's thought process until it reaches a conclusion or max iterations. - Similar to _invoke_loop in CrewAgentExecutor. Returns: str: The final result of the agent execution. """ - # Use the stored callbacks - callbacks = self._callbacks # Execute the agent loop formatted_answer = None @@ -372,7 +365,7 @@ class LiteAgent(BaseModel): i18n=self.i18n, messages=self._messages, llm=cast(LLM, self.llm), - callbacks=callbacks, + callbacks=self._callbacks, ) enforce_rpm_limit(self.request_within_rpm_limit) @@ -380,7 +373,7 @@ class LiteAgent(BaseModel): answer = get_llm_response( llm=cast(LLM, self.llm), messages=self._messages, - callbacks=callbacks, + callbacks=self._callbacks, printer=self._printer, ) formatted_answer = process_llm_response(answer, self.use_stop_words)