Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- README.md +37 -12
- my_ollama.py +66 -0
- requirements.txt +98 -0
README.md
CHANGED
|
@@ -1,12 +1,37 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# RAG Implementation with Ollama and LangChain
|
| 2 |
+
|
| 3 |
+
This repository contains an implementation of the Retrieval-Augmented Generation (RAG) model using Open AI's LLaMA large language model and the LangChain library.
|
| 4 |
+
|
| 5 |
+
## Overview
|
| 6 |
+
|
| 7 |
+
The code instantiates an OLLAMA model, loads Wikipedia content, splits the text into manageable chunks, creates sentence embeddings with SentenceTransformers, and builds a vector store using Chroma. Finally, it creates a QA chain using the OLLAMA model and the vector store retriever.
|
| 8 |
+
|
| 9 |
+
## Usage
|
| 10 |
+
|
| 11 |
+
To use this code, simply run the Python script. The output will be the generated response to a given question.
|
| 12 |
+
|
| 13 |
+
## Example Question
|
| 14 |
+
|
| 15 |
+
The example question used in this implementation is: "What is Tsai's energy policy?"
|
| 16 |
+
|
| 17 |
+

|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
## Dependencies
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
LangChain
|
| 25 |
+
|
| 26 |
+
OLLAMA
|
| 27 |
+
|
| 28 |
+
SentenceTransformers
|
| 29 |
+
|
| 30 |
+
Chroma
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
## Notes
|
| 34 |
+
|
| 35 |
+
This is my first attempt at implementing RAG using OLLAMA and LangChain. While the code is functional, it may not be optimized for performance or scalability. Further improvements and testing are needed to ensure the model's reliability.
|
| 36 |
+
|
| 37 |
+
I hope this helps! Let me know if you have any questions or need further assistance.
|
my_ollama.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
This works.
|
| 3 |
+
|
| 4 |
+
reference: https://www.youtube.com/watch?v=CPgp8MhmGVY
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
# Imports
|
| 8 |
+
import gradio as gr
|
| 9 |
+
from langchain_community.llms import Ollama
|
| 10 |
+
from langchain_community.document_loaders import WebBaseLoader
|
| 11 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 12 |
+
|
| 13 |
+
# dont use this embeddings
|
| 14 |
+
# from langchain.embeddings import GPT4AllEmbeddings
|
| 15 |
+
# from langchain_community.embeddings import GPT4AllEmbeddings
|
| 16 |
+
|
| 17 |
+
# from langchain.embeddings import SentenceTransformerEmbeddings
|
| 18 |
+
from langchain_community.embeddings import SentenceTransformerEmbeddings
|
| 19 |
+
|
| 20 |
+
# from langchain.vectorstores import Chroma
|
| 21 |
+
from langchain_community.vectorstores import Chroma
|
| 22 |
+
|
| 23 |
+
from langchain.chains import RetrievalQA
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# Instantiate the Ollama model
|
| 27 |
+
ollama = Ollama(
|
| 28 |
+
# base_url="http://localhost:3000",
|
| 29 |
+
model="llama3"
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Load the webpage content using WebBaseLoader
|
| 33 |
+
loader = WebBaseLoader("https://en.wikipedia.org/wiki/Tsai_Ing-wen")
|
| 34 |
+
data = loader.load()
|
| 35 |
+
|
| 36 |
+
# Split the text into manageable chunks using RecursiveCharacterTextSplitter
|
| 37 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=0)
|
| 38 |
+
all_splits = text_splitter.split_documents(data)
|
| 39 |
+
|
| 40 |
+
# Instantiate SentenceTransformers embeddings with the specified model
|
| 41 |
+
sentence_embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 42 |
+
|
| 43 |
+
# Create the vector store from the document splits and embeddings
|
| 44 |
+
vectorstore = Chroma.from_documents(documents=all_splits, embedding=sentence_embeddings)
|
| 45 |
+
|
| 46 |
+
# Create a QA chain using the Ollama model and the vector store retriever
|
| 47 |
+
qachain = RetrievalQA.from_chain_type(ollama, retriever=vectorstore.as_retriever())
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# Function to handle the question and return the answer
|
| 51 |
+
def answer_question(question):
|
| 52 |
+
response = qachain({"query": question})
|
| 53 |
+
return response["result"]
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
# Create a Gradio interface
|
| 57 |
+
interface = gr.Interface(
|
| 58 |
+
fn=answer_question,
|
| 59 |
+
inputs="text",
|
| 60 |
+
outputs="text",
|
| 61 |
+
title="RAG Model QA Interface",
|
| 62 |
+
description="Ask a question about Tsai Ing-wen and get an answer from the Retrieval-Augmented Generation model.",
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
# Launch the Gradio interface
|
| 66 |
+
interface.launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
aiofiles==23.2.1
|
| 2 |
+
aiohttp==3.9.5
|
| 3 |
+
aiosignal==1.3.1
|
| 4 |
+
altair==5.3.0
|
| 5 |
+
annotated-types==0.7.0
|
| 6 |
+
anyio==4.4.0
|
| 7 |
+
attrs==23.2.0
|
| 8 |
+
certifi==2024.6.2
|
| 9 |
+
charset-normalizer==3.3.2
|
| 10 |
+
click==8.1.7
|
| 11 |
+
contourpy==1.2.1
|
| 12 |
+
cycler==0.12.1
|
| 13 |
+
dataclasses-json==0.6.7
|
| 14 |
+
distro==1.9.0
|
| 15 |
+
dnspython==2.6.1
|
| 16 |
+
email_validator==2.2.0
|
| 17 |
+
fastapi==0.111.0
|
| 18 |
+
fastapi-cli==0.0.4
|
| 19 |
+
ffmpy==0.3.2
|
| 20 |
+
filelock==3.15.4
|
| 21 |
+
fonttools==4.53.0
|
| 22 |
+
frozenlist==1.4.1
|
| 23 |
+
fsspec==2024.6.0
|
| 24 |
+
gradio==4.36.1
|
| 25 |
+
gradio_client==1.0.1
|
| 26 |
+
h11==0.14.0
|
| 27 |
+
httpcore==1.0.5
|
| 28 |
+
httptools==0.6.1
|
| 29 |
+
httpx==0.27.0
|
| 30 |
+
huggingface-hub==0.23.4
|
| 31 |
+
idna==3.7
|
| 32 |
+
importlib_resources==6.4.0
|
| 33 |
+
Jinja2==3.1.4
|
| 34 |
+
jsonpatch==1.33
|
| 35 |
+
jsonpointer==3.0.0
|
| 36 |
+
jsonschema==4.22.0
|
| 37 |
+
jsonschema-specifications==2023.12.1
|
| 38 |
+
kiwisolver==1.4.5
|
| 39 |
+
langchain==0.2.5
|
| 40 |
+
langchain-community==0.2.5
|
| 41 |
+
langchain-core==0.2.9
|
| 42 |
+
langchain-text-splitters==0.2.1
|
| 43 |
+
langsmith==0.1.82
|
| 44 |
+
markdown-it-py==3.0.0
|
| 45 |
+
MarkupSafe==2.1.5
|
| 46 |
+
marshmallow==3.21.3
|
| 47 |
+
matplotlib==3.9.0
|
| 48 |
+
mdurl==0.1.2
|
| 49 |
+
multidict==6.0.5
|
| 50 |
+
mypy-extensions==1.0.0
|
| 51 |
+
numpy==1.26.4
|
| 52 |
+
openai==1.35.3
|
| 53 |
+
orjson==3.10.5
|
| 54 |
+
packaging==24.1
|
| 55 |
+
pandas==2.2.2
|
| 56 |
+
pillow==10.3.0
|
| 57 |
+
pydantic==2.7.4
|
| 58 |
+
pydantic_core==2.18.4
|
| 59 |
+
pydub==0.25.1
|
| 60 |
+
Pygments==2.18.0
|
| 61 |
+
pyparsing==3.1.2
|
| 62 |
+
python-dateutil==2.9.0.post0
|
| 63 |
+
python-dotenv==1.0.1
|
| 64 |
+
python-multipart==0.0.9
|
| 65 |
+
pytz==2024.1
|
| 66 |
+
PyYAML==6.0.1
|
| 67 |
+
referencing==0.35.1
|
| 68 |
+
regex==2024.5.15
|
| 69 |
+
requests==2.32.3
|
| 70 |
+
rich==13.7.1
|
| 71 |
+
rpds-py==0.18.1
|
| 72 |
+
ruff==0.4.10
|
| 73 |
+
safetensors==0.4.3
|
| 74 |
+
semantic-version==2.10.0
|
| 75 |
+
setuptools==69.5.1
|
| 76 |
+
shellingham==1.5.4
|
| 77 |
+
six==1.16.0
|
| 78 |
+
sniffio==1.3.1
|
| 79 |
+
SQLAlchemy==2.0.31
|
| 80 |
+
starlette==0.37.2
|
| 81 |
+
tenacity==8.4.2
|
| 82 |
+
tokenizers==0.19.1
|
| 83 |
+
tomlkit==0.12.0
|
| 84 |
+
toolz==0.12.1
|
| 85 |
+
tqdm==4.66.4
|
| 86 |
+
transformers==4.41.2
|
| 87 |
+
typer==0.12.3
|
| 88 |
+
typing-inspect==0.9.0
|
| 89 |
+
typing_extensions==4.12.2
|
| 90 |
+
tzdata==2024.1
|
| 91 |
+
ujson==5.10.0
|
| 92 |
+
urllib3==2.2.2
|
| 93 |
+
uvicorn==0.30.1
|
| 94 |
+
uvloop==0.19.0
|
| 95 |
+
watchfiles==0.22.0
|
| 96 |
+
websockets==11.0.3
|
| 97 |
+
wheel==0.43.0
|
| 98 |
+
yarl==1.9.4
|