hearing.context¶
Context stores and retrieval (RAG) for context-connected agents.
An agent with no context is just a chatbot — the point of hearing is agents
bound to a meeting and a project, drawing on accumulated knowledge (prior
takeaways, the codebase, research reports). This module provides the retrieval
side behind a tiny Retriever Protocol so the backend is swappable and
testable with a fake (per the hearing-agents skill).
The default KeywordRetriever is a dependency-free TF-IDF retriever —
good enough to wire context-connection end-to-end and fully deterministic for
tests. Swap in an embedding/vector backend (Qdrant + LlamaIndex, or the user’s
own local store — check my-packages / python-storage) behind the same
Protocol when you need semantic recall.
Module Attributes
maps texts to equal-length float vectors. |
Functions
|
Build an |
|
Build a |
|
Return an |
Classes
Adapt any callable into a |
|
|
A retrieved piece of context: the text plus where it came from. |
|
Semantic retriever: ranks chunks by cosine similarity of embeddings. |
|
Dependency-free TF-IDF retriever over a small in-memory corpus. |
|
Retrieves project/meeting context relevant to a query (RAG). |
|
Brings outside information in — one targeted query, |
|
Key-free, dependency-free |
- class hearing.context.CallableWebSearch(fn)[source]¶
Bases:
objectAdapt any callable into a
WebSearch(for custom backends/tests).fn(query, k)may return Chunks, strings, or dicts (coerced), and may be sync or async.
- class hearing.context.Chunk(text, title='', source='', score=0.0)[source]¶
Bases:
objectA retrieved piece of context: the text plus where it came from.
- hearing.context.EmbedFn¶
maps texts to equal-length float vectors.
- Type:
A batch embedder
alias of
Callable[[Sequence[str]],Sequence[Sequence[float]]]
- class hearing.context.EmbeddingRetriever(docs, *, embed)[source]¶
Bases:
objectSemantic retriever: ranks chunks by cosine similarity of embeddings.
The corpus is embedded once at construction;
searchembeds the query and returns the nearest chunks. The embedder is injected (embed=) so it is backend-agnostic and testable with a deterministic fake — useopenai_embedder()for real semantic recall.>>> import asyncio >>> fake = lambda texts: [[t.lower().count("db"), t.lower().count("ui")] for t in texts] >>> r = EmbeddingRetriever({"a": "db db", "b": "ui ui"}, embed=fake) >>> asyncio.run(r.search("db"))[0].title 'a'
- class hearing.context.KeywordRetriever(docs)[source]¶
Bases:
objectDependency-free TF-IDF retriever over a small in-memory corpus.
>>> import asyncio >>> r = KeywordRetriever({"db": "We will use PostgreSQL.", "ui": "React frontend."}) >>> hits = asyncio.run(r.search("postgresql migration")) >>> hits[0].title 'db'
Matching is purely lexical, so a query sharing no token with the corpus retrieves nothing — use
EmbeddingRetrieverfor semantic recall.>>> asyncio.run(r.search("which database?")) []
- class hearing.context.Retriever(*args, **kwargs)[source]¶
Bases:
ProtocolRetrieves project/meeting context relevant to a query (RAG).
- class hearing.context.WebSearch(*args, **kwargs)[source]¶
Bases:
ProtocolBrings outside information in — one targeted query,
kresults.
- class hearing.context.WikipediaSearch(lang='en', timeout=8.0, fetch=None)[source]¶
Bases:
objectKey-free, dependency-free
WebSearchover Wikipedia (stdlib urllib).Ideal for the live fact-check / “who is X / what is Y” use: one targeted query, a few result snippets with source URLs. The HTTP
fetchis injectable so tests need no network.
- hearing.context.build_embedding_retriever(source, *, embed=None)[source]¶
Build an
EmbeddingRetriever(default embedder: OpenAI) from a dict, a list, or a folder of.md/.txtdocs (same source forms asbuild_retriever()).- Return type:
- hearing.context.build_retriever(source)[source]¶
Build a
KeywordRetrieverfrom a dict, a list, or a folder/file.A path to a directory indexes its
.txt/.mdfiles (keyed by stem); a path to a single file indexes that file; a Mapping/Sequence is used directly.- Return type: