File size: 12,883 Bytes
4f6c67a
2c84c9e
090e237
9e36a10
b00bb52
77f262b
4f6c67a
090e237
2c84c9e
1066a1b
4c83d38
 
1066a1b
 
b00bb52
4c83d38
1066a1b
 
2c84c9e
ad255be
42556c6
87df6d8
42556c6
87df6d8
 
 
 
42556c6
3e65f9e
e4379b8
2c84c9e
b00bb52
42556c6
 
 
87df6d8
 
c73781a
 
 
 
 
4f6c67a
b00bb52
87df6d8
2c84c9e
b00bb52
42556c6
e4379b8
42556c6
87df6d8
 
1066a1b
87df6d8
 
 
b00bb52
87df6d8
cbd2e30
b00bb52
3bbf8f3
 
 
70ece37
3bbf8f3
 
 
 
 
 
 
 
 
 
9e36a10
3bbf8f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42556c6
 
 
 
 
3bbf8f3
 
 
9e36a10
3bbf8f3
 
 
42556c6
b00bb52
42556c6
f9e63d0
42556c6
1066a1b
 
3bbf8f3
1066a1b
 
3bbf8f3
 
 
 
4c83d38
b00bb52
42556c6
c73781a
42556c6
 
 
4c83d38
42556c6
c73781a
3bbf8f3
c73781a
42556c6
2c84c9e
cfd96b0
c73781a
cfd96b0
 
42556c6
2c84c9e
c240502
f9e63d0
 
 
3bbf8f3
4f6c67a
b00bb52
ad255be
42556c6
e4379b8
42556c6
 
 
4f6c67a
e4379b8
87df6d8
adbc7fe
87df6d8
42556c6
 
 
 
 
 
 
 
b00bb52
42556c6
87df6d8
4c83d38
e4379b8
 
afed0d7
e4379b8
 
4c83d38
ad255be
3e65f9e
b00bb52
42556c6
1066a1b
42556c6
1066a1b
 
 
 
 
 
 
 
 
 
 
 
 
1cff0af
 
1066a1b
 
4c83d38
b00bb52
1066a1b
 
 
 
 
 
 
 
4f6c67a
b00bb52
090e237
9e36a10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
090e237
9e36a10
 
 
 
090e237
9e36a10
 
090e237
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e36a10
 
 
 
090e237
9e36a10
 
 
 
090e237
9e36a10
090e237
 
9e36a10
4f6c67a
9e36a10
 
 
 
1066a1b
9e36a10
 
 
 
 
4f6c67a
b00bb52
42556c6
9e36a10
090e237
 
 
 
 
 
 
 
9e36a10
 
 
42556c6
87df6d8
 
1066a1b
 
 
87df6d8
3bbf8f3
70ece37
3bbf8f3
 
 
0f3b8dd
 
 
 
 
 
 
 
 
 
3bbf8f3
70ece37
3bbf8f3
70ece37
 
 
 
 
 
 
1066a1b
 
 
0f3b8dd
1066a1b
 
 
b00bb52
42556c6
 
090e237
42556c6
87df6d8
f9e63d0
e4379b8
1066a1b
b00bb52
42556c6
1066a1b
 
 
c73781a
87df6d8
9e36a10
4c83d38
9e36a10
3bbf8f3
 
 
 
 
1066a1b
9e36a10
b00bb52
9e36a10
 
090e237
c73781a
9e36a10
 
 
 
c73781a
b00bb52
3bbf8f3
 
 
 
 
 
 
 
 
 
1066a1b
 
 
 
 
 
 
 
 
4f6c67a
090e237
cbd2e30
 
 
9e36a10
1066a1b
 
 
b00bb52
87df6d8
 
b00bb52
 
e4379b8
1066a1b
4c83d38
cbd2e30
f9e63d0
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import os
import time
import threading
import requests

import streamlit as st
import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, TextIteratorStreamer

# Optional LangSmith (trace + feedback)
try:
    from langsmith import Client as LangSmithClient
    from langsmith import traceable
    from langsmith.run_helpers import get_current_run_tree
except Exception:
    LangSmithClient = None
    traceable = None
    get_current_run_tree = None


# =========================
# CONFIG
# =========================
MODEL_NAME = "teapotai/tinyteapot"
MAX_INPUT_TOKENS = 512
MAX_NEW_TOKENS = 192
TOP_K_SEARCH = 3
LOGO_URL = "https://teapotai.com/assets/logo.gif"

st.set_page_config(page_title="TeapotAI Chat", page_icon="🫖", layout="centered")


# =========================
# LOAD MODEL (CACHED)
# =========================
@st.cache_resource
def load_model():
    tok = AutoTokenizer.from_pretrained(MODEL_NAME)
    mdl = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
    dev = "cuda" if torch.cuda.is_available() else "cpu"
    mdl.to(dev).eval()
    return tok, mdl, dev


tokenizer, model, device = load_model()


# =========================
# LANGSMITH (OPTIONAL)
# =========================
@st.cache_resource
def get_langsmith():
    if (os.getenv("LANGCHAIN_API_KEY") or os.getenv("LANGSMITH_API_KEY")) and LangSmithClient:
        return LangSmithClient()
    return None


ls_client = get_langsmith()


# =========================
# SAMPLE SEED (with full debug fields)
# =========================
SAMPLE_QUESTION = "Who are you?"

DEFAULT_SYSTEM_PROMPT = (
    "You are Teapot, an open-source AI assistant optimized for running on low-end cpu devices, "
    "providing short, accurate responses without hallucinating while excelling at "
    "information extraction and text summarization. "
    "If the context does not answer the question, reply exactly: "
    "'I am sorry but I don't have any information on that'."
)

SAMPLE_SYSTEM_PROMPT = DEFAULT_SYSTEM_PROMPT
SAMPLE_CONTEXT = "Teapot is an open-source AI assistant optimized for running on low-end cpu devices."
SAMPLE_ANSWER = "I am Teapot, an open-source AI assistant optimized for running on low-end cpu devices."
SAMPLE_PROMPT = f"{SAMPLE_CONTEXT}\n{SAMPLE_SYSTEM_PROMPT}\n{SAMPLE_QUESTION}\n"

SAMPLE_USER_MSG = {"role": "user", "content": SAMPLE_QUESTION}
SAMPLE_ASSISTANT_MSG = {
    "role": "assistant",
    "content": SAMPLE_ANSWER,
    "context": SAMPLE_CONTEXT,
    "system_prompt": SAMPLE_SYSTEM_PROMPT,
    "question": SAMPLE_QUESTION,
    "prompt": SAMPLE_PROMPT,
    "search_time": 0.37,
    "gen_time": 0.67,
    "input_tokens": 245,
    "output_tokens": 24,
    "tps": 35.9,
    "trace_id": None,
    "feedback": None,
}


# =========================
# SESSION STATE
# =========================
if "messages" not in st.session_state:
    st.session_state.messages = []
if "seeded" not in st.session_state:
    st.session_state.seeded = False

# Seed exactly once on first load
if (not st.session_state.seeded) and (len(st.session_state.messages) == 0):
    st.session_state.messages = [SAMPLE_USER_MSG, SAMPLE_ASSISTANT_MSG]
    st.session_state.seeded = True


# =========================
# HEADER
# =========================
col1, col2 = st.columns([1, 7], vertical_alignment="center")
with col1:
    st.image(LOGO_URL, width=56)
with col2:
    st.markdown("## TeapotAI Chat")
    st.caption(
        "Teapot is a 77M-parameter LLM optimized for fast CPU inference that only generates answers "
        "from the provided context to minimize hallucinations."
    )


# =========================
# SIDEBAR
# =========================
with st.sidebar:
    st.markdown("### Settings")

    system_prompt = st.text_area(
        "System prompt",
        value=DEFAULT_SYSTEM_PROMPT,
        height=160,
    )

    local_context = st.text_area(
        "Local context (optional)",
        height=140,
        placeholder="Extra context appended after web snippets…",
    )

    if st.button("Clear chat"):
        st.session_state.messages = []
        st.session_state.seeded = True
        st.rerun()


# =========================
# WEB SEARCH (ALWAYS ON)
# =========================
def web_search_snippets(query: str):
    api_key = os.getenv("BRAVE_API_KEY") or st.secrets.get("BRAVE_API_KEY", None)
    if not api_key:
        return "", 0.0

    headers = {"X-Subscription-Token": api_key, "Accept": "application/json"}
    params = {"q": query, "count": TOP_K_SEARCH}

    t0 = time.perf_counter()
    try:
        r = requests.get(
            "https://api.search.brave.com/res/v1/web/search",
            headers=headers,
            params=params,
            timeout=6,
        )
        data = r.json()
    except Exception:
        return "", 0.0
    t1 = time.perf_counter()

    snippets = []
    for item in data.get("web", {}).get("results", [])[:TOP_K_SEARCH]:
        desc = (item.get("description") or "").replace("<strong>", "").replace("</strong>", "").strip()
        if desc:
            snippets.append(desc)

    return "\n\n".join(snippets), (t1 - t0)


# =========================
# CONTEXT TRUNCATION (TAIL)
# =========================
def truncate_context(web_ctx: str, local_ctx: str, system: str, question: str) -> str:
    ctx = f"{web_ctx}\n\n{local_ctx}".strip()
    base = f"\n{system}\n{question}\n"
    base_tokens = tokenizer.encode(base)
    budget = MAX_INPUT_TOKENS - len(base_tokens)
    if budget <= 0:
        return ""
    if not ctx:
        return ""
    ctx_tokens = tokenizer.encode(ctx)
    if len(ctx_tokens) <= budget:
        return ctx
    return tokenizer.decode(ctx_tokens[-budget:], skip_special_tokens=True)


def count_tokens(text: str) -> int:
    return len(tokenizer.encode(text)) if text else 0


def get_trace_id_if_available() -> str | None:
    if not get_current_run_tree:
        return None
    try:
        run = get_current_run_tree()
        return str(run.id) if run and getattr(run, "id", None) else None
    except Exception:
        return None


# =========================
# FEEDBACK HANDLER (attached to trace_id)
# =========================
def handle_feedback(idx: int):
    val = st.session_state.get(f"fb_{idx}")
    st.session_state.messages[idx]["feedback"] = val

    msg = st.session_state.messages[idx]
    trace_id = msg.get("trace_id")

    if ls_client and trace_id:
        score = 1 if val == "👍" else 0
        try:
            ls_client.create_feedback(
                trace_id=trace_id,
                key="thumb_rating",
                score=score,
                comment="thumbs_up" if score else "thumbs_down",
            )
        except Exception:
            pass


# =========================
# STREAMING + LANGSMITH FIX
# - We do NOT trace a generator.
# - We stream to UI while returning a SINGLE final string.
# =========================
_UI_STREAM = {"placeholder": None}  # set per-request


def _generate_with_streamer(prompt: str) -> str:
    """
    Runs model.generate with a TextIteratorStreamer and updates a Streamlit placeholder
    as chunks arrive. Returns the final full text.
    """
    inputs = tokenizer(prompt, return_tensors="pt").to(device)

    streamer = TextIteratorStreamer(
        tokenizer,
        skip_prompt=True,
        skip_special_tokens=True,
    )

    gen_kwargs = dict(
        **inputs,
        max_new_tokens=MAX_NEW_TOKENS,
        do_sample=False,
        num_beams=1,
        streamer=streamer,
    )

    t = threading.Thread(target=model.generate, kwargs=gen_kwargs, daemon=True)
    t.start()

    buf = ""
    ph = _UI_STREAM.get("placeholder")
    if ph is not None:
        ph.markdown("")  # ensure element exists before first chunk

    for chunk in streamer:
        buf += chunk
        if ph is not None:
            ph.markdown(buf)

    return buf


if traceable:

    @traceable(name="teapot_answer")
    def traced_answer_streaming(context: str, system_prompt: str, question: str) -> str:
        prompt = f"{context}\n{system_prompt}\n{question}\n"
        return _generate_with_streamer(prompt)

else:

    def traced_answer_streaming(context: str, system_prompt: str, question: str) -> str:
        prompt = f"{context}\n{system_prompt}\n{question}\n"
        return _generate_with_streamer(prompt)


# =========================
# INPUT FIRST (so latest user msg renders immediately)
# =========================
query = st.chat_input("Ask a question...")

if query:
    st.session_state.messages.append({"role": "user", "content": query})


# =========================
# RENDER HISTORY
# Row 1: message + feedback
# Row 2: inspect + debug metrics
# =========================
for i, msg in enumerate(st.session_state.messages):
    with st.chat_message(msg["role"]):
        if msg["role"] == "user":
            st.markdown(msg["content"])
            continue

        # Row 1
        msg_col, fb_col = st.columns([12, 1], vertical_alignment="center")
        with msg_col:
            st.markdown(msg.get("content", ""))
        with fb_col:
            key = f"fb_{i}"
            st.session_state.setdefault(key, msg.get("feedback"))
            st.feedback(
                "thumbs",
                key=key,
                disabled=msg.get("feedback") is not None,
                on_change=handle_feedback,
                args=(i,),
            )

        # Row 2
        inspect_col, metrics_col = st.columns([12, 1], vertical_alignment="center")
        with inspect_col:
            st.caption(
                f"🔎 {msg.get('search_time', 0.0):.2f}s (search) "
                f"🧠 {msg.get('gen_time', 0.0):.2f}s (generation) "
                f"⚡ {msg.get('tps', 0.0):.1f} tok/s  "
                f"🧾 {msg.get('input_tokens', 0)} input tokens • {msg.get('output_tokens', 0)} output tokens"
            )
        with metrics_col:
            with st.popover("ℹ️", help="Inspect"):
                st.markdown("**Context**")
                st.code(msg.get("context", ""), language="text")
                st.markdown("**System Prompt**")
                st.code(msg.get("system_prompt", ""), language="text")
                st.markdown("**Question**")
                st.code(msg.get("question", ""), language="text")


# =========================
# GENERATE ONLY IF THIS RUN RECEIVED A NEW QUERY
# =========================
if query:
    question = query

    # Web search
    web_ctx, search_time = web_search_snippets(question)

    # Context + truncation
    context = truncate_context(web_ctx, local_context, system_prompt, question)
    prompt = f"{context}\n{system_prompt}\n{question}\n"
    input_tokens = count_tokens(prompt)

    # Assistant response (stream to UI, return full string for LangSmith)
    with st.chat_message("assistant"):
        # Row 1: message + feedback (disabled live)
        msg_col, fb_col = st.columns([14, 1], vertical_alignment="center")
        with msg_col:
            placeholder = st.empty()
        with fb_col:
            st.feedback("thumbs", key="live_fb", disabled=True)

        _UI_STREAM["placeholder"] = placeholder

        start = time.perf_counter()
        answer = traced_answer_streaming(context, system_prompt, question)
        trace_id = get_trace_id_if_available()
        gen_time = time.perf_counter() - start

        _UI_STREAM["placeholder"] = None  # cleanup

        output_tokens = count_tokens(answer)
        tps = output_tokens / gen_time if gen_time > 0 else 0.0

        # Row 2: inspect + metrics
        inspect_col, metrics_col = st.columns([12, 1], vertical_alignment="center")
        with inspect_col:
            st.caption(
                f"🔎 {search_time:.2f}s (search) "
                f"🧠 {gen_time:.2f}s (generation) "
                f"⚡ {tps:.1f} tok/s  "
                f"🧾 {input_tokens} input tokens • {output_tokens} output tokens"
            )
        with metrics_col:
            with st.popover("ℹ️", help="Inspect"):
                st.markdown("**Context**")
                st.code(context, language="text")
                st.markdown("**System**")
                st.code(system_prompt, language="text")
                st.markdown("**Question**")
                st.code(question, language="text")
                st.markdown("**Prompt**")
                st.code(prompt, language="text")

    # Persist assistant message
    st.session_state.messages.append(
        {
            "role": "assistant",
            "content": answer,
            "context": context,
            "system_prompt": system_prompt,
            "question": question,
            "prompt": prompt,
            "search_time": search_time,
            "gen_time": gen_time,
            "input_tokens": input_tokens,
            "output_tokens": output_tokens,
            "tps": tps,
            "trace_id": trace_id,
            "feedback": None,
        }
    )