feat: Add AgentClient with timeout 60.
This commit is contained in:
18
ui/src/ui/agent_client.py
Normal file
18
ui/src/ui/agent_client.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import httpx
|
||||||
|
from logger import logger
|
||||||
|
|
||||||
|
|
||||||
|
class AgentClient:
|
||||||
|
def __init__(self):
|
||||||
|
self._client = httpx.AsyncClient()
|
||||||
|
|
||||||
|
async def send_message(self, message):
|
||||||
|
logger.info(f"Sending message to agent: {message}")
|
||||||
|
|
||||||
|
response = await self._client.post(
|
||||||
|
"http://localhost:8010/chat",
|
||||||
|
json={"messages": message},
|
||||||
|
timeout=60
|
||||||
|
)
|
||||||
|
|
||||||
|
return response
|
||||||
@@ -1,22 +1,20 @@
|
|||||||
import streamlit as st
|
import streamlit as st
|
||||||
|
from agent_client import AgentClient
|
||||||
|
from logger import logger
|
||||||
import random
|
import random
|
||||||
import time
|
import time
|
||||||
|
|
||||||
def response_generator():
|
|
||||||
response = random.choice(
|
|
||||||
[
|
|
||||||
"Hello there! How can I assist you today?",
|
|
||||||
"Hi, human! Is there anything I can help you with?",
|
|
||||||
"Do you need help?",
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
for word in response.split():
|
def response_generator(response: str = ""):
|
||||||
yield word + " "
|
logger.info(f"Generating response stream for: {response}")
|
||||||
time.sleep(0.05)
|
|
||||||
|
|
||||||
def main():
|
if response:
|
||||||
st.image("./assets/logo-naliia.png", width=100, caption="NaliiaBot")
|
for word in response.split():
|
||||||
|
yield word + " "
|
||||||
|
time.sleep(0.05)
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
st.image("src/ui/assets/logo-naliia.png", width=100, caption="NaliiaBot")
|
||||||
|
|
||||||
if "messages" not in st.session_state:
|
if "messages" not in st.session_state:
|
||||||
st.session_state.messages = []
|
st.session_state.messages = []
|
||||||
@@ -30,11 +28,13 @@ def main():
|
|||||||
st.chat_message("user").markdown(prompt)
|
st.chat_message("user").markdown(prompt)
|
||||||
st.session_state.messages.append({"role": "user", "content": prompt})
|
st.session_state.messages.append({"role": "user", "content": prompt})
|
||||||
|
|
||||||
response = f"Echo: {prompt}"
|
agent_client = AgentClient()
|
||||||
|
response = await agent_client.send_message(prompt)
|
||||||
|
|
||||||
|
formated_response = response.json()['response']['messages'][-1]['content']
|
||||||
|
|
||||||
with st.chat_message("assistant"):
|
with st.chat_message("assistant"):
|
||||||
response = st.write_stream(response_generator())
|
response = st.write_stream(response_generator(formated_response))
|
||||||
|
st.session_state.messages.append({"role": "assistant", "content": formated_response})
|
||||||
|
|
||||||
st.session_state.messages.append({"role": "assistant", "content": response})
|
if __name__ == "__main__": import asyncio; asyncio.run(main())
|
||||||
|
|
||||||
if __name__ == "__main__": main()
|
|
||||||
|
|||||||
Reference in New Issue
Block a user