Retrieval QA Chain
Overview
The Retrieval QA Chain in AnswerAI is a specialized chain designed to answer questions based on retrieved documents. Unlike the Conversational Retrival QA Chain, this chain focuses on providing accurate answers from a knowledge base without maintaining conversation history.
Key Differences from Conversational Chain
-
No Memory: The Retrieval QA Chain does not maintain memory of previous interactions. Each query is treated independently.
-
Document Retrieval: This chain actively retrieves relevant documents for each query, whereas the Conversational Retrival QA Chain relies on its conversation history.
-
Single-Turn Interactions: Optimized for standalone questions rather than multi-turn conversations.
-
Knowledge-Base Focused: Answers are derived from a specific knowledge base rather than general knowledge or conversation context.
Key Benefits
- Accuracy: Provides answers based on specific, retrieved information.
- Scalability: Can handle a large knowledge base efficiently.
- Consistency: Gives consistent answers to similar questions, regardless of conversation history.
- Flexibility: Can be easily updated with new information by updating the underlying vector store.
When to Use Retrieval QA Chain
Use this chain when:
- You need to answer questions based on a specific knowledge base or dataset.
- Conversation history is not important or relevant.
- You want to provide factual, consistent answers across different user sessions.
- Dealing with domain-specific queries that require accurate, up-to-date information.
How It Works
- Query Reception: The chain receives a question via the AnswerAI API.
- Document Retrieval: Relevant documents are fetched from the vector store.
- Context Preparation: Retrieved documents are prepared as context for the language model.
- Answer Generation: The language model generates an answer based on the question and retrieved context.
- Response Delivery: The answer is returned through the API.
Key Components
- Language Model: Processes the query and generates answers.
- Vector Store Retriever: Fetches relevant documents based on the query.
- Input Moderation (Optional): Ensures safe and appropriate inputs.
Using the Retrieval QA Chain with AnswerAI API
Basic Usage
import requests
API_URL = "http://localhost:3000/api/v1/prediction/<your-chatflowid>"
API_KEY = "your-api-key"
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
data = {
"question": "What are the main features of quantum computing?",
"overrideConfig": {
"vectorStoreRetriever": {
"type": "pinecone",
"indexName": "quantum-computing-index"
}
}
}
response = requests.post(API_URL, json=data, headers=headers)
print(response.json())
Adding Input Moderation
data = {
"question": "What are the main features of quantum computing?",
"overrideConfig": {
"vectorStoreRetriever": {
"type": "pinecone",
"indexName": "quantum-computing-index"
},
"inputModeration": [
{
"type": "toxicity",
"threshold": 0.8
}
]
}
}
response = requests.post(API_URL, json=data, headers=headers)
print(response.json())
Use Case Example: Technical Support System
import requests
API_URL = "http://localhost:3000/api/v1/prediction/<your-chatflowid>"
API_KEY = "your-api-key"
def technical_support(query):
data = {
"question": query,
"overrideConfig": {
"vectorStoreRetriever": {
"type": "pinecone",
"indexName": "technical-support-docs"
}
}
}
response = requests.post(API_URL, json=data, headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
})
return response.json()['text']
# Usage
print(technical_support("How do I reset my router?"))
print(technical_support("What are the system requirements for the latest software update?"))
Best Practices
- Quality of Vector Store: Ensure your vector store contains high-quality, relevant documents.
- Regular Updates: Keep your knowledge base up-to-date for accurate answers.
- Clear Queries: Encourage users to ask clear, specific questions for better retrieval.
- Fallback Mechanisms: Implement fallbacks for when the chain can't find relevant information.
- Monitoring: Regularly review the chain's performance and the relevance of retrieved documents.
Limitations and Considerations
- No Conversation Context: Unlike the Conversational Chain, this chain doesn't consider previous interactions.
- Retrieval Dependence: The quality of answers heavily depends on the quality and relevance of retrieved documents.
- Single-Query Focus: May not perform as well for complex queries that require multi-step reasoning.
By leveraging the Retrieval QA Chain in AnswerAI, you can create powerful question-answering systems that provide accurate, knowledge-based responses without the need for maintaining conversation history. This makes it ideal for scenarios where factual, consistent information retrieval is more important than contextual conversation.