We're building a RAG Chatbot powered by GPT-4 to answer questions about Allora Labs using content from our documentation, research papers, and codebase. The chatbot is accessible via Slack, Discord, and our documentation website.
Our Q&A chat project uses a Pinecone vector database called alloraproduction. Data is converted into 3072-dimensional vectors using the OpenAI text-embedding-3-large
model. A LangChain workflow connects this database with GPT-4. When a user submits a question via a FastAPI POST endpoint, the following happens:
- Receive Question: The user's question is received as plain text.
- Convert to Vector: The question is embedded into a vector.
- Generate Response: GPT-4, informed by context retrieved from Pinecone, generates a helpful answer.
This workflow effectively bridges the user's query with GPT-4 by gathering relevant background information from our stored context.
- Allora White Paper
- Merit-based Sortition in Decentralized Systems
- Optimizing Decentralized Online Learning for Supervised Regression and Classification Problems
- allora-chain
- allora-offchain-node
- coin prediction worker
- coin prediction reputer
- autogluon-prediction
To add new documentation, you must add additional data embeddings to the alloraproduction Pinecone database. Follow these steps:
-
Split & Vectorize:
Use the LangChain library (or another method that adheres to 3072 dimensions and usestext-embedding-3-large
) to split and vectorize your data. -
Store in Pinecone:
Ensure the vectorized data is stored in Pinecone. -
Automatic Update:
Any new embeddings are automatically included in responses. Verify insertion by searching for specific keys (e.g., an ID or source) in Pinecone.
Note: Adjust
chunk_size
andchunk_overlap
to control the granularity of your data representation.
Process Overview:
-
Load PDF:
UsesPyMuPDFLoader
to load a PDF from a local path. -
Split Document:
UsesRecursiveCharacterTextSplitter
to break the document into chunks. -
Generate Embeddings & Vector Store:
Creates text embeddings with OpenAI and stores them in a Pinecone vector store.
# insert local path of your pdf here
pdf_path = ""
curindex_name="alloraproduction"
loader = PyMuPDFLoader(pdf_path)
docs = loader.load() # This returns a list of Document objects
from langchain_text_splitters import RecursiveCharacterTextSplitter
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=250,
chunk_overlap=50
)
split_docs = text_splitter.split_documents(docs)
# set/get our server account APIs here
os.environ["OPENAI_API_KEY"] = ""
os.environ["PINECONE_API_KEY"] = ""
embeddings = OpenAIEmbeddings(model="text-embedding-3-large")
# load our server account API here
pc = Pinecone(api_key=os.getenv("PINECONE_API_KEY"))
index = pc.Index("alloraproduction")
vector_store = PineconeVectorStore.from_documents(
documents=split_docs,
embedding=embeddings,
index_name="alloraproduction"
)
-
Load Documents:
Clones a GitHub repository and filters for Markdown files usingGitLoader
. -
Split Documents:
Uses a language-aware splitter (RecursiveCharacterTextSplitter.from_language
) configured for Python to break the documents into manageable chunks. -
Generate Embeddings & Store Vectors:
Converts the document chunks into vectors using the OpenAItext-embedding-3-large
model and stores them in a Pinecone index.
import os
from langchain_community.document_loaders import GitLoader
from langchain_text_splitters import Language, RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
from langchain_pinecone import PineconeVectorStore
from dotenv import load_dotenv
from pinecone import Pinecone, ServerlessSpec
load_dotenv()
os.environ["PINECONE_API_KEY"] = ""
os.environ["OPENAI_API_KEY"] = ""
embeddings = OpenAIEmbeddings(model="text-embedding-3-large")
# 1. Clone and load Python files from repo
# change based on repo you want to copy from
clone_url = "https://github.com/allora-network/allora-offchain-node/"
# allora-offchain-node used as an example, change based on what repo you decide to vectorize
repo_path = "allora-offchain-node"
# change based on branch you want to copy from
branch = "dev"
loader = GitLoader(
clone_url=clone_url,
repo_path=repo_path,
branch=branch,
file_filter=lambda file_path: file_path.endswith(".md")
)
documents = loader.load()
# 2. Split documents using Python-aware splitter
python_splitter = RecursiveCharacterTextSplitter.from_language(
language=Language.PYTHON,
chunk_size=750,
chunk_overlap=200,
)
split_docs = python_splitter.split_documents(documents)
# 3. Initialize embeddings
embeddings = OpenAIEmbeddings(model="text-embedding-3-large")
# 4. Pinecone setup
index_name = "alloraproduction"
vector_dimension = 3072 # Dimension for text-embedding-3-large
# Initialize vector store with the new client
vector_db = PineconeVectorStore.from_documents(
documents=split_docs,
embedding=embeddings,
index_name=index_name,
pinecone_api_key=os.getenv("PINECONE_API_KEY") # Add this line
)
print("Vectorization complete. Documents stored in Pinecone.")
-
Define the Server URL:
Set the endpoint URL for your chatbot. -
Prepare the Request Payload:
Create a JSON object with your question. -
Send the Request:
POST the payload using Python'srequests
library. -
Handle the Response:
Print the chatbot's response and its sources.
import requests
# URL of the chatbot endpoint. Replace with your actual server URL.
url = "https://your-chatbot-endpoint.com/api/chat"
# The payload containing the message/question for the chatbot.
payload = {
"message": "What makes Allora's reward distribution different than others?"
}
try:
# Send a POST request to the server with the JSON payload.
response = requests.post(url, json=payload)
# Raise an error if the request was unsuccessful.
response.raise_for_status()
# Parse the JSON response.
data = response.json()
# Output the chatbot response and its sources.
print("Response:")
print("Message:", data.get("response"))
print("Sources:", data.get("sources"))
except requests.exceptions.HTTPError as http_err:
print(f"HTTP error occurred: {http_err}")
except Exception as err:
print(f"Other error occurred: {err}")
For this particular example, you should expect an output similar to:
Response:
Message: Allora's reward distribution is differentiated and based on a carefully designed incentive mechanism that aligns with the interests of the network and allows for continual learning and improvement.
Sources: ['/markdown_files4/pages/devs/reference/module-accounts.mdx', '/markdown_files4/pages/home/overview.mdx']
When new data is added, update this document to keep track of changes and ensure the knowledge context remains current.