-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
793 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Minimal initialization for the package | ||
# You might leave this empty, or you could dynamically load modules here | ||
# but don't execute any logic. | ||
|
||
# For example, to dynamically load modules you could: | ||
import os | ||
import importlib | ||
|
||
module_dir = os.path.dirname(__file__) | ||
|
||
for filename in os.listdir(module_dir): | ||
if filename.endswith('.py') and filename != '__init__.py': | ||
module_name = filename[:-3] | ||
importlib.import_module(f'.{module_name}', package=__name__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from langchain_community.llms import Ollama | ||
from langchain_community.document_loaders import WebBaseLoader | ||
from langchain_community.embeddings import OllamaEmbeddings | ||
from langchain.text_splitter import RecursiveCharacterTextSplitter | ||
from langchain_chroma import Chroma | ||
from operator import itemgetter | ||
from langchain import hub | ||
from langchain_core.output_parsers import StrOutputParser | ||
from langchain_core.runnables import RunnablePassthrough, RunnableParallel, RunnableLambda | ||
from langchain.retrievers import MergerRetriever | ||
from langchain.retrievers.document_compressors import DocumentCompressorPipeline | ||
|
||
def rag_pipeline(): | ||
try: | ||
def format_docs(docs): | ||
return "\\n".join(doc.page_content for doc in docs) | ||
|
||
llm = Ollama(model='llama3.1:latest',base_url='http://localhost:11434') | ||
|
||
loader = WebBaseLoader('https://ashwinaravind.github.io/') | ||
docs = loader.load() | ||
|
||
embedding = OllamaEmbeddings(model='mxbai-embed-large:latest',base_url='http://localhost:11434') | ||
|
||
splitter = RecursiveCharacterTextSplitter(chunk_size=1600, chunk_overlap=200) | ||
splits=splitter.split_documents(docs) | ||
c=Chroma.from_documents(documents=splits, embedding=embedding, collection_name='testindex-ragbuilder',) | ||
retrievers=[] | ||
retriever=c.as_retriever(search_type='similarity', search_kwargs={'k': 5}) | ||
retrievers.append(retriever) | ||
retriever=MergerRetriever(retrievers=retrievers) | ||
prompt = hub.pull("rlm/rag-prompt") | ||
rag_chain = ( | ||
RunnableParallel(context=retriever, question=RunnablePassthrough()) | ||
.assign(context=itemgetter("context") | RunnableLambda(format_docs)) | ||
.assign(answer=prompt | llm | StrOutputParser()) | ||
.pick(["answer", "context"])) | ||
return rag_chain | ||
except Exception as e: | ||
print(f"An error occurred: {e}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from langchain_community.llms import Ollama | ||
from langchain_community.document_loaders import WebBaseLoader | ||
from langchain_community.embeddings import OllamaEmbeddings | ||
from langchain.text_splitter import RecursiveCharacterTextSplitter | ||
from langchain_chroma import Chroma | ||
from operator import itemgetter | ||
from langchain import hub | ||
from langchain_core.output_parsers import StrOutputParser | ||
from langchain_core.runnables import RunnablePassthrough, RunnableParallel, RunnableLambda | ||
from langchain.retrievers import MergerRetriever | ||
from langchain.retrievers.document_compressors import DocumentCompressorPipeline | ||
|
||
def rag_pipeline(): | ||
try: | ||
def format_docs(docs): | ||
return "\\n".join(doc.page_content for doc in docs) | ||
|
||
llm = Ollama(model='llama3.1:latest',base_url='http://localhost:11434') | ||
|
||
loader = WebBaseLoader('https://ashwinaravind.github.io/') | ||
docs = loader.load() | ||
|
||
embedding = OllamaEmbeddings(model='mxbai-embed-large:latest',base_url='http://localhost:11434') | ||
|
||
splitter = RecursiveCharacterTextSplitter(chunk_size=1600, chunk_overlap=200) | ||
splits=splitter.split_documents(docs) | ||
c=Chroma.from_documents(documents=splits, embedding=embedding, collection_name='testindex-ragbuilder',) | ||
retrievers=[] | ||
retriever=c.as_retriever(search_type='similarity', search_kwargs={'k': 5}) | ||
retrievers.append(retriever) | ||
retriever=MergerRetriever(retrievers=retrievers) | ||
prompt = hub.pull("rlm/rag-prompt") | ||
rag_chain = ( | ||
RunnableParallel(context=retriever, question=RunnablePassthrough()) | ||
.assign(context=itemgetter("context") | RunnableLambda(format_docs)) | ||
.assign(answer=prompt | llm | StrOutputParser()) | ||
.pick(["answer", "context"])) | ||
return rag_chain | ||
except Exception as e: | ||
print(f"An error occurred: {e}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import os | ||
import importlib | ||
|
||
# Get the directory of this file | ||
module_dir = os.path.dirname(__file__) | ||
|
||
# Track whether any modules are found and imported | ||
modules_found = False | ||
|
||
# Iterate over all files in the directory | ||
for filename in os.listdir(module_dir): | ||
# Only consider .py files and skip __init__.py itself | ||
if filename.endswith('.py') and filename != '__init__.py': | ||
module_name = filename[:-3] # Strip .py extension | ||
# Dynamically import the module | ||
importlib.import_module(f'.{module_name}', package=__name__) | ||
modules_found = True # Mark that at least one module was found | ||
|
||
# If no modules were found, print a message or handle as needed | ||
if not modules_found: | ||
print("No Python modules found in the 'byor' folder.") |
Oops, something went wrong.