← all conversations

Local Knowledge Base Setup

2025-02-232 turns3,735 charsgpt-4o-mini
local-llmknowledge-baserag

Summary

User wants to set up a local knowledge base using their own documents and video transcripts with a local LLM alternative to GPT agents.

Messages

write basic code for this: In your experience what’s the best local alternative to gpt agents? Question | Help I wanted to setup a small local model with the ability to use my own documents/video transcripts to build up a knowledge base to initially rely on before browsing the web, or to use as general guidelines to what type of output I may need, what would be the best way to accomplish this in a local environment as opposed to setting up a custom gpt? Upvote 1 Downvote 0 Go to comments Share Share You could use Ollama, pick whichever model you want or that will run on your local machine, then use RAG with a ChromaDB or something similar to create embeddings of the knowledge base, then you could use something like SmolAgents to orchestrate and use their DuckDuckGo web client to perform web searches, etc. Comment
Here's some basic code to get you started on setting up a local environment using Ollama, ChromaDB, and SmolAgents to create a knowledge base and perform web searches with a DuckDuckGo web client. This assumes you have Ollama, ChromaDB, and SmolAgents installed. 1. **Set up ChromaDB for storing embeddings**: ```python import chromadb from chromadb.config import Settings # Set up ChromaDB client = chromadb.Client(Settings(chroma_db_impl="duckdb+parquet", persist_directory="./chromadb")) # Add your documents or video transcripts as embeddings def add_document_to_knowledge_base(doc_id, doc_text): # Here we assume you have a way to generate embeddings for the text embeddings = generate_embeddings(doc_text) # Replace with your embedding generation function client.upsert([{"id": doc_id, "embedding": embeddings, "metadata": {"text": doc_text}}]) # Example usage add_document_to_knowledge_base("doc1", "This is the first document text.") ``` 2. **Use Ollama for local inference**: Assuming you've already set up Ollama, use the model to generate responses based on the knowledge base: ```python import ollama # Load your local model model = ollama.open("gpt-3.5") # Query your knowledge base and use the model for reasoning def query_with_model(query): # First, get relevant information from the knowledge base relevant_info = retrieve_relevant_information(query) # You can use ChromaDB for this # Now use Ollama to generate an answer based on that information prompt = f"Given this information: {relevant_info}, answer the following question: {query}" response = model.chat([{"role": "user", "content": prompt}]) return response["text"] def retrieve_relevant_information(query): # Example function to retrieve relevant docs from the knowledge base results = client.query(query, n_results=3) # Adjust n_results based on your needs return " ".join([result["metadata"]["text"] for result in results["documents"]]) # Example query response = query_with_model("What is the best way to set up a local knowledge base?") print(response) ``` 3. **Integrating SmolAgents for orchestration**: ```python from smolagents import Agent, Task, Context # Set up SmolAgent to orchestrate the process agent = Agent() # Define a task to retrieve info and query the model def task_function(context: Context): query = context.get("query") response = query_with_model(query) context.set("response", response) # Add a task to the agent agent.add_task(Task(task_function, {"query": "What is the best way to set up a local knowledge base?"})) # Run the agent and get the response agent.run() ``` This setup provides a local solution where you can build a knowledge base from your documents and video transcripts, then use Ollama for inference and SmolAgents for orchestration. You can also extend this to perform web searches if necessary.
gpt-4o-mini