-
Notifications
You must be signed in to change notification settings - Fork 468
Search grounding #558
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Search grounding #558
Changes from 8 commits
12c25ff
250fb41
8ed4a25
fa1651d
84a5f29
fd39814
91fc30c
cc45552
c5cebf2
e60841e
2f67470
d15431b
eff6ea5
4e76777
af6ce40
3f348fa
a26120b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,27 @@ | |
"FunctionLibraryType", | ||
] | ||
|
||
Mode = protos.DynamicRetrievalConfig.Mode | ||
|
||
ModeOptions = Union[int, str, Mode] | ||
|
||
_MODE: dict[ModeOptions, Mode] = { | ||
Mode.MODE_UNSPECIFIED: Mode.MODE_UNSPECIFIED, | ||
0: Mode.MODE_UNSPECIFIED, | ||
"mode_unspecified": Mode.MODE_UNSPECIFIED, | ||
"unspecified": Mode.MODE_UNSPECIFIED, | ||
Mode.MODE_DYNAMIC: Mode.MODE_DYNAMIC, | ||
1: Mode.MODE_DYNAMIC, | ||
"mode_dynamic": Mode.MODE_DYNAMIC, | ||
"dynamic": Mode.MODE_DYNAMIC, | ||
} | ||
|
||
|
||
def to_mode(x: ModeOptions) -> Mode: | ||
if isinstance(x, str): | ||
x = x.lower() | ||
return _MODE[x] | ||
|
||
|
||
def pil_to_blob(img): | ||
# When you load an image with PIL you get a subclass of PIL.Image | ||
|
@@ -650,12 +671,56 @@ def _encode_fd(fd: FunctionDeclaration | protos.FunctionDeclaration) -> protos.F | |
return fd.to_proto() | ||
|
||
|
||
GoogleSearchRetrievalType = Union[protos.GoogleSearchRetrieval, dict[str, float]] | ||
|
||
|
||
def _make_google_search_retrieval(gsr: GoogleSearchRetrievalType): | ||
if isinstance(gsr, protos.GoogleSearchRetrieval): | ||
return gsr | ||
elif isinstance(gsr, Iterable) and not isinstance(gsr, Mapping): | ||
# Handle list of protos.Tool(...) and list of protos.GoogleSearchRetrieval | ||
return gsr | ||
elif isinstance(gsr, Mapping): | ||
if "mode" in gsr["dynamic_retrieval_config"]: | ||
shilpakancharla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print(to_mode(gsr["dynamic_retrieval_config"]["mode"])) | ||
# Create proto object from dictionary | ||
gsr = { | ||
"google_search_retrieval": { | ||
"dynamic_retrieval_config": { | ||
"mode": to_mode(gsr["dynamic_retrieval_config"]["mode"]), | ||
"dynamic_threshold": gsr["dynamic_retrieval_config"]["dynamic_threshold"], | ||
} | ||
} | ||
} | ||
print(gsr) | ||
elif "mode" in gsr.keys(): | ||
shilpakancharla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Create proto object from dictionary | ||
gsr = { | ||
"google_search_retrieval": { | ||
"dynamic_retrieval_config": { | ||
"mode": to_mode(gsr["mode"]), | ||
"dynamic_threshold": gsr["dynamic_threshold"], | ||
} | ||
} | ||
} | ||
return gsr | ||
else: | ||
raise TypeError( | ||
"Invalid input type. Expected an instance of `genai.GoogleSearchRetrieval`.\n" | ||
f"However, received an object of type: {type(gsr)}.\n" | ||
f"Object Value: {gsr}" | ||
) | ||
|
||
|
||
class Tool: | ||
"""A wrapper for `protos.Tool`, Contains a collection of related `FunctionDeclaration` objects.""" | ||
"""A wrapper for `protos.Tool`, Contains a collection of related `FunctionDeclaration` objects, | ||
protos.CodeExecution object, and protos.GoogleSearchRetrieval object.""" | ||
|
||
def __init__( | ||
shilpakancharla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self, | ||
shilpakancharla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*, | ||
function_declarations: Iterable[FunctionDeclarationType] | None = None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we check that only one of these is set? |
||
google_search_retrieval: Union[protos.GoogleSearchRetrieval, str] | None = None, | ||
code_execution: protos.CodeExecution | None = None, | ||
): | ||
# The main path doesn't use this but is seems useful. | ||
|
@@ -674,15 +739,32 @@ def __init__( | |
self._function_declarations = [] | ||
self._index = {} | ||
|
||
if google_search_retrieval: | ||
if isinstance(google_search_retrieval, str): | ||
google_search_retrieval = { | ||
"google_search_retrieval": { | ||
"dynamic_retrieval_config": {"mode": to_mode(google_search_retrieval)} | ||
} | ||
} | ||
else: | ||
_make_google_search_retrieval(google_search_retrieval) | ||
|
||
self._proto = protos.Tool( | ||
function_declarations=[_encode_fd(fd) for fd in self._function_declarations], | ||
google_search_retrieval=google_search_retrieval, | ||
code_execution=code_execution, | ||
) | ||
|
||
print(self._proto.google_search_retrieval) | ||
|
||
@property | ||
def function_declarations(self) -> list[FunctionDeclaration | protos.FunctionDeclaration]: | ||
return self._function_declarations | ||
|
||
@property | ||
def google_search_retrieval(self) -> protos.GoogleSearchRetrieval: | ||
return self._proto.google_search_retrieval | ||
|
||
@property | ||
def code_execution(self) -> protos.CodeExecution: | ||
return self._proto.code_execution | ||
|
@@ -711,7 +793,7 @@ class ToolDict(TypedDict): | |
|
||
|
||
ToolType = Union[ | ||
Tool, protos.Tool, ToolDict, Iterable[FunctionDeclarationType], FunctionDeclarationType | ||
str, Tool, protos.Tool, ToolDict, Iterable[FunctionDeclarationType], FunctionDeclarationType | ||
] | ||
|
||
|
||
|
@@ -723,20 +805,41 @@ def _make_tool(tool: ToolType) -> Tool: | |
code_execution = tool.code_execution | ||
else: | ||
code_execution = None | ||
shilpakancharla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return Tool(function_declarations=tool.function_declarations, code_execution=code_execution) | ||
|
||
if "google_search_retrieval" in tool: | ||
google_search_retrieval = tool.google_search_retrieval | ||
else: | ||
google_search_retrieval = None | ||
|
||
return Tool( | ||
function_declarations=tool.function_declarations, | ||
google_search_retrieval=google_search_retrieval, | ||
code_execution=code_execution, | ||
) | ||
elif isinstance(tool, dict): | ||
shilpakancharla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if "function_declarations" in tool or "code_execution" in tool: | ||
if ( | ||
"function_declarations" in tool | ||
or "google_search_retrieval" in tool | ||
or "code_execution" in tool | ||
): | ||
return Tool(**tool) | ||
else: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add a comment to explain this else? |
||
fd = tool | ||
return Tool(function_declarations=[protos.FunctionDeclaration(**fd)]) | ||
elif isinstance(tool, str): | ||
if tool.lower() == "code_execution": | ||
shilpakancharla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return Tool(code_execution=protos.CodeExecution()) | ||
# Check to see if one of the mode enums matches | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we want this block, people shouldn't be passing a Mode-strings as a tool.
shilpakancharla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
elif tool.lower() == "google_search_retrieval": | ||
return Tool(google_search_retrieval=protos.GoogleSearchRetrieval()) | ||
else: | ||
raise ValueError("The only string that can be passed as a tool is 'code_execution'.") | ||
raise ValueError( | ||
"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." | ||
) | ||
elif isinstance(tool, protos.CodeExecution): | ||
return Tool(code_execution=tool) | ||
elif isinstance(tool, protos.GoogleSearchRetrieval): | ||
return Tool(google_search_retrieval=tool) | ||
elif isinstance(tool, Iterable): | ||
return Tool(function_declarations=tool) | ||
else: | ||
|
@@ -792,7 +895,7 @@ def to_proto(self): | |
|
||
def _make_tools(tools: ToolsType) -> list[Tool]: | ||
if isinstance(tools, str): | ||
if tools.lower() == "code_execution": | ||
if tools.lower() == "code_execution" or tools.lower() == "google_search_retrieval": | ||
return [_make_tool(tools)] | ||
else: | ||
raise ValueError("The only string that can be passed as a tool is 'code_execution'.") | ||
|
Uh oh!
There was an error while loading. Please reload this page.