gemini-agent-framework 0.2.3__py3-none-any.whl → 0.2.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gemini_agent/__init__.py +1 -1
- gemini_agent/agent.py +32 -91
- {gemini_agent_framework-0.2.3.dist-info → gemini_agent_framework-0.2.4.dist-info}/METADATA +1 -1
- gemini_agent_framework-0.2.4.dist-info/RECORD +6 -0
- gemini_agent_framework-0.2.3.dist-info/RECORD +0 -6
- {gemini_agent_framework-0.2.3.dist-info → gemini_agent_framework-0.2.4.dist-info}/WHEEL +0 -0
- {gemini_agent_framework-0.2.3.dist-info → gemini_agent_framework-0.2.4.dist-info}/licenses/LICENSE +0 -0
gemini_agent/__init__.py
CHANGED
gemini_agent/agent.py
CHANGED
@@ -328,7 +328,7 @@ class Agent:
|
|
328
328
|
self,
|
329
329
|
user_prompt: str,
|
330
330
|
system_prompt: Optional[str] = None,
|
331
|
-
|
331
|
+
json_format: bool = False,
|
332
332
|
conversation_history: Optional[List[Dict[str, Any]]] = None,
|
333
333
|
debug_scope: Optional[str] = [],
|
334
334
|
) -> Any:
|
@@ -338,15 +338,14 @@ class Agent:
|
|
338
338
|
Args:
|
339
339
|
user_prompt: The user's input prompt
|
340
340
|
system_prompt: Optional system prompt to override the default
|
341
|
-
|
341
|
+
json_format: If True, response will be formatted as JSON. Default is False (plain text)
|
342
342
|
conversation_history: Optional list of previous conversation turns
|
343
343
|
|
344
344
|
Returns:
|
345
|
-
The model's response,
|
345
|
+
The model's response, formatted as JSON if json_format is True, otherwise plain text
|
346
346
|
"""
|
347
347
|
self._intermediate_results = {}
|
348
348
|
|
349
|
-
|
350
349
|
current_contents = conversation_history if conversation_history else []
|
351
350
|
|
352
351
|
# Add system instruction to payload
|
@@ -364,24 +363,10 @@ class Agent:
|
|
364
363
|
payload["tools"] = [{"functionDeclarations": self._registered_tools_json}]
|
365
364
|
payload["toolConfig"] = {"functionCallingConfig": {"mode": "AUTO"}}
|
366
365
|
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
if response_structure and not self._registered_tools_json:
|
372
|
-
apply_structure_later = False
|
373
|
-
# If response_structure is a string type, make it more flexible
|
374
|
-
if response_structure.get("type") == "string":
|
375
|
-
response_structure = {
|
376
|
-
"type": ["string", "object"],
|
377
|
-
"properties": {"value": {"type": "string"}},
|
378
|
-
}
|
379
|
-
payload["generationConfig"] = {
|
380
|
-
"response_mime_type": "application/json",
|
381
|
-
"response_schema": response_structure,
|
382
|
-
}
|
383
|
-
final_mime_type = "application/json"
|
384
|
-
final_response_schema = response_structure
|
366
|
+
# Don't set JSON formatting initially if tools are available
|
367
|
+
# We'll apply it later after tool calls are completed
|
368
|
+
apply_json_format_later = json_format and bool(self._registered_tools_json)
|
369
|
+
|
385
370
|
count = 0
|
386
371
|
while True:
|
387
372
|
self._log_json(payload, f"payload_{count}.json", debug_scope)
|
@@ -410,8 +395,6 @@ class Agent:
|
|
410
395
|
content = candidate["content"]
|
411
396
|
|
412
397
|
for part in content["parts"]:
|
413
|
-
|
414
|
-
|
415
398
|
if "functionCall" in part:
|
416
399
|
payload["contents"].append({"role": "model", "parts": [part]})
|
417
400
|
fc = part["functionCall"]
|
@@ -500,41 +483,32 @@ class Agent:
|
|
500
483
|
elif "text" in part:
|
501
484
|
final_text = part["text"]
|
502
485
|
|
503
|
-
if
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
self._log_text(
|
513
|
-
f"Warning: Failed to parse initially structured output: {e}. Continuing with raw text.",
|
514
|
-
debug_scope
|
515
|
-
)
|
516
|
-
|
517
|
-
elif apply_structure_later:
|
518
|
-
if not any(
|
519
|
-
"functionCall" in p
|
520
|
-
for p in content["parts"][content["parts"].index(part) + 1 :]
|
521
|
-
):
|
522
|
-
self._log_text("--- Attempting final structuring call ---", debug_scope)
|
523
|
-
# Include the full conversation history in the formatting payload
|
486
|
+
# Check if there are more function calls coming
|
487
|
+
has_more_function_calls = any(
|
488
|
+
"functionCall" in p
|
489
|
+
for p in content["parts"][content["parts"].index(part) + 1 :]
|
490
|
+
)
|
491
|
+
|
492
|
+
if not has_more_function_calls:
|
493
|
+
# If JSON format is requested and we have tools, make a final formatting call
|
494
|
+
if apply_json_format_later:
|
495
|
+
self._log_text("--- Making final JSON formatting call ---", debug_scope)
|
524
496
|
formatting_payload = {
|
497
|
+
"system_instruction": {
|
498
|
+
"parts": [{"text": system_prompt if system_prompt else ""},{"text": self._get_system_prompt()}]
|
499
|
+
},
|
525
500
|
"contents": payload["contents"] + [
|
526
501
|
{
|
527
502
|
"role": "user",
|
528
503
|
"parts": [
|
529
504
|
{
|
530
|
-
"text": f"Based on our conversation above, please format
|
505
|
+
"text": f"Based on our conversation above, please format your response as JSON. Here is the current response: {final_text}"
|
531
506
|
}
|
532
507
|
],
|
533
508
|
}
|
534
509
|
],
|
535
510
|
"generationConfig": {
|
536
|
-
"response_mime_type": "application/json"
|
537
|
-
"response_schema": response_structure,
|
511
|
+
"response_mime_type": "application/json"
|
538
512
|
},
|
539
513
|
}
|
540
514
|
self._log_json(formatting_payload, f"formatting_payload_{count}.json", debug_scope)
|
@@ -543,7 +517,7 @@ class Agent:
|
|
543
517
|
|
544
518
|
if "error" in structured_response_data:
|
545
519
|
self._log_text(
|
546
|
-
f"
|
520
|
+
f"JSON formatting call failed: {structured_response_data['error']}. Returning raw text.",
|
547
521
|
debug_scope
|
548
522
|
)
|
549
523
|
return final_text
|
@@ -556,57 +530,24 @@ class Agent:
|
|
556
530
|
return structured_output
|
557
531
|
except (KeyError, IndexError, json.JSONDecodeError) as e:
|
558
532
|
self._log_text(
|
559
|
-
f"Warning: Failed to parse
|
533
|
+
f"Warning: Failed to parse JSON response after formatting call: {e}. Returning raw text.",
|
560
534
|
debug_scope
|
561
535
|
)
|
562
536
|
return final_text
|
563
|
-
|
564
|
-
|
565
|
-
"functionCall" in p
|
566
|
-
for p in content["parts"][content["parts"].index(part) + 1 :]
|
567
|
-
):
|
568
|
-
if response_structure and not apply_structure_later:
|
569
|
-
self._log_text("--- Attempting final structuring call ---", debug_scope)
|
570
|
-
formatting_payload = {
|
571
|
-
"contents": [
|
572
|
-
{
|
573
|
-
"role": "user",
|
574
|
-
"parts": [
|
575
|
-
{
|
576
|
-
"text": f"Please format the following information according to the requested JSON structure:\n\n{final_text}"
|
577
|
-
}
|
578
|
-
],
|
579
|
-
}
|
580
|
-
],
|
581
|
-
"generationConfig": {
|
582
|
-
"response_mime_type": "application/json",
|
583
|
-
"response_schema": response_structure,
|
584
|
-
},
|
585
|
-
}
|
586
|
-
self._log_json(formatting_payload, f"formatting_payload_{count}.json", debug_scope)
|
587
|
-
count += 1
|
588
|
-
structured_response_data = self._call_gemini_api(formatting_payload, debug_scope)
|
589
|
-
|
590
|
-
if "error" in structured_response_data:
|
591
|
-
self._log_text(
|
592
|
-
f"Structuring call failed: {structured_response_data['error']}. Returning intermediate text."
|
593
|
-
, debug_scope
|
594
|
-
)
|
595
|
-
return final_text
|
596
|
-
|
537
|
+
elif json_format:
|
538
|
+
# Direct JSON formatting (no tools involved)
|
597
539
|
try:
|
598
|
-
|
599
|
-
"content"
|
600
|
-
]["parts"][0]["text"]
|
601
|
-
structured_output = json.loads(structured_text)
|
540
|
+
structured_output = json.loads(final_text)
|
602
541
|
return structured_output
|
603
|
-
except
|
542
|
+
except json.JSONDecodeError as e:
|
604
543
|
self._log_text(
|
605
|
-
f"Warning: Failed to parse
|
544
|
+
f"Warning: Failed to parse JSON response: {e}. Returning raw text.",
|
606
545
|
debug_scope
|
607
546
|
)
|
608
547
|
return final_text
|
609
|
-
|
548
|
+
else:
|
549
|
+
# Return plain text response
|
550
|
+
return final_text
|
610
551
|
continue
|
611
552
|
|
612
553
|
except (KeyError, IndexError) as e:
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: gemini-agent-framework
|
3
|
-
Version: 0.2.
|
3
|
+
Version: 0.2.4
|
4
4
|
Summary: A framework for building agents that use Gemini's function calling capabilities
|
5
5
|
Project-URL: Homepage, https://github.com/m7mdony/gemini-agent-framework
|
6
6
|
Project-URL: Documentation, https://m7mdony.github.io/gemini-agent-framework
|
@@ -0,0 +1,6 @@
|
|
1
|
+
gemini_agent/__init__.py,sha256=o91YZVXOYTEXZwEBocIH6dE7MBk2JBKy6-qEyl5oFZ0,68
|
2
|
+
gemini_agent/agent.py,sha256=Hk4TqameeOxDcbiF3yTIIK1UOSmkFQQelTH67vS74xg,25185
|
3
|
+
gemini_agent_framework-0.2.4.dist-info/METADATA,sha256=LY4ne20323PGI4AhgWrg8mQvqFKBNu6BjqdbvcRrszM,5032
|
4
|
+
gemini_agent_framework-0.2.4.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
5
|
+
gemini_agent_framework-0.2.4.dist-info/licenses/LICENSE,sha256=NHp9eKeWbB-Fp6Ff24BXSemJOa6UEZa9IAp9U7O9oBM,1073
|
6
|
+
gemini_agent_framework-0.2.4.dist-info/RECORD,,
|
@@ -1,6 +0,0 @@
|
|
1
|
-
gemini_agent/__init__.py,sha256=oQVInJJsdELQRFQPmj5oFTcxcP7gxj3kTPC66a7ZMZE,68
|
2
|
-
gemini_agent/agent.py,sha256=0MxUrVt91hHCpHqe-eCkLAlTYS9bec2VNwX-3Q3gNXo,28457
|
3
|
-
gemini_agent_framework-0.2.3.dist-info/METADATA,sha256=2ksoadA6mTEd3xM2611j46fiUvfX9pmkapZV_uKe1XQ,5032
|
4
|
-
gemini_agent_framework-0.2.3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
5
|
-
gemini_agent_framework-0.2.3.dist-info/licenses/LICENSE,sha256=NHp9eKeWbB-Fp6Ff24BXSemJOa6UEZa9IAp9U7O9oBM,1073
|
6
|
-
gemini_agent_framework-0.2.3.dist-info/RECORD,,
|
File without changes
|
{gemini_agent_framework-0.2.3.dist-info → gemini_agent_framework-0.2.4.dist-info}/licenses/LICENSE
RENAMED
File without changes
|