Update utils/haystack.py
Browse files- 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.
|
5 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
def start_haystack(openai_key):
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
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 =
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
except Exception as e:
|
60 |
-
|
|
|
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
|