langfun 0.0.2.dev20240329__tar.gz → 0.0.2.dev20240401__tar.gz
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.
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/PKG-INFO +1 -1
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/llms/__init__.py +5 -3
- langfun-0.0.2.dev20240329/langfun/core/llms/gemini.py → langfun-0.0.2.dev20240401/langfun/core/llms/google_genai.py +117 -15
- langfun-0.0.2.dev20240329/langfun/core/llms/gemini_test.py → langfun-0.0.2.dev20240401/langfun/core/llms/google_genai_test.py +76 -13
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun.egg-info/PKG-INFO +1 -1
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun.egg-info/SOURCES.txt +2 -2
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/LICENSE +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/README.md +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/__init__.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/__init__.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/coding/__init__.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/coding/python/__init__.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/coding/python/correction.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/coding/python/correction_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/coding/python/errors.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/coding/python/errors_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/coding/python/execution.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/coding/python/execution_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/coding/python/generation.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/coding/python/generation_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/coding/python/parsing.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/coding/python/parsing_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/coding/python/permissions.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/coding/python/permissions_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/component.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/component_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/concurrent.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/concurrent_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/console.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/console_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/eval/__init__.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/eval/base.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/eval/base_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/eval/matching.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/eval/matching_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/eval/scoring.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/eval/scoring_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/langfunc.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/langfunc_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/language_model.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/language_model_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/llms/cache/__init__.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/llms/cache/base.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/llms/cache/in_memory.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/llms/cache/in_memory_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/llms/fake.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/llms/fake_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/llms/llama_cpp.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/llms/llama_cpp_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/llms/openai.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/llms/openai_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/memories/__init__.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/memories/conversation_history.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/memories/conversation_history_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/memory.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/message.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/message_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/modalities/__init__.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/modalities/image.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/modalities/image_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/modalities/mime.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/modalities/mime_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/modalities/video.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/modalities/video_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/modality.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/modality_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/natural_language.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/natural_language_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/sampling.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/sampling_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/__init__.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/completion.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/completion_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/description.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/description_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/mapping.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/mapping_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/parsing.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/parsing_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/prompting.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/prompting_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/schema.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/schema_generation.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/schema_generation_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/schema_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/scoring.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/scoring_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/subscription.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/subscription_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/template.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/template_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/templates/__init__.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/templates/completion.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/templates/completion_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/templates/conversation.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/templates/conversation_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/templates/demonstration.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/templates/demonstration_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/templates/selfplay.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/templates/selfplay_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/text_formatting.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/text_formatting_test.py +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun.egg-info/dependency_links.txt +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun.egg-info/requires.txt +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun.egg-info/top_level.txt +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/setup.cfg +0 -0
- {langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/setup.py +0 -0
@@ -25,9 +25,11 @@ from langfun.core.llms.fake import StaticResponse
|
|
25
25
|
from langfun.core.llms.fake import StaticSequence
|
26
26
|
|
27
27
|
# Gemini models.
|
28
|
-
from langfun.core.llms.
|
29
|
-
from langfun.core.llms.
|
30
|
-
from langfun.core.llms.
|
28
|
+
from langfun.core.llms.google_genai import GenAI
|
29
|
+
from langfun.core.llms.google_genai import GeminiPro
|
30
|
+
from langfun.core.llms.google_genai import GeminiProVision
|
31
|
+
from langfun.core.llms.google_genai import Palm2
|
32
|
+
from langfun.core.llms.google_genai import Palm2_IT
|
31
33
|
|
32
34
|
# OpenAI models.
|
33
35
|
from langfun.core.llms.openai import OpenAI
|
@@ -13,6 +13,7 @@
|
|
13
13
|
# limitations under the License.
|
14
14
|
"""Gemini models exposed through Google Generative AI APIs."""
|
15
15
|
|
16
|
+
import abc
|
16
17
|
import functools
|
17
18
|
import os
|
18
19
|
from typing import Annotated, Any, Literal
|
@@ -20,14 +21,20 @@ from typing import Annotated, Any, Literal
|
|
20
21
|
import google.generativeai as genai
|
21
22
|
import langfun.core as lf
|
22
23
|
from langfun.core import modalities as lf_modalities
|
24
|
+
import pyglove as pg
|
23
25
|
|
24
26
|
|
25
27
|
@lf.use_init_args(['model'])
|
26
|
-
class
|
27
|
-
"""Language
|
28
|
+
class GenAI(lf.LanguageModel):
|
29
|
+
"""Language models provided by Google GenAI."""
|
28
30
|
|
29
31
|
model: Annotated[
|
30
|
-
Literal[
|
32
|
+
Literal[
|
33
|
+
'gemini-pro',
|
34
|
+
'gemini-pro-vision',
|
35
|
+
'text-bison-001',
|
36
|
+
'chat-bison-001',
|
37
|
+
],
|
31
38
|
'Model name.',
|
32
39
|
]
|
33
40
|
|
@@ -35,7 +42,8 @@ class Gemini(lf.LanguageModel):
|
|
35
42
|
str | None,
|
36
43
|
(
|
37
44
|
'API key. If None, the key will be read from environment variable '
|
38
|
-
"'GOOGLE_API_KEY'."
|
45
|
+
"'GOOGLE_API_KEY'. "
|
46
|
+
'Get an API key at https://ai.google.dev/tutorials/setup'
|
39
47
|
),
|
40
48
|
] = None
|
41
49
|
|
@@ -43,6 +51,9 @@ class Gemini(lf.LanguageModel):
|
|
43
51
|
False
|
44
52
|
)
|
45
53
|
|
54
|
+
# Set the default max concurrency to 8 workers.
|
55
|
+
max_concurrency = 8
|
56
|
+
|
46
57
|
def _on_bound(self):
|
47
58
|
super()._on_bound()
|
48
59
|
self.__dict__.pop('_api_initialized', None)
|
@@ -67,7 +78,11 @@ class Gemini(lf.LanguageModel):
|
|
67
78
|
return [
|
68
79
|
m.name.lstrip('models/')
|
69
80
|
for m in genai.list_models()
|
70
|
-
if
|
81
|
+
if (
|
82
|
+
'generateContent' in m.supported_generation_methods
|
83
|
+
or 'generateText' in m.supported_generation_methods
|
84
|
+
or 'generateMessage' in m.supported_generation_methods
|
85
|
+
)
|
71
86
|
]
|
72
87
|
|
73
88
|
@property
|
@@ -80,11 +95,6 @@ class Gemini(lf.LanguageModel):
|
|
80
95
|
"""Returns a string to identify the resource for rate control."""
|
81
96
|
return self.model_id
|
82
97
|
|
83
|
-
@property
|
84
|
-
def max_concurrency(self) -> int:
|
85
|
-
"""Max concurrent requests."""
|
86
|
-
return 8
|
87
|
-
|
88
98
|
def _generation_config(self, options: lf.LMSamplingOptions) -> dict[str, Any]:
|
89
99
|
"""Creates generation config from langfun sampling options."""
|
90
100
|
return genai.GenerationConfig(
|
@@ -117,7 +127,7 @@ class Gemini(lf.LanguageModel):
|
|
117
127
|
return chunks
|
118
128
|
|
119
129
|
def _response_to_result(
|
120
|
-
self, response: genai.types.GenerateContentResponse
|
130
|
+
self, response: genai.types.GenerateContentResponse | pg.Dict
|
121
131
|
) -> lf.LMSamplingResult:
|
122
132
|
"""Parses generative response into message."""
|
123
133
|
samples = []
|
@@ -149,17 +159,97 @@ class Gemini(lf.LanguageModel):
|
|
149
159
|
return self._response_to_result(response)
|
150
160
|
|
151
161
|
|
162
|
+
class _LegacyGenerativeModel(pg.Object):
|
163
|
+
"""Base for legacy GenAI generative model."""
|
164
|
+
|
165
|
+
model: str
|
166
|
+
|
167
|
+
def generate_content(
|
168
|
+
self,
|
169
|
+
input_content: list[str | genai.types.BlobDict],
|
170
|
+
generation_config: genai.GenerationConfig,
|
171
|
+
) -> pg.Dict:
|
172
|
+
"""Generate content."""
|
173
|
+
segments = []
|
174
|
+
for s in input_content:
|
175
|
+
if not isinstance(s, str):
|
176
|
+
raise ValueError(f'Unsupported modality: {s!r}')
|
177
|
+
segments.append(s)
|
178
|
+
return self.generate(' '.join(segments), generation_config)
|
179
|
+
|
180
|
+
@abc.abstractmethod
|
181
|
+
def generate(
|
182
|
+
self, prompt: str, generation_config: genai.GenerationConfig) -> pg.Dict:
|
183
|
+
"""Generate response based on prompt."""
|
184
|
+
|
185
|
+
|
186
|
+
class _LegacyCompletionModel(_LegacyGenerativeModel):
|
187
|
+
"""Legacy GenAI completion model."""
|
188
|
+
|
189
|
+
def generate(
|
190
|
+
self, prompt: str, generation_config: genai.GenerationConfig
|
191
|
+
) -> pg.Dict:
|
192
|
+
completion: genai.types.Completion = genai.generate_text(
|
193
|
+
model=f'models/{self.model}',
|
194
|
+
prompt=prompt,
|
195
|
+
temperature=generation_config.temperature,
|
196
|
+
top_k=generation_config.top_k,
|
197
|
+
top_p=generation_config.top_p,
|
198
|
+
candidate_count=generation_config.candidate_count,
|
199
|
+
max_output_tokens=generation_config.max_output_tokens,
|
200
|
+
stop_sequences=generation_config.stop_sequences,
|
201
|
+
)
|
202
|
+
return pg.Dict(
|
203
|
+
candidates=[
|
204
|
+
pg.Dict(content=pg.Dict(parts=[pg.Dict(text=c['output'])]))
|
205
|
+
for c in completion.candidates
|
206
|
+
]
|
207
|
+
)
|
208
|
+
|
209
|
+
|
210
|
+
class _LegacyChatModel(_LegacyGenerativeModel):
|
211
|
+
"""Legacy GenAI chat model."""
|
212
|
+
|
213
|
+
def generate(
|
214
|
+
self, prompt: str, generation_config: genai.GenerationConfig
|
215
|
+
) -> pg.Dict:
|
216
|
+
response: genai.types.ChatResponse = genai.chat(
|
217
|
+
model=f'models/{self.model}',
|
218
|
+
messages=prompt,
|
219
|
+
temperature=generation_config.temperature,
|
220
|
+
top_k=generation_config.top_k,
|
221
|
+
top_p=generation_config.top_p,
|
222
|
+
candidate_count=generation_config.candidate_count,
|
223
|
+
)
|
224
|
+
return pg.Dict(
|
225
|
+
candidates=[
|
226
|
+
pg.Dict(content=pg.Dict(parts=[pg.Dict(text=c['content'])]))
|
227
|
+
for c in response.candidates
|
228
|
+
]
|
229
|
+
)
|
230
|
+
|
231
|
+
|
152
232
|
class _ModelHub:
|
153
233
|
"""Google Generative AI model hub."""
|
154
234
|
|
155
235
|
def __init__(self):
|
156
236
|
self._model_cache = {}
|
157
237
|
|
158
|
-
def get(
|
238
|
+
def get(
|
239
|
+
self, model_name: str
|
240
|
+
) -> genai.GenerativeModel | _LegacyGenerativeModel:
|
159
241
|
"""Gets a generative model by model id."""
|
160
242
|
model = self._model_cache.get(model_name, None)
|
161
243
|
if model is None:
|
162
|
-
|
244
|
+
model_info = genai.get_model(f'models/{model_name}')
|
245
|
+
if 'generateContent' in model_info.supported_generation_methods:
|
246
|
+
model = genai.GenerativeModel(model_name)
|
247
|
+
elif 'generateText' in model_info.supported_generation_methods:
|
248
|
+
model = _LegacyCompletionModel(model_name)
|
249
|
+
elif 'generateMessage' in model_info.supported_generation_methods:
|
250
|
+
model = _LegacyChatModel(model_name)
|
251
|
+
else:
|
252
|
+
raise ValueError(f'Unsupported model: {model_name!r}')
|
163
253
|
self._model_cache[model_name] = model
|
164
254
|
return model
|
165
255
|
|
@@ -172,14 +262,26 @@ _GOOGLE_GENAI_MODEL_HUB = _ModelHub()
|
|
172
262
|
#
|
173
263
|
|
174
264
|
|
175
|
-
class GeminiPro(
|
265
|
+
class GeminiPro(GenAI):
|
176
266
|
"""Gemini Pro model."""
|
177
267
|
|
178
268
|
model = 'gemini-pro'
|
179
269
|
|
180
270
|
|
181
|
-
class GeminiProVision(
|
271
|
+
class GeminiProVision(GenAI):
|
182
272
|
"""Gemini Pro vision model."""
|
183
273
|
|
184
274
|
model = 'gemini-pro-vision'
|
185
275
|
multimodal = True
|
276
|
+
|
277
|
+
|
278
|
+
class Palm2(GenAI):
|
279
|
+
"""PaLM2 model."""
|
280
|
+
|
281
|
+
model = 'text-bison-001'
|
282
|
+
|
283
|
+
|
284
|
+
class Palm2_IT(GenAI): # pylint: disable=invalid-name
|
285
|
+
"""PaLM2 instruction-tuned model."""
|
286
|
+
|
287
|
+
model = 'chat-bison-001'
|
@@ -20,7 +20,7 @@ from unittest import mock
|
|
20
20
|
from google import generativeai as genai
|
21
21
|
import langfun.core as lf
|
22
22
|
from langfun.core import modalities as lf_modalities
|
23
|
-
from langfun.core.llms import
|
23
|
+
from langfun.core.llms import google_genai
|
24
24
|
import pyglove as pg
|
25
25
|
|
26
26
|
|
@@ -36,6 +36,29 @@ example_image = (
|
|
36
36
|
)
|
37
37
|
|
38
38
|
|
39
|
+
def mock_get_model(model_name, *args, **kwargs):
|
40
|
+
del args, kwargs
|
41
|
+
if 'gemini' in model_name:
|
42
|
+
method = 'generateContent'
|
43
|
+
elif 'chat' in model_name:
|
44
|
+
method = 'generateMessage'
|
45
|
+
else:
|
46
|
+
method = 'generateText'
|
47
|
+
return pg.Dict(supported_generation_methods=[method])
|
48
|
+
|
49
|
+
|
50
|
+
def mock_generate_text(*, model, prompt, **kwargs):
|
51
|
+
return pg.Dict(
|
52
|
+
candidates=[pg.Dict(output=f'{prompt} to {model} with {kwargs}')]
|
53
|
+
)
|
54
|
+
|
55
|
+
|
56
|
+
def mock_chat(*, model, messages, **kwargs):
|
57
|
+
return pg.Dict(
|
58
|
+
candidates=[pg.Dict(content=f'{messages} to {model} with {kwargs}')]
|
59
|
+
)
|
60
|
+
|
61
|
+
|
39
62
|
def mock_generate_content(content, generation_config, **kwargs):
|
40
63
|
del kwargs
|
41
64
|
c = generation_config
|
@@ -68,12 +91,12 @@ def mock_generate_content(content, generation_config, **kwargs):
|
|
68
91
|
)
|
69
92
|
|
70
93
|
|
71
|
-
class
|
72
|
-
"""Tests for
|
94
|
+
class GenAITest(unittest.TestCase):
|
95
|
+
"""Tests for Google GenAI model."""
|
73
96
|
|
74
97
|
def test_content_from_message_text_only(self):
|
75
98
|
text = 'This is a beautiful day'
|
76
|
-
model =
|
99
|
+
model = google_genai.GeminiPro()
|
77
100
|
chunks = model._content_from_message(lf.UserMessage(text))
|
78
101
|
self.assertEqual(chunks, [text])
|
79
102
|
|
@@ -85,9 +108,9 @@ class GeminiTest(unittest.TestCase):
|
|
85
108
|
|
86
109
|
# Non-multimodal model.
|
87
110
|
with self.assertRaisesRegex(ValueError, 'Unsupported modality'):
|
88
|
-
|
111
|
+
google_genai.GeminiPro()._content_from_message(message)
|
89
112
|
|
90
|
-
model =
|
113
|
+
model = google_genai.GeminiProVision()
|
91
114
|
chunks = model._content_from_message(message)
|
92
115
|
self.maxDiff = None
|
93
116
|
self.assertEqual(
|
@@ -118,7 +141,7 @@ class GeminiTest(unittest.TestCase):
|
|
118
141
|
],
|
119
142
|
),
|
120
143
|
)
|
121
|
-
model =
|
144
|
+
model = google_genai.GeminiProVision()
|
122
145
|
result = model._response_to_result(response)
|
123
146
|
self.assertEqual(
|
124
147
|
result,
|
@@ -129,26 +152,28 @@ class GeminiTest(unittest.TestCase):
|
|
129
152
|
)
|
130
153
|
|
131
154
|
def test_model_hub(self):
|
132
|
-
model =
|
155
|
+
model = google_genai._GOOGLE_GENAI_MODEL_HUB.get('gemini-pro')
|
133
156
|
self.assertIsNotNone(model)
|
134
|
-
self.assertIs(
|
157
|
+
self.assertIs(google_genai._GOOGLE_GENAI_MODEL_HUB.get('gemini-pro'), model)
|
135
158
|
|
136
159
|
def test_api_key_check(self):
|
137
160
|
with self.assertRaisesRegex(ValueError, 'Please specify `api_key`'):
|
138
|
-
_ =
|
161
|
+
_ = google_genai.GeminiPro()._api_initialized
|
139
162
|
|
140
|
-
self.assertTrue(
|
163
|
+
self.assertTrue(google_genai.GeminiPro(api_key='abc')._api_initialized)
|
141
164
|
os.environ['GOOGLE_API_KEY'] = 'abc'
|
142
|
-
self.assertTrue(
|
165
|
+
self.assertTrue(google_genai.GeminiPro()._api_initialized)
|
143
166
|
del os.environ['GOOGLE_API_KEY']
|
144
167
|
|
145
168
|
def test_call(self):
|
146
169
|
with mock.patch(
|
147
170
|
'google.generativeai.generative_models.GenerativeModel.generate_content'
|
148
171
|
) as mock_generate:
|
172
|
+
orig_get_model = genai.get_model
|
173
|
+
genai.get_model = mock_get_model
|
149
174
|
mock_generate.side_effect = mock_generate_content
|
150
175
|
|
151
|
-
lm =
|
176
|
+
lm = google_genai.GeminiPro(api_key='test_key')
|
152
177
|
self.maxDiff = None
|
153
178
|
self.assertEqual(
|
154
179
|
lm('hello', temperature=2.0, top_k=20).text,
|
@@ -157,6 +182,44 @@ class GeminiTest(unittest.TestCase):
|
|
157
182
|
'top_p=None, top_k=20, max_tokens=1024, stop=None.'
|
158
183
|
),
|
159
184
|
)
|
185
|
+
genai.get_model = orig_get_model
|
186
|
+
|
187
|
+
def test_call_with_legacy_completion_model(self):
|
188
|
+
orig_get_model = genai.get_model
|
189
|
+
genai.get_model = mock_get_model
|
190
|
+
orig_generate_text = genai.generate_text
|
191
|
+
genai.generate_text = mock_generate_text
|
192
|
+
|
193
|
+
lm = google_genai.Palm2(api_key='test_key')
|
194
|
+
self.maxDiff = None
|
195
|
+
self.assertEqual(
|
196
|
+
lm('hello', temperature=2.0, top_k=20).text,
|
197
|
+
(
|
198
|
+
"hello to models/text-bison-001 with {'temperature': 2.0, "
|
199
|
+
"'top_k': 20, 'top_p': None, 'candidate_count': 1, "
|
200
|
+
"'max_output_tokens': 1024, 'stop_sequences': None}"
|
201
|
+
),
|
202
|
+
)
|
203
|
+
genai.get_model = orig_get_model
|
204
|
+
genai.generate_text = orig_generate_text
|
205
|
+
|
206
|
+
def test_call_with_legacy_chat_model(self):
|
207
|
+
orig_get_model = genai.get_model
|
208
|
+
genai.get_model = mock_get_model
|
209
|
+
orig_chat = genai.chat
|
210
|
+
genai.chat = mock_chat
|
211
|
+
|
212
|
+
lm = google_genai.Palm2_IT(api_key='test_key')
|
213
|
+
self.maxDiff = None
|
214
|
+
self.assertEqual(
|
215
|
+
lm('hello', temperature=2.0, top_k=20).text,
|
216
|
+
(
|
217
|
+
"hello to models/chat-bison-001 with {'temperature': 2.0, "
|
218
|
+
"'top_k': 20, 'top_p': None, 'candidate_count': 1}"
|
219
|
+
),
|
220
|
+
)
|
221
|
+
genai.get_model = orig_get_model
|
222
|
+
genai.chat = orig_chat
|
160
223
|
|
161
224
|
|
162
225
|
if __name__ == '__main__':
|
@@ -57,8 +57,8 @@ langfun/core/eval/scoring_test.py
|
|
57
57
|
langfun/core/llms/__init__.py
|
58
58
|
langfun/core/llms/fake.py
|
59
59
|
langfun/core/llms/fake_test.py
|
60
|
-
langfun/core/llms/
|
61
|
-
langfun/core/llms/
|
60
|
+
langfun/core/llms/google_genai.py
|
61
|
+
langfun/core/llms/google_genai_test.py
|
62
62
|
langfun/core/llms/llama_cpp.py
|
63
63
|
langfun/core/llms/llama_cpp_test.py
|
64
64
|
langfun/core/llms/openai.py
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/coding/python/__init__.py
RENAMED
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/coding/python/correction.py
RENAMED
File without changes
|
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/coding/python/errors.py
RENAMED
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/coding/python/errors_test.py
RENAMED
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/coding/python/execution.py
RENAMED
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/coding/python/execution_test.py
RENAMED
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/coding/python/generation.py
RENAMED
File without changes
|
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/coding/python/parsing.py
RENAMED
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/coding/python/parsing_test.py
RENAMED
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/coding/python/permissions.py
RENAMED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/llms/cache/in_memory.py
RENAMED
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/llms/cache/in_memory_test.py
RENAMED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/modalities/image_test.py
RENAMED
File without changes
|
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/modalities/mime_test.py
RENAMED
File without changes
|
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/modalities/video_test.py
RENAMED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/natural_language_test.py
RENAMED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/completion.py
RENAMED
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/completion_test.py
RENAMED
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/description.py
RENAMED
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/description_test.py
RENAMED
File without changes
|
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/mapping_test.py
RENAMED
File without changes
|
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/parsing_test.py
RENAMED
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/prompting.py
RENAMED
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/prompting_test.py
RENAMED
File without changes
|
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/schema_generation.py
RENAMED
File without changes
|
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/schema_test.py
RENAMED
File without changes
|
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/structured/scoring_test.py
RENAMED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/templates/completion.py
RENAMED
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/templates/completion_test.py
RENAMED
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/templates/conversation.py
RENAMED
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/templates/conversation_test.py
RENAMED
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/templates/demonstration.py
RENAMED
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/templates/demonstration_test.py
RENAMED
File without changes
|
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/templates/selfplay_test.py
RENAMED
File without changes
|
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun/core/text_formatting_test.py
RENAMED
File without changes
|
{langfun-0.0.2.dev20240329 → langfun-0.0.2.dev20240401}/langfun.egg-info/dependency_links.txt
RENAMED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|