Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -78,6 +78,30 @@ def find_similar_products(query_embedding, top_k=8):
|
|
| 78 |
|
| 79 |
return matching_products
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
# Analyze query and find products
|
| 83 |
def analyze_query_and_find_products(query: str) -> str:
|
|
@@ -100,15 +124,22 @@ def analyze_query_and_find_products(query: str) -> str:
|
|
| 100 |
model="gpt-4o",
|
| 101 |
messages=analysis_messages,
|
| 102 |
temperature=0.7,
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
| 104 |
)
|
| 105 |
|
| 106 |
analysis = analysis_response.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
print("AAA:" +
|
| 109 |
|
| 110 |
# Generate embedding for the query
|
| 111 |
-
query_embedding = get_embedding(query + " " +
|
| 112 |
|
| 113 |
# Find similar products
|
| 114 |
matching_products = find_similar_products(query_embedding)
|
|
|
|
| 78 |
|
| 79 |
return matching_products
|
| 80 |
|
| 81 |
+
json_schema = {
|
| 82 |
+
"name": "CookingIngredientsSchema",
|
| 83 |
+
"description": "Extract cooking ingredients and analysis from user query.",
|
| 84 |
+
"strict": True,
|
| 85 |
+
"schema": {
|
| 86 |
+
"type": "object",
|
| 87 |
+
"properties": {
|
| 88 |
+
"analysis": {
|
| 89 |
+
"type": "string",
|
| 90 |
+
"description": "ๅฎๆด็้ๆฑๅๆ๏ผ่งฃ้็จๆถ็็ฎๆจๅ้่ฆ็็ฉๅใ"
|
| 91 |
+
},
|
| 92 |
+
"ingredients": {
|
| 93 |
+
"type": "array",
|
| 94 |
+
"description": "ๆๅ็้ฃๆๆ้้ต็ฉๅๆธ
ๅฎใ",
|
| 95 |
+
"items": {
|
| 96 |
+
"type": "string",
|
| 97 |
+
"description": "ๅฎไธ้ฃๆๆ้้ต็ฉๅๅ็จฑใ"
|
| 98 |
+
}
|
| 99 |
+
}
|
| 100 |
+
},
|
| 101 |
+
"required": ["analysis", "ingredients"],
|
| 102 |
+
"additionalProperties": False
|
| 103 |
+
}
|
| 104 |
+
}
|
| 105 |
|
| 106 |
# Analyze query and find products
|
| 107 |
def analyze_query_and_find_products(query: str) -> str:
|
|
|
|
| 124 |
model="gpt-4o",
|
| 125 |
messages=analysis_messages,
|
| 126 |
temperature=0.7,
|
| 127 |
+
response_format={
|
| 128 |
+
"type": "json_schema",
|
| 129 |
+
"json_schema": json_schema
|
| 130 |
+
}
|
| 131 |
)
|
| 132 |
|
| 133 |
analysis = analysis_response.choices[0].message.content
|
| 134 |
+
analysis_json = json.loads(analysis)
|
| 135 |
+
|
| 136 |
+
description = analysis_json["analysis"] # ๅๅพๅๆ็ตๆ
|
| 137 |
+
ingredients = analysis_json["ingredients"] # ๅๅพๆๅ็้ฃๆๆธ
ๅฎ
|
| 138 |
|
| 139 |
+
print("AAA:" + ingredients)
|
| 140 |
|
| 141 |
# Generate embedding for the query
|
| 142 |
+
query_embedding = get_embedding(query + " " + description)
|
| 143 |
|
| 144 |
# Find similar products
|
| 145 |
matching_products = find_similar_products(query_embedding)
|