File size: 3,709 Bytes
6706599
f891d17
a0f26fc
f891d17
 
 
60e1978
f891d17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be92fa1
 
 
 
 
 
 
 
f891d17
be92fa1
f891d17
 
 
 
 
 
 
a0f26fc
f891d17
 
 
 
 
 
 
 
 
a0f26fc
f891d17
 
 
a0f26fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f891d17
 
 
 
 
 
 
be92fa1
 
 
f891d17
 
 
 
 
 
be92fa1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1875a0a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import streamlit as st
import requests
import json

st.set_page_config(page_title="ASI1-mini Chatbot", layout="centered")
st.title("ASI1-mini LLM Chat")
st.caption("Powered by [asi1.ai](https://asi1.ai/)")

# Sidebar API Key Input
st.sidebar.subheader("πŸ”‘ API Key")
asi_api_key = st.sidebar.text_input("ASI1 API Key", type="password")
st.sidebar.caption(
    "Get your API key from [asi1.ai](https://asi1.ai/dashboard/api-keys)"
)

# API Docs Button
st.sidebar.markdown(
    """
    <a href="https://docs.asi1.ai/docs/" target="_blank">
        <button style="width: 100%; padding: 0.5em; font-size: 16px; background-color: #4CAF50; color: white; border: none; border-radius: 5px;">
            πŸ” Looking for API docs?
        </button>
    </a>
    """,
    unsafe_allow_html=True,
)

# Chat history initialization
if "chat_history" not in st.session_state:
    st.session_state.chat_history = []

history_to_render = st.session_state.chat_history.copy()
if (
    history_to_render
    and history_to_render[-1]["role"] == "assistant"
    and st.session_state.get("awaiting_response", False)
):
    history_to_render = history_to_render[:-1]

# Display past chat messages
for msg in history_to_render:
    with st.chat_message(msg["role"]):
        st.markdown(msg["content"])

# Chat input box
user_input = st.chat_input("Ask me anything...")


def stream_asi1_api(api_key, messages):
    url = "https://api.asi1.ai/v1/chat/completions"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json",
    }
    payload = {
        "model": "asi1-mini",
        "messages": messages,
        "temperature": 0.7,
        "stream": True,
        "max_tokens": 1024,
    }

    with requests.post(url, headers=headers, json=payload, stream=True) as response:
        response.raise_for_status()
        for line in response.iter_lines():
            if line:
                decoded_line = line.decode("utf-8").strip()
                if decoded_line.startswith("data: "):
                    data = decoded_line[len("data: ") :]
                    if data == "[DONE]":
                        break
                    try:
                        parsed = json.loads(data)
                        content_piece = (
                            parsed.get("choices", [{}])[0]
                            .get("delta", {})
                            .get("content", "")
                        )
                        if content_piece:
                            yield content_piece
                    except json.JSONDecodeError:
                        continue


# When user submits a message
if user_input:
    if not asi_api_key:
        st.error("Please enter your API key in the sidebar.")
    else:
        # Mark that we're about to stream a new response
        st.session_state["awaiting_response"] = True

        # Add user message to chat history
        st.session_state.chat_history.append({"role": "user", "content": user_input})
        with st.chat_message("user"):
            st.markdown(user_input)

        with st.chat_message("assistant"):
            response_stream = stream_asi1_api(
                asi_api_key, st.session_state.chat_history
            )
            placeholder = st.empty()
            partial_text = ""

            for chunk in response_stream:
                partial_text += chunk
                placeholder.markdown(partial_text + "β–Œ")

            placeholder.markdown(partial_text)

        # Save final assistant message to history
        st.session_state.chat_history.append(
            {"role": "assistant", "content": partial_text}
        )
        st.session_state["awaiting_response"] = False