Spaces:
Runtime error
Runtime error
Upload with huggingface_hub
Browse files- app.py +43 -0
- gatsby.pmpt.tpl +10 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Questions answering with Hugging Face embeddings. Adapted from the
|
| 2 |
+
# [LlamaIndex
|
| 3 |
+
# example](https://github.com/jerryjliu/gpt_index/blob/main/examples/gatsby/TestGatsby.ipynb).
|
| 4 |
+
|
| 5 |
+
import datasets
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
from minichain import EmbeddingPrompt, TemplatePrompt, show_log, start_chain
|
| 9 |
+
|
| 10 |
+
# Load data with embeddings (computed beforehand)
|
| 11 |
+
|
| 12 |
+
gatsby = datasets.load_from_disk("gatsby")
|
| 13 |
+
gatsby.add_faiss_index("embeddings")
|
| 14 |
+
|
| 15 |
+
# Fast KNN retieval prompt
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class KNNPrompt(EmbeddingPrompt):
|
| 19 |
+
def find(self, out, inp):
|
| 20 |
+
res = gatsby.get_nearest_examples("embeddings", np.array(out), 1)
|
| 21 |
+
return {"question": inp, "docs": res.examples["passages"]}
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# QA prompt to ask question with examples
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class QAPrompt(TemplatePrompt):
|
| 28 |
+
template_file = "gatsby.pmpt.tpl"
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
with start_chain("gatsby") as backend:
|
| 32 |
+
question = "What did Gatsby do before he met Daisy?"
|
| 33 |
+
prompt = KNNPrompt(
|
| 34 |
+
backend.HuggingFaceEmbed("sentence-transformers/all-mpnet-base-v2")
|
| 35 |
+
).chain(QAPrompt(backend.OpenAI()))
|
| 36 |
+
result = prompt(question)
|
| 37 |
+
print(result)
|
| 38 |
+
|
| 39 |
+
# + tags=["hide_inp"]
|
| 40 |
+
QAPrompt().show({"question": "Who was Gatsby?", "docs": ["doc1", "doc2", "doc3"]}, "")
|
| 41 |
+
# -
|
| 42 |
+
|
| 43 |
+
show_log("gatsby.log")
|
gatsby.pmpt.tpl
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Context information is below.
|
| 2 |
+
---------------------
|
| 3 |
+
|
| 4 |
+
{% for doc in docs %}
|
| 5 |
+
* {{doc}}
|
| 6 |
+
{% endfor %}
|
| 7 |
+
|
| 8 |
+
---------------------
|
| 9 |
+
|
| 10 |
+
Given the context information and not prior knowledge, answer the question: {{question}}
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
git+https://github.com/srush/minichain
|
| 3 |
+
manifest-ml
|