joaomdmoura / 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
17k stars 2.3k forks source link

Streamlit Integration #275

Open slavakurilyak opened 4 months ago

slavakurilyak commented 4 months ago

It would be great to see Streamlit integration with crewAI so these crews can escape from their terminals, pun intended

Related issue here

slavakurilyak commented 4 months ago

Here's an example implementation by @amadad:

https://github.com/amadad/civic-agentcy

speaks999 commented 4 months ago

The example above shows the final output but not the log messages. Need to be able to have the logs write to st.write_stream somehow.

slavakurilyak commented 4 months ago

@tonykipkemboi do you want tackle this one?

Your trip_planner_agent would benefit from this

speaks999 commented 4 months ago

Using step_callback=st.write in the agent startup will get you started but there must be a more elegant way.

slavakurilyak commented 4 months ago

Verbose mode is also useful since response can be streamed in real-time and it does not require a function to be called after each step of the agent (aka Step CallBack)

speaks999 commented 4 months ago

A complete Streamlit example, with streaming chat showing all agent responses, complete with agent icons, would probably increase usage of CrewAI by 10-30% or more. Can someone just create that? Thanks Modifying the trip planner agent would be a great example. Allowing people to interact with human-in-the-loop agents is also necessary.

slavakurilyak commented 4 months ago

Check out multi-agent-streamlit-ui by @camel-ai for inspiration

tonykipkemboi commented 4 months ago

So I played with the trip planner app last night for a few minutes trying to implement the streaming logs on the app under st.status.

The challenge lies in redirecting the logger outputs from CrewAI agents into the Streamlit app in a way that allows for near real-time updates.

I tried redirecting the logs to the Streamlit UI but that did not work so I ended up modifying the Crewai source code under the utilities/logger.py and was able to get some logs printed on the app but some others (the green log text in console) haven't got to it yet. This is not the best way to do this and maybe I just need a little brainstorming session with @joaomdmoura to get this part working.

slavakurilyak commented 4 months ago

@joaomdmoura it would be great to have your input on this

chrispangg commented 4 months ago

I was able to get some of the green log text in the console printed by modifying the agent.py file with the following:

  1. Add a callback init variable to agent.py
          callbacks: Optional[List[InstanceOf[BaseCallbackHandler]]] = Field(
              default=None, description="Callbacks to be executed"
          )
  2. In create_agent_executor() in the same Agent.py file, add the following callback to the CrewAgentExecutor
        self.agent_executor = CrewAgentExecutor(
            agent=RunnableAgent(runnable=inner_agent),
            **executor_args,
            callbacks=self.callbacks,
        )
  3. In your own code, pass in the callback handler (using the StdOutCallbackHandler as an example) when creating the agent:
        from langchain_core.callbacks import StdOutCallbackHandler
        callback_handler = StdOutCallbackHandler()
        finance_researcher = Agent(
            role="Finance Research Analyst",
            goal="Provide up-to-date finance market data analysis",
            backstory="An expert analyst with a keen eye for market trends.",
            tools=[web_search, web_scraper],
            callbacks=[callback_handler],
        )
speaks999 commented 4 months ago

This would be a great start. The next step would be to facilitate the human in the loop somehow. @joaomdmoura do you have a plan for this? Are you going to incorporate the change above? Thx

chrispangg commented 4 months ago

PR for the suggested change here

tonykipkemboi commented 3 months ago

Thanks for getting this out @chrispangg. I barely got time to keep looking into this.

chrispangg commented 3 months ago

PR merged - also added an example to the PR for printing steps in Streamlit https://github.com/joaomdmoura/crewAI/pull/333

slavakurilyak commented 3 months ago

@chrispangg really appreciate you implementing this

@joaomdmoura It would be great to see a how-to guide for this streamlit integration

Pusse-01 commented 2 months ago

I want to take human inputs when executing the agents. According to the documentation we can take it from the terminal by enabling hunan_input from the task. But any methods to get it from the streamlit app and give it to the agent? Any method to get user feedbacks while executing the agents?