-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.py
23 lines (20 loc) · 874 Bytes
/
query.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def query_pinecone(query, top_k):
# generate embeddings for the query
xq = retriever.encode([query]).tolist()
# search pinecone index for context passage with the answer
xc = index.query(xq, top_k=top_k, include_metadata=True)
return xc
def format_query(query, context):
# extract passage_text from Pinecone search result and add the <P> tag
context = [f"<P> {m['metadata']['passage_text']}" for m in context]
# concatinate all context passages
context = " ".join(context)
# contcatinate the query and context passages
query = f"question: {query} context: {context}"
return query
query = "when was the first electric power system built?"
result = query_pinecone(query, top_k=1)
from pprint import pprint
# format the query in the form generator expects the input
query = format_query(query, result["matches"])
pprint(query)