Spaces:
Sleeping
Sleeping
tonyliu404
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -95,6 +95,79 @@ def vector_search(user_query, collection):
|
|
95 |
pipeline = [vector_search_stage, unset_stage, project_stage]
|
96 |
results = mongo_collection.aggregate(pipeline)
|
97 |
return list(results)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
print("HELLO WORLD")
|
100 |
-
st.title("
|
|
|
95 |
pipeline = [vector_search_stage, unset_stage, project_stage]
|
96 |
results = mongo_collection.aggregate(pipeline)
|
97 |
return list(results)
|
98 |
+
|
99 |
+
def mongo_retriever(query):
|
100 |
+
documents = vector_search(query, mongo_collection)
|
101 |
+
return documents
|
102 |
+
|
103 |
+
|
104 |
+
template = """
|
105 |
+
You are an assistant for generating results based on user questions.
|
106 |
+
Use the provided context to generate a result based on the following JSON format:
|
107 |
+
{{
|
108 |
+
"name": "Recipe Name",
|
109 |
+
"minutes": 0,
|
110 |
+
"tags": [
|
111 |
+
"tag1",
|
112 |
+
"tag2",
|
113 |
+
"tag3"
|
114 |
+
],
|
115 |
+
"n_steps": 0,
|
116 |
+
"description": "A GENERAL description of the recipe goes here.",
|
117 |
+
"ingredients": [
|
118 |
+
"ingredient1",
|
119 |
+
"ingredient2",
|
120 |
+
"ingredient3"
|
121 |
+
],
|
122 |
+
"n_ingredients": 0,
|
123 |
+
"formatted_nutrition": [
|
124 |
+
"Calorie : per serving",
|
125 |
+
"Total Fat : % daily value",
|
126 |
+
"Sugar : % daily value",
|
127 |
+
"Sodium : % daily value",
|
128 |
+
"Protein : % daily value",
|
129 |
+
"Saturated Fat : % daily value",
|
130 |
+
"Total Carbohydrate : % daily value"
|
131 |
+
],
|
132 |
+
"formatted_steps": [
|
133 |
+
"1. Step 1 of the recipe.",
|
134 |
+
"2. Step 2 of the recipe.",
|
135 |
+
"3. Step 3 of the recipe."
|
136 |
+
]
|
137 |
+
}}
|
138 |
+
|
139 |
+
Instructions:
|
140 |
+
1. Focus on the user's specific request and avoid irrelevant ingredients or approaches.
|
141 |
+
2. Do not return anything other than the JSON.
|
142 |
+
3. If the answer is unclear or the context does not fully address the prompt, return []
|
143 |
+
4. Base the response on simple, healthy, and accessible ingredients and techniques.
|
144 |
+
5. Rewrite the description in third person
|
145 |
+
|
146 |
+
Context: {context}
|
147 |
+
|
148 |
+
When choosing a recipe from the context, FOLLOW these instructions:
|
149 |
+
1. The recipe should be makeable from scratch, using only proper ingredients and not other dishes or pre-made recipes
|
150 |
+
|
151 |
+
Question: {question}
|
152 |
+
"""
|
153 |
+
|
154 |
+
custom_rag_prompt = ChatPromptTemplate.from_template(template)
|
155 |
+
|
156 |
+
|
157 |
+
llm = ChatOpenAI(
|
158 |
+
model_name="gpt-3.5-turbo",
|
159 |
+
temperature=0.2)
|
160 |
+
|
161 |
+
|
162 |
+
rag_chain = (
|
163 |
+
{"context": mongo_retriever, "question": RunnablePassthrough()}
|
164 |
+
| custom_rag_prompt
|
165 |
+
| llm
|
166 |
+
| StrOutputParser()
|
167 |
+
)
|
168 |
+
|
169 |
+
def get_response(query):
|
170 |
+
return rag_chain.invoke(query)
|
171 |
|
172 |
print("HELLO WORLD")
|
173 |
+
st.title("RESSSSULTS")
|