|
def answer(query): |
|
# Encode the query using the bi-encoder and find potentially relevant passages |
|
question_embedding = bi_encoder.encode(query, convert_to_tensor=True) |
|
hits = util.semantic_search(question_embedding, corpus_embeddings, top_k=top_k) |
|
hits = [hit for hit in hits[0]] |
|
|
|
hits = sorted([hit['corpus_id'] for hit in hits]) |
|
context = "\n".join([passages[hit] for hit in hits]) |
|
|
|
template = """Context: |
|
<<context>> |
|
|
|
Answer the following question by paraphrasing it and then elaborate the answer: |
|
Q: <<query>> |
|
A: |
|
""" |
|
prompt = template.replace('<<context>>', context).replace('<<query>>', query) |
|
prompt_length = len(tokenizer(prompt)['input_ids']) |
|
response = openai.Completion.create(engine="text-davinci-003", prompt=prompt, max_tokens=4096-prompt_length, temperature=0.2) |
|
return response['choices'][0]['text'] |