This project implements a Retrieval-Augmented Generation (RAG) model for a Question Answering (QA) bot, utilizing Pinecone as the vector database and Cohere's API for text generation. The QA bot retrieves relevant information from a dataset and generates coherent answers.
The goal of this project is to develop a QA bot that combines retrieval-based and generative approaches. By leveraging Pinecone's vector database, the bot efficiently retrieves relevant documents, and using Cohere's language models, it generates accurate and contextually relevant answers.
-
Install Required Libraries:
pip install pinecone-client sentence-transformers cohere
-
Initialize Pinecone:
from pinecone import Pinecone pc = Pinecone(api_key="your_pinecone_api_key") index = pc.Index("quickstart")
-
Load the Embedding Model:
from sentence_transformers import SentenceTransformer model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
-
Initialize Cohere API:
import cohere cohere_client = cohere.Client("your_cohere_api_key")
Prepare your dataset by compiling documents that the QA bot will reference. For each document, generate embeddings using the loaded model:
documents = [
"Pinecone is a vector database used for fast and scalable machine learning applications.",
"Cohere is a platform that provides NLP models for various use cases like text generation.",
"The capital of France is Paris."
]
embeddings = model.encode(documents)No additional training is required for the pre-built models used in this project. Ensure that the models are properly loaded and the embeddings are correctly generated.
Upsert the generated embeddings into the Pinecone index:
for i, embedding in enumerate(embeddings):
index.upsert(vectors=[(f"doc_{i}", embedding.tolist())])Define the QA function that retrieves relevant documents and generates answers:
def qa_bot(query):
query_embedding = model.encode([query])[0]
results = index.query(
vector=query_embedding.tolist(),
top_k=5,
include_values=True,
include_metadata=True
)
retrieved_docs = [result['id'] for result in results['matches']]
answer = generate_answer(retrieved_docs, query)
return answerImplement the generate_answer function using Cohere's API:
def generate_answer(retrieved_docs, query):
context = " ".join(retrieved_docs)
response = cohere_client.generate(
prompt=f"{context}\n\nQuestion: {query}\nAnswer:",
max_tokens=50
)
return response.generations[0].text.strip()query = "What is Pinecone?"
answer = qa_bot(query)
print(f"Question: {query}\nAnswer: {answer}")Output:
Question: What is Pinecone?
Answer: Pinecone is a vector database used for fast and scalable machine learning applications.
Contributions are welcome! Please open an issue or submit a pull request with your proposed changes.
This project is licensed under the MIT License.