saad-k7 commited on
Commit
1ff3fc7
1 Parent(s): e61f1a0

messages update

Browse files
Files changed (4) hide show
  1. app.py +4 -7
  2. custom_retriever.py +98 -0
  3. data/content.txt +0 -0
  4. requirements.txt +2 -1
app.py CHANGED
@@ -2,15 +2,12 @@ import gradio as gr
2
  from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
3
  from dotenv import load_dotenv
4
  import os
 
5
 
6
  load_dotenv()
7
 
8
- documents = SimpleDirectoryReader("data").load_data()
9
- index = VectorStoreIndex.from_documents(documents)
10
- query_engine = index.as_query_engine()
11
-
12
- def update(query):
13
- response = query_engine.query(query)
14
  return str(response)
15
 
16
  with gr.Blocks() as demo:
@@ -19,6 +16,6 @@ with gr.Blocks() as demo:
19
  inp = gr.Textbox(placeholder="Please enter your query", label="User Query")
20
  out = gr.Textbox()
21
  btn = gr.Button("Run")
22
- btn.click(fn=update, inputs=inp, outputs=out)
23
 
24
  demo.launch()
 
2
  from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
3
  from dotenv import load_dotenv
4
  import os
5
+ from custom_retriever import query_engine
6
 
7
  load_dotenv()
8
 
9
+ def get_response(query):
10
+ response = query_engine.custom_query(query)
 
 
 
 
11
  return str(response)
12
 
13
  with gr.Blocks() as demo:
 
16
  inp = gr.Textbox(placeholder="Please enter your query", label="User Query")
17
  out = gr.Textbox()
18
  btn = gr.Button("Run")
19
+ btn.click(fn=get_response, inputs=inp, outputs=out)
20
 
21
  demo.launch()
custom_retriever.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from llama_index.vector_stores.pinecone import PineconeVectorStore
2
+ from llama_index.core.schema import NodeWithScore
3
+ from typing import List, Any, Optional
4
+ from llama_index.core import PromptTemplate, get_response_synthesizer, VectorStoreIndex, ChatPromptTemplate, QueryBundle
5
+ from llama_index.core.vector_stores import VectorStoreQuery
6
+ from llama_index.embeddings.openai import OpenAIEmbedding
7
+ from llama_index.core.retrievers import BaseRetriever, VectorIndexRetriever
8
+ from llama_index.core.query_engine import CustomQueryEngine
9
+ from llama_index.core.response_synthesizers import BaseSynthesizer
10
+ from llama_index.llms.openai.base import OpenAI
11
+ from pinecone import Pinecone, PodSpec
12
+ from dotenv import load_dotenv
13
+
14
+ load_dotenv()
15
+
16
+ llm = OpenAI(model="gpt-4-0125-preview", temperature=0.7)
17
+ pc = Pinecone(api_key="cf85ee13-6be8-4b4b-b512-fbad9f4ce781")
18
+
19
+ embed_model = OpenAIEmbedding(model="text-embedding-3-small")
20
+ synthesizer = get_response_synthesizer(response_mode="compact")
21
+
22
+ index = pc.Index("test-index")
23
+
24
+ qa_prompt = PromptTemplate(
25
+ "Context \n"
26
+ "---------------------\n"
27
+ "{context_str}\n"
28
+ "---------------------\n"
29
+ "Given the context and without prior knowledge answer the user query.\n"
30
+ "User Query: {query_str}\n"
31
+ "Answer: ",
32
+ )
33
+
34
+ class PineconeRetriever(BaseRetriever):
35
+ def __init__(self, vector_store: PineconeVectorStore, embed_model: Any, query_mode: str = "default", similarity_top_k: int = 10) -> None:
36
+ self._vector_store = vector_store
37
+ self._embed_model = embed_model
38
+ self._query_mode = query_mode
39
+ self._similarity_top_k = similarity_top_k
40
+ super().__init__()
41
+
42
+ def _retrieve(self, query_bundle: QueryBundle) -> List[NodeWithScore]:
43
+ print("Yup we are using the custom retriever")
44
+ query_embedding = embed_model.get_query_embedding(query_bundle)
45
+ vector_store_query = VectorStoreQuery(
46
+ query_embedding=query_embedding,
47
+ similarity_top_k=self._similarity_top_k,
48
+ mode=self._query_mode,
49
+ )
50
+ query_result = self._vector_store.query(vector_store_query)
51
+ nodes_with_scores = []
52
+ for index, node in enumerate(query_result.nodes):
53
+ score: Optional[float] = None
54
+ if query_result.similarities is not None:
55
+ score = query_result.similarities[index]
56
+ nodes_with_scores.append(NodeWithScore(node=node, score=score))
57
+
58
+ return nodes_with_scores
59
+
60
+ class RAGStringQueryEngine(CustomQueryEngine):
61
+ """RAG String Query Engine."""
62
+
63
+ retriever: PineconeRetriever
64
+ response_synthesizer: BaseSynthesizer
65
+ llm: OpenAI
66
+ qa_prompt: PromptTemplate
67
+
68
+ def custom_query(self, query_str: str):
69
+ nodes = self.retriever._retrieve(query_str)
70
+ context_str = "\n\n".join([n.node.get_content() for n in nodes])
71
+ qa_prompt_str = qa_prompt.format(
72
+ context_str=context_str,
73
+ query_str=query_str,
74
+ )
75
+ print("QA Prompt STR: ", qa_prompt_str)
76
+ response = self.llm.complete(qa_prompt_str)
77
+ return str(response)
78
+
79
+ def print_retrieved_nodes(self, query_str: str):
80
+ nodes = self.retriever._retrieve(query_str)
81
+ # print(nodes[0].score)
82
+ for n in nodes:
83
+ print("Node Score: ", n.score)
84
+ print(n.node.get_content())
85
+
86
+
87
+ vector_store = PineconeVectorStore(pinecone_index=index)
88
+ vector_index = VectorStoreIndex.from_vector_store(vector_store=vector_store)
89
+ # retriever = VectorIndexRetriever(index=vector_index, similarity_top_k=2)
90
+ retriever = PineconeRetriever(vector_store=vector_store, embed_model=embed_model, query_mode="default", similarity_top_k=4)
91
+
92
+
93
+ query_engine = RAGStringQueryEngine(
94
+ retriever=retriever,
95
+ response_synthesizer=synthesizer,
96
+ llm=llm,
97
+ qa_prompt=qa_prompt,
98
+ )
data/content.txt DELETED
The diff for this file is too large to render. See raw diff
 
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  llama-index==0.10.11
2
  gradio==4.28.3
3
- python-dotenv==1.0.1
 
 
1
  llama-index==0.10.11
2
  gradio==4.28.3
3
+ python-dotenv==1.0.1
4
+ pinecone-client==3.0.3