-
-
Notifications
You must be signed in to change notification settings - Fork 629
Open
Description
The custom pipeline's init.py is the following:
try:
import os
from pydantic import BaseModel
from typing import List, Optional, Union, Iterator, Generator
from openai import OpenAI
from langchain_community.document_loaders import TextLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
from langchain.chains import RetrievalQA
#from langchain.llms import OpenAI
from langchain.llms import HuggingFaceHub
from langchain.embeddings import HuggingFaceEmbeddings
from langchain_community.llms import OpenAI
from langchain_core.prompts import PromptTemplate
import time
from pydantic import BaseModel, Field
from typing import List, Optional, Union, Iterator, Generator, Literal
from blueprints.function_calling_blueprint import Pipeline as Blueprint
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
print("All necessary libraries imported successfully.")
except ImportError as e:
# This will log the exact import error
print(f"Failed to import a library: {e}")
raise
class FAISSRAG(object):
"""
A class to handle the RAG process using FAISS for document indexing and retrieval.
"""
def __init__(self):
self.name="faiss-rag-model"
"""
Initializes the FAISS RAG instance by loading documents and creating the vector store.
"""
try:
print("initiating FAISS")
#self.embeddings = OpenAIEmbeddings(
# model="text-embedding-3-large"
#)
self.embeddings =HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
script_dir = os.path.dirname(os.path.abspath(__file__))
docs_path = os.path.join(script_dir, 'docs')
print(f"Looking for docs at: {docs_path}")
self.docs = []
# Check if the docs directory exists and is not empty
if not os.path.exists(docs_path) or not os.listdir(docs_path):
print("Docs directory is empty or does not exist. Please add a .txt file.")
# You can either raise an error or return here to stop the initialization.
raise FileNotFoundError(f"No .txt files found in {docs_path}")
for file in os.listdir(docs_path):
if file.endswith(".txt"):
file_path = os.path.join(docs_path, file)
loader = TextLoader(file_path)
self.docs.extend(loader.load())
print(f"Loaded {len(self.docs)} documents.")
# Split documents into smaller chunks
self.text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
self.texts = self.text_splitter.split_documents(self.docs)
# Create the FAISS vector store and retriever
self.vectorstore = FAISS.from_documents(self.texts, self.embeddings)
self.retriever = self.vectorstore.as_retriever()
self.qa_chain = RetrievalQA.from_chain_type(
#llm=OpenAI(model_name="gpt-3.5-turbo", temperature=0),
llm = HuggingFaceHub(repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",model_kwargs={"temperature": 0.5, "max_length": 1024},huggingfacehub_api_token="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",task="text-generation"),
retriever=self.retriever,
chain_type="stuff"
)
except Exception as e:
print(f"Error during FAISSRAG initialization: {e}")
raise e
def get_relevant_documents(self, query: str) -> List[str]:
"""
Retrieves relevant documents from the vector store based on a query.
"""
try:
docs = self.retriever.get_relevant_documents(query)
return [doc.page_content for doc in docs]
except Exception as e:
print(f"Error getting documents from FAISS: {e}")
return []
def generate_completion(self, messages: List[dict], model_id: str, stream: bool = False) -> str:
# Use a streaming-capable LLM
#llm = ChatOpenAI(model_name="gpt-3.5-turbo", streaming=True, temperature=0)
llm = HuggingFaceHub(repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",model_kwargs={"temperature": 0.5, "max_length": 1024},huggingfacehub_api_token="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",task="text-generation")
# Convert your list of dicts to LangChain messages
langchain_messages = [
SystemMessage(content=messages[0]['content']), # Assuming first message is system
HumanMessage(content=messages[1]['content']) # Assuming second is human
]
# Stream the LLM response
for chunk in llm.stream(langchain_messages):
yield chunk.content # Yield only the content string
class Pipeline(Blueprint):
class Valves(BaseModel):
pass
"""
A class representing a single-model RAG pipeline.
This class is required for the Open WebUI Pipelines server to recognize your script.
"""
api_version = "v1"
def __init__(self):
"""
Initializes the pipeline with FAISS RAG.
"""
try:
self.name="faiss-rag-model"
#self.type = "pipe"
self.faiss_rag = FAISSRAG()
print("Pipeline instance created!")
except Exception as e:
# This will print the error to your console
print(f"Error during pipeline initialization: {e}")
# You can also print the full traceback for more details
import traceback
traceback.print_exc()
raise e
def pipelines(self) -> List[dict]:
"""
This method is crucial for the pipeline to be visible in the Open WebUI admin panel.
It must return a list of dictionaries, where each dictionary represents a model
that the pipeline provides.
"""
print("Calling pipelines() method")
return [
{
"id": "faiss-rag-model",
"name": "FAISS RAG Pipeline",
"description": "A RAG pipeline using FAISS for document retrieval."
}
]
# Inside your Pipeline class
def pipe(
self,
user_message: str,
model_id: str,
messages: List[dict],
body: dict,
) -> Union[str, Iterator, Generator]:
# ... (your existing logic for fetching documents and creating context)
# Simplified call to your RAG generator
response_generator = self.faiss_rag.generate_completion(messages=messages, model_id=model_id, stream=True)
# Stream the plain text content back
for chunk in response_generator:
# The delta contains the chunk of content you need to yield
content_chunk = chunk.choices[0]["delta"]["content"]
yield content_chunk
pipeline = Pipeline()
That is loaded inside C:\Users\UserName\Desktop\pipelines\pipelines
as cloned from https://github.com/open-webui/pipelines
I also run a local instance of https://github.com/open-webui/open-webui.git which is also run using uvicorn
cmd
when I create a connection (under settings) to add localhost:9099 and set the default password 0p3n-w3bu! (the connection is validated) and the model can be selected from the chat UI, but when I prompt it I keep getting the error 404:
INFO: 127.0.0.1:51522 - "GET /api/version HTTP/1.1" 404 Not Found
INFO: 127.0.0.1:61060 - "POST /v1/chat/completions HTTP/1.1" 404 Not Found
INFO: 127.0.0.1:55577 - "GET /v1/models HTTP/1.1" 200 OK
INFO: 127.0.0.1:60946 - "GET /models HTTP/1.1" 200 OK
INFO: 127.0.0.1:60951 - "GET /api/tags HTTP/1.1" 404 Not Found
Any clues?
Thanks.
Metadata
Metadata
Assignees
Labels
No labels