Spaces:
Running
Running
adjusted session management
Browse files
app.py
CHANGED
@@ -76,22 +76,31 @@ async def retrieve_food_data(query):
|
|
76 |
async def stream_completion(message_history):
|
77 |
msg = cl.Message(content="")
|
78 |
await msg.send()
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
|
|
|
|
89 |
await msg.stream_token(token)
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
93 |
|
94 |
async def handle_conversation(user_query):
|
|
|
|
|
|
|
|
|
95 |
message_history = cl.user_session.get("message_history", [])
|
96 |
food_data = cl.user_session.get("food_data")
|
97 |
|
@@ -125,27 +134,28 @@ async def handle_chat_start():
|
|
125 |
message_history = cl.user_session.get("message_history", [])
|
126 |
if not message_history:
|
127 |
welcome_message = """
|
128 |
-
## Welcome to the USDA Food Assistant!
|
129 |
|
130 |
-
|
|
|
|
|
131 |
|
132 |
-
|
133 |
|
134 |
-
|
135 |
|
136 |
-
|
137 |
|
138 |
-
|
139 |
|
140 |
-
|
141 |
-
|
142 |
|
143 |
-
|
144 |
|
145 |
-
|
146 |
|
147 |
-
|
148 |
-
|
149 |
msg = cl.Message(content=welcome_message)
|
150 |
await msg.send()
|
151 |
cl.user_session.set("message_history", message_history)
|
|
|
76 |
async def stream_completion(message_history):
|
77 |
msg = cl.Message(content="")
|
78 |
await msg.send()
|
79 |
+
|
80 |
+
try:
|
81 |
+
stream = await client.chat.completions.create(
|
82 |
+
messages=message_history,
|
83 |
+
stream=True,
|
84 |
+
model="gpt-4o",
|
85 |
+
temperature=0
|
86 |
+
)
|
87 |
+
|
88 |
+
response_content = ""
|
89 |
+
async for part in stream:
|
90 |
+
token = part.choices[0].delta.content or ""
|
91 |
await msg.stream_token(token)
|
92 |
+
response_content += token
|
93 |
+
|
94 |
+
message_history.append({"role": "assistant", "content": response_content})
|
95 |
+
await msg.update()
|
96 |
+
except Exception as e:
|
97 |
+
logging.error(f"Error during stream completion: {e}")
|
98 |
|
99 |
async def handle_conversation(user_query):
|
100 |
+
if not cl.user_session:
|
101 |
+
logging.error("Session disconnected.")
|
102 |
+
return
|
103 |
+
|
104 |
message_history = cl.user_session.get("message_history", [])
|
105 |
food_data = cl.user_session.get("food_data")
|
106 |
|
|
|
134 |
message_history = cl.user_session.get("message_history", [])
|
135 |
if not message_history:
|
136 |
welcome_message = """
|
|
|
137 |
|
138 |
+
## Welcome to the USDA Food Assistant!
|
139 |
+
|
140 |
+
---
|
141 |
|
142 |
+
### Platform Overview
|
143 |
|
144 |
+
This platform is natively connected to the USDA's FoodData Central (FDC) Dataset, which includes comprehensive details about the ingredient and nutrient profiling of all foods available on US shelves. Whether you're looking for information on a specific branded food item or general ingredient, we've got the data you need to make informed choices.
|
145 |
|
146 |
+
---
|
147 |
|
148 |
+
### How It Works
|
149 |
|
150 |
+
- **Step 1:** To begin, simply provide the name of the food item you're interested in.
|
151 |
+
- **Step 2:** Once the food data is retrieved, you can ask more detailed questions regarding the nutrients, ingredients, or potential allergens specific to that food.
|
152 |
|
153 |
+
**Note:** Nutrient values are provided per 100 grams of the edible portion of food. If you need serving size data, we can provide that as well, with clear explanations on how the nutrient values apply.
|
154 |
|
155 |
+
Feel free to ask about any food or nutrient details, and we'll provide the most accurate information available!
|
156 |
|
157 |
+
---
|
158 |
+
"""
|
159 |
msg = cl.Message(content=welcome_message)
|
160 |
await msg.send()
|
161 |
cl.user_session.set("message_history", message_history)
|