Spaces:
Runtime error
Runtime error
Commit
·
617bb16
1
Parent(s):
2e9668f
latest 2 pipelines
Browse files- app.py +48 -16
- faiss_document_store.db +0 -0
- my_faiss_index.faiss +0 -0
- my_faiss_index.json +1 -0
app.py
CHANGED
|
@@ -5,6 +5,8 @@ import sys
|
|
| 5 |
|
| 6 |
import streamlit as st
|
| 7 |
from haystack import Pipeline
|
|
|
|
|
|
|
| 8 |
|
| 9 |
logging.basicConfig(
|
| 10 |
level=logging.DEBUG,
|
|
@@ -17,26 +19,56 @@ p_1 = None
|
|
| 17 |
p_2 = None
|
| 18 |
|
| 19 |
|
| 20 |
-
def
|
| 21 |
-
|
| 22 |
-
# file_paths = glob.glob("data/*")
|
| 23 |
-
# ds = indexing_pipeline.get_node("DocumentStore")
|
| 24 |
-
# ds.delete_all_documents()
|
| 25 |
-
# indexing_pipeline.run(file_paths=file_paths)
|
| 26 |
-
# ds.update_embeddings(indexing_pipeline.get_node("Retriever"))
|
| 27 |
-
# ds.save(config_path="my_faiss_config.json", index_path="my_faiss_index.faiss")
|
| 28 |
|
| 29 |
-
os.environ["OPENAI_API_KEY"] = st.secrets["OPENAI_API_KEY"]
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
|
| 38 |
def main():
|
| 39 |
-
app_init()
|
| 40 |
st.title("Haystack Demo")
|
| 41 |
input = st.text_input("Query ...")
|
| 42 |
|
|
@@ -49,13 +81,13 @@ def main():
|
|
| 49 |
|
| 50 |
with col_1:
|
| 51 |
st.text("PLAIN")
|
| 52 |
-
answers =
|
| 53 |
for ans in answers:
|
| 54 |
st.text(ans.answer)
|
| 55 |
|
| 56 |
with col_2:
|
| 57 |
st.write(query_type.upper())
|
| 58 |
-
answers =
|
| 59 |
for ans in answers:
|
| 60 |
st.text(ans.answer)
|
| 61 |
|
|
|
|
| 5 |
|
| 6 |
import streamlit as st
|
| 7 |
from haystack import Pipeline
|
| 8 |
+
from haystack.nodes import Shaper, PromptNode, PromptTemplate
|
| 9 |
+
from haystack.schema import Document
|
| 10 |
|
| 11 |
logging.basicConfig(
|
| 12 |
level=logging.DEBUG,
|
|
|
|
| 19 |
p_2 = None
|
| 20 |
|
| 21 |
|
| 22 |
+
def get_plain_pipeline():
|
| 23 |
+
prompt_open_ai = PromptModel(model_name_or_path="text-davinci-003", api_key=api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
|
|
|
| 25 |
|
| 26 |
+
# Now let make one PromptNode use the default model and the other one the OpenAI model:
|
| 27 |
+
plain_llm_template = PromptTemplate(name="plain_llm", prompt_text="Answer the following question: $query")
|
| 28 |
+
node_openai = PromptNode(prompt_open_ai, default_prompt_template=plain_llm_template, max_length=300)
|
| 29 |
+
|
| 30 |
+
pipeline = Pipeline()
|
| 31 |
+
pipeline.add_node(component=node_openai, name="prompt_node", inputs=["Query"])
|
| 32 |
+
return pipeline
|
| 33 |
+
|
| 34 |
+
def get_ret_aug_pipeline():
|
| 35 |
+
ds = FAISSDocumentStore(faiss_index_path="my_faiss_index.faiss",
|
| 36 |
+
faiss_config_path="my_faiss_index.json")
|
| 37 |
+
|
| 38 |
+
retriever = EmbeddingRetriever(
|
| 39 |
+
document_store=ds,
|
| 40 |
+
embedding_model="sentence-transformers/multi-qa-mpnet-base-dot-v1",
|
| 41 |
+
model_format="sentence_transformers",
|
| 42 |
+
top_k=2
|
| 43 |
+
)
|
| 44 |
+
shaper = Shaper(func="join_documents", inputs={"documents": "documents"}, outputs=["documents"])
|
| 45 |
+
|
| 46 |
+
default_template= PromptTemplate(
|
| 47 |
+
name="question-answering",
|
| 48 |
+
prompt_text="Given the context please answer the question. Context: $documents; Question: "
|
| 49 |
+
"$query; Answer:",
|
| 50 |
+
)
|
| 51 |
+
# Let's initiate the PromptNode
|
| 52 |
+
node = PromptNode("text-davinci-003", default_prompt_template=default_template, api_key=api_key, max_length=500)
|
| 53 |
|
| 54 |
+
# Let's create a pipeline with Shaper and PromptNode
|
| 55 |
+
pipe = Pipeline()
|
| 56 |
+
pipe.add_node(component=retriever, name='retriever', inputs=['Query'])
|
| 57 |
+
pipe.add_node(component=shaper, name="shaper", inputs=["retriever"])
|
| 58 |
+
pipe.add_node(component=node, name="prompt_node", inputs=["shaper"])
|
| 59 |
+
return pipe
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def app_init():
|
| 63 |
+
|
| 64 |
+
os.environ["OPENAI_API_KEY"] = st.secrets["OPENAI_API_KEY"]
|
| 65 |
+
p1 = get_plain_pipeline()
|
| 66 |
+
p2 = get_ret_aug_pipeline()
|
| 67 |
+
return p1, p2
|
| 68 |
|
| 69 |
|
| 70 |
def main():
|
| 71 |
+
p1, p2 = app_init()
|
| 72 |
st.title("Haystack Demo")
|
| 73 |
input = st.text_input("Query ...")
|
| 74 |
|
|
|
|
| 81 |
|
| 82 |
with col_1:
|
| 83 |
st.text("PLAIN")
|
| 84 |
+
answers = p1.run(input)["answers"]
|
| 85 |
for ans in answers:
|
| 86 |
st.text(ans.answer)
|
| 87 |
|
| 88 |
with col_2:
|
| 89 |
st.write(query_type.upper())
|
| 90 |
+
answers = p2.run(input)["answers"]
|
| 91 |
for ans in answers:
|
| 92 |
st.text(ans.answer)
|
| 93 |
|
faiss_document_store.db
CHANGED
|
Binary files a/faiss_document_store.db and b/faiss_document_store.db differ
|
|
|
my_faiss_index.faiss
CHANGED
|
Binary files a/my_faiss_index.faiss and b/my_faiss_index.faiss differ
|
|
|
my_faiss_index.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"faiss_index_factory_str": "Flat"}
|