feat: add DeepSearch multi-agent researcher with async jobs, vector store, and RAG chat
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
import asyncio
|
||||
|
||||
from devplacepy.services.deepsearch import chat as chat_module
|
||||
from devplacepy.services.deepsearch.chat import DeepsearchChat
|
||||
from devplacepy.services.deepsearch.embeddings import local_embed
|
||||
from devplacepy.services.deepsearch.store import Chunk, VectorStore
|
||||
|
||||
|
||||
def _seed(collection):
|
||||
store = VectorStore(collection)
|
||||
chunks = [
|
||||
Chunk(uid="c0", text="The transistor was invented at Bell Labs.", url="https://a.example", title="A"),
|
||||
Chunk(uid="c1", text="Silicon wafers are used to make chips.", url="https://b.example", title="B"),
|
||||
]
|
||||
vectors = local_embed([c.text for c in chunks]).vectors
|
||||
store.add(chunks, vectors)
|
||||
|
||||
|
||||
def test_answer_is_grounded_and_cited(monkeypatch):
|
||||
collection = "ds_chat_test_grounded"
|
||||
_seed(collection)
|
||||
try:
|
||||
async def fake_embed(texts, api_key, **kwargs):
|
||||
return local_embed(texts)
|
||||
|
||||
async def fake_complete(messages, api_key, **kwargs):
|
||||
return "The transistor was invented at Bell Labs [1]."
|
||||
|
||||
monkeypatch.setattr(chat_module, "embed_texts", fake_embed)
|
||||
monkeypatch.setattr(chat_module, "complete_chat", fake_complete)
|
||||
|
||||
chat = DeepsearchChat(collection, "k")
|
||||
answer = asyncio.run(chat.answer("where was the transistor invented"))
|
||||
assert "Bell Labs" in answer.text
|
||||
assert answer.citations
|
||||
assert answer.citations[0]["url"].startswith("https://")
|
||||
finally:
|
||||
VectorStore(collection).drop()
|
||||
|
||||
|
||||
def test_answer_when_no_chunks(monkeypatch):
|
||||
collection = "ds_chat_test_empty"
|
||||
try:
|
||||
async def fake_embed(texts, api_key, **kwargs):
|
||||
return local_embed(texts)
|
||||
|
||||
monkeypatch.setattr(chat_module, "embed_texts", fake_embed)
|
||||
chat = DeepsearchChat(collection, "k")
|
||||
answer = asyncio.run(chat.answer("anything"))
|
||||
assert answer.citations == []
|
||||
assert "did not capture" in answer.text
|
||||
finally:
|
||||
VectorStore(collection).drop()
|
||||
@@ -0,0 +1,52 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from devplacepy.services.deepsearch.embeddings import local_embed
|
||||
from devplacepy.services.deepsearch.store import Chunk, VectorStore
|
||||
|
||||
|
||||
def _chunks():
|
||||
texts = [
|
||||
"The transistor was invented at Bell Labs in nineteen forty seven.",
|
||||
"Silicon is the primary semiconductor material used in chips.",
|
||||
"Quantum tunnelling limits how small a transistor can become.",
|
||||
]
|
||||
chunks = [
|
||||
Chunk(uid=f"c{i}", text=text, url=f"https://s{i}.example", title=f"Source {i}")
|
||||
for i, text in enumerate(texts)
|
||||
]
|
||||
return chunks
|
||||
|
||||
|
||||
def test_add_and_count():
|
||||
store = VectorStore("ds_store_test_count")
|
||||
try:
|
||||
chunks = _chunks()
|
||||
vectors = local_embed([c.text for c in chunks]).vectors
|
||||
store.add(chunks, vectors)
|
||||
assert store.count() == 3
|
||||
finally:
|
||||
store.drop()
|
||||
|
||||
|
||||
def test_hybrid_search_returns_relevant_chunk():
|
||||
store = VectorStore("ds_store_test_hybrid")
|
||||
try:
|
||||
chunks = _chunks()
|
||||
vectors = local_embed([c.text for c in chunks]).vectors
|
||||
store.add(chunks, vectors)
|
||||
query = "where was the transistor invented"
|
||||
query_vector = local_embed([query]).vectors[0]
|
||||
results = store.hybrid_search(query, query_vector, top_k=2)
|
||||
assert results
|
||||
assert any("transistor" in r.text.lower() for r in results)
|
||||
finally:
|
||||
store.drop()
|
||||
|
||||
|
||||
def test_keyword_scores_rank_match_higher():
|
||||
store = VectorStore("ds_store_test_keyword")
|
||||
chunks = _chunks()
|
||||
scores = store.keyword_scores("silicon semiconductor", chunks)
|
||||
assert scores
|
||||
best = max(scores, key=scores.get)
|
||||
assert "silicon" in next(c.text for c in chunks if c.uid == best).lower()
|
||||
Reference in New Issue
Block a user