brime

Build a RAG chatbot with Brime

End-to-end recipe — Brime retriever + LangChain + your favourite LLM.

This recipe builds a citation-aware question-answerer in ~40 lines of Python. We use BrimeRetriever (web search → Document objects) + LangChain LCEL + an LLM of your choice.

What you'll build

user question  ──►  BrimeRetriever  ──►  Document[] (ranked web chunks)
                                            │
                                            ▼
                          ChatPromptTemplate("Context: {context}\n...")
                                            │
                                            ▼
                                       Chat model
                                            │
                                            ▼
                                  Answer with [1][2] citations

1. Install

pip install langchain-brime langchain-anthropic

(Swap langchain-anthropic for langchain-openai or any LangChain chat model.)

2. Set keys

export BRIME_API_KEY="sk-brime-..."
export ANTHROPIC_API_KEY="sk-ant-..."

3. The chain

from langchain_brime import BrimeRetriever
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnableLambda, RunnablePassthrough
from langchain_anthropic import ChatAnthropic

# 1) Retriever — pulls 5 ranked chunks from the live web for any question.
retriever = BrimeRetriever(k=5, topic="general")

# 2) Format Documents into a citation-friendly context block.
def format_docs(docs):
    return "\n\n".join(
        f"[{i+1}] {d.metadata['title']}\n{d.page_content}\nURL: {d.metadata['url']}"
        for i, d in enumerate(docs)
    )

# 3) Prompt — instructs the model to cite [N] inline, then list sources.
prompt = ChatPromptTemplate.from_template("""
You are a careful research assistant. Use ONLY the context below.
Inline-cite sources with [1], [2], etc., matching the order in the context.

Context:
{context}

Question: {question}

Answer:""")

# 4) Chat model.
llm = ChatAnthropic(model="claude-3-5-haiku-latest", temperature=0)

# 5) Compose the chain.
chain = (
    {
        "context": retriever | RunnableLambda(format_docs),
        "question": RunnablePassthrough(),
    }
    | prompt
    | llm
)

# 6) Run it.
question = "How are global EV sales tracking in 2025?"
response = chain.invoke(question)
print(response.content)

4. Run it

$ python rag.py
Global electric-vehicle sales in 2025 are on track to exceed 18 million
units, a roughly 25% year-over-year rise [1][2]:
1. China remains the largest single market, with BYD and Tesla trading
   the monthly lead through Q3 [1].
2. Europe's share is up sharply after the EU Fit-for-55 incentive
   reset took effect in March 2025 [2].

Sources:
[1] IEA Global EV Outlook 2025 — iea.org
[2] BloombergNEF EV Market Update Q3 2025 — bloomberg.com

(Output paraphrased; your run will surface different sources.)

5. What to tweak

  • k. k=10 widens the context; k=3 keeps prompts tight.
  • Domains. BrimeRetriever(domains=["docs.python.org", "stackoverflow.com"]) restricts to a curated set.
  • Time range. BrimeRetriever(time_range="month", topic="news") for fresh content.

6. Add streaming

LangChain LCEL chains support .stream() out of the box. Brime SSE isn't involved here — only the LLM streams. The retriever runs once at the start and returns Documents synchronously.

for chunk in chain.stream(question):
    print(chunk.content, end="", flush=True)

7. Going async

import asyncio
from langchain_brime import BrimeRetriever

async def main():
    retriever = BrimeRetriever(k=5)
    docs = await retriever.ainvoke("Tesla Cybertruck recall 2025")
    for d in docs:
        print(d.metadata["url"])

asyncio.run(main())

The same chain composes with chain.ainvoke(question).

Where to next

Verified against langchain-brime>=0.2.0 and brime>=0.2.0.