-
Notifications
You must be signed in to change notification settings - Fork 29
/
prompt.py
25 lines (19 loc) · 895 Bytes
/
prompt.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
import pathway as pw
from datetime import datetime
from common.openaiapi_helper import openai_chat_completion
def prompt(index, embedded_query, user_query):
@pw.udf
def build_prompt(local_indexed_data, query):
docs_str = "\n".join(local_indexed_data)
prompt = f"Given the following data: \n {docs_str} \nanswer this query: {query}, Assume that current date is: {datetime.now()}. and clean the output"
return prompt
query_context = embedded_query + index.get_nearest_items(
embedded_query.vector, k=3, collapse_rows=True
).select(local_indexed_data_list=pw.this.doc).promise_universe_is_equal_to(embedded_query)
prompt = query_context.select(
prompt=build_prompt(pw.this.local_indexed_data_list, user_query)
)
return prompt.select(
query_id=pw.this.id,
result=openai_chat_completion(pw.this.prompt),
)