pipelines/examples/llamaindex_ollama_pipeline.py
2024-05-30 22:24:29 -07:00

45 lines
1.5 KiB
Python

from typing import List, Union, Generator, Iterator
from schemas import OpenAIChatMessage
class Pipeline:
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
Settings.embed_model = OllamaEmbedding(
model_name="nomic-embed-text",
base_url="http://localhost:11434",
)
Settings.llm = Ollama(model="llama3")
# This function is called when the server is started.
global documents, index
self.documents = SimpleDirectoryReader("./data").load_data()
self.index = VectorStoreIndex.from_documents(self.documents)
pass
async def on_shutdown(self):
# This function is called when the server is stopped.
pass
def pipe(
self, user_message: str, model_id: str, messages: List[dict], body: dict
) -> Union[str, Generator, Iterator]:
# 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)
query_engine = self.index.as_query_engine(streaming=True)
response = query_engine.query(user_message)
return response.response_gen