RAG प्रश्न-उत्तर फंक्शन
बस हो ही गया! RAG वर्कफ़्लो का अंतिम चरण है प्राप्त किए गए दस्तावेज़ों को एक question-answering मॉडल के साथ इंटीग्रेट करना।
एक prompt_with_context_builder() फंक्शन पहले से परिभाषित है और आपके लिए उपलब्ध है। यह फंक्शन Pinecone इंडेक्स से प्राप्त दस्तावेज़ों को लेता है और उन्हें ऐसे प्रॉम्प्ट में शामिल करता है जिसे question-answering मॉडल उपयोग कर सके:
def prompt_with_context_builder(query, docs):
delim = '\n\n---\n\n'
prompt_start = 'Answer the question based on the context below.\n\nContext:\n'
prompt_end = f'\n\nQuestion: {query}\nAnswer:'
prompt = prompt_start + delim.join(docs) + prompt_end
return prompt
आप question_answering() फंक्शन इम्प्लीमेंट करेंगे, जो OpenAI के भाषा मॉडल gpt-4o-mini को अतिरिक्त संदर्भ और स्रोत प्रदान करेगा ताकि वह आपके प्रश्नों का बेहतर उत्तर दे सके।
यह अभ्यास पाठ्यक्रम का हिस्सा है
Pinecone के साथ AI Applications बनाना
अभ्यास निर्देश
- अपनी API key के साथ Pinecone क्लाइंट इनिशियलाइज़ करें (OpenAI क्लाइंट
clientनाम से उपलब्ध है)। 'youtube_rag_dataset'namespace सेqueryटेक्स्ट के सबसे मिलते-जुलते तीन दस्तावेज़ रिट्रीव करें।- दिए गए
promptऔरsys_promptपर OpenAI के'gpt-4o-mini'मॉडल का उपयोग करके प्रतिक्रिया जनरेट करें; मॉडल कोchat_modelफंक्शन आर्ग्युमेंट से निर्दिष्ट किया गया है.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Initialize the Pinecone client
pc = Pinecone(api_key="____")
index = pc.Index('pinecone-datacamp')
query = "How to build next-level Q&A with OpenAI"
# Retrieve the top three most similar documents and their sources
documents, sources = retrieve(____, top_k=____, namespace='____', emb_model="text-embedding-3-small")
prompt_with_context = prompt_with_context_builder(query, documents)
print(prompt_with_context)
def question_answering(prompt, sources, chat_model):
sys_prompt = "You are a helpful assistant that always answers questions."
# Use OpenAI chat completions to generate a response
res = ____(
model=____,
messages=[
{"role": "system", "content": ____},
{"role": "user", "content": ____}
],
temperature=0
)
answer = res.choices[0].message.content.strip()
answer += "\n\nSources:"
for source in sources:
answer += "\n" + source[0] + ": " + source[1]
return answer
answer = question_answering(
prompt=prompt_with_context,
sources=sources,
chat_model='gpt-4o-mini')
print(answer)