jacktol commited on
Commit
fa5738e
1 Parent(s): b1238e4

adjusted session management

Browse files
Files changed (2) hide show
  1. app.py +14 -10
  2. requirements.txt +1 -0
app.py CHANGED
@@ -75,7 +75,7 @@ async def retrieve_food_data(query):
75
 
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(
@@ -85,11 +85,14 @@ async def stream_completion(message_history):
85
  temperature=0
86
  )
87
 
 
88
  async for part in stream:
89
  token = part.choices[0].delta.content or ""
90
  await msg.stream_token(token)
 
91
 
92
- await msg.send()
 
93
  except Exception as e:
94
  logging.error(f"Error during stream completion: {e}")
95
 
@@ -128,9 +131,10 @@ async def handle_message(message: cl.Message):
128
 
129
  @cl.on_chat_start
130
  async def handle_chat_start():
131
- message_history = cl.user_session.get("message_history", [])
132
- if not message_history:
133
- welcome_message = """
 
134
 
135
  ## Welcome to the USDA Food Assistant!
136
 
@@ -138,7 +142,7 @@ async def handle_chat_start():
138
 
139
  ### Platform Overview
140
 
141
- 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.
142
 
143
  ---
144
 
@@ -146,13 +150,13 @@ This platform is natively connected to the USDA's FoodData Central (FDC) Dataset
146
 
147
  - **Step 1:** To begin, simply provide the name of the food item you're interested in.
148
  - **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.
149
-
150
  **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.
151
 
152
  Feel free to ask about any food or nutrient details, and we'll provide the most accurate information available!
153
 
154
  ---
 
155
  """
156
- msg = cl.Message(content=welcome_message)
157
- await msg.send()
158
- cl.user_session.set("message_history", message_history)
 
75
 
76
  async def stream_completion(message_history):
77
  msg = cl.Message(content="")
78
+ await msg.send() # Initialize streaming message
79
 
80
  try:
81
  stream = await client.chat.completions.create(
 
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.send() # Finalize message after streaming
96
  except Exception as e:
97
  logging.error(f"Error during stream completion: {e}")
98
 
 
131
 
132
  @cl.on_chat_start
133
  async def handle_chat_start():
134
+ if cl.user_session.get("message_history") is None:
135
+ cl.user_session.set("message_history", [])
136
+
137
+ welcome_message = """
138
 
139
  ## Welcome to the USDA Food Assistant!
140
 
 
142
 
143
  ### Platform Overview
144
 
145
+ 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 ingedient, we've got the data you need to make informed choices.
146
 
147
  ---
148
 
 
150
 
151
  - **Step 1:** To begin, simply provide the name of the food item you're interested in.
152
  - **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.
153
+
154
  **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.
155
 
156
  Feel free to ask about any food or nutrient details, and we'll provide the most accurate information available!
157
 
158
  ---
159
+
160
  """
161
+ msg = cl.Message(content=welcome_message)
162
+ await msg.send()
 
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  chainlit
2
  openai
3
  pinecone
 
4
  protoc_gen_openapiv2
 
1
  chainlit
2
  openai
3
  pinecone
4
+ websockets
5
  protoc_gen_openapiv2