Spaces:
Running
Running
Upload app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ import os
|
|
4 |
import asyncio
|
5 |
from pymongo import MongoClient
|
6 |
from langchain_community.vectorstores import MongoDBAtlasVectorSearch
|
7 |
-
from
|
8 |
from langchain_community.llms import OpenAI
|
9 |
# from langchain_community.prompts import PromptTemplate
|
10 |
# from langchain.chains import LLMChain
|
@@ -15,7 +15,7 @@ import json
|
|
15 |
MONGODB_ATLAS_CLUSTER_URI = os.getenv('MONGODB_ATLAS_CLUSTER_URI')
|
16 |
client = MongoClient(MONGODB_ATLAS_CLUSTER_URI)
|
17 |
db_name = 'sample_mflix'
|
18 |
-
collection_name = '
|
19 |
collection = client[db_name][collection_name]
|
20 |
|
21 |
## Create a vector search index
|
@@ -38,11 +38,18 @@ print ('Creating vector search index')
|
|
38 |
vector_store = MongoDBAtlasVectorSearch(embedding=OpenAIEmbeddings(), collection=collection, index_name='vector_index', text_key='plot', embedding_key='plot_embedding')
|
39 |
|
40 |
def get_movies(message, history):
|
41 |
-
|
|
|
|
|
|
|
|
|
42 |
for movie in movies:
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
46 |
|
47 |
demo = gr.ChatInterface(get_movies, examples=["What movies are scary?", "Find me a comedy", "Movies for kids"], title="Movies Atlas Vector Search", submit_btn="Search").queue()
|
48 |
|
|
|
4 |
import asyncio
|
5 |
from pymongo import MongoClient
|
6 |
from langchain_community.vectorstores import MongoDBAtlasVectorSearch
|
7 |
+
from langchain_openai import OpenAIEmbeddings
|
8 |
from langchain_community.llms import OpenAI
|
9 |
# from langchain_community.prompts import PromptTemplate
|
10 |
# from langchain.chains import LLMChain
|
|
|
15 |
MONGODB_ATLAS_CLUSTER_URI = os.getenv('MONGODB_ATLAS_CLUSTER_URI')
|
16 |
client = MongoClient(MONGODB_ATLAS_CLUSTER_URI)
|
17 |
db_name = 'sample_mflix'
|
18 |
+
collection_name = 'embedded_movies'
|
19 |
collection = client[db_name][collection_name]
|
20 |
|
21 |
## Create a vector search index
|
|
|
38 |
vector_store = MongoDBAtlasVectorSearch(embedding=OpenAIEmbeddings(), collection=collection, index_name='vector_index', text_key='plot', embedding_key='plot_embedding')
|
39 |
|
40 |
def get_movies(message, history):
|
41 |
+
# Use AsyncIO to run the similarity search in the background
|
42 |
+
# movies = vector_store.similarity_search(message, 3)
|
43 |
+
print ('Searching for: ' + message)
|
44 |
+
movies = vector_store.similarity_search(message, 3)
|
45 |
+
retrun_text = ''
|
46 |
for movie in movies:
|
47 |
+
retrun_text = retrun_text + movie.metadata['title'] + '\n------------\n' + movie.page_content + '\n\n'
|
48 |
+
|
49 |
+
for i in range(len(retrun_text)):
|
50 |
+
time.sleep(0.05)
|
51 |
+
yield "Found: " + "\n\n" + retrun_text[: i+1]
|
52 |
+
|
53 |
|
54 |
demo = gr.ChatInterface(get_movies, examples=["What movies are scary?", "Find me a comedy", "Movies for kids"], title="Movies Atlas Vector Search", submit_btn="Search").queue()
|
55 |
|