Skip to content

Instantly share code, notes, and snippets.

@ashhadulislam
Created January 10, 2024 17:50
Show Gist options
  • Save ashhadulislam/e44660967554b731a8056168dbad5914 to your computer and use it in GitHub Desktop.
Save ashhadulislam/e44660967554b731a8056168dbad5914 to your computer and use it in GitHub Desktop.
Streamlit chatbot connected to MemGPT (OpenAI Back end)
import streamlit as st
import requests
base_url = "http://localhost:8283/"
headers = {"accept": "application/json"}
st.title("MemGPT Connected Bot")
# check if memgpt server and agents are available
agent_name="agent_1"
user_name=st.text_input("Enter your name", value="Abd")
agent_details_URL=base_url+f"agents?user_id={user_name}"
response = requests.get(agent_details_URL, headers=headers)
response=response.json()
print(response)
if "agent_names" not in response:
st.warning('No MemGPT server running', icon="⚠️")
else:
agent_names=response["agent_names"]
print(agent_names)
if len(agent_names)==0:
st.warning('No agents available. Make sure you set up the agents.', icon="⚠️")
else:
agent_name = st.selectbox("Choose agent", agent_names, index=0)
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# React to user input
if prompt := st.chat_input("What is up?"):
# Display user message in chat message container
st.chat_message("user").markdown(prompt)
# Add user message to chat history
print(f"User said {prompt}")
st.session_state.messages.append({"role": "user", "content": prompt})
# need to make a megpt call to get the response
send_message_URL=base_url+"agents/message"
payload = {
"user_id": f"{user_name}",
"agent_id": f"{agent_name}",
"message": f"{prompt}",
"stream": False,
"role": "user"
}
headers = {
"accept": "application/json",
"content-type": "application/json"
}
response = requests.post(send_message_URL, json=payload, headers=headers)
response=response.json()
if "messages" not in response:
st.warning('Incorrect response from MemGPT', icon="⚠️")
else:
list_responses=response["messages"]
if len(list_responses)<4:
st.warning('Incorrect number of responses from MemGPT', icon="⚠️")
else:
assistant_message=list_responses[2]
if "assistant_message" not in assistant_message:
st.warning('Missing assistant_message', icon="⚠️")
else:
assistant_message=assistant_message["assistant_message"]
response = f"{agent_name}: {assistant_message}"
# Display assistant response in chat message container
with st.chat_message("assistant"):
st.markdown(response)
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": response})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment