forked from microsoft/promptflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_context.py
31 lines (25 loc) · 1.04 KB
/
find_context.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
import faiss
from jinja2 import Environment, FileSystemLoader
import os
from utils.index import FAISSIndex
from utils.oai import OAIEmbedding, render_with_token_limit
from utils.logging import log
def find_context(question: str, index_path: str):
index = FAISSIndex(index=faiss.IndexFlatL2(1536), embedding=OAIEmbedding())
index.load(path=index_path)
snippets = index.query(question, top_k=5)
template = Environment(
loader=FileSystemLoader(os.path.dirname(os.path.abspath(__file__)))
).get_template("qna_prompt.md")
token_limit = int(os.environ.get("PROMPT_TOKEN_LIMIT"))
# Try to render the template with token limit and reduce snippet count if it fails
while True:
try:
prompt = render_with_token_limit(
template, token_limit, question=question, context=enumerate(snippets)
)
break
except ValueError:
snippets = snippets[:-1]
log(f"Reducing snippet count to {len(snippets)} to fit token limit")
return prompt, snippets