Skip to content

Add ChatSession.to_dict #361

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions google/generativeai/generative_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ def _prepare_request(
contents: content_types.ContentsType,
generation_config: generation_types.GenerationConfigType | None = None,
safety_settings: safety_types.SafetySettingOptions | None = None,
tools: content_types.FunctionLibraryType | None,
tool_config: content_types.ToolConfigType | None,
tools: content_types.FunctionLibraryType | None = None,
tool_config: content_types.ToolConfigType | None = None,
) -> protos.GenerateContentRequest:
"""Creates a `protos.GenerateContentRequest` from raw inputs."""
if hasattr(self, "_cached_content") and any([self._system_instruction, tools, tool_config]):
Expand Down Expand Up @@ -509,6 +509,29 @@ def __init__(
self._last_received: generation_types.BaseGenerateContentResponse | None = None
self.enable_automatic_function_calling = enable_automatic_function_calling

def to_dict(self, tools=True):
if tools == True and self.model._tools is not None:
pass # raise ValueError("")

request = self.model._prepare_request(contents=self.history)
return type(request).to_dict(
request, use_integers_for_enums=False, including_default_value_fields=False
)

@classmethod
def from_dict(cls, obj):
request = protos.GenerateContentRequest(obj)
model = GenerativeModel(
model_name=request.model,
generation_config=request.generation_config,
tool_config=request.tool_config,
tools=request.tools,
safety_settings=request.safety_settings,
system_instruction=request.system_instruction,
)

return model.start_chat(history=request.contents)

def send_message(
self,
content: content_types.ContentType,
Expand Down
8 changes: 4 additions & 4 deletions google/generativeai/types/generation_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,9 +554,9 @@ def rewrite_stream_error():
class GenerateContentResponse(BaseGenerateContentResponse):
@classmethod
def from_iterator(cls, iterator: Iterable[protos.GenerateContentResponse]):
iterator = iter(iterator)
iterator = iter(iterator) # type: ignore
with rewrite_stream_error():
response = next(iterator)
response = next(iterator) # type: ignore

return cls(
done=False,
Expand All @@ -581,7 +581,7 @@ def __iter__(self):

# Always have the next chunk available.
if len(self._chunks) == 0:
self._chunks.append(next(self._iterator))
self._chunks.append(next(self._iterator)) # type: ignore

for n in itertools.count():
if self._error:
Expand All @@ -594,7 +594,7 @@ def __iter__(self):
return

try:
item = next(self._iterator)
item = next(self._iterator) # type: ignore
except StopIteration:
self._done = True
except Exception as e:
Expand Down
Loading
Loading