-
Notifications
You must be signed in to change notification settings - Fork 201
/
discuss_types.py
208 lines (160 loc) · 6.5 KB
/
discuss_types.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# -*- coding: utf-8 -*-
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Type definitions for the discuss service."""
import abc
import dataclasses
from typing import Any, Dict, Union, Iterable, Optional, Tuple, List
from typing_extensions import TypedDict
import google.ai.generativelanguage as glm
from google.generativeai import string_utils
from google.generativeai.types import safety_types
from google.generativeai.types import citation_types
__all__ = [
"MessageDict",
"MessageOptions",
"MessagesOptions",
"ExampleDict",
"ExampleOptions",
"ExamplesOptions",
"MessagePromptDict",
"MessagePromptOptions",
"ResponseDict",
"ChatResponse",
"AuthorError",
]
class TokenCount(TypedDict):
token_count: int
class MessageDict(TypedDict):
"""A dict representation of a `glm.Message`."""
author: str
content: str
citation_metadata: Optional[citation_types.CitationMetadataDict]
MessageOptions = Union[str, MessageDict, glm.Message]
MESSAGE_OPTIONS = (str, dict, glm.Message)
MessagesOptions = Union[
MessageOptions,
Iterable[MessageOptions],
]
MESSAGES_OPTIONS = (MESSAGE_OPTIONS, Iterable)
class ExampleDict(TypedDict):
"""A dict representation of a `glm.Example`."""
input: MessageOptions
output: MessageOptions
ExampleOptions = Union[
Tuple[MessageOptions, MessageOptions],
Iterable[MessageOptions],
ExampleDict,
glm.Example,
]
EXAMPLE_OPTIONS = (glm.Example, dict, Iterable)
ExamplesOptions = Union[ExampleOptions, Iterable[ExampleOptions]]
class MessagePromptDict(TypedDict, total=False):
"""A dict representation of a `glm.MessagePrompt`."""
context: str
examples: ExamplesOptions
messages: MessagesOptions
MessagePromptOptions = Union[
str,
glm.Message,
Iterable[Union[str, glm.Message]],
MessagePromptDict,
glm.MessagePrompt,
]
MESSAGE_PROMPT_KEYS = {"context", "examples", "messages"}
class ResponseDict(TypedDict):
"""A dict representation of a `glm.GenerateMessageResponse`."""
messages: List[MessageDict]
candidates: List[MessageDict]
@string_utils.prettyprint
@dataclasses.dataclass(init=False)
class ChatResponse(abc.ABC):
"""A chat response from the model.
* Use `response.last` (settable) for easy access to the text of the last response.
(`messages[-1]['content']`)
* Use `response.messages` to access the message history (including `.last`).
* Use `response.candidates` to access all the responses generated by the model.
Other attributes are just saved from the arguments to `genai.chat`, so you
can easily continue a conversation:
```
import google.generativeai as genai
genai.configure(api_key=os.environ['GOOGLE_API_KEY'])
response = genai.chat(messages=["Hello."])
print(response.last) # 'Hello! What can I help you with?'
response.reply("Can you tell me a joke?")
```
See `genai.chat` for more details.
Attributes:
candidates: A list of candidate responses from the model.
The top candidate is appended to the `messages` field.
This list will contain a *maximum* of `candidate_count` candidates.
It may contain fewer (duplicates are dropped), it will contain at least one.
Note: The `temperature` field affects the variability of the responses. Low
temperatures will return few candidates. Setting `temperature=0` is deterministic,
so it will only ever return one candidate.
filters: This indicates which `types.SafetyCategory`(s) blocked a
candidate from this response, the lowest `types.HarmProbability`
that triggered a block, and the `types.HarmThreshold` setting for that category.
This indicates the smallest change to the `types.SafetySettings` that would be
necessary to unblock at least 1 response.
The blocking is configured by the `types.SafetySettings` in the request (or the
default `types.SafetySettings` of the API).
messages: Contains all the `messages` that were passed when the model was called,
plus the top `candidate` message.
model: The model name.
context: Text that should be provided to the model first, to ground the response.
examples: Examples of what the model should generate.
messages: A snapshot of the conversation history sorted chronologically.
temperature: Controls the randomness of the output. Must be positive.
candidate_count: The **maximum** number of generated response messages to return.
top_k: The maximum number of tokens to consider when sampling.
top_p: The maximum cumulative probability of tokens to consider when sampling.
"""
model: str
context: str
examples: List[ExampleDict]
messages: List[Optional[MessageDict]]
temperature: Optional[float]
candidate_count: Optional[int]
candidates: List[MessageDict]
filters: List[safety_types.ContentFilterDict]
top_p: Optional[float] = None
top_k: Optional[float] = None
@property
@abc.abstractmethod
def last(self) -> Optional[str]:
"""A settable property that provides simple access to the last response string
A shortcut for `response.messages[0]['content']`.
"""
pass
def to_dict(self) -> Dict[str, Any]:
result = {
"model": self.model,
"context": self.context,
"examples": self.examples,
"messages": self.messages,
"temperature": self.temperature,
"candidate_count": self.candidate_count,
"top_p": self.top_p,
"top_k": self.top_k,
"candidates": self.candidates,
}
return result
@abc.abstractmethod
def reply(self, message: MessageOptions) -> "ChatResponse":
"Add a message to the conversation, and get the model's response."
pass
class AuthorError(Exception):
"""Raised by the `chat` (or `reply`) functions when the author list can't be normalized."""
pass