{"id":2072,"date":"2025-05-27T18:05:08","date_gmt":"2025-05-27T10:05:08","guid":{"rendered":"https:\/\/www.fanyamin.com\/wordpress\/?p=2072"},"modified":"2025-05-27T18:05:08","modified_gmt":"2025-05-27T10:05:08","slug":"build-local-knowledge-base-2","status":"publish","type":"post","link":"https:\/\/www.fanyamin.com\/wordpress\/?p=2072","title":{"rendered":"build local knowledge base 2"},"content":{"rendered":"<h1>Building a Local Knowledge Base and Agent with LLM<\/h1>\n<p>Here's a detailed guide on how to create a local knowledge base from your files and build an agent that can interact with this knowledge.<\/p>\n<h2>Part 1: Building a Local Knowledge Base<\/h2>\n<h3>Step 1: Set Up Your Environment<\/h3>\n<pre><code class=\"language-bash\"># Create a virtual environment\npython -m venv llm_kb_env\nsource llm_kb_env\/bin\/activate  # On Windows: llm_kb_env\\Scripts\\activate\n\n# Install necessary packages\npip install langchain langchain_community pypdf python-docx sentence-transformers chromadb\npip install llama-index openai faiss-cpu tiktoken<\/code><\/pre>\n<h3>Step 2: Organize Your Documents<\/h3>\n<ol>\n<li>Create a directory for your document collection\n<pre><code class=\"language-bash\">mkdir -p data\/documents<\/code><\/pre>\n<\/li>\n<li>Place your files (code, txt, pdf, etc.) in this directory<\/li>\n<\/ol>\n<h3>Step 3: Document Loading and Processing<\/h3>\n<pre><code class=\"language-python\">from langchain.document_loaders import (\n    TextLoader,\n    PyPDFLoader,\n    DirectoryLoader,\n    UnstructuredMarkdownLoader,\n    CSVLoader\n)\nfrom langchain.text_splitter import RecursiveCharacterTextSplitter\n\n# Configure loaders for different file types\nloaders = {\n    &quot;txt&quot;: DirectoryLoader(&quot;data\/documents\/&quot;, glob=&quot;**\/*.txt&quot;, loader_cls=TextLoader),\n    &quot;pdf&quot;: DirectoryLoader(&quot;data\/documents\/&quot;, glob=&quot;**\/*.pdf&quot;, loader_cls=PyPDFLoader),\n    &quot;md&quot;: DirectoryLoader(&quot;data\/documents\/&quot;, glob=&quot;**\/*.md&quot;, loader_cls=UnstructuredMarkdownLoader),\n    &quot;csv&quot;: DirectoryLoader(&quot;data\/documents\/&quot;, glob=&quot;**\/*.csv&quot;, loader_cls=CSVLoader),\n    # Add more file types as needed\n}\n\n# Load documents\ndocuments = []\nfor loader_type, loader in loaders.items():\n    try:\n        documents.extend(loader.load())\n        print(f&quot;Loaded {loader_type} documents&quot;)\n    except Exception as e:\n        print(f&quot;Error loading {loader_type} documents: {e}&quot;)\n\n# Split documents into chunks\ntext_splitter = RecursiveCharacterTextSplitter(\n    chunk_size=1000,\n    chunk_overlap=200,\n    length_function=len\n)\nchunks = text_splitter.split_documents(documents)\nprint(f&quot;Split into {len(chunks)} chunks&quot;)<\/code><\/pre>\n<h3>Step 4: Create Vector Embeddings<\/h3>\n<pre><code class=\"language-python\">from langchain.embeddings import HuggingFaceEmbeddings\nfrom langchain.vectorstores import Chroma, FAISS\n\n# Initialize embeddings\nembedding_model = HuggingFaceEmbeddings(model_name=&quot;all-MiniLM-L6-v2&quot;)\n\n# Option 1: Create a ChromaDB vector store\nvectorstore = Chroma.from_documents(\n    documents=chunks,\n    embedding=embedding_model,\n    persist_directory=&quot;data\/chroma_db&quot;\n)\nvectorstore.persist()  # Save to disk\n\n# Option 2: Create a FAISS vector store (in-memory, can be saved)\nfaiss_index = FAISS.from_documents(chunks, embedding_model)\nfaiss_index.save_local(&quot;data\/faiss_index&quot;)<\/code><\/pre>\n<h2>Part 2: Building an Agent Based on the Knowledge Base<\/h2>\n<h3>Step 5: Set Up Retrieval System<\/h3>\n<pre><code class=\"language-python\">from langchain.retrievers import ContextualCompressionRetriever\nfrom langchain.retrievers.document_compressors import LLMChainExtractor\nfrom langchain.chat_models import ChatOpenAI\nfrom langchain.chains import RetrievalQA\n\n# Load your vectorstore\nvectorstore = Chroma(persist_directory=&quot;data\/chroma_db&quot;, embedding_function=embedding_model)\n\n# Create a retriever\nretriever = vectorstore.as_retriever(search_type=&quot;similarity&quot;, search_kwargs={&quot;k&quot;: 5})\n\n# Optional: Add contextual compression for better results\nllm = ChatOpenAI(temperature=0)  # You can use local models too\ncompressor = LLMChainExtractor.from_llm(llm)\ncompression_retriever = ContextualCompressionRetriever(\n    base_compressor=compressor,\n    base_retriever=retriever\n)\n\n# Create a question-answering chain\nqa_chain = RetrievalQA.from_chain_type(\n    llm=llm,\n    chain_type=&quot;stuff&quot;,  # Other options: &quot;map_reduce&quot;, &quot;refine&quot;\n    retriever=retriever,\n    return_source_documents=True,\n    verbose=True\n)<\/code><\/pre>\n<h3>Step 6: Create an Agent with Tools<\/h3>\n<pre><code class=\"language-python\">from langchain.agents import Tool, initialize_agent, AgentType\nfrom langchain.memory import ConversationBufferMemory\n\n# Define a search tool using our QA chain\nsearch_tool = Tool(\n    name=&quot;Document Search&quot;,\n    func=qa_chain.run,\n    description=&quot;Useful for searching information in your documents. Input should be a question.&quot;\n)\n\n# Optional: Add a calculation tool\nfrom langchain.tools import Tool\nfrom langchain.chains import LLMMathChain\n\nllm_math_chain = LLMMathChain.from_llm(llm=llm, verbose=True)\nmath_tool = Tool(\n    name=&quot;Calculator&quot;,\n    func=llm_math_chain.run,\n    description=&quot;Useful for performing calculations. Input should be a mathematical expression.&quot;\n)\n\n# Create a memory system\nmemory = ConversationBufferMemory(memory_key=&quot;chat_history&quot;, return_messages=True)\n\n# Initialize the agent\nagent = initialize_agent(\n    tools=[search_tool, math_tool],\n    llm=llm,\n    agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION,\n    verbose=True,\n    memory=memory\n)<\/code><\/pre>\n<h3>Step 7: Create a Simple Interface<\/h3>\n<pre><code class=\"language-python\">def query_agent(query):\n    try:\n        response = agent.run(input=query)\n        return response\n    except Exception as e:\n        return f&quot;Error: {str(e)}&quot;\n\n# Interactive loop\nif __name__ == &quot;__main__&quot;:\n    print(&quot;Welcome to your document assistant! Type &#039;exit&#039; to quit.&quot;)\n    while True:\n        query = input(&quot;Question: &quot;)\n        if query.lower() == &#039;exit&#039;:\n            break\n        response = query_agent(query)\n        print(f&quot;Answer: {response}&quot;)<\/code><\/pre>\n<h2>Part 3: Using Local LLMs (Optional)<\/h2>\n<p>If you want to keep everything local, replace the OpenAI model with a local model:<\/p>\n<pre><code class=\"language-python\">from langchain.llms import HuggingFacePipeline\nimport torch\nfrom transformers import AutoTokenizer, AutoModelForCausalLM, pipeline\n\n# Load local model\nmodel_id = &quot;TheBloke\/Llama-2-7B-Chat-GGUF&quot;  # Choose an appropriate model\ntokenizer = AutoTokenizer.from_pretrained(model_id)\nmodel = AutoModelForCausalLM.from_pretrained(\n    model_id, \n    device_map=&quot;auto&quot;,\n    torch_dtype=torch.float16,\n    load_in_8bit=True  # For memory efficiency\n)\n\n# Create a pipeline\npipe = pipeline(\n    &quot;text-generation&quot;,\n    model=model,\n    tokenizer=tokenizer,\n    max_length=2048,\n    temperature=0.3,\n    top_p=0.95,\n    repetition_penalty=1.15\n)\n\n# Create LLM\nlocal_llm = HuggingFacePipeline(pipeline=pipe)\n\n# Replace the OpenAI model with your local model in the QA chain and agent<\/code><\/pre>\n<h2>Part 4: Advanced Features<\/h2>\n<h3>Adding a Web Search Tool<\/h3>\n<pre><code class=\"language-python\">from langchain.utilities import GoogleSearchAPIWrapper\n\nsearch = GoogleSearchAPIWrapper()\nweb_search_tool = Tool(\n    name=&quot;Web Search&quot;,\n    description=&quot;Search Google for recent or factual information.&quot;,\n    func=search.run\n)\n\n# Add to your agent&#039;s tools list<\/code><\/pre>\n<h3>Metadata Filtering<\/h3>\n<pre><code class=\"language-python\"># When querying the vectorstore\nmetadata_filter = {&quot;source&quot;: &quot;important_document.pdf&quot;}\nfiltered_docs = vectorstore.similarity_search(\n    &quot;your query&quot;, \n    k=5, \n    filter=metadata_filter\n)<\/code><\/pre>\n<h3>Evaluation<\/h3>\n<pre><code class=\"language-python\">from langchain.evaluation.qa import QAEvalChain\n\n# Create some examples\nexamples = [\n    {&quot;query&quot;: &quot;What is X?&quot;, &quot;answer&quot;: &quot;X is Y according to the documents.&quot;}\n    # Add more examples\n]\n\n# Create an evaluation chain\neval_chain = QAEvalChain.from_llm(llm)\ngraded_outputs = eval_chain.evaluate(examples, [qa_chain.run(eg[&quot;query&quot;]) for eg in examples])<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Building a Local Knowledge Base and Agent with LLM Here&#8217;s a detailed guide on how to create a local knowledge base from your files and build an agent that can interact with this knowledge. Part 1: Building a Local Knowledge Base Step 1: Set Up Your Environment # Create a virtual environment python -m venv [&hellip;] <a class=\"read-more\" href=\"https:\/\/www.fanyamin.com\/wordpress\/?p=2072\" title=\"Permanent Link to: build local knowledge base 2\">&rarr;Read&nbsp;more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-2072","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2072"}],"collection":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2072"}],"version-history":[{"count":1,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2072\/revisions"}],"predecessor-version":[{"id":2073,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2072\/revisions\/2073"}],"wp:attachment":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}