Spaces:
Sleeping
Sleeping
Commit
·
020020a
1
Parent(s):
dc0f057
add download button
Browse files- src/download.py +46 -0
- src/streamlit_app.py +19 -6
src/download.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.messages import AIMessage, HumanMessage, ToolMessage
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
def format_conversation_for_download(conversation: list) -> str:
|
| 5 |
+
"""Formats the conversation history for download."""
|
| 6 |
+
formatted_messages = []
|
| 7 |
+
tool_id_to_name_map = {}
|
| 8 |
+
for message in conversation:
|
| 9 |
+
if isinstance(message, AIMessage) and message.tool_calls:
|
| 10 |
+
for tool_call in message.tool_calls:
|
| 11 |
+
tool_id_to_name_map[tool_call["id"]] = tool_call["name"]
|
| 12 |
+
|
| 13 |
+
for message in conversation:
|
| 14 |
+
if isinstance(message, HumanMessage):
|
| 15 |
+
formatted_messages.append({"role": "user", "content": message.content})
|
| 16 |
+
elif isinstance(message, AIMessage):
|
| 17 |
+
if not message.content and not message.tool_calls:
|
| 18 |
+
continue
|
| 19 |
+
|
| 20 |
+
ai_dict = {"role": "assistant"}
|
| 21 |
+
if message.content:
|
| 22 |
+
ai_dict["content"] = message.content
|
| 23 |
+
|
| 24 |
+
if message.tool_calls:
|
| 25 |
+
ai_dict["tool_calls"] = [
|
| 26 |
+
{
|
| 27 |
+
"type": "function",
|
| 28 |
+
"function": {
|
| 29 |
+
"name": tc["name"],
|
| 30 |
+
"arguments": tc["args"],
|
| 31 |
+
},
|
| 32 |
+
}
|
| 33 |
+
for tc in message.tool_calls
|
| 34 |
+
]
|
| 35 |
+
formatted_messages.append(ai_dict)
|
| 36 |
+
elif isinstance(message, ToolMessage):
|
| 37 |
+
tool_name = tool_id_to_name_map.get(message.tool_call_id)
|
| 38 |
+
formatted_messages.append(
|
| 39 |
+
{
|
| 40 |
+
"role": "tool",
|
| 41 |
+
"name": tool_name,
|
| 42 |
+
"content": message.content,
|
| 43 |
+
}
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
return json.dumps(formatted_messages, indent=4)
|
src/streamlit_app.py
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from receptionist_agent import get_agent_response
|
| 3 |
from langchain_core.messages import AIMessage, HumanMessage, ToolMessage, messages_to_dict
|
| 4 |
import json
|
| 5 |
from datetime import datetime
|
| 6 |
import time
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
st.set_page_config(layout="wide")
|
| 12 |
|
| 13 |
if "conversation" not in st.session_state:
|
| 14 |
-
st.session_state.conversation = [
|
| 15 |
-
AIMessage(content="Thanks for calling, this is Receptionist Agent! Can I help you schedule a service or would you like help with something else?")
|
| 16 |
-
]
|
| 17 |
start_time = datetime.now().strftime("%y%m%d-%H%M%S")
|
| 18 |
st.session_state.conversation_file = f"conversations/conversation_{start_time}.json"
|
| 19 |
|
|
@@ -31,7 +34,7 @@ def submit_message():
|
|
| 31 |
st.session_state.response_time = f"{end - start:.2f}"
|
| 32 |
|
| 33 |
st.session_state.conversation.extend(new_messages)
|
| 34 |
-
|
| 35 |
|
| 36 |
st.subheader("Receptionist Agent")
|
| 37 |
|
|
@@ -50,6 +53,16 @@ for message in st.session_state.conversation:
|
|
| 50 |
with st.chat_message("tool"):
|
| 51 |
st.write(f"Tool `{message.tool_call_id}` output: `{message.content}`")
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
if "response_time" in st.session_state:
|
| 54 |
st.caption(f"Response time: {st.session_state.response_time}s")
|
| 55 |
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
import streamlit as st
|
| 3 |
from receptionist_agent import get_agent_response
|
| 4 |
from langchain_core.messages import AIMessage, HumanMessage, ToolMessage, messages_to_dict
|
| 5 |
import json
|
| 6 |
from datetime import datetime
|
| 7 |
import time
|
| 8 |
+
from download import format_conversation_for_download
|
| 9 |
|
| 10 |
+
#import langsmith
|
| 11 |
+
|
| 12 |
+
load_dotenv()
|
| 13 |
+
|
| 14 |
+
#langsmith.debug = True
|
| 15 |
|
| 16 |
st.set_page_config(layout="wide")
|
| 17 |
|
| 18 |
if "conversation" not in st.session_state:
|
| 19 |
+
st.session_state.conversation = [AIMessage(content="Thanks for calling, this is Receptionist Agent! Can I help you schedule a service or would you like help with something else?")]
|
|
|
|
|
|
|
| 20 |
start_time = datetime.now().strftime("%y%m%d-%H%M%S")
|
| 21 |
st.session_state.conversation_file = f"conversations/conversation_{start_time}.json"
|
| 22 |
|
|
|
|
| 34 |
st.session_state.response_time = f"{end - start:.2f}"
|
| 35 |
|
| 36 |
st.session_state.conversation.extend(new_messages)
|
| 37 |
+
save_conversation()
|
| 38 |
|
| 39 |
st.subheader("Receptionist Agent")
|
| 40 |
|
|
|
|
| 53 |
with st.chat_message("tool"):
|
| 54 |
st.write(f"Tool `{message.tool_call_id}` output: `{message.content}`")
|
| 55 |
|
| 56 |
+
if len(st.session_state.conversation) > 1:
|
| 57 |
+
_, col2 = st.columns([0.75, 0.25])
|
| 58 |
+
with col2:
|
| 59 |
+
st.download_button(
|
| 60 |
+
label="Download Conversation",
|
| 61 |
+
data=format_conversation_for_download(st.session_state.conversation),
|
| 62 |
+
file_name=st.session_state.conversation_file.replace(".json", "_formatted.json"),
|
| 63 |
+
mime="application/json",
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
if "response_time" in st.session_state:
|
| 67 |
st.caption(f"Response time: {st.session_state.response_time}s")
|
| 68 |
|