crewAIInc / crewAI

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.
https://crewai.com
MIT License
19.09k stars 2.63k forks source link

CrewAI on Deepseek Coder web api..... $0.28 per million tokens.. worth fixing !! #597

Closed evmond1 closed 2 weeks ago

evmond1 commented 4 months ago

Very odd one here, this tool works like a dream with GPT4, however when I moved to deepseek cloud api (muuuuuch cheaper but looks awesome), the crew execution chain or at least the agent actions aren't rendering in the streamlit st_chat. Does anybody have any clue why this could be, I can see the execution chain in the terminal, so no idea why not in streamlit, as the callbacks weren't changed ?

import os import streamlit as st from crewai import Crew, Process, Task, Agent from langchain_openai import ChatOpenAI from langchain_core.callbacks import BaseCallbackHandler from langchain.globals import set_verbose, get_verbose

set_verbose(True) # or False to control verbosity

Project directory setup

def get_project_directory(): session_id = st.session_state.get("sessionid", "001") return os.path.join(os.getcwd(), f"project{session_id}")

project_dir = get_project_directory() os.makedirs(project_dir, exist_ok=True)

Initialize the LLM with the specified model

manager_llm = ChatOpenAI( temperature=0, # Adjust temperature as needed model="deepseek-chat" # Replace with the desired model )

Initialize session state messages

if "messages" not in st.session_state: st.session_state["messages"] = [{"role": "assistant", "content": "Welcome! Please provide your software specifications to get started."}]

Custom callback handler to manage agent interactions

class MyCustomHandler(BaseCallbackHandler): def init(self, agent_name: str, directory: str) -> None: self.agent_name = agent_name self.directory = directory

def log_message(self, role, message):
    if "messages" not in st.session_state:
        st.session_state["messages"] = []
    st.session_state["messages"].append({"role": role, "content": message})
    st.chat_message(role).write(message)

def on_chain_start(self, serialized, inputs, **kwargs):
    message = f"{self.agent_name} is starting action with input: {inputs}"
    self.log_message("assistant", message)

def on_chain_end(self, outputs, **kwargs):
    message = f"{self.agent_name} completed action with output: {outputs}"
    self.log_message(self.agent_name, message)

    file_path = os.path.join(self.directory, f"{self.agent_name}.txt")
    with open(file_path, 'w') as file:
        file.write(str(outputs))

def on_tool_start(self, serialized, input_str, **kwargs):
    message = f"{self.agent_name} is starting a tool with input: {input_str}"
    self.log_message("assistant", message)

def on_tool_end(self, output, **kwargs):
    message = f"{self.agent_name} completed a tool with output: {output}"
    self.log_message(self.agent_name, message)

Utility function to create agents

def create_agent(role, backstory, goal, agent_name, tools=[]): return Agent( role=role, backstory=backstory, goal=goal, llm=manager_llm, callbacks=[MyCustomHandler(agent_name, project_dir)], tools=tools )

Define individual agents

software_consultant = create_agent( 'Software Consultant', 'A Software Consultant known for ensuring projects meet client requirements to the letter.', 'Provide clear recommendations for screens, features, and user stories.', "Software Consultant" )

project_manager = create_agent( 'Project Manager', 'A world-leading project manager orchestrating the team to meet goals effectively.', 'Outline specific, detailed specifications for the developer, based on the client brief.', "Project Manager" )

developer = create_agent( 'Developer', 'A world-leading developer known for delivering impeccable and scalable code.', 'Develop a complete software module adhering strictly to the user requirements.', "Developer" )

code_reviewer = create_agent( 'Code Reviewer', 'A world-class code reviewer renowned for identifying and implementing best practices.', 'Review the developer\'s code to ensure optimal functionality and usability.', "Code Reviewer" )

debugger = create_agent( 'Debugger', 'A meticulous debugger who is world-leading in finding and solving software issues.', 'Execute the software to find and document bugs.', "Debugger" )

Define tasks with prioritization and dependencies

tasks = [ Task( description=( "Software Consultant: Provide a detailed analysis of client requirements." "Offer clear recommendations for screens, features, and user stories." ), agent=software_consultant, expected_output="A list of detailed recommendations including user stories, screens, and features.", priority=1 # Highest priority ), Task( description="Project Manager: Convert the Software Consultant's recommendations into technical specifications.", agent=project_manager, expected_output="Technical specifications detailing file structure, coding standards, and a step-by-step guide.", depends_on=[0] # Dependent on task 0 (consultant) ), Task( description="Developer: Implement the software following the project manager's technical specifications.", agent=developer, expected_output="Complete Python files with requirements.txt and README.md.", depends_on=[1] # Dependent on task 1 (project manager) ), Task( description="Code Reviewer: Review the developer's code to ensure optimal functionality and usability.", agent=code_reviewer, expected_output="Comprehensive review report identifying issues and confirming compliance.", depends_on=[2] # Dependent on task 2 (developer) ), Task( description="Debugger: Identify and document bugs in the software with steps to reproduce.", agent=debugger, expected_output="A bug report with reproduction steps and recommendations for fixing.", depends_on=[2] # Dependent on task 2 (developer) ) ]

Create the development crew with dynamic task execution

development_crew = Crew( tasks=tasks, agents=[software_consultant, project_manager, developer, code_reviewer, debugger], manager_llm=manager_llm, process=Process.hierarchical, # Ensures task dependency handling memory=True )

Define the Streamlit app

st.title("💻 Virtual Software Development Agency")

Display all previous messages in the Streamlit chat

for msg in st.session_state["messages"]: st.chat_message(msg["role"]).write(msg["content"])

Capture user input for software specification

if software_spec := st.chat_input("Provide your software requirements:"): st.session_state["messages"].append({"role": "user", "content": software_spec}) st.chat_message("user").write(software_spec)

# Execute the crew tasks
final_results = development_crew.kickoff(inputs={"software_spec": software_spec})
result = f"## Here is the Final Result\n\n{final_results}"
st.session_state["messages"].append({"role": "assistant", "content": result})
st.chat_message("assistant").write(result)
slavakurilyak commented 4 months ago

You need to use Huggingface not OpenAI endpoints, example:

from langchain_community.llms import HuggingFaceEndpoint

or

from langchain import HuggingFaceHub
github-actions[bot] commented 3 weeks ago

This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] commented 2 weeks ago

This issue was closed because it has been stalled for 5 days with no activity.