Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,8 +12,6 @@ from langchain_core.output_parsers import StrOutputParser
|
|
| 12 |
|
| 13 |
output_parser = StrOutputParser()
|
| 14 |
|
| 15 |
-
# from langchain_community.prompts import PromptTemplate
|
| 16 |
-
# from langchain.chains import LLMChain
|
| 17 |
import json
|
| 18 |
|
| 19 |
|
|
@@ -24,35 +22,35 @@ db_name = 'sample_mflix'
|
|
| 24 |
collection_name = 'embedded_movies'
|
| 25 |
collection = client[db_name][collection_name]
|
| 26 |
|
| 27 |
-
|
| 28 |
-
vector_store = MongoDBAtlasVectorSearch(embedding=OpenAIEmbeddings(), collection=collection, index_name='vector_index', text_key='plot', embedding_key='plot_embedding')
|
| 29 |
-
llm = ChatOpenAI()
|
| 30 |
-
prompt = ChatPromptTemplate.from_messages([
|
| 31 |
-
("system", "You are a movie recommendation engine please elaborate on movies."),
|
| 32 |
-
("user", "List of movies: {input}")
|
| 33 |
-
|
| 34 |
-
chain = prompt | llm | output_parser
|
| 35 |
|
| 36 |
-
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
-
|
| 40 |
|
| 41 |
def get_movies(message, history):
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
|
| 57 |
|
| 58 |
demo = gr.ChatInterface(get_movies, examples=["What movies are scary?", "Find me a comedy", "Movies for kids"], title="Movies Atlas Vector Search",description="This small chat uses a similarity search to find relevant movies, it uses an MongoDB Atlase Vector Search read more here: https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-tutorial",submit_btn="Search").queue()
|
|
|
|
| 12 |
|
| 13 |
output_parser = StrOutputParser()
|
| 14 |
|
|
|
|
|
|
|
| 15 |
import json
|
| 16 |
|
| 17 |
|
|
|
|
| 22 |
collection_name = 'embedded_movies'
|
| 23 |
collection = client[db_name][collection_name]
|
| 24 |
|
| 25 |
+
try:
|
| 26 |
+
vector_store = MongoDBAtlasVectorSearch(embedding=OpenAIEmbeddings(), collection=collection, index_name='vector_index', text_key='plot', embedding_key='plot_embedding')
|
| 27 |
+
llm = ChatOpenAI()
|
| 28 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 29 |
+
("system", "You are a movie recommendation engine please elaborate on movies."),
|
| 30 |
+
("user", "List of movies: {input}")
|
| 31 |
+
])
|
| 32 |
+
chain = prompt | llm | output_parser
|
| 33 |
|
| 34 |
+
except:
|
| 35 |
+
#If open ai key is wrong
|
| 36 |
+
print ('Open AI key is wrong')
|
| 37 |
+
vector_store = None
|
| 38 |
|
| 39 |
def get_movies(message, history):
|
| 40 |
|
| 41 |
+
try:
|
| 42 |
+
movies = vector_store.similarity_search(message, 3)
|
| 43 |
+
return_text = ''
|
| 44 |
+
for movie in movies:
|
| 45 |
+
return_text = return_text + 'Title : ' + movie.metadata['title'] + '\n------------\n' + 'Plot: ' + movie.page_content + '\n\n'
|
| 46 |
+
|
| 47 |
+
print_llm_text = chain.invoke({"input": return_text})
|
| 48 |
+
|
| 49 |
+
for i in range(len(print_llm_text)):
|
| 50 |
+
time.sleep(0.05)
|
| 51 |
+
yield "Found: " + "\n\n" + print_llm_text[: i+1]
|
| 52 |
+
except:
|
| 53 |
+
yield "Please clone the repo and add your open ai key as well as your MongoDB Atlas UR in the Secret Section of you Space\n OPENAI_API_KEY (your Open AI key) and MONGODB_ATLAS_CLUSTER_URI (0.0.0.0/0 whitelisted instance with Vector index created) \n\n For more information : https://mongodb.com/products/platform/atlas-vector-search"
|
| 54 |
|
| 55 |
|
| 56 |
demo = gr.ChatInterface(get_movies, examples=["What movies are scary?", "Find me a comedy", "Movies for kids"], title="Movies Atlas Vector Search",description="This small chat uses a similarity search to find relevant movies, it uses an MongoDB Atlase Vector Search read more here: https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-tutorial",submit_btn="Search").queue()
|