David Chu
commited on
fix: improve output format instruction following
Browse filesGemini suddenly stops responding in the json
schema we requested in the system prompt. Instead
it's generating normal text. Repeating the
schema in user prompt seems to fix the issue
for now.
- app/agent.py +13 -2
app/agent.py
CHANGED
@@ -15,9 +15,20 @@ CONFIG = types.GenerateContentConfig(
|
|
15 |
literature.search_medical_literature,
|
16 |
],
|
17 |
system_instruction=(Path(__file__).parent / "system_instruction.txt").read_text(),
|
18 |
-
thinking_config=types.ThinkingConfig(include_thoughts=True),
|
19 |
)
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
SOURCE_TOOL_NAMES = {
|
22 |
literature.search_medical_literature.__name__,
|
23 |
dailymed.find_drug_set_ids.__name__,
|
@@ -81,7 +92,7 @@ def validate_response(response: types.GenerateContentResponse) -> models.Stateme
|
|
81 |
def respond(client: genai.Client, query: str) -> models.Statements:
|
82 |
resp = client.models.generate_content(
|
83 |
model="gemini-2.5-flash-preview-05-20",
|
84 |
-
contents=query,
|
85 |
config=CONFIG,
|
86 |
)
|
87 |
return validate_response(resp)
|
|
|
15 |
literature.search_medical_literature,
|
16 |
],
|
17 |
system_instruction=(Path(__file__).parent / "system_instruction.txt").read_text(),
|
18 |
+
thinking_config=types.ThinkingConfig(include_thoughts=True, thinking_budget=1024),
|
19 |
)
|
20 |
|
21 |
+
RESPONSE_FORMAT = """\
|
22 |
+
|
23 |
+
Return in JSON matching this specification:
|
24 |
+
|
25 |
+
Source = { "title": string, "url": str }
|
26 |
+
Statement = { "text": string, "sources": array<Source> }
|
27 |
+
Return: array<Statement>
|
28 |
+
|
29 |
+
Do not return the response in a markdown code block.
|
30 |
+
Do not include the citation in the `text` field."""
|
31 |
+
|
32 |
SOURCE_TOOL_NAMES = {
|
33 |
literature.search_medical_literature.__name__,
|
34 |
dailymed.find_drug_set_ids.__name__,
|
|
|
92 |
def respond(client: genai.Client, query: str) -> models.Statements:
|
93 |
resp = client.models.generate_content(
|
94 |
model="gemini-2.5-flash-preview-05-20",
|
95 |
+
contents=query + RESPONSE_FORMAT,
|
96 |
config=CONFIG,
|
97 |
)
|
98 |
return validate_response(resp)
|