forked from open-webui/pipelines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
llamaindex_pipeline.py
49 lines (37 loc) · 1.47 KB
/
llamaindex_pipeline.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
title: Llama Index Pipeline
author: open-webui
date: 2024-05-30
version: 1.0
license: MIT
description: A pipeline for retrieving relevant information from a knowledge base using the Llama Index library.
requirements: llama-index
"""
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):
import os
# Set the OpenAI API key
os.environ["OPENAI_API_KEY"] = "your-api-key-here"
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
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
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