|
import streamlit as st |
|
import time |
|
import requests |
|
import json |
|
|
|
def ask(prompt): |
|
url = 'http://localhost:8080/v1/chat/completions' |
|
myobj = { |
|
"model": "ggml-gpt4all-j.bin", |
|
"messages": [{"role": "user", "content": prompt}], |
|
"temperature": 0.9 |
|
} |
|
myheaders = { "Content-Type" : "application/json" } |
|
|
|
x = requests.post(url, json = myobj, headers=myheaders) |
|
|
|
print(x.text) |
|
|
|
json_data = json.loads(x.text) |
|
|
|
return json_data["choices"][0]["message"]["content"] |
|
|
|
|
|
def main(): |
|
|
|
st.set_page_config(page_title="Ask your LLM") |
|
st.header("Ask your Question π¬") |
|
|
|
|
|
if "messages" not in st.session_state: |
|
st.session_state.messages = [] |
|
|
|
|
|
for message in st.session_state.messages: |
|
with st.chat_message(message["role"]): |
|
st.markdown(message["content"]) |
|
|
|
|
|
st.markdown( |
|
""" |
|
<script> |
|
var element = document.getElementById("end-of-chat"); |
|
element.scrollIntoView({behavior: "smooth"}); |
|
</script> |
|
""", |
|
unsafe_allow_html=True, |
|
) |
|
|
|
|
|
if prompt := st.chat_input("What is up?"): |
|
|
|
st.chat_message("user").markdown(prompt) |
|
|
|
st.session_state.messages.append({"role": "user", "content": prompt}) |
|
print(f"User has asked the following question: {prompt}") |
|
|
|
|
|
response = "" |
|
with st.spinner('Processing...'): |
|
response = ask(prompt) |
|
|
|
|
|
|
|
with st.chat_message("assistant"): |
|
st.markdown(response) |
|
|
|
st.session_state.messages.append({"role": "assistant", "content": response}) |
|
|
|
if __name__ == "__main__": |
|
main() |