Spaces:
Running
Running
Commit
Β·
f891d17
1
Parent(s):
6706599
Basic chat interface
Browse files
app.py
CHANGED
@@ -1,4 +1,85 @@
|
|
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
import streamlit as st
|
3 |
+
import requests
|
4 |
+
from dotenv import load_dotenv
|
5 |
|
6 |
+
load_dotenv()
|
7 |
+
|
8 |
+
st.set_page_config(page_title="ASI1-mini Chatbot", layout="centered")
|
9 |
+
st.title("ASI1-mini LLM Chat")
|
10 |
+
st.caption("Powered by [asi1.ai](https://docs.asi1.ai/)")
|
11 |
+
|
12 |
+
# Sidebar API Key Input
|
13 |
+
st.sidebar.subheader("π API Key")
|
14 |
+
asi_api_key = st.sidebar.text_input("ASI1 API Key", type="password")
|
15 |
+
st.sidebar.caption(
|
16 |
+
"Get your API key from [asi1.ai](https://asi1.ai/dashboard/api-keys)"
|
17 |
+
)
|
18 |
+
|
19 |
+
# API Docs Button
|
20 |
+
st.sidebar.markdown(
|
21 |
+
"""
|
22 |
+
<a href="https://docs.asi1.ai/docs/" target="_blank">
|
23 |
+
<button style="width: 100%; padding: 0.5em; font-size: 16px; background-color: #4CAF50; color: white; border: none; border-radius: 5px;">
|
24 |
+
π Looking for API docs?
|
25 |
+
</button>
|
26 |
+
</a>
|
27 |
+
""",
|
28 |
+
unsafe_allow_html=True,
|
29 |
+
)
|
30 |
+
|
31 |
+
# Chat history initialization
|
32 |
+
if "chat_history" not in st.session_state:
|
33 |
+
st.session_state.chat_history = []
|
34 |
+
|
35 |
+
# Display past chat messages
|
36 |
+
for msg in st.session_state.chat_history:
|
37 |
+
with st.chat_message(msg["role"]):
|
38 |
+
st.markdown(msg["content"])
|
39 |
+
|
40 |
+
# Chat input box
|
41 |
+
user_input = st.chat_input("Ask me anything...")
|
42 |
+
|
43 |
+
|
44 |
+
def call_asi1_api(api_key, messages):
|
45 |
+
url = "https://api.asi1.ai/v1/chat/completions"
|
46 |
+
headers = {
|
47 |
+
"Authorization": f"Bearer {api_key}",
|
48 |
+
"Content-Type": "application/json",
|
49 |
+
}
|
50 |
+
payload = {
|
51 |
+
"model": "asi1-mini",
|
52 |
+
"messages": messages,
|
53 |
+
"temperature": 0.7,
|
54 |
+
"fun_mode": False,
|
55 |
+
"web_search": False,
|
56 |
+
"stream": False,
|
57 |
+
"max_tokens": 1024,
|
58 |
+
}
|
59 |
+
|
60 |
+
response = requests.post(url, headers=headers, json=payload)
|
61 |
+
response.raise_for_status()
|
62 |
+
data = response.json()
|
63 |
+
return data.get("choices", [{}])[0].get("message", {}).get("content", "")
|
64 |
+
|
65 |
+
|
66 |
+
# When user submits a message
|
67 |
+
if user_input:
|
68 |
+
if not asi_api_key:
|
69 |
+
st.error("Please enter your API key in the sidebar.")
|
70 |
+
else:
|
71 |
+
# Add user message to chat history
|
72 |
+
st.session_state.chat_history.append({"role": "user", "content": user_input})
|
73 |
+
with st.chat_message("user"):
|
74 |
+
st.markdown(user_input)
|
75 |
+
|
76 |
+
with st.chat_message("assistant"):
|
77 |
+
with st.spinner("Thinking..."):
|
78 |
+
try:
|
79 |
+
reply = call_asi1_api(asi_api_key, st.session_state.chat_history)
|
80 |
+
st.markdown(reply)
|
81 |
+
st.session_state.chat_history.append(
|
82 |
+
{"role": "assistant", "content": reply}
|
83 |
+
)
|
84 |
+
except Exception as e:
|
85 |
+
st.error(f"Error: {e}")
|