sabatale commited on
Commit
5a92112
·
verified ·
1 Parent(s): 7ec00a3

Update utils/haystack.py

Browse files
Files changed (1) hide show
  1. utils/haystack.py +58 -49
utils/haystack.py CHANGED
@@ -1,61 +1,70 @@
1
  import streamlit as st
2
- from mastodon_fetcher_haystack.mastodon_fetcher import MastodonFetcher
3
  from haystack import Pipeline
4
- from haystack.components.generators import OpenAIGenerator
5
- from haystack.components.builders import PromptBuilder
 
 
 
 
 
 
 
6
 
7
  def start_haystack(openai_key):
8
- #Use this function to contruct a pipeline
9
- fetcher = MastodonFetcher()
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- mastodon_template = """You will be given a post stream belonging to a specific Mastodon profile. Answer with a summary of what they've lately been posting about and in what languages.
12
- You may go into some detail about what topics they tend to like postint about. Please also mention their overall tone, for example: positive,
13
- negative, political, sarcastic or something else.
14
-
15
- Examples:
16
-
17
- Post stream: [@deepset_ai](https://mastodon.social/@deepset_ai): Come join our Haystack server for our first Discord event tomorrow, a deepset AMA session with @rusic_milos @malte_pietsch…
18
- [@deepset_ai](https://mastodon.social/@deepset_ai): Join us for a chat! On Thursday 25th we are hosting a 'deepset - Ask Me Anything' session on our brand new Discord. Come…
19
- [@deepset_ai](https://mastodon.social/@deepset_ai): Curious about how you can use @OpenAI GPT3 in a Haystack pipeline? This week we released Haystack 1.7 with which we introdu…
20
- [@deepset_ai](https://mastodon.social/@deepset_ai): So many updates from @deepset_ai today!
21
-
22
- Summary: This user has lately been reposting posts from @deepset_ai. The topics of the posts have been around the Haystack community, NLP and GPT. They've
23
- been posting in English, and have had a positive, informative tone.
24
-
25
- Post stream: I've directed my team to set sharper rules on how we deal with unidentified objects.\n\nWe will inventory, improve ca…
26
- the incursion by China’s high-altitude balloon, we enhanced radar to pick up slower objects.\n \nBy doing so, w…
27
- I gave an update on the United States’ response to recent aerial objects.
28
-
29
- Summary: This user has lately been posting about having sharper rules to deal with unidentified objects and an incursuin by China's high-altitude
30
- baloon. Their pots have mostly been neutral but determined in tone. They mostly post in English.
31
-
32
- Post stream: {{ documents }}
33
-
34
- Summary:
35
- """
36
- prompt_builder = PromptBuilder(template=mastodon_template)
37
- llm = OpenAIGenerator(model_name="gpt-4", api_key=openai_key)
38
-
39
- st.session_state["haystack_started"] = True
40
-
41
- mastodon_pipeline = Pipeline()
42
- mastodon_pipeline.add_component("fetcher", fetcher)
43
- mastodon_pipeline.add_component("prompt_builder", prompt_builder)
44
- mastodon_pipeline.add_component("llm", llm)
45
-
46
-
47
- mastodon_pipeline.connect("fetcher.documents", "prompt_builder.documents")
48
- mastodon_pipeline.connect("prompt_builder.prompt", "llm.prompt")
49
-
50
- return mastodon_pipeline
51
 
52
 
53
  @st.cache_data(show_spinner=True)
54
  def query(username, _pipeline):
55
  try:
56
- replies = _pipeline.run(data={"fetcher": {"username": username,
57
- "last_k_posts": 20}})
58
- result = replies['llm']['replies']
 
 
 
 
 
 
 
 
 
 
 
59
  except Exception as e:
60
- result = ["Please make sure you are providing a correct, public Mastodon account"]
 
61
  return result
 
1
  import streamlit as st
 
2
  from haystack import Pipeline
3
+ from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
4
+ from haystack_integrations.document_stores.pinecone import PineconeDocumentStore
5
+ from haystack.components.builders.answer_builder import AnswerBuilder
6
+ from haystack.components.builders.prompt_builder import PromptBuilder
7
+ from haystack.document_stores.in_memory import InMemoryDocumentStore
8
+ from haystack_integrations.components.embedders.cohere import CohereTextEmbedder
9
+ from haystack_integrations.components.retrievers.pinecone import PineconeEmbeddingRetriever
10
+ from haystack_integrations.components.generators.cohere import CohereGenerator
11
+ from haystack import Document
12
 
13
  def start_haystack(openai_key):
14
+ document_store = PineconeDocumentStore(dimension=1024, index="zen", environment = "gcp-starter")
15
+ query = "It doesn't work on Android. The app is not blocking call!!!"
16
+
17
+ template = """
18
+ You are a support agent replying to customers' messages. Use the context to answer the customer, starting by greeting them and ending with goodbyes.
19
+
20
+ DO NOT TRY TO GUESS INFORMATION. If the context doesn't provide you with the answer, ONLY say this: [].
21
+
22
+ Context:
23
+ {% for document in documents %}
24
+ {{ document.content }}
25
+ {% endfor %}
26
+
27
+ Customer's message: {{ query }}?
28
+ """
29
 
30
+ st.session_state["haystack_started"] = True
31
+
32
+ pipe = Pipeline()
33
+
34
+ pipe.add_component("text_embedder", CohereTextEmbedder(model="embed-english-v3.0"))
35
+ pipe.add_component("retriever", PineconeEmbeddingRetriever(document_store=document_store, top_k=3))
36
+ pipe.add_component("prompt_builder", PromptBuilder(template=template))
37
+ pipe.add_component("llm", CohereGenerator(model="command-nightly"))
38
+ pipe.add_component("answer_builder", AnswerBuilder())
39
+
40
+ pipe.connect("text_embedder.embedding", "retriever.query_embedding")
41
+ pipe.connect("retriever", "prompt_builder.documents")
42
+ pipe.connect("prompt_builder", "llm")
43
+ pipe.connect("llm.replies", "answer_builder.replies")
44
+ pipe.connect("llm.meta", "answer_builder.meta")
45
+ pipe.connect("retriever", "answer_builder.documents")
46
+
47
+ return pipe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
 
50
  @st.cache_data(show_spinner=True)
51
  def query(username, _pipeline):
52
  try:
53
+ replies = pipe.run({
54
+ "text_embedder": {
55
+ "text": query
56
+ },
57
+ "prompt_builder": {
58
+ "query": query
59
+ },
60
+ "answer_builder": {
61
+ "query": query
62
+ }
63
+ })
64
+
65
+ print(result)
66
+ result = replies['answer_builder']['answers']
67
  except Exception as e:
68
+ print(e)
69
+ result = ["Something went wrong!"]
70
  return result