Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -34,23 +34,33 @@ try:
|
|
34 |
Document(content="Victor, a retired astronaut in Houston, is contacted by an alien intelligence through a mysterious transmission. As he seeks to uncover the truth, he is drawn into an interstellar adventure that reveals the universe's greatest mysteries and humanity's place among the stars.")
|
35 |
]
|
36 |
|
|
|
37 |
document_store = MongoDBAtlasDocumentStore(
|
38 |
-
database_name="sample_mflix",
|
39 |
-
collection_name="haystack_embedded_movies",
|
40 |
-
vector_search_index="default",
|
41 |
)
|
|
|
|
|
42 |
doc_writer = DocumentWriter(document_store=document_store, policy=DuplicatePolicy.SKIP)
|
|
|
|
|
43 |
doc_embedder = OpenAIDocumentEmbedder()
|
44 |
-
|
|
|
45 |
indexing_pipe = Pipeline()
|
46 |
indexing_pipe.add_component(instance=doc_embedder, name="doc_embedder")
|
47 |
indexing_pipe.add_component(instance=doc_writer, name="doc_writer")
|
48 |
|
|
|
49 |
indexing_pipe.connect("doc_embedder.documents", "doc_writer.documents")
|
|
|
|
|
50 |
indexing_pipe.run({"doc_embedder": {"documents": documents}})
|
51 |
-
|
|
|
52 |
prompt_template = """
|
53 |
-
You are a movie recommendation engine use the following
|
54 |
{% for doc in documents %}
|
55 |
{{ doc.content }}
|
56 |
{% endfor %}
|
@@ -58,20 +68,28 @@ try:
|
|
58 |
\Query: {{query}}
|
59 |
\nAnswer:
|
60 |
"""
|
61 |
-
|
|
|
62 |
rag_pipeline = Pipeline()
|
63 |
rag_pipeline.add_component("text_embedder", OpenAITextEmbedder())
|
64 |
-
|
65 |
|
|
|
66 |
rag_pipeline.add_component(instance=MongoDBAtlasEmbeddingRetriever(document_store=document_store,top_k=15), name="retriever")
|
|
|
|
|
67 |
rag_pipeline.add_component(instance=PromptBuilder(template=prompt_template), name="prompt_builder")
|
|
|
|
|
68 |
rag_pipeline.add_component(instance=OpenAIGenerator(), name="llm")
|
|
|
|
|
69 |
rag_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
70 |
rag_pipeline.connect("retriever", "prompt_builder.documents")
|
71 |
rag_pipeline.connect("prompt_builder", "llm")
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
75 |
|
76 |
|
77 |
def get_movies(message, history):
|
|
|
34 |
Document(content="Victor, a retired astronaut in Houston, is contacted by an alien intelligence through a mysterious transmission. As he seeks to uncover the truth, he is drawn into an interstellar adventure that reveals the universe's greatest mysteries and humanity's place among the stars.")
|
35 |
]
|
36 |
|
37 |
+
# Initializing a document store in MongoDB Atlas to store the narrative documents.
|
38 |
document_store = MongoDBAtlasDocumentStore(
|
39 |
+
database_name="sample_mflix", # Specifies the database name.
|
40 |
+
collection_name="haystack_embedded_movies", # Specifies the collection name where documents will be stored.
|
41 |
+
vector_search_index="default", # The search index for vectorized content.
|
42 |
)
|
43 |
+
|
44 |
+
# Setting up a document writer to handle the insertion of documents into the MongoDB collection.
|
45 |
doc_writer = DocumentWriter(document_store=document_store, policy=DuplicatePolicy.SKIP)
|
46 |
+
|
47 |
+
# Initializing a document embedder to convert text content into vectorized form.
|
48 |
doc_embedder = OpenAIDocumentEmbedder()
|
49 |
+
|
50 |
+
# Creating a pipeline for indexing documents. The pipeline includes embedding and writing documents.
|
51 |
indexing_pipe = Pipeline()
|
52 |
indexing_pipe.add_component(instance=doc_embedder, name="doc_embedder")
|
53 |
indexing_pipe.add_component(instance=doc_writer, name="doc_writer")
|
54 |
|
55 |
+
# Connecting the components of the pipeline for document flow.
|
56 |
indexing_pipe.connect("doc_embedder.documents", "doc_writer.documents")
|
57 |
+
|
58 |
+
# Running the pipeline with the list of documents to index them in MongoDB.
|
59 |
indexing_pipe.run({"doc_embedder": {"documents": documents}})
|
60 |
+
|
61 |
+
# Template for generating prompts for a movie recommendation engine.
|
62 |
prompt_template = """
|
63 |
+
You are a movie recommendation engine use the following context documents.\nDocuments:
|
64 |
{% for doc in documents %}
|
65 |
{{ doc.content }}
|
66 |
{% endfor %}
|
|
|
68 |
\Query: {{query}}
|
69 |
\nAnswer:
|
70 |
"""
|
71 |
+
|
72 |
+
# Setting up a retrieval-augmented generation (RAG) pipeline for generating responses.
|
73 |
rag_pipeline = Pipeline()
|
74 |
rag_pipeline.add_component("text_embedder", OpenAITextEmbedder())
|
|
|
75 |
|
76 |
+
# Adding a component for retrieving related documents from MongoDB based on the query embedding.
|
77 |
rag_pipeline.add_component(instance=MongoDBAtlasEmbeddingRetriever(document_store=document_store,top_k=15), name="retriever")
|
78 |
+
|
79 |
+
# Building prompts based on retrieved documents to be used for generating responses.
|
80 |
rag_pipeline.add_component(instance=PromptBuilder(template=prompt_template), name="prompt_builder")
|
81 |
+
|
82 |
+
# Adding a language model generator to produce the final text output.
|
83 |
rag_pipeline.add_component(instance=OpenAIGenerator(), name="llm")
|
84 |
+
|
85 |
+
# Connecting the components of the RAG pipeline to ensure proper data flow.
|
86 |
rag_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
87 |
rag_pipeline.connect("retriever", "prompt_builder.documents")
|
88 |
rag_pipeline.connect("prompt_builder", "llm")
|
89 |
+
|
90 |
+
# Exception handling to catch and display errors during the pipeline execution.
|
91 |
+
except Exception as e:
|
92 |
+
print("An error occurred: \n" + error_message)
|
93 |
|
94 |
|
95 |
def get_movies(message, history):
|