1
+ import asyncio
1
2
import os
3
+ import tempfile
4
+ from functools import lru_cache
2
5
3
6
os .chdir (os .path .dirname (os .path .abspath (__file__ )))
4
7
os .environ ['SERPAPI_API_KEY' ] = ''
5
8
6
- import tempfile
7
-
8
9
import langchain
9
10
import streamlit as st
10
11
from langchain .cache import InMemoryCache
12
+ from streamlit_chat import message
11
13
12
14
from agent import AgentHelper
13
15
from docGPT import DocGPT
@@ -102,13 +104,14 @@ def load_api_key() -> None:
102
104
chain_type = 'refine' ,
103
105
)
104
106
docGPT_spec_tool = agent_ .create_doc_chat (docGPT_spec )
105
- except Exception :
106
- st .error ('#### ⚠️ :red[You have not pass OpenAPI key. (Or your api key cannot use.)]' )
107
-
107
+ except Exception as e :
108
+ print (e )
109
+ pass
110
+
108
111
try :
109
112
search_tool = agent_ .get_searp_chain
110
113
except Exception as e :
111
- st .warning ('⚠️ You have not pass SEARPAPI key. (Or your api key cannot use.) Try Refresh ' )
114
+ st .warning ('⚠️ You have not pass SEARPAPI key. (Or your api key cannot use.)' )
112
115
113
116
try :
114
117
calculate_tool = agent_ .get_calculate_chain
@@ -118,18 +121,45 @@ def load_api_key() -> None:
118
121
calculate_tool , search_tool
119
122
]
120
123
agent_ .initialize (tools )
121
- except Exception :
122
- pass
124
+ except Exception as e :
125
+ print (e )
126
+
127
+
128
+ if not st .session_state ['openai_api_key' ]:
129
+ st .error ('⚠️ :red[You have not pass OpenAPI key. (Or your api key cannot use.)] Necessary' )
123
130
124
131
st .write ('---' )
125
132
126
- with st .container ():
127
- query = st .text_input ('#### Question:' )
128
- response = None
133
+ if 'response' not in st .session_state :
134
+ st .session_state ['response' ] = ['How can I help you?' ]
135
+
136
+ if 'query' not in st .session_state :
137
+ st .session_state ['query' ] = ['Hi' ]
138
+
129
139
140
+ @lru_cache (maxsize = 20 )
141
+ async def get_response (query : str ):
130
142
if agent_ and query and query != '' :
131
- response = 'loading...'
132
- response = agent_ .query (query )
143
+ response = agent_ .query (query )
144
+ return response
145
+
146
+
147
+ query = st .text_input (
148
+ "#### Question:" ,
149
+ placeholder = 'Enter your question'
150
+ )
151
+
152
+ response_container = st .container ()
153
+ user_container = st .container ()
154
+
155
+ with user_container :
156
+ if query :
157
+ response = asyncio .run (get_response (query ))
158
+ st .session_state .query .append (query )
159
+ st .session_state .response .append (response )
133
160
134
- st .write ('### :blue[Response]:' )
135
- st .write (response )
161
+ with response_container :
162
+ if st .session_state ['response' ]:
163
+ for i in range (len (st .session_state ['response' ])- 1 , - 1 , - 1 ):
164
+ message (st .session_state ["response" ][i ], key = str (i ))
165
+ message (st .session_state ['query' ][i ], is_user = True , key = str (i ) + '_user' )
0 commit comments