This is a simple Retrieval-Augmented Generation (RAG) web application that allows you to query a contacts database using natural language. It uses Ollama for local AI models to classify queries, generate embeddings, and provide answers based on relevant contact information.
- Node.js installed
- Ollama installed and running locally
- Ollama models:
llama3(for chat and classification)mxbai-embed-large(for generating embeddings)
First, ensure Ollama is installed and running. Then pull the required models:
ollama pull llama3
ollama pull mxbai-embed-largeThe application provides a web interface where you can ask questions about contacts in natural language. For example:
- "What's John's phone number?"
- "Give me the email for the person named Sarah"
The system:
- Classifies whether the query is about contact information
- If it is, generates an embedding for the query
- Finds the most similar contact using cosine similarity
- Uses the relevant contact information as context for the LLM to generate an answer
- If it's not a contact query, answers directly using the LLM
Before running the application, you need to build embeddings for your contacts database.
- Ensure you have a
contacts.jsonfile with your contact data. The format should be an array of objects like:
[
{
"name": "John Doe",
"phone": "123-456-7890",
"email": "john@example.com",
"address": "123 Main St, Anytown, USA"
}
]- Run the embedding builder:
node build_embeddings.jsThis will:
- Read the contacts from
contacts.json - Generate embeddings for each contact using the
mxbai-embed-largemodel - Save the contacts with their embeddings to
contacts_with_embeddings.json
- Start the Node.js server:
node server.jsThe server will run on http://localhost:8000 by default.
-
Open your browser and navigate to
http://localhost:8000 -
Type your query in the input field and click Send or press Enter
- Port: Set the
PORTenvironment variable to change the server port (default: 8000) - Ollama URL: Set the
OLLAMA_URLenvironment variable to point to a different Ollama instance (default: http://localhost:11434)
Example:
PORT=3000 OLLAMA_URL=http://localhost:11434 node server.jsindex.html— The main web pagestyles.css— CSS styles for the interfacemain.js— Frontend JavaScript for handling user input and displaying responsesserver.js— Node.js server that serves static files and proxies requests to Ollamabuild_embeddings.js— Script to generate embeddings for contactscontacts.json— Your contacts database (input)
- This server is intended for local development only
- It sets permissive CORS headers for proxied responses
- Do not expose this server on the public internet without proper security measures