Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from llama_index.core.indices.vector_store.base import VectorStoreIndex
|
2 |
+
from llama_index.vector_stores.qdrant import QdrantVectorStore
|
3 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
4 |
+
from llama_index.core import Settings
|
5 |
+
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, StorageContext
|
6 |
+
import qdrant_client
|
7 |
+
from llama_index.core.indices.query.schema import QueryBundle
|
8 |
+
from llama_index.llms.gemini import Gemini
|
9 |
+
from llama_index.embeddings.gemini import GeminiEmbedding
|
10 |
+
from llama_index.core.memory import ChatMemoryBuffer
|
11 |
+
import gradio as gr
|
12 |
+
|
13 |
+
def embed_setup():
|
14 |
+
Settings.embed_model = GeminiEmbedding(api_key=os.getenv("GEMINI_API_KEY"), model_name="models/embedding-001")
|
15 |
+
Settings.llm = Gemini(api_key=os.getenv("GEMINI_API_KEY"), temperature=0.1,model_name="models/gemini-pro")
|
16 |
+
|
17 |
+
def qdrant_setup():
|
18 |
+
client = qdrant_client.QdrantClient(
|
19 |
+
os.getenv("QDRANT_URL"),
|
20 |
+
api_key = os.getenv("QDRANT_API_KEY"),
|
21 |
+
)
|
22 |
+
return client
|
23 |
+
|
24 |
+
def llm_setup():
|
25 |
+
llm = Gemini(api_key=os.getenv("GEMINI_API_KEY"), temperature=0.6,model_name="models/gemini-pro")
|
26 |
+
return llm
|
27 |
+
|
28 |
+
|
29 |
+
def query_index(index, similarity_top_k=3, streaming=True):
|
30 |
+
memory = ChatMemoryBuffer.from_defaults(token_limit=4000)
|
31 |
+
chat_engine = index.as_chat_engine(
|
32 |
+
chat_mode="context",
|
33 |
+
memory=memory,
|
34 |
+
system_prompt = (
|
35 |
+
"""You are an AI assistant named Gemini, created by Google. Your task is to provide helpful, accurate, and concise responses to user queries.
|
36 |
+
|
37 |
+
Context information is below:
|
38 |
+
----------------
|
39 |
+
{context_str}
|
40 |
+
----------------
|
41 |
+
Always answer based on the information in the context and be precise
|
42 |
+
Given this context, please respond to the following user query:
|
43 |
+
{query_str}
|
44 |
+
|
45 |
+
Also suggest 3 more questions based on the the context that the user can ask
|
46 |
+
Your response:"""
|
47 |
+
),)
|
48 |
+
return chat_engine
|
49 |
+
|
50 |
+
def get_response(text,history=None):
|
51 |
+
# Use the initialized query engine to perform the query
|
52 |
+
response = str(chat_engine.chat(text))
|
53 |
+
return response
|
54 |
+
|
55 |
+
embed_setup()
|
56 |
+
client = qdrant_setup()
|
57 |
+
llm = llm_setup()
|
58 |
+
vector_store = QdrantVectorStore(client = client,collection_name=os.getenv("COLLECTION_NAME"))
|
59 |
+
index = VectorStoreIndex.from_vector_store(llm = llm, vector_store = vector_store)
|
60 |
+
chat_engine = query_index(index) # initialize the query engine
|
61 |
+
|
62 |
+
|
63 |
+
t = gr.ChatInterface(get_response, analytics_enabled=True)
|
64 |
+
t.launch(debug=True, share=True)
|