ExamplescriptintermediateRunnableresearch-brief
Chat
Runnable example (intermediate) for script using docling, ipykernel.
Key Facts
- Level
- intermediate
- Runtime
- Python • OpenAI API
- Pattern
- Context-backed research with explicit evidence
- Interaction
- Live sandbox • Script
- Updated
- 14 March 2026
Navigate this example
Library
Browse examplesReopen the wider library to compare adjacent patterns and linked learning paths.Interaction
Run sandbox nowTry the interaction directly in this example’s guided sandbox surface.Source
Open full sourceRead the real implementation, highlighted checkpoints, and runtime requirements.MCP
Call via MCPUse the same resource inside agents, deterministic exports, and MCP setup flows.
Linked principles
5-chat.py
python
import streamlit as st
import lancedb
from openai import OpenAI
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Initialize OpenAI client
client = OpenAI()
# Initialize LanceDB connection
@st.cache_resource
def init_db():
"""Initialize database connection.
Returns:
LanceDB table object
"""
db = lancedb.connect("data/lancedb")
return db.open_table("docling")
def get_context(query: str, table, num_results: int = 5) -> str:
"""Search the database for relevant context.
Args:
query: User's question
table: LanceDB table object
num_results: Number of results to return
Returns:
str: Concatenated context from relevant chunks with source information
"""
results = table.search(query).limit(num_results).to_pandas()
contexts = []
for _, row in results.iterrows():
# Extract metadata
filename = row["metadata"]["filename"]
page_numbers = row["metadata"]["page_numbers"]
title = row["metadata"]["title"]
# Build source citation
source_parts = []
if filename:
source_parts.append(filename)
if page_numbers:
source_parts.append(f"p. {', '.join(str(p) for p in page_numbers)}")
source = f"\nSource: {' - '.join(source_parts)}"
if title:
source += f"\nTitle: {title}"
contexts.append(f"{row['text']}{source}")
return "\n\n".join(contexts)
def get_chat_response(messages, context: str) -> str:
"""Get streaming response from OpenAI API.
Args:
messages: Chat history
context: Retrieved context from database
Returns:
str: Model's response
"""
system_prompt = f"""You are a helpful assistant that answers questions based on the provided context.
Use only the information from the context to answer questions. If you're unsure or the context
doesn't contain the relevant information, say so.
Context:
{context}
"""
messages_with_context = [{"role": "system", "content": system_prompt}, *messages]
# Create the streaming response
stream = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages_with_context,
temperature=0.7,
stream=True,
)
# Use Streamlit's built-in streaming capability
response = st.write_stream(stream)
return response
# Initialize Streamlit app
st.title("📚 Document Q&A")
# Initialize session state for chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Initialize database connection
table = init_db()
# Display chat messages
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Chat input
if prompt := st.chat_input("Ask a question about the document"):
# Display user message
with st.chat_message("user"):
st.markdown(prompt)
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# Get relevant context
with st.status("Searching document...", expanded=False) as status:
context = get_context(prompt, table)
st.markdown(
"""
<style>
.search-result {
margin: 10px 0;
padding: 10px;
border-radius: 4px;
background-color: #f0f2f6;
}
.search-result summary {
cursor: pointer;
color: #0f52ba;
font-weight: 500;
}
.search-result summary:hover {
color: #1e90ff;
}
.metadata {
font-size: 0.9em;
color: #666;
font-style: italic;
}
</style>
""",
unsafe_allow_html=True,
)
st.write("Found relevant sections:")
for chunk in context.split("\n\n"):
# Split into text and metadata parts
parts = chunk.split("\n")
text = parts[0]
metadata = {
line.split(": ")[0]: line.split(": ")[1]
for line in parts[1:]
if ": " in line
}
source = metadata.get("Source", "Unknown source")
title = metadata.get("Title", "Untitled section")
st.markdown(
f"""
<div class="search-result">
<details>
<summary>{source}</summary>
<div class="metadata">Section: {title}</div>
<div style="margin-top: 8px;">{text}</div>
</details>
</div>
""",
unsafe_allow_html=True,
)
# Display assistant response first
with st.chat_message("assistant"):
# Get model response with streaming
response = get_chat_response(st.session_state.messages, context)
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": response})
Related principles
- P4trustApply progressive disclosure to system agencyProvide the minimum information necessary by default, while enabling users to inspect additional detail when confidence, understanding, or intervention is required.Open principle →
- P6visibilityExpose meaningful operational state, not internal complexityPresent the state of the system in language and structures that are relevant to the user, rather than exposing low-level internals that do not support action or understanding.Open principle →
- P7trustEstablish trust through inspectabilityUsers should be able to examine how a result was produced when confidence, accountability, or decision quality is important.Open principle →