-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstreamlit_ui.py
68 lines (56 loc) · 2.51 KB
/
streamlit_ui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# NOTES:
# This is simple single agent using a mock Asana API to manage tasks and projects by means of Tools.
# This is a streamlit version of the console.py.
# To run this in streamlit
# - navigate to the directory where this file is located
# - execute the following: streamlit run streamlit_ui.py
import os
import sys
import asyncio
import streamlit as st
from datetime import datetime
from dotenv import load_dotenv
from pydantic_ai import Agent
from pydantic_ai.messages import TextPart
from asana_tools import AsanaTools
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
from _utils.message_history import MessageHistory
from _utils.utils import Utils
load_dotenv()
tools = AsanaTools()
agent = Agent(
model="openai:gpt-4o-mini",
tools=tools.get_tools(),
system_prompt=(
"You are a personal assistant to help manage project tasks. "
"Anytime the user requests a list of projects or tasks, always retrieve it using tools. "
f"The current date is: {datetime.now().date()}"
)
)
async def main_async():
st.title("Project/Task Manager")
if "messages" not in st.session_state:
# load for a database
st.session_state.messages = MessageHistory()
# display all user and ai messages
message_history = st.session_state.messages.get_all_messages()
for message in message_history:
for part in message.parts:
if part.part_kind in ["user-prompt", "text"]:
with st.chat_message("human" if part.part_kind == "user-prompt" else "ai"):
st.markdown(part.content)
if prompt := st.chat_input("What would you like research today?"):
st.chat_message("user").markdown(prompt)
# stream the ai response
response_content = ""
with st.chat_message("assistant"):
message_placeholder = st.empty()
async with agent.run_stream(prompt, message_history=message_history) as result:
async for chunk in Utils.stream_result_async(result):
response_content += chunk
message_placeholder.markdown(response_content)
# update the latest history
st.session_state.messages.assign(result.all_messages())
st.session_state.messages.append(TextPart(content=response_content))
if __name__ == "__main__":
asyncio.run(main_async())