Last active
February 25, 2024 10:28
-
-
Save apotterri/7db7253e76c6f05d68e4b473070a2f2b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import appmap | |
r = appmap.Recording() | |
with r: | |
import rag | |
rag.run() | |
with os.fdopen(sys.stdout.fileno(), "w", closefd=False) as stdout: | |
stdout.write(appmap.generation.dump(r)) | |
stdout.flush() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import appmap | |
from appmap import Recording | |
from haystack import Document, Pipeline | |
from haystack.components.builders.prompt_builder import PromptBuilder | |
from haystack.components.generators import OpenAIGenerator | |
from haystack.components.retrievers.in_memory import InMemoryBM25Retriever | |
from haystack.document_stores.in_memory import InMemoryDocumentStore | |
from haystack.utils import Secret | |
def run(): | |
# Write documents to InMemoryDocumentStore | |
document_store = InMemoryDocumentStore() | |
document_store.write_documents([ | |
Document(content="My name is Jean and I live in Paris."), | |
Document(content="My name is Mark and I live in Berlin."), | |
Document(content="My name is Giorgio and I live in Rome.") | |
]) | |
# Build a RAG pipeline | |
prompt_template = """ | |
Given these documents, answer the question. | |
Documents: | |
{% for doc in documents %} | |
{{ doc.content }} | |
{% endfor %} | |
Question: {{question}} | |
Answer: | |
""" | |
retriever = InMemoryBM25Retriever(document_store=document_store) | |
prompt_builder = PromptBuilder(template=prompt_template) | |
llm = OpenAIGenerator() | |
rag_pipeline = Pipeline() | |
rag_pipeline.add_component("retriever", retriever) | |
rag_pipeline.add_component("prompt_builder", prompt_builder) | |
rag_pipeline.add_component("llm", llm) | |
rag_pipeline.connect("retriever", "prompt_builder.documents") | |
rag_pipeline.connect("prompt_builder", "llm") | |
# Ask a question | |
question = "Who lives in Paris?" | |
results = rag_pipeline.run( | |
{ | |
"retriever": {"query": question}, | |
"prompt_builder": {"question": question}, | |
} | |
) | |
print(results["llm"]["replies"]) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
appmap | |
haystack-ai |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment