langfun 0.0.2.dev20240507__tar.gz → 0.0.2.dev20240513__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.dev20240507 → langfun-0.0.2.dev20240513}/PKG-INFO +2 -1
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/__init__.py +5 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/eval/base.py +4 -2
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/langfunc.py +1 -17
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/langfunc_test.py +4 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/llms/__init__.py +8 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/llms/fake.py +6 -6
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/llms/google_genai.py +8 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/llms/openai.py +3 -2
- langfun-0.0.2.dev20240513/langfun/core/llms/vertexai.py +291 -0
- langfun-0.0.2.dev20240513/langfun/core/llms/vertexai_test.py +233 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/modalities/image.py +1 -3
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/modalities/mime.py +6 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/modalities/video.py +1 -3
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/structured/__init__.py +2 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/structured/prompting.py +31 -7
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/structured/prompting_test.py +43 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/template.py +22 -1
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun.egg-info/PKG-INFO +2 -1
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun.egg-info/SOURCES.txt +2 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun.egg-info/requires.txt +1 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/LICENSE +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/README.md +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/__init__.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/coding/__init__.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/coding/python/__init__.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/coding/python/correction.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/coding/python/correction_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/coding/python/errors.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/coding/python/errors_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/coding/python/execution.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/coding/python/execution_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/coding/python/generation.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/coding/python/generation_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/coding/python/parsing.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/coding/python/parsing_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/coding/python/permissions.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/coding/python/permissions_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/component.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/component_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/concurrent.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/concurrent_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/console.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/console_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/eval/__init__.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/eval/base_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/eval/matching.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/eval/matching_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/eval/patching.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/eval/patching_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/eval/scoring.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/eval/scoring_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/language_model.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/language_model_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/llms/anthropic.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/llms/anthropic_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/llms/cache/__init__.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/llms/cache/base.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/llms/cache/in_memory.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/llms/cache/in_memory_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/llms/fake_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/llms/google_genai_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/llms/groq.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/llms/groq_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/llms/llama_cpp.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/llms/llama_cpp_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/llms/openai_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/memories/__init__.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/memories/conversation_history.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/memories/conversation_history_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/memory.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/message.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/message_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/modalities/__init__.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/modalities/image_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/modalities/mime_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/modalities/video_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/modality.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/modality_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/natural_language.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/natural_language_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/sampling.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/sampling_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/structured/completion.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/structured/completion_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/structured/description.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/structured/description_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/structured/function_generation.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/structured/function_generation_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/structured/mapping.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/structured/mapping_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/structured/parsing.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/structured/parsing_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/structured/schema.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/structured/schema_generation.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/structured/schema_generation_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/structured/schema_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/structured/scoring.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/structured/scoring_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/subscription.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/subscription_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/template_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/templates/__init__.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/templates/completion.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/templates/completion_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/templates/conversation.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/templates/conversation_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/templates/demonstration.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/templates/demonstration_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/templates/selfplay.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/templates/selfplay_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/text_formatting.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun/core/text_formatting_test.py +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun.egg-info/dependency_links.txt +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/langfun.egg-info/top_level.txt +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/setup.cfg +0 -0
- {langfun-0.0.2.dev20240507 → langfun-0.0.2.dev20240513}/setup.py +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: langfun
|
3
|
-
Version: 0.0.2.
|
3
|
+
Version: 0.0.2.dev20240513
|
4
4
|
Summary: Langfun: Language as Functions.
|
5
5
|
Home-page: https://github.com/google/langfun
|
6
6
|
Author: Langfun Authors
|
@@ -21,6 +21,7 @@ Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
21
|
Classifier: Topic :: Software Development :: Libraries
|
22
22
|
Description-Content-Type: text/markdown
|
23
23
|
License-File: LICENSE
|
24
|
+
Requires-Dist: google-cloud-aiplatform>=1.5.0
|
24
25
|
Requires-Dist: google-generativeai>=0.3.2
|
25
26
|
Requires-Dist: jinja2>=3.1.2
|
26
27
|
Requires-Dist: openai==0.27.2
|
@@ -33,6 +33,11 @@ complete = structured.complete
|
|
33
33
|
score = structured.score
|
34
34
|
generate_class = structured.generate_class
|
35
35
|
|
36
|
+
# Helper functions for input/output transformations based on
|
37
|
+
# `lf.query` (e.g. jax-on-beam could use these for batch processing)
|
38
|
+
query_prompt = structured.query_prompt
|
39
|
+
query_output = structured.query_output
|
40
|
+
|
36
41
|
source_form = structured.source_form
|
37
42
|
function_gen = structured.function_gen
|
38
43
|
|
@@ -1179,7 +1179,7 @@ class Evaluation(Evaluable):
|
|
1179
1179
|
|
1180
1180
|
def process(self, example: Any, **kwargs) -> lf.Message:
|
1181
1181
|
"""Process an example and returns its output."""
|
1182
|
-
prompt = self.prompt
|
1182
|
+
prompt = lf.Template.from_value(self.prompt, example=example)
|
1183
1183
|
if self.method == 'call':
|
1184
1184
|
return lf_structured.call(
|
1185
1185
|
prompt,
|
@@ -1207,7 +1207,9 @@ class Evaluation(Evaluable):
|
|
1207
1207
|
else:
|
1208
1208
|
assert self.method == 'complete', self.method
|
1209
1209
|
assert isinstance(self.schema.spec, pg.typing.Object), self.schema
|
1210
|
-
|
1210
|
+
# TODO(daiyip): Currently multi-modal inputs within the prompt for
|
1211
|
+
# completion is not supported.
|
1212
|
+
input_value = self.schema.spec.cls.partial(prompt.render().text)
|
1211
1213
|
return lf_structured.complete(
|
1212
1214
|
input_value,
|
1213
1215
|
lm=self.lm,
|
@@ -14,7 +14,7 @@
|
|
14
14
|
"""LangFunc: Language-based functions."""
|
15
15
|
|
16
16
|
import dataclasses
|
17
|
-
from typing import Annotated, Type
|
17
|
+
from typing import Annotated, Type
|
18
18
|
|
19
19
|
from langfun.core import component
|
20
20
|
from langfun.core import language_model
|
@@ -328,22 +328,6 @@ class LangFunc(
|
|
328
328
|
"""Transforms the output message before returning from __call__."""
|
329
329
|
return lm_output
|
330
330
|
|
331
|
-
@classmethod
|
332
|
-
def from_value(
|
333
|
-
cls, value: Union[str, template_lib.Template], **kwargs
|
334
|
-
) -> 'LangFunc':
|
335
|
-
"""Create a LangFunc object from a string or template."""
|
336
|
-
if isinstance(value, LangFunc):
|
337
|
-
return value
|
338
|
-
if isinstance(value, template_lib.Template):
|
339
|
-
lfun = LangFunc(value.template_str, **kwargs)
|
340
|
-
# So lfun could acccess all attributes from value.
|
341
|
-
lfun.sym_setparent(value)
|
342
|
-
return lfun
|
343
|
-
if isinstance(value, str):
|
344
|
-
return LangFunc(template_str=value, **kwargs)
|
345
|
-
return LangFunc('{{input}}', input=value, **kwargs)
|
346
|
-
|
347
331
|
|
348
332
|
# Register converter from str to LangFunc, therefore we can always
|
349
333
|
# pass strs to attributes that accept LangFunc.
|
@@ -57,6 +57,10 @@ class BasicTest(unittest.TestCase):
|
|
57
57
|
l2 = LangFunc.from_value(l1)
|
58
58
|
self.assertIs(l2, l1)
|
59
59
|
|
60
|
+
l3 = LangFunc.from_value(l1, x=1)
|
61
|
+
self.assertIsNot(l3, l1)
|
62
|
+
self.assertTrue(pg.eq(l3, LangFunc('Hello', x=1)))
|
63
|
+
|
60
64
|
c = template_lib.Template(
|
61
65
|
'{{x}} + {{l}}',
|
62
66
|
x=1,
|
@@ -27,6 +27,7 @@ from langfun.core.llms.fake import StaticSequence
|
|
27
27
|
# Gemini models.
|
28
28
|
from langfun.core.llms.google_genai import GenAI
|
29
29
|
from langfun.core.llms.google_genai import GeminiPro
|
30
|
+
from langfun.core.llms.google_genai import GeminiPro1_5
|
30
31
|
from langfun.core.llms.google_genai import GeminiProVision
|
31
32
|
from langfun.core.llms.google_genai import Palm2
|
32
33
|
from langfun.core.llms.google_genai import Palm2_IT
|
@@ -73,6 +74,13 @@ from langfun.core.llms.groq import GroqLlama2_70B
|
|
73
74
|
from langfun.core.llms.groq import GroqMistral_8x7B
|
74
75
|
from langfun.core.llms.groq import GroqGemma7B_IT
|
75
76
|
|
77
|
+
from langfun.core.llms.vertexai import VertexAI
|
78
|
+
from langfun.core.llms.vertexai import VertexAIGeminiPro1_5
|
79
|
+
from langfun.core.llms.vertexai import VertexAIGeminiPro1
|
80
|
+
from langfun.core.llms.vertexai import VertexAIGeminiPro1Vision
|
81
|
+
from langfun.core.llms.vertexai import VertexAIPalm2
|
82
|
+
from langfun.core.llms.vertexai import VertexAIPalm2_32K
|
83
|
+
|
76
84
|
|
77
85
|
# LLaMA C++ models.
|
78
86
|
from langfun.core.llms.llama_cpp import LlamaCppRemote
|
@@ -57,12 +57,12 @@ class StaticResponse(Fake):
|
|
57
57
|
"""Language model that always gives the same canned response."""
|
58
58
|
|
59
59
|
response: Annotated[
|
60
|
-
str,
|
60
|
+
str | lf.Message,
|
61
61
|
'A canned response that will be returned regardless of the prompt.'
|
62
62
|
]
|
63
63
|
|
64
64
|
def _response_from(self, prompt: lf.Message) -> lf.Message:
|
65
|
-
return lf.AIMessage(self.response)
|
65
|
+
return lf.AIMessage.from_value(self.response)
|
66
66
|
|
67
67
|
|
68
68
|
@lf.use_init_args(['mapping'])
|
@@ -70,12 +70,12 @@ class StaticMapping(Fake):
|
|
70
70
|
"""A static mapping from prompt to response."""
|
71
71
|
|
72
72
|
mapping: Annotated[
|
73
|
-
dict[str, str],
|
73
|
+
dict[str, str | lf.Message],
|
74
74
|
'A mapping from prompt to response.'
|
75
75
|
]
|
76
76
|
|
77
77
|
def _response_from(self, prompt: lf.Message) -> lf.Message:
|
78
|
-
return lf.AIMessage(self.mapping[prompt])
|
78
|
+
return lf.AIMessage.from_value(self.mapping[prompt])
|
79
79
|
|
80
80
|
|
81
81
|
@lf.use_init_args(['sequence'])
|
@@ -83,7 +83,7 @@ class StaticSequence(Fake):
|
|
83
83
|
"""A static sequence of responses to use."""
|
84
84
|
|
85
85
|
sequence: Annotated[
|
86
|
-
list[str],
|
86
|
+
list[str | lf.Message],
|
87
87
|
'A sequence of strings as the response.'
|
88
88
|
]
|
89
89
|
|
@@ -92,6 +92,6 @@ class StaticSequence(Fake):
|
|
92
92
|
self._pos = 0
|
93
93
|
|
94
94
|
def _response_from(self, prompt: lf.Message) -> lf.Message:
|
95
|
-
r = lf.AIMessage(self.sequence[self._pos])
|
95
|
+
r = lf.AIMessage.from_value(self.sequence[self._pos])
|
96
96
|
self._pos += 1
|
97
97
|
return r
|
@@ -34,6 +34,7 @@ class GenAI(lf.LanguageModel):
|
|
34
34
|
'gemini-pro-vision',
|
35
35
|
'text-bison-001',
|
36
36
|
'chat-bison-001',
|
37
|
+
'gemini-1.5-pro-latest',
|
37
38
|
],
|
38
39
|
'Model name.',
|
39
40
|
]
|
@@ -262,6 +263,13 @@ _GOOGLE_GENAI_MODEL_HUB = _ModelHub()
|
|
262
263
|
#
|
263
264
|
|
264
265
|
|
266
|
+
class GeminiPro1_5(GenAI): # pylint: disable=invalid-name
|
267
|
+
"""Gemini Pro latest model."""
|
268
|
+
|
269
|
+
model = 'gemini-1.5-pro-latest'
|
270
|
+
multimodal = True
|
271
|
+
|
272
|
+
|
265
273
|
class GeminiPro(GenAI):
|
266
274
|
"""Gemini Pro model."""
|
267
275
|
|
@@ -233,8 +233,9 @@ class OpenAI(lf.LanguageModel):
|
|
233
233
|
for chunk in prompt.chunk():
|
234
234
|
if isinstance(chunk, str):
|
235
235
|
item = dict(type='text', text=chunk)
|
236
|
-
elif isinstance(chunk, lf_modalities.Image)
|
237
|
-
|
236
|
+
elif isinstance(chunk, lf_modalities.Image):
|
237
|
+
uri = chunk.uri or chunk.content_uri
|
238
|
+
item = dict(type='image_url', image_url=dict(url=uri))
|
238
239
|
else:
|
239
240
|
raise ValueError(f'Unsupported modality object: {chunk!r}.')
|
240
241
|
content.append(item)
|
@@ -0,0 +1,291 @@
|
|
1
|
+
# Copyright 2023 The Langfun Authors
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
"""Vertex AI generative models."""
|
15
|
+
|
16
|
+
import functools
|
17
|
+
import os
|
18
|
+
from typing import Annotated, Any
|
19
|
+
|
20
|
+
from google.auth import credentials as credentials_lib
|
21
|
+
import langfun.core as lf
|
22
|
+
from langfun.core import modalities as lf_modalities
|
23
|
+
import pyglove as pg
|
24
|
+
|
25
|
+
|
26
|
+
SUPPORTED_MODELS_AND_SETTINGS = {
|
27
|
+
'gemini-1.5-pro-preview-0409': pg.Dict(api='gemini', rpm=5),
|
28
|
+
'gemini-1.0-pro': pg.Dict(api='gemini', rpm=300),
|
29
|
+
'gemini-1.0-pro-vision': pg.Dict(api='gemini', rpm=100),
|
30
|
+
# PaLM APIs.
|
31
|
+
'text-bison': pg.Dict(api='palm', rpm=1600),
|
32
|
+
'text-bison-32k': pg.Dict(api='palm', rpm=300),
|
33
|
+
'text-unicorn': pg.Dict(api='palm', rpm=100),
|
34
|
+
}
|
35
|
+
|
36
|
+
|
37
|
+
@lf.use_init_args(['model'])
|
38
|
+
class VertexAI(lf.LanguageModel):
|
39
|
+
"""Language model served on VertexAI."""
|
40
|
+
|
41
|
+
model: pg.typing.Annotated[
|
42
|
+
pg.typing.Enum(
|
43
|
+
pg.MISSING_VALUE, list(SUPPORTED_MODELS_AND_SETTINGS.keys())
|
44
|
+
),
|
45
|
+
(
|
46
|
+
'Vertex AI model name. See '
|
47
|
+
'https://cloud.google.com/vertex-ai/generative-ai/docs/learn/models '
|
48
|
+
'for details.'
|
49
|
+
),
|
50
|
+
]
|
51
|
+
|
52
|
+
project: Annotated[
|
53
|
+
str | None,
|
54
|
+
(
|
55
|
+
'Vertex AI project ID. Or set from environment variable '
|
56
|
+
'VERTEXAI_PROJECT.'
|
57
|
+
),
|
58
|
+
] = None
|
59
|
+
|
60
|
+
location: Annotated[
|
61
|
+
str | None,
|
62
|
+
(
|
63
|
+
'Vertex AI service location. Or set from environment variable '
|
64
|
+
'VERTEXAI_LOCATION.'
|
65
|
+
),
|
66
|
+
] = None
|
67
|
+
|
68
|
+
credentials: Annotated[
|
69
|
+
credentials_lib.Credentials | None,
|
70
|
+
(
|
71
|
+
'Credentials to use. If None, the default credentials to the '
|
72
|
+
'environment will be used.'
|
73
|
+
),
|
74
|
+
] = None
|
75
|
+
|
76
|
+
multimodal: Annotated[bool, 'Whether this model has multimodal support.'] = (
|
77
|
+
False
|
78
|
+
)
|
79
|
+
|
80
|
+
def _on_bound(self):
|
81
|
+
super()._on_bound()
|
82
|
+
self.__dict__.pop('_api_initialized', None)
|
83
|
+
|
84
|
+
@functools.cached_property
|
85
|
+
def _api_initialized(self):
|
86
|
+
project = self.project or os.environ.get('VERTEXAI_PROJECT', None)
|
87
|
+
if not project:
|
88
|
+
raise ValueError(
|
89
|
+
'Please specify `project` during `__init__` or set environment '
|
90
|
+
'variable `VERTEXAI_PROJECT` with your Vertex AI project ID.'
|
91
|
+
)
|
92
|
+
|
93
|
+
location = self.location or os.environ.get('VERTEXAI_LOCATION', None)
|
94
|
+
if not location:
|
95
|
+
raise ValueError(
|
96
|
+
'Please specify `location` during `__init__` or set environment '
|
97
|
+
'variable `VERTEXAI_LOCATION` with your Vertex AI service location.'
|
98
|
+
)
|
99
|
+
|
100
|
+
credentials = self.credentials
|
101
|
+
# Placeholder for Google-internal credentials.
|
102
|
+
from google.cloud.aiplatform import vertexai # pylint: disable=g-import-not-at-top
|
103
|
+
vertexai.init(project=project, location=location, credentials=credentials)
|
104
|
+
return True
|
105
|
+
|
106
|
+
@property
|
107
|
+
def model_id(self) -> str:
|
108
|
+
"""Returns a string to identify the model."""
|
109
|
+
return f'VertexAI({self.model})'
|
110
|
+
|
111
|
+
@property
|
112
|
+
def resource_id(self) -> str:
|
113
|
+
"""Returns a string to identify the resource for rate control."""
|
114
|
+
return self.model_id
|
115
|
+
|
116
|
+
@property
|
117
|
+
def max_concurrency(self) -> int:
|
118
|
+
"""Returns the maximum number of concurrent requests."""
|
119
|
+
return self.rate_to_max_concurrency(
|
120
|
+
requests_per_min=SUPPORTED_MODELS_AND_SETTINGS[self.model].rpm,
|
121
|
+
tokens_per_min=0,
|
122
|
+
)
|
123
|
+
|
124
|
+
def _generation_config(
|
125
|
+
self, options: lf.LMSamplingOptions
|
126
|
+
) -> Any: # generative_models.GenerationConfig
|
127
|
+
"""Creates generation config from langfun sampling options."""
|
128
|
+
from google.cloud.aiplatform.vertexai.preview import generative_models # pylint: disable=g-import-not-at-top
|
129
|
+
return generative_models.GenerationConfig(
|
130
|
+
temperature=options.temperature,
|
131
|
+
top_p=options.top_p,
|
132
|
+
top_k=options.top_k,
|
133
|
+
max_output_tokens=options.max_tokens,
|
134
|
+
stop_sequences=options.stop,
|
135
|
+
)
|
136
|
+
|
137
|
+
def _content_from_message(
|
138
|
+
self, prompt: lf.Message
|
139
|
+
) -> list[str | Any]:
|
140
|
+
"""Gets generation input from langfun message."""
|
141
|
+
from google.cloud.aiplatform.vertexai.preview import generative_models # pylint: disable=g-import-not-at-top
|
142
|
+
chunks = []
|
143
|
+
for lf_chunk in prompt.chunk():
|
144
|
+
if isinstance(lf_chunk, str):
|
145
|
+
chunk = lf_chunk
|
146
|
+
elif self.multimodal and isinstance(lf_chunk, lf_modalities.Image):
|
147
|
+
chunk = generative_models.Image.from_bytes(lf_chunk.to_bytes())
|
148
|
+
else:
|
149
|
+
raise ValueError(f'Unsupported modality: {lf_chunk!r}')
|
150
|
+
chunks.append(chunk)
|
151
|
+
return chunks
|
152
|
+
|
153
|
+
def _generation_response_to_message(
|
154
|
+
self,
|
155
|
+
response: Any, # generative_models.GenerationResponse
|
156
|
+
) -> lf.Message:
|
157
|
+
"""Parses generative response into message."""
|
158
|
+
return lf.AIMessage(response.text)
|
159
|
+
|
160
|
+
def _sample(self, prompts: list[lf.Message]) -> list[lf.LMSamplingResult]:
|
161
|
+
assert self._api_initialized, 'Vertex AI API is not initialized.'
|
162
|
+
return lf.concurrent_execute(
|
163
|
+
self._sample_single,
|
164
|
+
prompts,
|
165
|
+
executor=self.resource_id,
|
166
|
+
max_workers=self.max_concurrency,
|
167
|
+
# NOTE(daiyip): Vertex has its own policy on handling
|
168
|
+
# with rate limit, so we do not retry on errors.
|
169
|
+
retry_on_errors=None,
|
170
|
+
)
|
171
|
+
|
172
|
+
def _sample_single(self, prompt: lf.Message) -> lf.LMSamplingResult:
|
173
|
+
if self.sampling_options.n > 1:
|
174
|
+
raise ValueError(
|
175
|
+
f'`n` greater than 1 is not supported: {self.sampling_options.n}.'
|
176
|
+
)
|
177
|
+
api = SUPPORTED_MODELS_AND_SETTINGS[self.model].api
|
178
|
+
match api:
|
179
|
+
case 'gemini':
|
180
|
+
return self._sample_generative_model(prompt)
|
181
|
+
case 'palm':
|
182
|
+
return self._sample_text_generation_model(prompt)
|
183
|
+
case _:
|
184
|
+
raise ValueError(f'Unsupported API: {api}')
|
185
|
+
|
186
|
+
def _sample_generative_model(self, prompt: lf.Message) -> lf.LMSamplingResult:
|
187
|
+
"""Samples a generative model."""
|
188
|
+
model = _VERTEXAI_MODEL_HUB.get_generative_model(self.model)
|
189
|
+
input_content = self._content_from_message(prompt)
|
190
|
+
response = model.generate_content(
|
191
|
+
input_content,
|
192
|
+
generation_config=self._generation_config(self.sampling_options),
|
193
|
+
)
|
194
|
+
usage_metadata = response.usage_metadata
|
195
|
+
usage = lf.LMSamplingUsage(
|
196
|
+
prompt_tokens=usage_metadata.prompt_token_count,
|
197
|
+
completion_tokens=usage_metadata.candidates_token_count,
|
198
|
+
total_tokens=usage_metadata.total_token_count,
|
199
|
+
)
|
200
|
+
return lf.LMSamplingResult(
|
201
|
+
[
|
202
|
+
# Scoring is not supported.
|
203
|
+
lf.LMSample(
|
204
|
+
self._generation_response_to_message(response), score=0.0
|
205
|
+
),
|
206
|
+
],
|
207
|
+
usage=usage,
|
208
|
+
)
|
209
|
+
|
210
|
+
def _sample_text_generation_model(
|
211
|
+
self, prompt: lf.Message
|
212
|
+
) -> lf.LMSamplingResult:
|
213
|
+
"""Samples a text generation model."""
|
214
|
+
model = _VERTEXAI_MODEL_HUB.get_text_generation_model(self.model)
|
215
|
+
predict_options = dict(
|
216
|
+
temperature=self.sampling_options.temperature,
|
217
|
+
top_k=self.sampling_options.top_k,
|
218
|
+
top_p=self.sampling_options.top_p,
|
219
|
+
max_output_tokens=self.sampling_options.max_tokens,
|
220
|
+
stop_sequences=self.sampling_options.stop,
|
221
|
+
)
|
222
|
+
response = model.predict(prompt.text, **predict_options)
|
223
|
+
return lf.LMSamplingResult([
|
224
|
+
# Scoring is not supported.
|
225
|
+
lf.LMSample(lf.AIMessage(response.text), score=0.0)
|
226
|
+
])
|
227
|
+
|
228
|
+
|
229
|
+
class _ModelHub:
|
230
|
+
"""Vertex AI model hub."""
|
231
|
+
|
232
|
+
def __init__(self):
|
233
|
+
self._generative_model_cache = {}
|
234
|
+
self._text_generation_model_cache = {}
|
235
|
+
|
236
|
+
def get_generative_model(
|
237
|
+
self, model_id: str
|
238
|
+
) -> Any: # generative_models.GenerativeModel:
|
239
|
+
"""Gets a generative model by model id."""
|
240
|
+
model = self._generative_model_cache.get(model_id, None)
|
241
|
+
if model is None:
|
242
|
+
from google.cloud.aiplatform.vertexai.preview import generative_models # pylint: disable=g-import-not-at-top
|
243
|
+
model = generative_models.GenerativeModel(model_id)
|
244
|
+
self._generative_model_cache[model_id] = model
|
245
|
+
return model
|
246
|
+
|
247
|
+
def get_text_generation_model(
|
248
|
+
self, model_id: str
|
249
|
+
) -> Any: # language_models.TextGenerationModel
|
250
|
+
"""Gets a text generation model by model id."""
|
251
|
+
model = self._text_generation_model_cache.get(model_id, None)
|
252
|
+
if model is None:
|
253
|
+
from google.cloud.aiplatform.vertexai import language_models # pylint: disable=g-import-not-at-top
|
254
|
+
model = language_models.TextGenerationModel.from_pretrained(model_id)
|
255
|
+
self._text_generation_model_cache[model_id] = model
|
256
|
+
return model
|
257
|
+
|
258
|
+
|
259
|
+
_VERTEXAI_MODEL_HUB = _ModelHub()
|
260
|
+
|
261
|
+
|
262
|
+
class VertexAIGeminiPro1_5(VertexAI): # pylint: disable=invalid-name
|
263
|
+
"""Vertex AI Gemini 1.5 Pro model."""
|
264
|
+
|
265
|
+
model = 'gemini-1.5-pro-preview-0409'
|
266
|
+
multimodal = True
|
267
|
+
|
268
|
+
|
269
|
+
class VertexAIGeminiPro1(VertexAI): # pylint: disable=invalid-name
|
270
|
+
"""Vertex AI Gemini 1.0 Pro model."""
|
271
|
+
|
272
|
+
model = 'gemini-1.0-pro'
|
273
|
+
|
274
|
+
|
275
|
+
class VertexAIGeminiPro1Vision(VertexAI): # pylint: disable=invalid-name
|
276
|
+
"""Vertex AI Gemini 1.0 Pro model."""
|
277
|
+
|
278
|
+
model = 'gemini-1.0-pro-vision'
|
279
|
+
multimodal = True
|
280
|
+
|
281
|
+
|
282
|
+
class VertexAIPalm2(VertexAI): # pylint: disable=invalid-name
|
283
|
+
"""Vertex AI PaLM2 text generation model."""
|
284
|
+
|
285
|
+
model = 'text-bison'
|
286
|
+
|
287
|
+
|
288
|
+
class VertexAIPalm2_32K(VertexAI): # pylint: disable=invalid-name
|
289
|
+
"""Vertex AI PaLM2 text generation model (32K context length)."""
|
290
|
+
|
291
|
+
model = 'text-bison-32k'
|