This repository is deprecated and no longer actively maintained. It contains outdated code examples or practices that do not align with current MongoDB best practices. While the repository remains accessible for reference purposes, we strongly discourage its use in production environments. Users should be aware that this repository will not receive any further updates, bug fixes, or security patches. This code may expose you to security vulnerabilities, compatibility issues with current MongoDB versions, and potential performance problems. Any implementation based on this repository is at the user's own risk. For up-to-date resources, please refer to the MongoDB Developer Center.
This demo sets up a basic question-answering system using Azure OpenAI and LangChain. The system retrieves relevant documents and generates responses based on those documents.
- Azure Account: Access to Azure OpenAI service.
- Python Environment: Python installed with necessary libraries.
- LangChain Library: Install LangChain.
-
Create Azure OpenAI Resource:
- Go to the Azure portal and create an Azure OpenAI resource.
- Note the endpoint and API key.
-
Install Azure OpenAI Python SDK:
pip install openai
Step 2: Install LangChain
pip install langchain
Step 3: Create a Simple Document Store For this demo, you can use a simple list of documents.
documents = [
{"title": "Document 1", "text": "Azure OpenAI provides powerful language models."},
{"title": "Document 2", "text": "LangChain simplifies the integration of language models with data sources."},
{"title": "Document 3", "text": "Retrieval-Augmented Generation (RAG) enhances the capability of language models by using external data."}
]
Step 4: Implement the Retrieval Component
For simplicity, we'll implement a basic retrieval function that searches for the most relevant document.
def simple_retrieval(query, documents):
# Basic keyword search
results = [doc for doc in documents if query.lower() in doc['text'].lower()]
return results if results else documents
Step 5: Set Up Azure OpenAI Integration Use the OpenAI SDK to generate responses.
import openai
openai.api_key = "YOUR_AZURE_OPENAI_API_KEY"
openai.api_base = "YOUR_AZURE_OPENAI_ENDPOINT"
def generate_response(prompt):
response = openai.Completion.create(
engine="davinci-codex", # or another model available in your Azure OpenAI resource
prompt=prompt,
max_tokens=100
)
return response.choices[0].text.strip()
Step 6: Combine Retrieval and Generation Integrate the retrieval and generation steps.
def rag_query(query):
# Step 1: Retrieve relevant documents
retrieved_docs = simple_retrieval(query, documents)
# Step 2: Generate response using the retrieved documents
context = "\n".join([doc['text'] for doc in retrieved_docs])
prompt = f"Context: {context}\n\nQuestion: {query}\nAnswer:"
return generate_response(prompt)
query = "What is RAG?"
response = rag_query(query)
print(response)
Running the Demo:
Ensure all necessary libraries are installed. Replace placeholders with your actual Azure OpenAI API key and endpoint. Run the script to see the Retrieval-Augmented Generation in action.