Update app.py
Browse files
app.py
CHANGED
|
@@ -1,136 +1,71 @@
|
|
| 1 |
import os
|
| 2 |
-
import textwrap
|
| 3 |
-
|
| 4 |
-
#from llama_index.query_engine.retriever_query_engine import RetrieverQueryEngine
|
| 5 |
-
from llama_index.core.query_engine import RetrieverQueryEngine
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
#from llama_index import VectorStoreIndex, SimpleDirectoryReader, Document
|
| 9 |
-
# new (v0.10+)
|
| 10 |
-
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
|
| 11 |
-
from llama_index.core.schema import Document # safest place for Document
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
from llama_index.vector_stores import DeepLakeVectorStore
|
| 15 |
-
from langchain.chat_models import ChatOpenAI
|
| 16 |
import chainlit as cl
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
import os
|
| 20 |
-
os.environ["OPENAI_API_KEY"]= os.environ.get("open_ai")
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
from llama_index import SimpleDirectoryReader, Document, StorageContext, OpenAIEmbedding, ServiceContext, PromptHelper, VectorStoreIndex
|
| 25 |
-
from llama_index.vector_stores import PineconeVectorStore, QdrantVectorStore, SimpleVectorStore, DeepLakeVectorStore
|
| 26 |
-
from transformers import BertTokenizerFast
|
| 27 |
-
import openai
|
| 28 |
-
from llama_index.llms import OpenAI
|
| 29 |
-
from llama_index import ServiceContext
|
| 30 |
-
from llama_index.embeddings import OpenAIEmbedding
|
| 31 |
-
from llama_index.node_parser import SimpleNodeParser
|
| 32 |
-
from llama_index.text_splitter import TokenTextSplitter
|
| 33 |
-
|
| 34 |
-
from llama_index import StorageContext, load_index_from_storage
|
| 35 |
-
from llama_index import load_index_from_storage, load_indices_from_storage, load_graph_from_storage
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
#dataset_path ="hub://cxcxxaaaaaz/text_embedding" # if we comment this out and don't pass the path then GPTDeepLakeIndex will create dataset in memory
|
| 40 |
-
from llama_index.storage.storage_context import StorageContext
|
| 41 |
-
from llama_index import VectorStoreIndex, SimpleDirectoryReader, Document
|
| 42 |
-
from llama_index.vector_stores import ChromaVectorStore
|
| 43 |
-
|
| 44 |
-
# Create an index over the documnts
|
| 45 |
-
#vector_store = DeepLakeVectorStore(dataset_path=dataset_path
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
import chromadb
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
|
| 61 |
-
storage_context = StorageContext.from_defaults(vector_store=vector_store)
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
#vector_store = LanceDBVectorStore1(uri="./sample_data/")
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
#storage_context = StorageContext.from_defaults(vector_store=vector_store)
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
llm = OpenAI(model='gpt-3.5-turbo', temperature=0.1)
|
| 74 |
-
embed_model = OpenAIEmbedding()
|
| 75 |
-
|
| 76 |
-
#node_parser = SimpleNodeParser(text_splitter=TokenTextSplitter(chunk_size=2924, chunk_overlap=20))
|
| 77 |
-
|
| 78 |
-
prompt_helper = PromptHelper(
|
| 79 |
-
context_window=2000,
|
| 80 |
-
num_output=256,
|
| 81 |
-
chunk_overlap_ratio=0.1,
|
| 82 |
-
chunk_size_limit=200
|
| 83 |
-
)
|
| 84 |
-
|
| 85 |
-
import tiktoken
|
| 86 |
-
from llama_index.callbacks import CallbackManager, TokenCountingHandler
|
| 87 |
-
from llama_index import load_index_from_storage, load_indices_from_storage, load_graph_from_storage
|
| 88 |
-
|
| 89 |
-
token_counter = TokenCountingHandler(tokenizer=tiktoken.encoding_for_model("gpt-3.5-turbo").encode)
|
| 90 |
-
|
| 91 |
-
callback_manager = CallbackManager([token_counter])
|
| 92 |
|
|
|
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
| 97 |
)
|
| 98 |
-
from llama_index import set_global_service_context
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
index = VectorStoreIndex.from_documents([], vectorstore=vector_store, storage_context=storage_context, service_context=service_context)
|
| 103 |
-
|
| 104 |
-
|
| 105 |
|
|
|
|
|
|
|
|
|
|
| 106 |
@cl.on_chat_start
|
| 107 |
-
async def
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
# Substitute your connection string here
|
| 112 |
-
|
| 113 |
-
query_engine = index.as_query_engine(
|
| 114 |
-
service_context=service_context,
|
| 115 |
-
streaming=True,
|
| 116 |
-
)
|
| 117 |
-
|
| 118 |
-
|
| 119 |
cl.user_session.set("query_engine", query_engine)
|
| 120 |
-
|
| 121 |
-
|
| 122 |
|
| 123 |
@cl.on_message
|
| 124 |
-
async def
|
| 125 |
-
query_engine = cl.user_session.get("query_engine")
|
|
|
|
| 126 |
response = await cl.make_async(query_engine.query)(message.content)
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
for token in response.response_gen:
|
| 131 |
-
await
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
|
|
|
| 135 |
|
| 136 |
-
await
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import chainlit as cl
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import chromadb
|
| 4 |
|
| 5 |
+
# --- LlamaIndex (v0.10+) imports ---
|
| 6 |
+
from llama_index.core import VectorStoreIndex, Settings, StorageContext
|
| 7 |
+
from llama_index.llms.openai import OpenAI
|
| 8 |
+
from llama_index.embeddings.openai import OpenAIEmbedding
|
| 9 |
+
from llama_index.vector_stores.chroma import ChromaVectorStore
|
| 10 |
+
|
| 11 |
+
# --------------------
|
| 12 |
+
# OpenAI credentials
|
| 13 |
+
# --------------------
|
| 14 |
+
# Prefer OPENAI_API_KEY; fall back to `open_ai` if that’s how your Space stores it.
|
| 15 |
+
_openai_key = os.getenv("OPENAI_API_KEY") or os.getenv("open_ai")
|
| 16 |
+
if _openai_key:
|
| 17 |
+
os.environ["OPENAI_API_KEY"] = _openai_key
|
| 18 |
+
else:
|
| 19 |
+
raise RuntimeError(
|
| 20 |
+
"Missing OPENAI_API_KEY (or 'open_ai'). "
|
| 21 |
+
"Set it in your Space secrets."
|
| 22 |
+
)
|
| 23 |
|
| 24 |
+
# --------------------
|
| 25 |
+
# LlamaIndex global settings (recommended in v0.10+)
|
| 26 |
+
# --------------------
|
| 27 |
+
Settings.llm = OpenAI(model="gpt-4o-mini", temperature=0.1)
|
| 28 |
+
Settings.embed_model = OpenAIEmbedding(model="text-embedding-3-small")
|
| 29 |
+
|
| 30 |
+
# --------------------
|
| 31 |
+
# Vector store: Chroma (persistent)
|
| 32 |
+
# --------------------
|
| 33 |
+
chroma_client = chromadb.PersistentClient(path="./chroma_db")
|
| 34 |
+
chroma_collection = chroma_client.get_or_create_collection("vector")
|
| 35 |
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
storage_context = StorageContext.from_defaults(vector_store=vector_store)
|
| 38 |
|
| 39 |
+
# Create an index “shell” over the vector store.
|
| 40 |
+
# (Use `index.insert_nodes(...)` or build from docs later when you ingest.)
|
| 41 |
+
index = VectorStoreIndex.from_vector_store(
|
| 42 |
+
vector_store=vector_store,
|
| 43 |
+
storage_context=storage_context,
|
| 44 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
# --------------------
|
| 47 |
+
# Chainlit app
|
| 48 |
+
# --------------------
|
| 49 |
@cl.on_chat_start
|
| 50 |
+
async def on_chat_start():
|
| 51 |
+
# Build a streaming query engine
|
| 52 |
+
query_engine = index.as_query_engine(streaming=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
cl.user_session.set("query_engine", query_engine)
|
| 54 |
+
await cl.Message(content="Hi! Ask me anything.").send()
|
|
|
|
| 55 |
|
| 56 |
@cl.on_message
|
| 57 |
+
async def on_message(message: cl.Message):
|
| 58 |
+
query_engine = cl.user_session.get("query_engine")
|
| 59 |
+
# Run query in a thread pool (Chainlit helper)
|
| 60 |
response = await cl.make_async(query_engine.query)(message.content)
|
| 61 |
|
| 62 |
+
# Stream tokens as they arrive
|
| 63 |
+
reply = cl.Message(content="")
|
| 64 |
for token in response.response_gen:
|
| 65 |
+
await reply.stream_token(token)
|
| 66 |
|
| 67 |
+
# Ensure final text is set (in case engine also returns response_txt)
|
| 68 |
+
if getattr(response, "response_txt", None):
|
| 69 |
+
reply.content = response.response_txt
|
| 70 |
|
| 71 |
+
await reply.send()
|