retrieval.py 833 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. from typing import Dict
  2. from pydantic import BaseModel
  3. from
  4. from main import app
  5. from services import collection_view
  6. class RetrievalSetting(BaseModel):
  7. top_k: int
  8. score_threshold: float
  9. embedding_instruct: str | None = None
  10. reranker_instruct: str | None = None
  11. class Retrieval(BaseModel):
  12. knowledge_id: str
  13. query: str
  14. retrieval_setting: RetrievalSetting
  15. metadata_condition: Dict | None = None
  16. @app.post('/retrieval')
  17. def retrieval(req: Retrieval):
  18. chunks = collection_view.search(
  19. req.query,
  20. expand_chunk=[1, 1],
  21. limit=req.retrieval_setting.top_k,
  22. )
  23. return {
  24. 'records': [
  25. {
  26. 'content': chunk.data.text,
  27. 'score': chunk.score,
  28. 'title': chunk.data.documentSet.documentSetName,
  29. 'metadata': {
  30. 'document_id': str(chunk.data.documentSet.documentSetId),
  31. },
  32. } for chunk in chunks
  33. ]
  34. }