Spaces:
Runtime error
Runtime error
ageraustine
commited on
Commit
•
1841e7e
1
Parent(s):
e93179f
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,12 @@
|
|
|
|
|
|
|
|
1 |
from langchain_core.messages import SystemMessage
|
2 |
from langchain_openai import ChatOpenAI
|
3 |
from langchain_core.pydantic_v1 import BaseModel, Field
|
4 |
from langgraph.graph import MessageGraph, END
|
5 |
from langgraph.checkpoint.sqlite import SqliteSaver
|
6 |
from langchain_core.messages import HumanMessage
|
7 |
-
import streamlit as st
|
8 |
from typing import List
|
9 |
import os
|
10 |
import uuid
|
@@ -22,6 +24,11 @@ If you're asking anything please be friendly and comment on any of the info you
|
|
22 |
|
23 |
After you are able to discerne all the information, call the relevant tool"""
|
24 |
|
|
|
|
|
|
|
|
|
|
|
25 |
llm = ChatOpenAI(temperature=0)
|
26 |
|
27 |
def get_messages_info(messages):
|
@@ -88,45 +95,54 @@ graph = workflow.compile(checkpointer=memory)
|
|
88 |
|
89 |
config = {"configurable": {"thread_id": str(uuid.uuid4())}}
|
90 |
|
91 |
-
# Helper function to execute the LangChain logic
|
92 |
-
def execute_langchain(user_input):
|
93 |
-
output_list = []
|
94 |
-
for output in graph.stream([HumanMessage(content=user_input)], config=config):
|
95 |
-
if "__end__" in output:
|
96 |
-
continue
|
97 |
-
for key, value in output.items():
|
98 |
-
output_list.append((key, value))
|
99 |
-
return output_list
|
100 |
-
|
101 |
# Streamlit app layout
|
102 |
st.title("LangChain Chat")
|
103 |
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
-
|
107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
-
# Display chat history
|
110 |
-
for message in st.session_state["chat_history"]:
|
111 |
-
st.chat_message(message["message"], key=message["message"])
|
112 |
-
|
113 |
-
user_input = st.text_input("Enter your Response")
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
if user_input:
|
120 |
-
# Execute LangChain logic
|
121 |
-
outputs = execute_langchain(user_input)
|
122 |
-
# Update chat history
|
123 |
-
st.session_state["chat_history"].append({"message": user_input, "role": "user"})
|
124 |
-
st.session_state["chat_history"].append({"message": outputs, "role": "bot"})
|
125 |
|
|
|
|
|
|
|
|
|
126 |
|
|
|
|
|
127 |
|
128 |
-
|
129 |
-
if
|
130 |
-
|
131 |
-
|
132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import streamlit as st
|
3 |
+
from streamlit_chat import message
|
4 |
from langchain_core.messages import SystemMessage
|
5 |
from langchain_openai import ChatOpenAI
|
6 |
from langchain_core.pydantic_v1 import BaseModel, Field
|
7 |
from langgraph.graph import MessageGraph, END
|
8 |
from langgraph.checkpoint.sqlite import SqliteSaver
|
9 |
from langchain_core.messages import HumanMessage
|
|
|
10 |
from typing import List
|
11 |
import os
|
12 |
import uuid
|
|
|
24 |
|
25 |
After you are able to discerne all the information, call the relevant tool"""
|
26 |
|
27 |
+
|
28 |
+
OPENAI_API_KEY='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'
|
29 |
+
os.environ['OPENAI_API_KEY'] = OPENAI_API_KEY
|
30 |
+
|
31 |
+
|
32 |
llm = ChatOpenAI(temperature=0)
|
33 |
|
34 |
def get_messages_info(messages):
|
|
|
95 |
|
96 |
config = {"configurable": {"thread_id": str(uuid.uuid4())}}
|
97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
# Streamlit app layout
|
99 |
st.title("LangChain Chat")
|
100 |
|
101 |
+
# Initialise session state variables
|
102 |
+
if 'generated' not in st.session_state:
|
103 |
+
st.session_state['generated'] = []
|
104 |
+
if 'past' not in st.session_state:
|
105 |
+
st.session_state['past'] = []
|
106 |
+
if 'messages' not in st.session_state:
|
107 |
+
st.session_state['messages'] = [
|
108 |
+
{"role": "system", "content": "You are a helpful assistant."}
|
109 |
+
]
|
110 |
+
|
111 |
|
112 |
+
# reset everything
|
113 |
+
if clear_button:
|
114 |
+
st.session_state['generated'] = []
|
115 |
+
st.session_state['past'] = []
|
116 |
+
st.session_state['messages'] = [
|
117 |
+
{"role": "system", "content": "You are a helpful assistant."}
|
118 |
+
]
|
119 |
+
|
120 |
|
|
|
|
|
|
|
|
|
|
|
121 |
|
122 |
+
# container for chat history
|
123 |
+
response_container = st.container()
|
124 |
+
# container for text box
|
125 |
+
container = st.container()
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
+
with container:
|
128 |
+
with st.form(key='my_form', clear_on_submit=True):
|
129 |
+
user_input = st.text_area("You:", key='input', height=100)
|
130 |
+
submit_button = st.form_submit_button(label='Send')
|
131 |
|
132 |
+
if submit_button and user_input:
|
133 |
+
st.session_state['messages'].append({"role": "user", "content": user_input})
|
134 |
|
135 |
+
for output in graph.stream([HumanMessage(content=st.session_state['messages'])], config=config):
|
136 |
+
if "__end__" in output:
|
137 |
+
continue
|
138 |
+
# stream() yields dictionaries with output keyed by node name
|
139 |
+
for key, value in output.items():
|
140 |
+
st.session_state['messages'].append({"role": "assistant", "content": value})
|
141 |
+
st.session_state['past'].append(user_input)
|
142 |
+
st.session_state['generated'].append(value)
|
143 |
+
|
144 |
+
if st.session_state['generated']:
|
145 |
+
with response_container:
|
146 |
+
for i in range(len(st.session_state['generated'])):
|
147 |
+
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user')
|
148 |
+
message(st.session_state["generated"][i], key=str(i))
|