Tim Luka Horstmann commited on
Commit
16a3faa
·
1 Parent(s): a9456f8

Better system prompt

Browse files
Files changed (1) hide show
  1. app.py +29 -29
app.py CHANGED
@@ -146,59 +146,59 @@ async def stream_response(query, history):
146
  yield chunk
147
 
148
  async def stream_response_gemini(query, history):
149
- """Stream response using Gemini API"""
150
  logger.info(f"Processing query with Gemini: {query}")
151
  start_time = time.time()
152
  first_token_logged = False
153
 
 
154
  current_date = datetime.now().strftime("%Y-%m-%d")
155
-
156
  system_prompt = (
157
- "You are Tim Luka Horstmann, a Computer Scientist. A user is asking you a question. Respond as yourself, using the first person, in a friendly and concise manner. "
 
158
  "For questions about your CV, base your answer *exclusively* on the provided CV information below and do not add any details not explicitly stated. "
159
- "For casual questions not covered by the CV, respond naturally but limit answers to general truths about yourself (e.g., your current location is Paris, France, or your field is AI) "
160
- "and say 'I don't have specific details to share about that' if pressed for specifics beyond the CV or FAQs. Do not invent facts, experiences, or opinions not supported by the CV or FAQs. "
161
- f"Today's date is {current_date}. "
162
- f"CV: {full_cv_text}"
163
  )
164
 
165
- # Build messages for Gemini (no system role - embed instructions in first user message)
166
- messages = []
167
-
168
- # Add conversation history
169
- for msg in history:
170
- role = "user" if msg["role"] == "user" else "model"
171
- messages.append(types.Content(role=role, parts=[types.Part.from_text(text=msg["content"])]))
172
-
173
- # Add current query with system prompt embedded
174
- if not history: # If no history, include system prompt with the first message
175
- combined_query = f"{system_prompt}\n\nUser question: {query}"
176
- else:
177
- combined_query = query
178
-
179
- messages.append(types.Content(role="user", parts=[types.Part.from_text(text=combined_query)]))
180
 
181
  try:
182
  response = gemini_client.models.generate_content_stream(
183
  model=gemini_model,
184
- contents=messages,
185
  config=types.GenerateContentConfig(
186
- temperature=0.3,
187
- top_p=0.7,
188
- max_output_tokens=512,
 
189
  response_mime_type="text/plain",
190
  )
191
  )
192
-
193
  for chunk in response:
194
  if chunk.text:
195
  if not first_token_logged:
196
  logger.info(f"First token time (Gemini): {time.time() - start_time:.2f}s")
197
  first_token_logged = True
198
  yield f"data: {chunk.text}\n\n"
199
-
200
  yield "data: [DONE]\n\n"
201
-
202
  except Exception as e:
203
  logger.error(f"Gemini API error: {str(e)}")
204
  yield f"data: Sorry, I encountered an error with Gemini API: {str(e)}\n\n"
 
146
  yield chunk
147
 
148
  async def stream_response_gemini(query, history):
149
+ """Stream response using Gemini API with a proper system_instruction."""
150
  logger.info(f"Processing query with Gemini: {query}")
151
  start_time = time.time()
152
  first_token_logged = False
153
 
154
+ # Build the system instruction once
155
  current_date = datetime.now().strftime("%Y-%m-%d")
 
156
  system_prompt = (
157
+ "You are Tim Luka Horstmann, a Computer Scientist. "
158
+ "A user is asking you a question. Respond as yourself, using the first person, in a friendly and concise manner. "
159
  "For questions about your CV, base your answer *exclusively* on the provided CV information below and do not add any details not explicitly stated. "
160
+ "For casual questions not covered by the CV, respond naturally but limit answers to general truths about yourself (e.g., your current location is Paris, France) "
161
+ "and say 'I don't have specific details to share about that' if pressed for specifics beyond the CV or FAQs. "
162
+ f"Today's date is {current_date}. CV: {full_cv_text}"
 
163
  )
164
 
165
+ # Build only the user/model history as contents
166
+ contents = [
167
+ *[
168
+ types.Content(
169
+ role=msg["role"],
170
+ parts=[types.Part.from_text(msg["content"])]
171
+ )
172
+ for msg in history
173
+ ],
174
+ # Always append the current user query at the end
175
+ types.Content(
176
+ role="user",
177
+ parts=[types.Part.from_text(query)]
178
+ )
179
+ ]
180
 
181
  try:
182
  response = gemini_client.models.generate_content_stream(
183
  model=gemini_model,
184
+ contents=contents,
185
  config=types.GenerateContentConfig(
186
+ system_instruction=system_prompt,
187
+ # temperature=0.3,
188
+ # top_p=0.7,
189
+ # max_output_tokens=512,
190
  response_mime_type="text/plain",
191
  )
192
  )
193
+
194
  for chunk in response:
195
  if chunk.text:
196
  if not first_token_logged:
197
  logger.info(f"First token time (Gemini): {time.time() - start_time:.2f}s")
198
  first_token_logged = True
199
  yield f"data: {chunk.text}\n\n"
 
200
  yield "data: [DONE]\n\n"
201
+
202
  except Exception as e:
203
  logger.error(f"Gemini API error: {str(e)}")
204
  yield f"data: Sorry, I encountered an error with Gemini API: {str(e)}\n\n"