11from __future__ import annotations
22from typing import List
3- from pydantic import BaseModel
3+ from pydantic import BaseModel , Field
44
55from openai import OpenAI
66from pinecone import Pinecone
1818
1919# ====== Pydantic models ======
2020class DocItem (BaseModel ):
21- """Single retrieved document item ."""
22- source : str
23- text : str
24- score : float
21+ """Single retrieved chunk from the RAG knowledge base ."""
22+ source : str = Field (..., description = "Chunk origin (e.g. docs, howto, tutorial)." )
23+ text : str = Field (..., description = "Retrieved text snippet." )
24+ score : float = Field (..., description = "Similarity score (higher = more relevant)." )
2525
2626
2727class RetrieveDocsOutput (BaseModel ):
28- """Structured response containing a list of retrieved document items."""
29- docs : List [DocItem ]
28+ """List of retrieved chunks sorted by relevance."""
29+ docs : List [DocItem ] = Field (
30+ default_factory = list ,
31+ description = "Relevant chunks returned from the RAG store."
32+ )
3033
3134
3235# ====== Initialization ======
@@ -73,9 +76,8 @@ def _get_embedding(text: str) -> List[float]:
7376
7477def retrieve_docs (query : str ) -> RetrieveDocsOutput :
7578 """
76- Query Pinecone using an OpenAI embedding for the given query.
77- Returns a structured object:
78- { "docs": [ { "source": str, "text": str, "score": float }, ... ] }
79+ Fetch prioritized chunks from your RAG store—documentation, how-tos, tutorials and articles—
80+ based on a single search query.
7981 """
8082 vec = _get_embedding (query )
8183 items : List [DocItem ] = []
@@ -100,4 +102,11 @@ def retrieve_docs(query: str) -> RetrieveDocsOutput:
100102
101103 # Sort results by descending score
102104 items .sort (key = lambda d : - d .score )
103- return RetrieveDocsOutput (docs = items )
105+ output = RetrieveDocsOutput (docs = items )
106+
107+ print (f"[retrieve_docs] Query: { query !r} " )
108+ print (f"[retrieve_docs] Retrieved { len (items )} document(s):" )
109+ for i , d in enumerate (items , 1 ):
110+ print (f" { i } . [{ d .source } ] score={ d .score :.4f} — { d .text [:100 ]!r} " )
111+
112+ return output
0 commit comments