diff --git a/examples/llm_generations_example.py b/examples/llm_generations_example.py index 688f1536c..39a997526 100644 --- a/examples/llm_generations_example.py +++ b/examples/llm_generations_example.py @@ -2,8 +2,8 @@ Example demonstrating the new LLM generations and logprobs functionality. """ -from crewai import Agent, Task, Crew, LLM -from crewai.utilities.xml_parser import extract_xml_content, extract_multiple_xml_tags +from crewai import Agent, Task, LLM +from crewai.utilities.xml_parser import extract_xml_content def example_multiple_generations(): @@ -133,7 +133,7 @@ def example_logprobs_analysis(): print(f"Available logprobs data: {len(logprobs)} choices") if usage: - print(f"\nToken usage:") + print("\nToken usage:") print(f"Prompt tokens: {usage.get('prompt_tokens', 'N/A')}") print(f"Completion tokens: {usage.get('completion_tokens', 'N/A')}") print(f"Total tokens: {usage.get('total_tokens', 'N/A')}") diff --git a/src/crewai/utilities/agent_utils.py b/src/crewai/utilities/agent_utils.py index 93e242234..ac9db962a 100644 --- a/src/crewai/utilities/agent_utils.py +++ b/src/crewai/utilities/agent_utils.py @@ -109,7 +109,11 @@ def handle_max_iterations_exceeded( ) raise ValueError("Invalid response from LLM call - None or empty.") - formatted_answer = format_answer(answer) + text_answer = answer + if isinstance(answer, dict) and "content" in answer: + text_answer = answer["content"] + + formatted_answer = format_answer(str(text_answer)) # Return the formatted answer, regardless of its type return formatted_answer @@ -149,11 +153,18 @@ def get_llm_response( ) -> Union[str, Dict[str, Any]]: """Call the LLM and return the response, handling any invalid responses.""" try: - answer = llm.call( - messages, - callbacks=callbacks, - return_full_completion=return_full_completion, - ) + from crewai.llm import LLM + if isinstance(llm, LLM) and return_full_completion: + answer = llm.call( + messages, + callbacks=callbacks, + return_full_completion=return_full_completion, + ) + else: + answer = llm.call( + messages, + callbacks=callbacks, + ) except Exception as e: printer.print( content=f"Error during LLM call: {e}", @@ -190,12 +201,12 @@ def process_llm_response( if not use_stop_words: try: # Preliminary parsing to check for errors. - format_answer(text_answer) + format_answer(str(text_answer)) except OutputParserException as e: if FINAL_ANSWER_AND_PARSABLE_ACTION_ERROR_MESSAGE in e.error: - text_answer = text_answer.split("Observation:")[0].strip() + text_answer = str(text_answer).split("Observation:")[0].strip() - return format_answer(text_answer) + return format_answer(str(text_answer)) def handle_agent_action_core( diff --git a/tests/test_integration_llm_features.py b/tests/test_integration_llm_features.py index 251c34fb6..b19f8877b 100644 --- a/tests/test_integration_llm_features.py +++ b/tests/test_integration_llm_features.py @@ -1,4 +1,3 @@ -import pytest from unittest.mock import Mock, patch from crewai import Agent, Task, Crew, LLM from crewai.lite_agent import LiteAgent diff --git a/tests/test_llm_generations_logprobs.py b/tests/test_llm_generations_logprobs.py index d6f52d19a..45f650172 100644 --- a/tests/test_llm_generations_logprobs.py +++ b/tests/test_llm_generations_logprobs.py @@ -1,8 +1,7 @@ -import pytest from unittest.mock import Mock, patch -from crewai import Agent, Task, LLM +from crewai import Agent, LLM from crewai.tasks.task_output import TaskOutput -from crewai.lite_agent import LiteAgent, LiteAgentOutput +from crewai.lite_agent import LiteAgentOutput from crewai.utilities.xml_parser import ( extract_xml_content, extract_all_xml_content, @@ -202,7 +201,7 @@ class TestXMLParser: """Test removing XML tags and their content.""" text = "Keep this Remove this and this" result = remove_xml_tags(text, ["thinking"]) - assert result == "Keep this and this" + assert result == "Keep this and this" def test_strip_xml_tags_keep_content(self): """Test stripping XML tags but keeping content.""" diff --git a/tests/test_xml_parser_examples.py b/tests/test_xml_parser_examples.py index 24194d978..a4555650b 100644 --- a/tests/test_xml_parser_examples.py +++ b/tests/test_xml_parser_examples.py @@ -1,4 +1,3 @@ -import pytest from crewai.utilities.xml_parser import ( extract_xml_content, extract_all_xml_content,