71
71
"FunctionLibraryType" ,
72
72
]
73
73
74
+ Mode = protos .DynamicRetrievalConfig .Mode
75
+
76
+ ModeOptions = Union [str , str , Mode ]
77
+
78
+ _MODE : dict [ModeOptions , Mode ] = {
79
+ Mode .MODE_UNSPECIFIED : Mode .MODE_UNSPECIFIED ,
80
+ 0 : Mode .MODE_UNSPECIFIED ,
81
+ "mode_unspecified" : Mode .MODE_UNSPECIFIED ,
82
+ "unspecified" : Mode .MODE_UNSPECIFIED ,
83
+ Mode .DYNAMIC : Mode .DYNAMIC ,
84
+ 1 : Mode .DYNAMIC ,
85
+ "mode_dynamic" : Mode .DYNAMIC ,
86
+ "dynamic" : Mode .DYNAMIC ,
87
+ }
88
+
89
+
90
+ def to_mode (x : ModeOptions ) -> Mode :
91
+ if isinstance (x , str ):
92
+ x = x .lower ()
93
+ return _MODE [x ]
94
+
74
95
75
96
def pil_to_blob (img ):
76
97
# When you load an image with PIL you get a subclass of PIL.Image
@@ -656,6 +677,7 @@ class Tool:
656
677
def __init__ (
657
678
self ,
658
679
function_declarations : Iterable [FunctionDeclarationType ] | None = None ,
680
+ google_search_retrieval : protos .GoogleSearchRetrieval | None = None ,
659
681
code_execution : protos .CodeExecution | None = None ,
660
682
):
661
683
# The main path doesn't use this but is seems useful.
@@ -676,6 +698,7 @@ def __init__(
676
698
677
699
self ._proto = protos .Tool (
678
700
function_declarations = [_encode_fd (fd ) for fd in self ._function_declarations ],
701
+ google_search_retrieval = google_search_retrieval ,
679
702
code_execution = code_execution ,
680
703
)
681
704
@@ -723,20 +746,36 @@ def _make_tool(tool: ToolType) -> Tool:
723
746
code_execution = tool .code_execution
724
747
else :
725
748
code_execution = None
726
- return Tool (function_declarations = tool .function_declarations , code_execution = code_execution )
749
+ return Tool (
750
+ function_declarations = tool .function_declarations ,
751
+ google_search_retrieval = tool .google_search_retrieval ,
752
+ code_execution = code_execution ,
753
+ )
727
754
elif isinstance (tool , dict ):
728
- if "function_declarations" in tool or "code_execution" in tool :
755
+ if (
756
+ "function_declarations" in tool
757
+ or "google_search_retrieval" in tool
758
+ or "code_execution" in tool
759
+ ):
729
760
return Tool (** tool )
730
761
else :
731
762
fd = tool
732
763
return Tool (function_declarations = [protos .FunctionDeclaration (** fd )])
733
764
elif isinstance (tool , str ):
734
765
if tool .lower () == "code_execution" :
735
766
return Tool (code_execution = protos .CodeExecution ())
767
+ # Check to see if one of the mode enums matches
768
+ elif to_mode (tool ) == Mode .MODE_UNSPECIFIED or to_mode (tool ) == Mode .DYNAMIC :
769
+ mode = to_mode (tool )
770
+ return Tool (google_search_retrieval = protos .GoogleSearchRetrieval (mode = mode ))
736
771
else :
737
- raise ValueError ("The only string that can be passed as a tool is 'code_execution'." )
772
+ raise ValueError (
773
+ "The only string that can be passed as a tool is 'code_execution', or one of the specified values for the `mode` parameter for google_search_retrieval."
774
+ )
738
775
elif isinstance (tool , protos .CodeExecution ):
739
776
return Tool (code_execution = tool )
777
+ elif isinstance (tool , protos .GoogleSearchRetrieval ):
778
+ return Tool (google_search_retrieval = tool )
740
779
elif isinstance (tool , Iterable ):
741
780
return Tool (function_declarations = tool )
742
781
else :
0 commit comments