Using Langchain with Ollama and Python
AI Summary
- Goal: Interact with documents to extract answers using embeddings.
- Tools: Use embeddings with LLMs to gain insights from notes, PDFs, etc.
- Platform: Olama, a tool for working with LLMs on laptops.
- Example: Demonstrates using Python and Lang chain to query a Wikipedia article.
- Setup:
- Install Lang chain:
pip install langchain
- Import Olama:
from langchain.lms import olama
- Initialize Olama:
olama = Olama(base_url='http://localhost:11434', model='llama2')
- Test Olama:
print(olama('Why is the sky blue?'))
- Document Loading:
- Install BeautifulSoup:
pip install bs4
- Import web-based loader:
from langchain.document_loaders import web-based loader
- Load document:
loader = web-based loader(url)
,data = loader.load()
- Document Chunking:
- Use Vector database (e.g., Chroma DB) to handle document chunks.
- Install text splitter:
from langchain.text_splitter import recursive_character_text_splitter
- Configure text splitter:
text_splitter = recursive_character_text_splitter(chunk_size=500, overlap=0)
- Split document:
all_splits = text_splitter.split(data)
- Vector Storage:
- Convert chunks to vectors using embeddings.
- Install GPT and Chroma DB:
pip install GPT-for-all chromodb
- Import embeddings and database:
from langchain.embeddings import GPT_for_all_embeddings
,from langchain.vector_stores import chroma
- Create Vector store:
vector_store = chroma.from_documents(all_splits, GPT_for_all_embeddings)
- Querying:
- Use Lang chain’s retrieval QA Chain.
- Import retrieval QA:
from langchain.chains import retrieval QA
- Initialize QA chain:
QA_chain = retrieval QA(olama, vector_store as retriever)
- Ask a question:
QA_chain.query('When was Hawaii's request for major disaster declaration approved?')
- Optimization:
- Adjust text chunk overlap for better results.
- Usage Tips:
- Use different loaders for multiple documents.
- Maintain data store between queries to avoid re-importing.
- Future Content: Upcoming video on JavaScript implementation.