File size: 1,385 Bytes
cd607b2
 
 
7b856a8
cd607b2
 
 
7b856a8
 
 
cd607b2
7b856a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd607b2
 
7b856a8
cd607b2
 
 
 
 
 
 
 
 
 
 
 
 
7b856a8
cd607b2
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# + tags=["hide_inp"]
desc = """
# QA

Questions answering with embeddings.  Adapted from [OpenAI Notebook](https://github.com/openai/openai-cookbook/blob/main/examples/Question_answering_using_embeddings.ipynb).
"""
# -

import datasets
import numpy as np

from minichain import EmbeddingPrompt, TemplatePrompt, show_log, start_chain

# We use Hugging Face Datasets as the database by assigning
# a FAISS index.

olympics = datasets.load_from_disk("olympics.data")
olympics.add_faiss_index("embeddings")


# Fast KNN retieval prompt


class KNNPrompt(EmbeddingPrompt):
    def find(self, out, inp):
        res = olympics.get_nearest_examples("embeddings", np.array(out), 3)
        return {"question": inp, "docs": res.examples["content"]}


# QA prompt to ask question with examples


class QAPrompt(TemplatePrompt):
    template_file = "qa.pmpt.tpl"


with start_chain("qa") as backend:
    prompt = KNNPrompt(backend.OpenAIEmbed()).chain(QAPrompt(backend.OpenAI()))
    
question = "Who won the 2020 Summer Olympics men's high jump?"

gradio = prompt.to_gradio(fields=["query"],
                          examples=[question],
                          description=desc)
if __name__ == "__main__":
    gradio.launch()



# # + tags=["hide_inp"]
# QAPrompt().show(
#     {"question": "Who won the race?", "docs": ["doc1", "doc2", "doc3"]}, "Joe Bob"
# )
# # -

# show_log("qa.log")