| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import os |
|
|
| from haystack import Pipeline |
| from haystack.components.builders.prompt_builder import PromptBuilder |
| from haystack.components.generators.openai.gpt35 import GPT35Generator |
| from haystack.components.retrievers.memory import MemoryBM25Retriever |
| from haystack.dataclasses.document import Document |
| from haystack.document_stores.memory import MemoryDocumentStore |
|
|
| docstore = MemoryDocumentStore() |
|
|
| |
| docstore.write_documents( |
| [ |
| Document(content="This is not the answer you are looking for.", meta={"name": "Obi-Wan Kenobi"}), |
| Document(content="This is the way.", meta={"name": "Mandalorian"}), |
| Document(content="The answer to life, the universe and everything is 42.", meta={"name": "Deep Thought"}), |
| Document(content="When you play the game of thrones, you win or you die.", meta={"name": "Cersei Lannister"}), |
| Document(content="Winter is coming.", meta={"name": "Ned Stark"}), |
| ] |
| ) |
|
|
| |
| retriever = MemoryBM25Retriever(document_store=docstore, top_k=3) |
|
|
| |
| template = """Given the context please answer the question. |
| Context: |
| {# We're receiving a list of lists, so we handle it like this #} |
| {% for list in documents %} |
| {% for doc in list %} |
| {{- doc -}}; |
| {% endfor %} |
| {% endfor %} |
| Question: {{ question }}; |
| Answer: |
| """ |
| prompt_builder = PromptBuilder(template) |
|
|
| |
| OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY", "") |
| generator = GPT35Generator(api_key=OPENAI_API_KEY) |
|
|
| |
| pipe = Pipeline() |
|
|
| pipe.add_component("docs_retriever", retriever) |
| pipe.add_component("builder", prompt_builder) |
| pipe.add_component("gpt35", generator) |
|
|
| pipe.connect("docs_retriever.documents", "builder.documents") |
| pipe.connect("builder.prompt", "gpt35.prompt") |
|
|
| |
| query = "What is the answer to life, the universe and everything?" |
| result = pipe.run({"docs_retriever": {"queries": [query]}, "builder": {"question": query}}) |
|
|
| print(result["gpt35"]["replies"]) |
|
|
|
|
| |
| inputs = [ |
| {"docs_retriever": {"queries": ["What is the answer?"]}, "builder": {"question": "What is the answer?"}}, |
| { |
| "docs_retriever": {"queries": ["Take a deep breath and think. What is the answer?"]}, |
| "builder": {"question": "Take a deep breath and think. What is the answer?"}, |
| }, |
| { |
| "docs_retriever": {"queries": ["What is the answer to life, the universe and everything?"]}, |
| "builder": {"question": "What is the answer to life, the universe and everything?"}, |
| }, |
| ] |
|
|
| |
| |
| |
| |
| expected_output = [ |
| { |
| |
| "docs_retriever": { |
| "documents": [ |
| [ |
| Document( |
| content="The answer to life, the universe and everything is 42.", meta={"name": "Deep Thought"} |
| ), |
| Document(content="This is not the answer you are looking for.", meta={"name": "Obi-Wan Kenobi"}), |
| Document(content="This is the way.", meta={"name": "Mandalorian"}), |
| ] |
| ] |
| }, |
| |
| "builder": {"prompt": "I should write the actual template here but I'm lazy so I won't."}, |
| |
| "gpt35": {"replies": ["The answer to life, the universe and everything is 42."], "metadata": {}}, |
| }, |
| { |
| "docs_retriever": { |
| "documents": [ |
| [ |
| Document( |
| content="The answer to life, the universe and everything is 42.", meta={"name": "Deep Thought"} |
| ), |
| Document(content="This is not the answer you are looking for.", meta={"name": "Obi-Wan Kenobi"}), |
| Document(content="This is the way.", meta={"name": "Mandalorian"}), |
| ] |
| ] |
| }, |
| "builder": {"prompt": "I should write the actual template here but I'm lazy so I won't."}, |
| "gpt35": {"replies": ["The answer to life, the universe and everything is 42."], "metadata": {}}, |
| }, |
| { |
| "docs_retriever": { |
| "documents": [ |
| [ |
| Document( |
| content="The answer to life, the universe and everything is 42.", meta={"name": "Deep Thought"} |
| ), |
| Document(content="This is not the answer you are looking for.", meta={"name": "Obi-Wan Kenobi"}), |
| Document(content="This is the way.", meta={"name": "Mandalorian"}), |
| ] |
| ] |
| }, |
| "builder": {"prompt": "I should write the actual template here but I'm lazy so I won't."}, |
| "gpt35": {"replies": ["The answer to life, the universe and everything is 42."], "metadata": {}}, |
| }, |
| ] |
|
|
| eval_result = eval(pipe, inputs=inputs, expected_output=expected_output) |
| metrics = result.calculate_metrics(Metric.SAS) |
| metrics.save("path/to/file.csv") |
|
|