Retrieval फंक्शन बनाना
Retrieval Augmented Generation (RAG) वर्कफ़्लो में एक अहम चरण है डेटाबेस से डेटा लाना. इस अभ्यास में, आप retrieve() नाम का एक कस्टम फंक्शन डिज़ाइन करेंगे, जो कोर्स के अंतिम अभ्यास में इसी महत्वपूर्ण प्रक्रिया को अंजाम देगा.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Pinecone के साथ AI Applications बनाना
अभ्यास निर्देश
- अपने API key के साथ Pinecone क्लाइंट इनिशियलाइज़ करें (OpenAI क्लाइंट
clientके रूप में उपलब्ध है). retrieveफंक्शन परिभाषित करें, जो चार पैरामीटर ले:query,top_k,namespace, औरemb_model.emb_modelआर्ग्युमेंट का उपयोग करके इनपुटqueryको एम्बेड करें.query_embके सबसे मिलते-जुलतेtop_kवेक्टर मेटाडेटा सहित प्राप्त करें, और फंक्शन को दिए गएnamespaceको आर्ग्युमेंट के रूप में निर्दिष्ट करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Initialize the Pinecone client
pc = Pinecone(api_key="____")
index = pc.Index('pinecone-datacamp')
# Define a retrieve function that takes four arguments: query, top_k, namespace, and emb_model
def retrieve(query, top_k, namespace, emb_model):
# Encode the input query using OpenAI
query_response = ____(
input=____,
model=____
)
query_emb = query_response.data[0].embedding
# Query the index using the query_emb
docs = index.query(vector=____, top_k=____, namespace=____, include_metadata=True)
retrieved_docs = []
sources = []
for doc in docs['matches']:
retrieved_docs.append(doc['metadata']['text'])
sources.append((doc['metadata']['title'], doc['metadata']['url']))
return retrieved_docs, sources
documents, sources = retrieve(
query="How to build next-level Q&A with OpenAI",
top_k=3,
namespace='youtube_rag_dataset',
emb_model="text-embedding-3-small"
)
print(documents)
print(sources)