Spaces:
Running
Running
Commit
·
a0f26fc
1
Parent(s):
c448082
Added streaming
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
-
|
4 |
|
5 |
st.set_page_config(page_title="ASI1-mini Chatbot", layout="centered")
|
6 |
st.title("ASI1-mini LLM Chat")
|
@@ -38,7 +38,7 @@ for msg in st.session_state.chat_history:
|
|
38 |
user_input = st.chat_input("Ask me anything...")
|
39 |
|
40 |
|
41 |
-
def
|
42 |
url = "https://api.asi1.ai/v1/chat/completions"
|
43 |
headers = {
|
44 |
"Authorization": f"Bearer {api_key}",
|
@@ -50,14 +50,30 @@ def call_asi1_api(api_key, messages):
|
|
50 |
"temperature": 0.7,
|
51 |
"fun_mode": False,
|
52 |
"web_search": False,
|
53 |
-
"stream":
|
54 |
"max_tokens": 1024,
|
55 |
}
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
|
63 |
# When user submits a message
|
@@ -72,11 +88,17 @@ if user_input:
|
|
72 |
|
73 |
with st.chat_message("assistant"):
|
74 |
with st.spinner("Thinking..."):
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
+
import json
|
4 |
|
5 |
st.set_page_config(page_title="ASI1-mini Chatbot", layout="centered")
|
6 |
st.title("ASI1-mini LLM Chat")
|
|
|
38 |
user_input = st.chat_input("Ask me anything...")
|
39 |
|
40 |
|
41 |
+
def stream_asi1_api(api_key, messages):
|
42 |
url = "https://api.asi1.ai/v1/chat/completions"
|
43 |
headers = {
|
44 |
"Authorization": f"Bearer {api_key}",
|
|
|
50 |
"temperature": 0.7,
|
51 |
"fun_mode": False,
|
52 |
"web_search": False,
|
53 |
+
"stream": True,
|
54 |
"max_tokens": 1024,
|
55 |
}
|
56 |
|
57 |
+
with requests.post(url, headers=headers, json=payload, stream=True) as response:
|
58 |
+
response.raise_for_status()
|
59 |
+
for line in response.iter_lines():
|
60 |
+
if line:
|
61 |
+
decoded_line = line.decode("utf-8").strip()
|
62 |
+
if decoded_line.startswith("data: "):
|
63 |
+
data = decoded_line[len("data: ") :]
|
64 |
+
if data == "[DONE]":
|
65 |
+
break
|
66 |
+
try:
|
67 |
+
parsed = json.loads(data)
|
68 |
+
content_piece = (
|
69 |
+
parsed.get("choices", [{}])[0]
|
70 |
+
.get("delta", {})
|
71 |
+
.get("content", "")
|
72 |
+
)
|
73 |
+
if content_piece:
|
74 |
+
yield content_piece
|
75 |
+
except json.JSONDecodeError:
|
76 |
+
continue
|
77 |
|
78 |
|
79 |
# When user submits a message
|
|
|
88 |
|
89 |
with st.chat_message("assistant"):
|
90 |
with st.spinner("Thinking..."):
|
91 |
+
response_stream = stream_asi1_api(
|
92 |
+
asi_api_key, st.session_state.chat_history
|
93 |
+
)
|
94 |
+
placeholder = st.empty()
|
95 |
+
partial_text = ""
|
96 |
+
|
97 |
+
for chunk in response_stream:
|
98 |
+
partial_text += chunk
|
99 |
+
placeholder.markdown(partial_text + "▌")
|
100 |
+
|
101 |
+
placeholder.markdown(partial_text)
|
102 |
+
st.session_state.chat_history.append(
|
103 |
+
{"role": "assistant", "content": partial_text}
|
104 |
+
)
|