xke
commited on
Commit
·
375dd80
1
Parent(s):
a1be205
update ui
Browse files
app.py
CHANGED
|
@@ -1,15 +1,18 @@
|
|
| 1 |
-
import os
|
| 2 |
import streamlit as st
|
| 3 |
from crewai import Crew, Process, Agent, Task
|
| 4 |
from langchain_core.callbacks import BaseCallbackHandler
|
| 5 |
from langchain_openai import ChatOpenAI
|
| 6 |
from typing import Any, Dict
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
# documentation
|
| 9 |
# https://www.linkedin.com/pulse/building-simple-user-interfaces-crewai-streamlit-robyn-le-sueur-azvdf/
|
| 10 |
|
| 11 |
# Initialize the OpenAI model for use with agents
|
| 12 |
-
openai = ChatOpenAI(model="gpt-3.5-turbo", temperature=0.
|
| 13 |
|
| 14 |
class CustomHandler(BaseCallbackHandler):
|
| 15 |
"""A custom handler for logging interactions within the process chain."""
|
|
@@ -20,8 +23,9 @@ class CustomHandler(BaseCallbackHandler):
|
|
| 20 |
|
| 21 |
def on_chain_start(self, serialized: Dict[str, Any], outputs: Dict[str, Any], **kwargs: Any) -> None:
|
| 22 |
"""Log the start of a chain with user input."""
|
| 23 |
-
|
| 24 |
-
st.
|
|
|
|
| 25 |
|
| 26 |
def on_agent_action(self, serialized: Dict[str, Any], inputs: Dict[str, Any], **kwargs: Any) -> None:
|
| 27 |
"""""Log the action taken by an agent during a chain run."""
|
|
@@ -30,8 +34,9 @@ class CustomHandler(BaseCallbackHandler):
|
|
| 30 |
|
| 31 |
def on_chain_end(self, outputs: Dict[str, Any], **kwargs: Any) -> None:
|
| 32 |
"""Log the end of a chain with the output generated by an agent."""
|
| 33 |
-
|
| 34 |
-
st.
|
|
|
|
| 35 |
|
| 36 |
# Define agents with their specific roles and goals
|
| 37 |
project_manager = Agent(
|
|
@@ -52,7 +57,7 @@ coder = Agent(
|
|
| 52 |
You produce functional, feature complete code.''',
|
| 53 |
goal='Develop high-quality, well-structured Python code.',
|
| 54 |
llm=openai,
|
| 55 |
-
callbacks=[CustomHandler("Coder")],
|
| 56 |
)
|
| 57 |
|
| 58 |
# Streamlit UI setup
|
|
@@ -97,6 +102,6 @@ if prompt := st.chat_input():
|
|
| 97 |
final = project_crew.kickoff()
|
| 98 |
|
| 99 |
# Display the final result
|
| 100 |
-
result = f"
|
| 101 |
st.session_state.messages.append({"role": "assistant", "content": result})
|
| 102 |
st.chat_message("assistant").write(result)
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from crewai import Crew, Process, Agent, Task
|
| 3 |
from langchain_core.callbacks import BaseCallbackHandler
|
| 4 |
from langchain_openai import ChatOpenAI
|
| 5 |
from typing import Any, Dict
|
| 6 |
|
| 7 |
+
## TODO
|
| 8 |
+
## use an example, and clarify which agent is speaking in the UI
|
| 9 |
+
|
| 10 |
+
|
| 11 |
# documentation
|
| 12 |
# https://www.linkedin.com/pulse/building-simple-user-interfaces-crewai-streamlit-robyn-le-sueur-azvdf/
|
| 13 |
|
| 14 |
# Initialize the OpenAI model for use with agents
|
| 15 |
+
openai = ChatOpenAI(model="gpt-3.5-turbo", temperature=0.1)
|
| 16 |
|
| 17 |
class CustomHandler(BaseCallbackHandler):
|
| 18 |
"""A custom handler for logging interactions within the process chain."""
|
|
|
|
| 23 |
|
| 24 |
def on_chain_start(self, serialized: Dict[str, Any], outputs: Dict[str, Any], **kwargs: Any) -> None:
|
| 25 |
"""Log the start of a chain with user input."""
|
| 26 |
+
# comment out verbose chain input
|
| 27 |
+
#st.session_state.messages.append({"role": "assistant", "content": outputs['input']})
|
| 28 |
+
#st.chat_message("assistant").write(outputs['input'])
|
| 29 |
|
| 30 |
def on_agent_action(self, serialized: Dict[str, Any], inputs: Dict[str, Any], **kwargs: Any) -> None:
|
| 31 |
"""""Log the action taken by an agent during a chain run."""
|
|
|
|
| 34 |
|
| 35 |
def on_chain_end(self, outputs: Dict[str, Any], **kwargs: Any) -> None:
|
| 36 |
"""Log the end of a chain with the output generated by an agent."""
|
| 37 |
+
message = f"**{self.agent_name}:**\n\n{outputs['output']}"
|
| 38 |
+
st.session_state.messages.append({"role": self.agent_name, "content": message})
|
| 39 |
+
st.chat_message(self.agent_name).write(message)
|
| 40 |
|
| 41 |
# Define agents with their specific roles and goals
|
| 42 |
project_manager = Agent(
|
|
|
|
| 57 |
You produce functional, feature complete code.''',
|
| 58 |
goal='Develop high-quality, well-structured Python code.',
|
| 59 |
llm=openai,
|
| 60 |
+
callbacks=[CustomHandler("Python Coder")],
|
| 61 |
)
|
| 62 |
|
| 63 |
# Streamlit UI setup
|
|
|
|
| 102 |
final = project_crew.kickoff()
|
| 103 |
|
| 104 |
# Display the final result
|
| 105 |
+
result = f"### Crew Manager's Final Result: \n\n {final}"
|
| 106 |
st.session_state.messages.append({"role": "assistant", "content": result})
|
| 107 |
st.chat_message("assistant").write(result)
|