Spaces:
Runtime error
Runtime error
Update app.py
Browse files33 unmodified lines34353637383940414243444546474849505152535455561 unmodified line585960616263646566676869707172737475767733 unmodified linesDocument(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.")]document_store = MongoDBAtlasDocumentStore(database_name="sample_mflix",collection_name="haystack_embedded_movies",vector_search_index="default",)doc_writer = DocumentWriter(document_store=document_store, policy=DuplicatePolicy.SKIP)doc_embedder = OpenAIDocumentEmbedder()indexing_pipe = Pipeline()indexing_pipe.add_component(instance=doc_embedder, name="doc_embedder")indexing_pipe.add_component(instance=doc_writer, name="doc_writer")indexing_pipe.connect("doc_embedder.documents", "doc_writer.documents")indexing_pipe.run({"doc_embedder": {"documents": documents}})prompt_template = """You are a movie recommendation engine use the following cotext documents.\nDocuments:{% for doc in documents %}{{ doc.content }}{% endfor %}1 unmodified line\Query: {{query}}\nAnswer:"""rag_pipeline = Pipeline()rag_pipeline.add_component("text_embedder", OpenAITextEmbedder())rag_pipeline.add_component(instance=MongoDBAtlasEmbeddingRetriever(document_store=document_store,top_k=15), name="retriever")rag_pipeline.add_component(instance=PromptBuilder(template=prompt_template), name="prompt_builder")rag_pipeline.add_component(instance=OpenAIGenerator(), name="llm")rag_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")rag_pipeline.connect("retriever", "prompt_builder.documents")rag_pipeline.connect("prompt_builder", "llm")except Exception as e:print("An error occurred: \n" + error_message)def get_movies(message, history):33 unmodified lines3435363738394041424344454647484950515253545556575859606162636465661 unmodified line6869707172737475767778798081828384858687888990919293949533 unmodified linesDocument(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.")]# Initializing a document store in MongoDB Atlas to store the narrative documents.document_store = MongoDBAtlasDocumentStore(database_name="sample_mflix", # Specifies the database name.collection_name="haystack_embedded_movies", # Specifies the collection name where documents will be stored.vector_search_index="default", # The search index for vectorized content.)# Setting up a document writer to handle the insertion of documents into the MongoDB collection.doc_writer = DocumentWriter(document_store=document_store, policy=DuplicatePolicy.SKIP)# Initializing a document embedder to convert text content into vectorized form.doc_embedder = OpenAIDocumentEmbedder()# Creating a pipeline for indexing documents. The pipeline includes embedding and writing documents.indexing_pipe = Pipeline()indexing_pipe.add_component(instance=doc_embedder, name="doc_embedder")indexing_pipe.add_component(instance=doc_writer, name="doc_writer")# Connecting the components of the pipeline for document flow.indexing_pipe.connect("doc_embedder.documents", "doc_writer.documents")# Running the pipeline with the list of documents to index them in MongoDB.indexing_pipe.run({"doc_embedder": {"documents": documents}})# Template for generating prompts for a movie recommendation engine.prompt_template = """You are a movie recommendation engine use the following context documents.\nDocuments:{% for doc in documents %}{{ doc.content }}{% endfor %}1 unmodified line\Query: {{query}}\nAnswer:"""# Setting up a retrieval-augmented generation (RAG) pipeline for generating responses.rag_pipeline = Pipeline()rag_pipeline.add_component("text_embedder", OpenAITextEmbedder())# Adding a component for retrieving related documents from MongoDB based on the query embedding.rag_pipeline.add_component(instance=MongoDBAtlasEmbeddingRetriever(document_store=document_store,top_k=15), name="retriever")# Building prompts based on retrieved documents to be used for generating responses.rag_pipeline.add_component(instance=PromptBuilder(template=prompt_template), name="prompt_builder")# Adding a language model generator to produce the final text output.rag_pipeline.add_component(instance=OpenAIGenerator(), name="llm")# Connecting the components of the RAG pipeline to ensure proper data flow.rag_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")rag_pipeline.connect("retriever", "prompt_builder.documents")rag_pipeline.connect("prompt_builder", "llm")# Exception handling to catch and display errors during the pipeline execution.except Exception as e:print("An error occurred: \n" + error_message)def get_movies(message, history):
app.py+29-11