This commit is contained in:
Timothy J. Baek 2024-05-21 18:33:16 -07:00
parent 24e02a9017
commit 13f054714b
6 changed files with 211 additions and 209 deletions

View File

@ -37,8 +37,9 @@ def load_modules_from_directory(directory):
for loaded_module in load_modules_from_directory("./pipelines"): for loaded_module in load_modules_from_directory("./pipelines"):
# Do something with the loaded module # Do something with the loaded module
print("Loaded:", loaded_module.__name__) print("Loaded:", loaded_module.__name__)
PIPELINES[loaded_module.__name__] = { PIPELINES[loaded_module.__name__] = {
"module": loaded_module, "module": loaded_module.Pipeline(),
"id": loaded_module.__name__, "id": loaded_module.__name__,
"name": loaded_module.__name__, "name": loaded_module.__name__,
} }

View File

@ -1,95 +1,98 @@
from typing import List, Union, Generator from typing import List, Union, Generator
from schemas import OpenAIChatMessage from schemas import OpenAIChatMessage
import os import os
import asyncio
basic_rag_pipeline = None
def get_response( class Pipeline:
user_message: str, messages: List[OpenAIChatMessage] def __init__(self):
) -> Union[str, Generator]: self.basic_rag_pipeline = None
# This is where you can add your custom RAG pipeline.
# Typically, you would retrieve relevant information from your knowledge base and synthesize it to generate a response.
print(messages) async def on_startup(self):
print(user_message) os.environ["OPENAI_API_KEY"] = "your_openai_api_key_here"
question = user_message from haystack.components.embedders import SentenceTransformersDocumentEmbedder
response = basic_rag_pipeline.run( from haystack.components.embedders import SentenceTransformersTextEmbedder
{"text_embedder": {"text": question}, "prompt_builder": {"question": question}} from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
) from haystack.components.builders import PromptBuilder
from haystack.components.generators import OpenAIGenerator
return response["llm"]["replies"][0] from haystack.document_stores.in_memory import InMemoryDocumentStore
from datasets import load_dataset
from haystack import Document
from haystack import Pipeline
async def on_startup(): document_store = InMemoryDocumentStore()
global basic_rag_pipeline
os.environ["OPENAI_API_KEY"] = "your_openai_api_key_here" dataset = load_dataset("bilgeyucel/seven-wonders", split="train")
docs = [Document(content=doc["content"], meta=doc["meta"]) for doc in dataset]
from haystack.components.embedders import SentenceTransformersDocumentEmbedder doc_embedder = SentenceTransformersDocumentEmbedder(
from haystack.components.embedders import SentenceTransformersTextEmbedder model="sentence-transformers/all-MiniLM-L6-v2"
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever )
from haystack.components.builders import PromptBuilder doc_embedder.warm_up()
from haystack.components.generators import OpenAIGenerator
from haystack.document_stores.in_memory import InMemoryDocumentStore docs_with_embeddings = doc_embedder.run(docs)
document_store.write_documents(docs_with_embeddings["documents"])
from datasets import load_dataset text_embedder = SentenceTransformersTextEmbedder(
from haystack import Document model="sentence-transformers/all-MiniLM-L6-v2"
from haystack import Pipeline )
document_store = InMemoryDocumentStore() retriever = InMemoryEmbeddingRetriever(document_store)
dataset = load_dataset("bilgeyucel/seven-wonders", split="train") template = """
docs = [Document(content=doc["content"], meta=doc["meta"]) for doc in dataset] Given the following information, answer the question.
doc_embedder = SentenceTransformersDocumentEmbedder( Context:
model="sentence-transformers/all-MiniLM-L6-v2" {% for document in documents %}
) {{ document.content }}
doc_embedder.warm_up() {% endfor %}
docs_with_embeddings = doc_embedder.run(docs) Question: {{question}}
document_store.write_documents(docs_with_embeddings["documents"]) Answer:
"""
text_embedder = SentenceTransformersTextEmbedder( prompt_builder = PromptBuilder(template=template)
model="sentence-transformers/all-MiniLM-L6-v2"
)
retriever = InMemoryEmbeddingRetriever(document_store) generator = OpenAIGenerator(model="gpt-3.5-turbo")
template = """ self.basic_rag_pipeline = Pipeline()
Given the following information, answer the question. # Add components to your pipeline
self.basic_rag_pipeline.add_component("text_embedder", text_embedder)
self.basic_rag_pipeline.add_component("retriever", retriever)
self.basic_rag_pipeline.add_component("prompt_builder", prompt_builder)
self.basic_rag_pipeline.add_component("llm", generator)
Context: # Now, connect the components to each other
{% for document in documents %} self.basic_rag_pipeline.connect(
{{ document.content }} "text_embedder.embedding", "retriever.query_embedding"
{% endfor %} )
self.basic_rag_pipeline.connect("retriever", "prompt_builder.documents")
self.basic_rag_pipeline.connect("prompt_builder", "llm")
Question: {{question}} pass
Answer:
"""
prompt_builder = PromptBuilder(template=template) async def on_shutdown(self):
# This function is called when the server is stopped.
pass
generator = OpenAIGenerator(model="gpt-3.5-turbo") def get_response(
self, user_message: str, messages: List[OpenAIChatMessage]
) -> Union[str, Generator]:
# This is where you can add your custom RAG pipeline.
# Typically, you would retrieve relevant information from your knowledge base and synthesize it to generate a response.
basic_rag_pipeline = Pipeline() print(messages)
# Add components to your pipeline print(user_message)
basic_rag_pipeline.add_component("text_embedder", text_embedder)
basic_rag_pipeline.add_component("retriever", retriever)
basic_rag_pipeline.add_component("prompt_builder", prompt_builder)
basic_rag_pipeline.add_component("llm", generator)
# Now, connect the components to each other question = user_message
basic_rag_pipeline.connect("text_embedder.embedding", "retriever.query_embedding") response = self.basic_rag_pipeline.run(
basic_rag_pipeline.connect("retriever", "prompt_builder.documents") {
basic_rag_pipeline.connect("prompt_builder", "llm") "text_embedder": {"text": question},
"prompt_builder": {"question": question},
}
)
# This function is called when the server is started. return response["llm"]["replies"][0]
pass
async def on_shutdown():
# This function is called when the server is stopped.
pass

View File

@ -3,83 +3,82 @@ from schemas import OpenAIChatMessage
import os import os
import asyncio import asyncio
index = None
documents = None
class Pipeline:
def __init__(self):
self.documents = None
self.index = None
def get_response( async def on_startup(self):
user_message: str, messages: List[OpenAIChatMessage] from llama_index.embeddings.ollama import OllamaEmbedding
) -> Union[str, Generator]: from llama_index.llms.ollama import Ollama
# This is where you can add your custom RAG pipeline. from llama_index.core import VectorStoreIndex, Settings
# Typically, you would retrieve relevant information from your knowledge base and synthesize it to generate a response. from llama_index.readers.github import GithubRepositoryReader, GithubClient
print(messages) Settings.embed_model = OllamaEmbedding(
print(user_message) model_name="nomic-embed-text",
base_url="http://localhost:11434",
)
Settings.llm = Ollama(model="llama3")
query_engine = index.as_query_engine(streaming=True) global index, documents
response = query_engine.query(user_message)
return response.response_gen github_token = os.environ.get("GITHUB_TOKEN")
owner = "open-webui"
repo = "plugin-server"
branch = "main"
github_client = GithubClient(github_token=github_token, verbose=True)
async def on_startup(): reader = GithubRepositoryReader(
github_client=github_client,
owner=owner,
repo=repo,
use_parser=False,
verbose=False,
filter_file_extensions=(
[
".png",
".jpg",
".jpeg",
".gif",
".svg",
".ico",
"json",
".ipynb",
],
GithubRepositoryReader.FilterType.EXCLUDE,
),
)
from llama_index.embeddings.ollama import OllamaEmbedding loop = asyncio.new_event_loop()
from llama_index.llms.ollama import Ollama
from llama_index.core import VectorStoreIndex, Settings
from llama_index.readers.github import GithubRepositoryReader, GithubClient
Settings.embed_model = OllamaEmbedding( reader._loop = loop
model_name="nomic-embed-text",
base_url="http://localhost:11434",
)
Settings.llm = Ollama(model="llama3")
global index, documents try:
# Load data from the branch
self.documents = await asyncio.to_thread(reader.load_data, branch=branch)
self.index = VectorStoreIndex.from_documents(documents)
finally:
loop.close()
github_token = os.environ.get("GITHUB_TOKEN") print(self.documents)
owner = "open-webui" print(self.index)
repo = "plugin-server"
branch = "main"
github_client = GithubClient(github_token=github_token, verbose=True) async def on_shutdown(self):
# This function is called when the server is stopped.
pass
reader = GithubRepositoryReader( def get_response(
github_client=github_client, self, user_message: str, messages: List[OpenAIChatMessage]
owner=owner, ) -> Union[str, Generator]:
repo=repo, # This is where you can add your custom RAG pipeline.
use_parser=False, # Typically, you would retrieve relevant information from your knowledge base and synthesize it to generate a response.
verbose=False,
filter_file_extensions=(
[
".png",
".jpg",
".jpeg",
".gif",
".svg",
".ico",
"json",
".ipynb",
],
GithubRepositoryReader.FilterType.EXCLUDE,
),
)
loop = asyncio.new_event_loop() print(messages)
print(user_message)
reader._loop = loop query_engine = self.index.as_query_engine(streaming=True)
response = query_engine.query(user_message)
try: return response.response_gen
# Load data from the branch
documents = await asyncio.to_thread(reader.load_data, branch=branch)
index = VectorStoreIndex.from_documents(documents)
finally:
loop.close()
print(documents)
print(index)
async def on_shutdown():
# This function is called when the pipeline is stopped.
pass

View File

@ -2,47 +2,45 @@ from typing import List, Union, Generator
from schemas import OpenAIChatMessage from schemas import OpenAIChatMessage
documents = None class Pipeline:
index = None def __init__(self):
self.documents = None
self.index = None
async def on_startup(self):
from llama_index.embeddings.ollama import OllamaEmbedding
from llama_index.llms.ollama import Ollama
from llama_index.core import Settings, VectorStoreIndex, SimpleDirectoryReader
def get_response( Settings.embed_model = OllamaEmbedding(
user_message: str, messages: List[OpenAIChatMessage] model_name="nomic-embed-text",
) -> Union[str, Generator]: base_url="http://localhost:11434",
# This is where you can add your custom RAG pipeline. )
# Typically, you would retrieve relevant information from your knowledge base and synthesize it to generate a response. Settings.llm = Ollama(model="llama3")
print(messages) # This function is called when the server is started.
print(user_message) global documents, index
query_engine = index.as_query_engine(streaming=True) self.documents = SimpleDirectoryReader("./data").load_data()
response = query_engine.query(user_message) self.index = VectorStoreIndex.from_documents(documents)
pass
print(response) async def on_shutdown(self):
# This function is called when the server is stopped.
pass
return response.response_gen def get_response(
self, user_message: str, messages: List[OpenAIChatMessage]
) -> Union[str, Generator]:
# This is where you can add your custom RAG pipeline.
# Typically, you would retrieve relevant information from your knowledge base and synthesize it to generate a response.
print(messages)
print(user_message)
async def on_startup(): query_engine = self.index.as_query_engine(streaming=True)
from llama_index.embeddings.ollama import OllamaEmbedding response = query_engine.query(user_message)
from llama_index.llms.ollama import Ollama
from llama_index.core import Settings, VectorStoreIndex, SimpleDirectoryReader
Settings.embed_model = OllamaEmbedding( print(response)
model_name="nomic-embed-text",
base_url="http://localhost:11434",
)
Settings.llm = Ollama(model="llama3")
# This function is called when the server is started. return response.response_gen
global documents, index
documents = SimpleDirectoryReader("./data").load_data()
index = VectorStoreIndex.from_documents(documents)
pass
async def on_shutdown():
# This function is called when the server is stopped.
pass

View File

@ -1,40 +1,39 @@
from typing import List, Union, Generator from typing import List, Union, Generator
from schemas import OpenAIChatMessage from schemas import OpenAIChatMessage
documents = None
index = None
class Pipeline:
def __init__(self):
self.documents = None
self.index = None
def get_response( async def on_startup(self):
user_message: str, messages: List[OpenAIChatMessage] import os
) -> Union[str, Generator]:
# This is where you can add your custom RAG pipeline.
# Typically, you would retrieve relevant information from your knowledge base and synthesize it to generate a response.
print(messages) # Set the OpenAI API key
print(user_message) os.environ["OPENAI_API_KEY"] = "your-api-key-here"
query_engine = index.as_query_engine(streaming=True) from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
response = query_engine.query(user_message)
return response.response_gen self.documents = SimpleDirectoryReader("./data").load_data()
self.index = VectorStoreIndex.from_documents(self.documents)
# This function is called when the server is started.
pass
async def on_shutdown(self):
# This function is called when the server is stopped.
pass
async def on_startup(): def get_response(
global documents, index self, user_message: str, messages: List[OpenAIChatMessage]
import os ) -> Union[str, Generator]:
# This is where you can add your custom RAG pipeline.
# Typically, you would retrieve relevant information from your knowledge base and synthesize it to generate a response.
# Set the OpenAI API key print(messages)
os.environ["OPENAI_API_KEY"] = "your_openai_api_key_here" print(user_message)
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader query_engine = self.index.as_query_engine(streaming=True)
response = query_engine.query(user_message)
documents = SimpleDirectoryReader("./data").load_data() return response.response_gen
index = VectorStoreIndex.from_documents(documents)
# This function is called when the server is started.
pass
async def on_shutdown():
# This function is called when the server is stopped.
pass

View File

@ -2,25 +2,27 @@ from typing import List, Union, Generator
from schemas import OpenAIChatMessage from schemas import OpenAIChatMessage
def get_response( class Pipeline:
user_message: str, messages: List[OpenAIChatMessage] def __init__(self):
) -> Union[str, Generator]: pass
# This is where you can add your custom pipelines like RAG.
print(messages) async def on_startup(self):
print(user_message) # This function is called when the server is started.
print("onstartup")
print(__name__)
return f"pipeline response to: {user_message}" pass
async def on_shutdown():
# This function is called when the server is stopped.
pass
async def on_startup(): def get_response(
# This function is called when the server is started. self, user_message: str, messages: List[OpenAIChatMessage]
print("onstartup") ) -> Union[str, Generator]:
print(__name__) # This is where you can add your custom pipelines like RAG.
pass print(messages)
print(user_message)
return f"pipeline response to: {user_message}"
async def on_shutdown():
# This function is called when the server is stopped.
pass