Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
bhancockio committed Jan 6, 2025
1 parent cea5a28 commit 33ca1e3
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 121 deletions.
10 changes: 10 additions & 0 deletions src/crewai/cli/crew_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,17 @@ def run_chat():
crew_tool_schema = generate_crew_tool_schema(crew_chat_inputs)
system_message = build_system_message(crew_chat_inputs)

# Call the LLM to generate the introductory message
introductory_message = chat_llm.call(
messages=[{"role": "system", "content": system_message}]
)
click.secho(f"\nAssistant: {introductory_message}\n", fg="green")

messages = [
{"role": "system", "content": system_message},
{"role": "assistant", "content": introductory_message},
]

available_functions = {
crew_chat_inputs.crew_name: create_tool_function(crew, messages),
}
Expand Down Expand Up @@ -77,6 +85,8 @@ def build_system_message(crew_chat_inputs: ChatInputs) -> str:
"If a user asks a question outside the crew's scope, provide a brief answer and remind them of the crew's purpose. "
"After calling the tool, be prepared to take user feedback and make adjustments as needed. "
"If you are ever unsure about a user's request or need clarification, ask the user for more information."
"Before doing anything else, introduce yourself with a friendly message like: 'Hey! I'm here to help you with [crew's purpose]. Could you please provide me with [inputs] so we can get started?' "
"For example: 'Hey! I'm here to help you with uncovering and reporting cutting-edge developments through thorough research and detailed analysis. Could you please provide me with a topic you're interested in? This will help us generate a comprehensive research report and detailed analysis.'"
f"\nCrew Name: {crew_chat_inputs.crew_name}"
f"\nCrew Description: {crew_chat_inputs.crew_description}"
)
Expand Down
86 changes: 0 additions & 86 deletions src/crewai/cli/fetch_crew_inputs.py

This file was deleted.

7 changes: 1 addition & 6 deletions src/crewai/cli/templates/crew/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#!/usr/bin/env python
import sys
import json
import warnings

from {{folder_name}}.crew import {{crew_name}}
from crewai.utilities.llm_utils import create_llm

warnings.filterwarnings("ignore", category=SyntaxWarning, module="pysbd")

Expand All @@ -15,13 +13,10 @@

def run():
"""
Run the crew, allowing CLI overrides for required inputs.
Usage example:
uv run run_crew -- --topic="New Topic" --some_other_field="Value"
Run the crew.
"""
inputs = {
'topic': 'AI LLMs'
# Add any other default fields here
}

try:
Expand Down
4 changes: 0 additions & 4 deletions src/crewai/crew.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,6 @@ class Crew(BaseModel):
default=None,
description="LLM used to handle chatting with the crew.",
)
chat_inputs: Optional[ChatInputs] = Field(
default=None,
description="Holds descriptions of the crew as well as named inputs for chat usage.",
)
_knowledge: Optional[Knowledge] = PrivateAttr(
default=None,
)
Expand Down
11 changes: 3 additions & 8 deletions src/crewai/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ def __init__(
self.callbacks = callbacks
self.context_window_size = 0

# For safety, we disable passing init params to next calls
litellm.drop_params = True

self.set_callbacks(callbacks)
Expand Down Expand Up @@ -247,40 +246,36 @@ def call(
function_name = tool_call.function.name

if function_name in available_functions:
# Parse arguments
try:
function_args = json.loads(tool_call.function.arguments)
except json.JSONDecodeError as e:
logging.warning(f"Failed to parse function arguments: {e}")
return text_response # Fallback to text response
return text_response

fn = available_functions[function_name]
try:
# Call the actual tool function
result = fn(**function_args)

# Return the result directly
return result

except Exception as e:
logging.error(
f"Error executing function '{function_name}': {e}"
)
return text_response # Fallback to text response
return text_response

else:
logging.warning(
f"Tool call requested unknown function '{function_name}'"
)
return text_response # Fallback to text response
return text_response

except Exception as e:
# Check if context length was exceeded, otherwise log
if not LLMContextLengthExceededException(
str(e)
)._is_context_limit_error(str(e)):
logging.error(f"LiteLLM call failed: {str(e)}")
# Re-raise the exception
raise

def supports_function_calling(self) -> bool:
Expand Down
28 changes: 13 additions & 15 deletions src/crewai/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ def _execute_core(
self.retry_count += 1
context = self.i18n.errors("validation_error").format(
guardrail_result_error=guardrail_result.error,
task_output=task_output.raw
task_output=task_output.raw,
)
printer = Printer()
printer.print(
Expand Down Expand Up @@ -431,9 +431,7 @@ def _execute_core(
content = (
json_output
if json_output
else pydantic_output.model_dump_json()
if pydantic_output
else result
else pydantic_output.model_dump_json() if pydantic_output else result
)
self._save_file(content)

Expand All @@ -453,11 +451,12 @@ def prompt(self) -> str:
tasks_slices = [self.description, output]
return "\n".join(tasks_slices)


def interpolate_inputs_and_add_conversation_history(self, inputs: Dict[str, Union[str, int, float]]) -> None:
def interpolate_inputs_and_add_conversation_history(
self, inputs: Dict[str, Union[str, int, float]]
) -> None:
"""Interpolate inputs into the task description, expected output, and output file path.
Add conversation history if present.
Args:
inputs: Dictionary mapping template variables to their values.
Supported value types are strings, integers, and floats.
Expand Down Expand Up @@ -497,16 +496,15 @@ def interpolate_inputs_and_add_conversation_history(self, inputs: Dict[str, Unio
input_string=self._original_output_file, inputs=inputs
)
except (KeyError, ValueError) as e:
raise ValueError(f"Error interpolating output_file path: {str(e)}") from e

raise ValueError(
f"Error interpolating output_file path: {str(e)}"
) from e

if "crew_chat_messages" in inputs and inputs["crew_chat_messages"]:
# Fetch the conversation history instruction using self.i18n.slice
conversation_instruction = self.i18n.slice(
"conversation_history_instruction"
)
print("crew_chat_messages:", inputs["crew_chat_messages"])

# Ensure that inputs["crew_chat_messages"] is a string
crew_chat_messages_json = str(inputs["crew_chat_messages"])

try:
Expand All @@ -515,15 +513,15 @@ def interpolate_inputs_and_add_conversation_history(self, inputs: Dict[str, Unio
print("An error occurred while parsing crew chat messages:", e)
raise

# Process the messages to build conversation history
conversation_history = "\n".join(
f"{msg['role'].capitalize()}: {msg['content']}"
for msg in crew_chat_messages
if isinstance(msg, dict) and "role" in msg and "content" in msg
)

# Add the instruction and conversation history to the description
self.description += f"\n\n{conversation_instruction}\n\n{conversation_history}"
self.description += (
f"\n\n{conversation_instruction}\n\n{conversation_history}"
)

def interpolate_only(
self, input_string: Optional[str], inputs: Dict[str, Union[str, int, float]]
Expand Down
2 changes: 0 additions & 2 deletions src/crewai/utilities/llm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def create_llm(
if isinstance(llm_value, str):
try:
created_llm = LLM(model=llm_value)
print(f"LLM created with model='{llm_value}'")
return created_llm
except Exception as e:
print(f"Failed to instantiate LLM with model='{llm_value}': {e}")
Expand Down Expand Up @@ -197,7 +196,6 @@ def _llm_via_environment_or_fallback() -> Optional[LLM]:
# Try creating the LLM
try:
new_llm = LLM(**llm_params)
print(f"LLM created with model='{model_name}'")
return new_llm
except Exception as e:
print(
Expand Down

0 comments on commit 33ca1e3

Please sign in to comment.