-
Notifications
You must be signed in to change notification settings - Fork 0
/
llamaindex_article_rag.py
65 lines (51 loc) · 2.16 KB
/
llamaindex_article_rag.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
title: LlamaIndex + Gemini Test Pipeline
author: [email protected]
date: 2024-11-15
version: 1.0
description: Test pipeline for verifying LlamaIndex and Gemini functionality
requirements: llama-index, llama-index-llms-gemini
"""
from typing import Generator, Iterator, List, Union
from pydantic import BaseModel
from llama_index.llms.gemini import Gemini
from llama_index.core import Document, VectorStoreIndex, Settings
class Pipeline:
"""Test Pipeline for Llama Index functionality"""
class Valves(BaseModel):
"""Test options for WebUI"""
test_text: str = "The capital of France is Paris. The capital of Italy is Rome."
def __init__(self):
self.documents = None
self.index = None
self.valves = self.Valves()
async def on_startup(self):
try:
# Create a test document
test_doc = Document(text=self.valves.test_text)
# Initialize settings with default embedding model
Settings.llm = Gemini(model="gemini-1.5-pro-001")
# Create index from test document
self.index = VectorStoreIndex.from_documents([test_doc])
print("Llama Index initialization successful!")
return True
except Exception as e:
print(f"Llama Index initialization failed: {str(e)}")
return False
async def on_shutdown(self):
print("Pipeline shutdown")
async def on_valves_updated(self) -> None:
print(f"Updating index with new test text: {self.valves.test_text}")
await self.on_startup()
def pipe(
self, user_message: str, model_id: str, messages: List[dict], body: dict
) -> Union[str, Generator, Iterator]:
try:
# Test querying the index
if self.index is None:
return "Index not initialized. Please check startup logs."
query_engine = self.index.as_query_engine(streaming=True)
response = query_engine.query(user_message)
return response.response_gen
except Exception as e:
return f"Query failed: {str(e)}"