Spaces:
Sleeping
Sleeping
File size: 7,250 Bytes
005e9fd | 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 | """Gradio UI for the Rust documentation assistant."""
import logging
from dataclasses import dataclass, field
import gradio as gr
from llama_index.core.base.llms.types import ChatMessage, MessageRole
from rag.config import DEFAULT_PROVIDER, PROVIDERS, RETRIEVAL_MODE, RETRIEVAL_TOP_K
from rag.index import download_index_if_missing, load_retriever
from rag.pipeline import get_response_stream
from rag.prompts import render_search, render_sources, source_count
logging.basicConfig(level=logging.INFO)
logging.getLogger("httpx").setLevel(logging.WARNING)
download_index_if_missing()
def get_retriever(book: str | None = None):
"""The retriever for one search, scoped to a book when the agent asked for one.
`load_retriever` is cached on its arguments, so we build the unscoped
retriever once and a scoped one at most once per book.
"""
return load_retriever(RETRIEVAL_MODE, RETRIEVAL_TOP_K, book)
get_retriever() # build the default eagerly to reduce latency for answer to first question
DESCRIPTION = """Ask a question about Rust and get an answer grounded in the official documentation
— The Book, the Reference, Rust by Example, the Rustonomicon, and the async book.
Paste a key for one provider below. It is used only to generate answers, and is never stored or logged.
Search runs locally with an open source embedding model, so your key pays only for the answer."""
EXAMPLES = [
"Why can't I have two mutable references to the same value?",
"What does the ? operator do?",
"How do I share mutable state between threads?",
"error[E0502]: cannot borrow as mutable",
"When should I use Box<dyn Error> instead of a custom error enum?",
"How can I append a value to a vector?"
]
def on_provider_change(provider: str):
spec = PROVIDERS[provider]
return (
gr.Dropdown(choices=list(spec.models), value=spec.default_model),
# We restate `type` because this replaces the component, and a
# Textbox built without it defaults to plain text, which would put a key
# the visitor had already pasted on screen.
gr.Textbox(
label=spec.key_label,
placeholder=f"Paste your {spec.key_label}",
type="password",
),
)
TITLE_LIMIT = 72
@dataclass
class Turn:
question: str
messages: list[gr.ChatMessage] = field(default_factory=list)
answer: str = ""
def display_messages(turns: list[Turn], pending: Turn | None = None) -> list[gr.ChatMessage]:
shown: list[gr.ChatMessage] = []
for turn in [*turns, *([pending] if pending else [])]:
shown.append(gr.ChatMessage(role="user", content=turn.question))
shown.extend(turn.messages)
return shown
def model_messages(turns: list[Turn]) -> list[ChatMessage]:
history: list[ChatMessage] = []
for turn in turns:
if not turn.answer:
continue
history.append(ChatMessage(role=MessageRole.USER, content=turn.question))
history.append(ChatMessage(role=MessageRole.ASSISTANT, content=turn.answer))
return history
def answer_or_spinner(answer: str) -> gr.ChatMessage:
if answer:
return gr.ChatMessage(role="assistant", content=answer)
return gr.ChatMessage(
role="assistant", content="", metadata={"title": "Thinking…", "status": "pending"}
)
def loop_messages(state) -> list[gr.ChatMessage]:
messages = []
for search in state.searches:
query, nodes = search.query, search.nodes
short = query if len(query) <= TITLE_LIMIT else query[:TITLE_LIMIT].rstrip() + "…"
scope = f" · {search.book}" if search.book else ""
counts = (
f" — {len(nodes)} excerpts, {source_count(nodes)} sources" if nodes else ""
)
messages.append(
gr.ChatMessage(
role="assistant",
content=render_search(query, search.thought, nodes),
metadata={"title": f"🔍 {short}{scope}{counts}", "status": "done"},
)
)
return messages
async def on_submit(
question: str | None,
turns: list[Turn],
provider: str,
model: str,
api_key: str | None,
):
question = (question or "").strip()
api_key = api_key or ""
if not question:
yield display_messages(turns), "", turns
return
pending = Turn(question=question)
yield display_messages(turns, pending), "", turns
stream = get_response_stream(
question, get_retriever, provider, api_key, model, model_messages(turns)
)
state = None
answer = ""
try:
async for state in stream:
answer = state.answer
pending.messages = loop_messages(state) + [answer_or_spinner(answer)]
yield display_messages(turns, pending), "", turns
except Exception as exc:
detail = f"**{type(exc).__name__}:** {exc}"
body = f"{answer}\n\n{detail}".strip()
pending.messages = (loop_messages(state) if state else []) + [
gr.ChatMessage(role="assistant", content=body)
]
yield display_messages(turns, pending), "", turns + [pending]
return
sources = render_sources(state.nodes, answer) if state else ""
pending.messages = loop_messages(state) + [
gr.ChatMessage(role="assistant", content=answer + sources)
]
pending.answer = answer
yield display_messages(turns, pending), "", turns + [pending]
def build_ui() -> gr.Blocks:
default = PROVIDERS[DEFAULT_PROVIDER]
with gr.Blocks(title="Rust Docs Assistant", fill_height=True) as chat:
gr.Markdown("# Rust Docs Assistant 🦀")
gr.Markdown(DESCRIPTION)
with gr.Row():
provider = gr.Dropdown(
choices=[(spec.label, slug) for slug, spec in PROVIDERS.items()],
value=DEFAULT_PROVIDER,
label="Provider",
)
model = gr.Dropdown(
choices=list(default.models), value=default.default_model, label="Model"
)
api_key = gr.Textbox(
label=default.key_label,
placeholder=f"Paste your {default.key_label}",
type="password",
)
turns = gr.State([])
chatbot = gr.Chatbot(
height=480,
label="Conversation",
buttons=["copy"],
placeholder="Ask a question about Rust to get started.",
)
question = gr.Textbox(
placeholder="Ask about ownership, lifetimes, traits, async…",
show_label=False,
submit_btn=True,
)
gr.Examples(examples=EXAMPLES, inputs=question, label="Try one")
clear = gr.Button("Clear conversation", variant="secondary")
provider.change(on_provider_change, inputs=provider, outputs=[model, api_key])
question.submit(
on_submit,
inputs=[question, turns, provider, model, api_key],
outputs=[chatbot, question, turns],
)
clear.click(lambda: ([], "", []), outputs=[chatbot, question, turns])
return chat
if __name__ == "__main__":
build_ui().queue(default_concurrency_limit=8).launch(footer_links=["settings"])
|