malte commited on
Commit
892aae2
1 Parent(s): cfc6a06

update to haystack 1.17.1: simplify pipelines, remove shaper

Browse files
Files changed (3) hide show
  1. app.py +2 -1
  2. requirements.txt +1 -1
  3. utils/backend.py +10 -13
app.py CHANGED
@@ -47,8 +47,9 @@ if st.session_state.get('query') and run_pressed:
47
  '\n This may take a few mins and might also fail if OpenAI API server is down.'):
48
  answers_2 = p3.run(ip)
49
  placeholder_retrieval_augmented.markdown(answers_2['results'][0])
 
50
  with st.expander("See source:"):
51
- src = answers_2['invocation_context']['documents'][0].replace("$", "\$")
52
  split_marker = "\n\n" if "\n\n" in src else "\n"
53
  src = " ".join(src.split(split_marker))[0:2000] + "..."
54
  st.write(src)
 
47
  '\n This may take a few mins and might also fail if OpenAI API server is down.'):
48
  answers_2 = p3.run(ip)
49
  placeholder_retrieval_augmented.markdown(answers_2['results'][0])
50
+ print(answers_2)
51
  with st.expander("See source:"):
52
+ src = answers_2['invocation_context']['documents'][0].content.replace("$", "\$")
53
  split_marker = "\n\n" if "\n\n" in src else "\n"
54
  src = " ".join(src.split(split_marker))[0:2000] + "..."
55
  st.write(src)
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- git+https://github.com/deepset-ai/haystack.git@ffd02c29f7cc83a119b6440bfbabaacdaaac3a19#egg=farm-haystack
2
  faiss-cpu==1.7.2
3
  sqlalchemy>=1.4.2,<2
4
  sqlalchemy_utils
 
1
+ farm-haystack==1.17.1
2
  faiss-cpu==1.7.2
3
  sqlalchemy>=1.4.2,<2
4
  sqlalchemy_utils
utils/backend.py CHANGED
@@ -9,7 +9,7 @@ from haystack.nodes.retriever.web import WebRetriever
9
  def get_plain_pipeline():
10
  prompt_open_ai = PromptModel(model_name_or_path="text-davinci-003", api_key=st.secrets["OPENAI_API_KEY"])
11
  # Now let make one PromptNode use the default model and the other one the OpenAI model:
12
- plain_llm_template = PromptTemplate(name="plain_llm", prompt_text="Answer the following question: $query")
13
  node_openai = PromptNode(prompt_open_ai, default_prompt_template=plain_llm_template, max_length=300)
14
  pipeline = Pipeline()
15
  pipeline.add_node(component=node_openai, name="prompt_node", inputs=["Query"])
@@ -27,22 +27,21 @@ def get_retrieval_augmented_pipeline():
27
  model_format="sentence_transformers",
28
  top_k=2
29
  )
30
- shaper = Shaper(func="join_documents", inputs={"documents": "documents"}, outputs=["documents"])
31
 
32
  default_template = PromptTemplate(
33
  name="question-answering",
34
- prompt_text="Given the context please answer the question. Context: $documents; Question: "
35
- "$query; Answer:",
36
  )
 
37
  # Let's initiate the PromptNode
38
  node = PromptNode("text-davinci-003", default_prompt_template=default_template,
39
  api_key=st.secrets["OPENAI_API_KEY"], max_length=500)
40
 
41
- # Let's create a pipeline with Shaper and PromptNode
42
  pipeline = Pipeline()
43
  pipeline.add_node(component=retriever, name='retriever', inputs=['Query'])
44
- pipeline.add_node(component=shaper, name="shaper", inputs=["retriever"])
45
- pipeline.add_node(component=node, name="prompt_node", inputs=["shaper"])
46
  return pipeline
47
 
48
 
@@ -50,18 +49,16 @@ def get_retrieval_augmented_pipeline():
50
  def get_web_retrieval_augmented_pipeline():
51
  search_key = st.secrets["WEBRET_API_KEY"]
52
  web_retriever = WebRetriever(api_key=search_key, search_engine_provider="SerperDev")
53
- shaper = Shaper(func="join_documents", inputs={"documents": "documents"}, outputs=["documents"])
54
  default_template = PromptTemplate(
55
  name="question-answering",
56
- prompt_text="Given the context please answer the question. Context: $documents; Question: "
57
- "$query; Answer:",
58
  )
59
  # Let's initiate the PromptNode
60
  node = PromptNode("text-davinci-003", default_prompt_template=default_template,
61
  api_key=st.secrets["OPENAI_API_KEY"], max_length=500)
62
- # Let's create a pipeline with Shaper and PromptNode
63
  pipeline = Pipeline()
64
  pipeline.add_node(component=web_retriever, name='retriever', inputs=['Query'])
65
- pipeline.add_node(component=shaper, name="shaper", inputs=["retriever"])
66
- pipeline.add_node(component=node, name="prompt_node", inputs=["shaper"])
67
  return pipeline
 
9
  def get_plain_pipeline():
10
  prompt_open_ai = PromptModel(model_name_or_path="text-davinci-003", api_key=st.secrets["OPENAI_API_KEY"])
11
  # Now let make one PromptNode use the default model and the other one the OpenAI model:
12
+ plain_llm_template = PromptTemplate(name="plain_llm", prompt_text="Answer the following question: {query}")
13
  node_openai = PromptNode(prompt_open_ai, default_prompt_template=plain_llm_template, max_length=300)
14
  pipeline = Pipeline()
15
  pipeline.add_node(component=node_openai, name="prompt_node", inputs=["Query"])
 
27
  model_format="sentence_transformers",
28
  top_k=2
29
  )
 
30
 
31
  default_template = PromptTemplate(
32
  name="question-answering",
33
+ prompt_text="Given the context please answer the question. Context: {join(documents)}; Question: "
34
+ "{query}; Answer:",
35
  )
36
+ print(default_template.prompt_text)
37
  # Let's initiate the PromptNode
38
  node = PromptNode("text-davinci-003", default_prompt_template=default_template,
39
  api_key=st.secrets["OPENAI_API_KEY"], max_length=500)
40
 
41
+ # Let's create a simple retrieval augmented pipeline with the retriever + PromptNode
42
  pipeline = Pipeline()
43
  pipeline.add_node(component=retriever, name='retriever', inputs=['Query'])
44
+ pipeline.add_node(component=node, name="prompt_node", inputs=["retriever"])
 
45
  return pipeline
46
 
47
 
 
49
  def get_web_retrieval_augmented_pipeline():
50
  search_key = st.secrets["WEBRET_API_KEY"]
51
  web_retriever = WebRetriever(api_key=search_key, search_engine_provider="SerperDev")
 
52
  default_template = PromptTemplate(
53
  name="question-answering",
54
+ prompt_text="Given the context please answer the question. Context: {join(documents)}; Question: "
55
+ "{query}; Answer:",
56
  )
57
  # Let's initiate the PromptNode
58
  node = PromptNode("text-davinci-003", default_prompt_template=default_template,
59
  api_key=st.secrets["OPENAI_API_KEY"], max_length=500)
60
+ # Let's create a pipeline with the webretriever + PromptNode
61
  pipeline = Pipeline()
62
  pipeline.add_node(component=web_retriever, name='retriever', inputs=['Query'])
63
+ pipeline.add_node(component=node, name="prompt_node", inputs=["retriever"])
 
64
  return pipeline