Spaces:
Runtime error
Runtime error
Upload with huggingface_hub
Browse files- app.py +46 -0
- qa.pmpt.tpl +12 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# # QA
|
2 |
+
|
3 |
+
# Questions answering with embeddings. Adapted from [OpenAI
|
4 |
+
# Notebook](https://github.com/openai/openai-cookbook/blob/main/examples/Question_answering_using_embeddings.ipynb).
|
5 |
+
|
6 |
+
import datasets
|
7 |
+
import numpy as np
|
8 |
+
|
9 |
+
from minichain import EmbeddingPrompt, TemplatePrompt, show_log, start_chain
|
10 |
+
|
11 |
+
# We use Hugging Face Datasets as the database by assigning
|
12 |
+
# a FAISS index.
|
13 |
+
|
14 |
+
olympics = datasets.load_from_disk("olympics.data")
|
15 |
+
olympics.add_faiss_index("embeddings")
|
16 |
+
|
17 |
+
|
18 |
+
# Fast KNN retieval prompt
|
19 |
+
|
20 |
+
|
21 |
+
class KNNPrompt(EmbeddingPrompt):
|
22 |
+
def find(self, out, inp):
|
23 |
+
res = olympics.get_nearest_examples("embeddings", np.array(out), 3)
|
24 |
+
return {"question": inp, "docs": res.examples["content"]}
|
25 |
+
|
26 |
+
|
27 |
+
# QA prompt to ask question with examples
|
28 |
+
|
29 |
+
|
30 |
+
class QAPrompt(TemplatePrompt):
|
31 |
+
template_file = "qa.pmpt.tpl"
|
32 |
+
|
33 |
+
|
34 |
+
with start_chain("qa") as backend:
|
35 |
+
question = "Who won the 2020 Summer Olympics men's high jump?"
|
36 |
+
prompt = KNNPrompt(backend.OpenAIEmbed()).chain(QAPrompt(backend.OpenAI()))
|
37 |
+
result = prompt(question)
|
38 |
+
print(result)
|
39 |
+
|
40 |
+
# + tags=["hide_inp"]
|
41 |
+
QAPrompt().show(
|
42 |
+
{"question": "Who won the race?", "docs": ["doc1", "doc2", "doc3"]}, "Joe Bob"
|
43 |
+
)
|
44 |
+
# -
|
45 |
+
|
46 |
+
show_log("qa.log")
|
qa.pmpt.tpl
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Answer the question as truthfully as possible using the provided context, and if the answer is not contained within the text below, say "I don't know."
|
2 |
+
|
3 |
+
Context:
|
4 |
+
|
5 |
+
{% for doc in docs %}
|
6 |
+
* {{doc}}
|
7 |
+
{% endfor %}
|
8 |
+
|
9 |
+
Q: {{question}}
|
10 |
+
|
11 |
+
A:
|
12 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
git+https://github.com/srush/minichain
|
3 |
+
manifest-ml
|