Tim Luka Horstmann
commited on
Commit
·
16a3faa
1
Parent(s):
a9456f8
Better system prompt
Browse files
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.
|
|
|
|
| 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
|
| 160 |
-
"and say 'I don't have specific details to share about that' if pressed for specifics beyond the CV or FAQs.
|
| 161 |
-
f"Today's date is {current_date}. "
|
| 162 |
-
f"CV: {full_cv_text}"
|
| 163 |
)
|
| 164 |
|
| 165 |
-
# Build
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
|
| 181 |
try:
|
| 182 |
response = gemini_client.models.generate_content_stream(
|
| 183 |
model=gemini_model,
|
| 184 |
-
contents=
|
| 185 |
config=types.GenerateContentConfig(
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
|
|
|
| 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"
|