xke commited on
Commit ·
a1be205
1
Parent(s): 259e501
add requirements
Browse files- main.py +0 -102
- requirements.txt +4 -0
main.py
DELETED
|
@@ -1,102 +0,0 @@
|
|
| 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.5)
|
| 13 |
-
|
| 14 |
-
class CustomHandler(BaseCallbackHandler):
|
| 15 |
-
"""A custom handler for logging interactions within the process chain."""
|
| 16 |
-
|
| 17 |
-
def __init__(self, agent_name: str) -> None:
|
| 18 |
-
super().__init__()
|
| 19 |
-
self.agent_name = agent_name
|
| 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 |
-
st.session_state.messages.append({"role": "assistant", "content": outputs['input']})
|
| 24 |
-
st.chat_message("assistant").write(outputs['input'])
|
| 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."""
|
| 28 |
-
st.session_state.messages.append({"role": "assistant", "content": inputs['input']})
|
| 29 |
-
st.chat_message("assistant").write(inputs['input'])
|
| 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 |
-
st.session_state.messages.append({"role": self.agent_name, "content": outputs['output']})
|
| 34 |
-
st.chat_message(self.agent_name).write(outputs['output'])
|
| 35 |
-
|
| 36 |
-
# Define agents with their specific roles and goals
|
| 37 |
-
project_manager = Agent(
|
| 38 |
-
role='Project Manager',
|
| 39 |
-
backstory='''You are the project manager.
|
| 40 |
-
You consider the task and break it down into smaller tasks to be performed by the team.
|
| 41 |
-
You do not write code.''',
|
| 42 |
-
goal='Generate actionable steps for task completion.',
|
| 43 |
-
llm=openai,
|
| 44 |
-
callbacks=[CustomHandler("Project Manager")],
|
| 45 |
-
)
|
| 46 |
-
|
| 47 |
-
coder = Agent(
|
| 48 |
-
role='Python Coder',
|
| 49 |
-
backstory='''You are a Senior Python Developer responsible for writing clean, efficient and robust Python code
|
| 50 |
-
that is easy to read and understand.
|
| 51 |
-
You write code using object oriented programming principles and follow best practices.
|
| 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
|
| 59 |
-
st.title("💬 CrewAI Coding Studio")
|
| 60 |
-
|
| 61 |
-
# Initialize the message log in session state if not already present
|
| 62 |
-
if "messages" not in st.session_state:
|
| 63 |
-
st.session_state["messages"] = [{"role": "assistant", "content": "What code do you want us to write?"}]
|
| 64 |
-
|
| 65 |
-
# Display existing messages
|
| 66 |
-
for msg in st.session_state.messages:
|
| 67 |
-
st.chat_message(msg["role"]).write(msg["content"])
|
| 68 |
-
|
| 69 |
-
# Handle user input
|
| 70 |
-
if prompt := st.chat_input():
|
| 71 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 72 |
-
st.chat_message("user").write(prompt)
|
| 73 |
-
|
| 74 |
-
# Define tasks for each agent
|
| 75 |
-
task_plan = Task(
|
| 76 |
-
description=f"""Consider how you would go about the task, '{prompt}'.
|
| 77 |
-
Create a plan to complete the task.
|
| 78 |
-
The final step should always require delivering feature complete code""",
|
| 79 |
-
agent=project_manager,
|
| 80 |
-
expected_output="A detailed plan for the team to complete the task.",
|
| 81 |
-
)
|
| 82 |
-
|
| 83 |
-
task_code = Task(
|
| 84 |
-
description="Write feature complete code that is simple, efficient and adheres to object oriented principles.",
|
| 85 |
-
agent=coder,
|
| 86 |
-
expected_output="Well-written and structured code that is feature complete, simple, efficient and adheres to object oriented principles.",
|
| 87 |
-
)
|
| 88 |
-
|
| 89 |
-
# Set up the crew and process tasks hierarchically
|
| 90 |
-
project_crew = Crew(
|
| 91 |
-
tasks=[task_plan, task_code],
|
| 92 |
-
agents=[project_manager, coder],
|
| 93 |
-
process=Process.hierarchical,
|
| 94 |
-
manager_llm=openai,
|
| 95 |
-
manager_callbacks=[CustomHandler("Crew Manager")]
|
| 96 |
-
)
|
| 97 |
-
final = project_crew.kickoff()
|
| 98 |
-
|
| 99 |
-
# Display the final result
|
| 100 |
-
result = f"## Here is the Final Result \n\n {final}"
|
| 101 |
-
st.session_state.messages.append({"role": "assistant", "content": result})
|
| 102 |
-
st.chat_message("assistant").write(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
crewai
|
| 3 |
+
langchain-core
|
| 4 |
+
langchain-openai
|