Zaious commited on
Commit
277e513
ยท
verified ยท
1 Parent(s): f06cf27

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -3
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
- max_tokens=500
 
 
 
104
  )
105
 
106
  analysis = analysis_response.choices[0].message.content
 
 
 
 
107
 
108
- print("AAA:" + analysis)
109
 
110
  # Generate embedding for the query
111
- query_embedding = get_embedding(query + " " + analysis)
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)