|
import streamlit as st |
|
from haystack import Pipeline |
|
from haystack_integrations.document_stores.pinecone import PineconeDocumentStore |
|
from haystack.components.builders.answer_builder import AnswerBuilder |
|
from haystack.components.builders.prompt_builder import PromptBuilder |
|
from haystack_integrations.components.embedders.cohere import CohereTextEmbedder |
|
from haystack_integrations.components.retrievers.pinecone import PineconeEmbeddingRetriever |
|
from haystack_integrations.components.generators.cohere import CohereGenerator |
|
from haystack import Document |
|
|
|
def start_haystack(openai_key): |
|
document_store = PineconeDocumentStore(dimension=1024, index="zen", environment = "gcp-starter") |
|
|
|
template = """ |
|
You are a support agent replying to customers' messages. Use the context to answer the customer, starting by greeting them and ending with goodbyes. |
|
|
|
DO NOT TRY TO GUESS INFORMATION. If the context doesn't provide you with the answer, ONLY say this: []. |
|
|
|
Context: |
|
{% for document in documents %} |
|
{{ document.content }} |
|
{% endfor %} |
|
|
|
Customer's message: {{ query }}? |
|
""" |
|
|
|
st.session_state["haystack_started"] = True |
|
|
|
pipe = Pipeline() |
|
|
|
pipe.add_component("text_embedder", CohereTextEmbedder(model="embed-english-v3.0")) |
|
pipe.add_component("retriever", PineconeEmbeddingRetriever(document_store=document_store, top_k=3)) |
|
pipe.add_component("prompt_builder", PromptBuilder(template=template)) |
|
pipe.add_component("llm", CohereGenerator(model="command-nightly")) |
|
pipe.add_component("answer_builder", AnswerBuilder()) |
|
|
|
pipe.connect("text_embedder.embedding", "retriever.query_embedding") |
|
pipe.connect("retriever", "prompt_builder.documents") |
|
pipe.connect("prompt_builder", "llm") |
|
pipe.connect("llm.replies", "answer_builder.replies") |
|
pipe.connect("llm.meta", "answer_builder.meta") |
|
pipe.connect("retriever", "answer_builder.documents") |
|
|
|
return pipe |
|
|
|
|
|
@st.cache_data(show_spinner=True) |
|
def query(username, _pipe): |
|
try: |
|
question = "It doesn't work on Android. The app is not blocking call!!!" |
|
|
|
replies = _pipe.run({ |
|
"text_embedder": { |
|
"text": question |
|
}, |
|
"prompt_builder": { |
|
"query": question |
|
}, |
|
"answer_builder": { |
|
"query": question |
|
} |
|
}) |
|
|
|
result = replies['answer_builder']['answers'] |
|
print(result) |
|
except Exception as e: |
|
print("Hay:") |
|
print(e) |
|
result = ["Something went wrong!"] |
|
return result |