langfun 0.1.2.dev202501050804__py3-none-any.whl → 0.1.2.dev202501060804__py3-none-any.whl

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.
@@ -11,105 +11,18 @@
11
11
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
- """Tests for Gemini models."""
14
+ """Tests for VertexAI models."""
15
15
 
16
- import base64
17
16
  import os
18
- from typing import Any
19
17
  import unittest
20
18
  from unittest import mock
21
19
 
22
- import langfun.core as lf
23
- from langfun.core import modalities as lf_modalities
24
20
  from langfun.core.llms import vertexai
25
- import pyglove as pg
26
- import requests
27
-
28
-
29
- example_image = (
30
- b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x18\x00\x00\x00\x18\x04'
31
- b'\x03\x00\x00\x00\x12Y \xcb\x00\x00\x00\x18PLTE\x00\x00'
32
- b'\x00fff_chaag_cg_ch^ci_ciC\xedb\x94\x00\x00\x00\x08tRNS'
33
- b'\x00\n\x9f*\xd4\xff_\xf4\xe4\x8b\xf3a\x00\x00\x00>IDATx'
34
- b'\x01c \x05\x08)"\xd8\xcc\xae!\x06pNz\x88k\x19\\Q\xa8"\x10'
35
- b'\xc1\x14\x95\x01%\xc1\n\xa143Ta\xa8"D-\x84\x03QM\x98\xc3'
36
- b'\x1a\x1a\x1a@5\x0e\x04\xa0q\x88\x05\x00\x07\xf8\x18\xf9'
37
- b'\xdao\xd0|\x00\x00\x00\x00IEND\xaeB`\x82'
38
- )
39
-
40
-
41
- def mock_requests_post(url: str, json: dict[str, Any], **kwargs):
42
- del url, kwargs
43
- c = pg.Dict(json['generationConfig'])
44
- content = json['contents'][0]['parts'][0]['text']
45
- response = requests.Response()
46
- response.status_code = 200
47
- response._content = pg.to_json_str({
48
- 'candidates': [
49
- {
50
- 'content': {
51
- 'role': 'model',
52
- 'parts': [
53
- {
54
- 'text': (
55
- f'This is a response to {content} with '
56
- f'temperature={c.temperature}, '
57
- f'top_p={c.topP}, '
58
- f'top_k={c.topK}, '
59
- f'max_tokens={c.maxOutputTokens}, '
60
- f'stop={"".join(c.stopSequences)}.'
61
- )
62
- },
63
- ],
64
- },
65
- },
66
- ],
67
- 'usageMetadata': {
68
- 'promptTokenCount': 3,
69
- 'candidatesTokenCount': 4,
70
- }
71
- }).encode()
72
- return response
73
21
 
74
22
 
75
23
  class VertexAITest(unittest.TestCase):
76
24
  """Tests for Vertex model with REST API."""
77
25
 
78
- def test_content_from_message_text_only(self):
79
- text = 'This is a beautiful day'
80
- model = vertexai.VertexAIGeminiPro1_5_002()
81
- chunks = model._content_from_message(lf.UserMessage(text))
82
- self.assertEqual(chunks, {'role': 'user', 'parts': [{'text': text}]})
83
-
84
- def test_content_from_message_mm(self):
85
- image = lf_modalities.Image.from_bytes(example_image)
86
- message = lf.UserMessage(
87
- 'This is an <<[[image]]>>, what is it?', image=image
88
- )
89
-
90
- # Non-multimodal model.
91
- with self.assertRaisesRegex(lf.ModalityError, 'Unsupported modality'):
92
- vertexai.VertexAIGeminiPro1()._content_from_message(message)
93
-
94
- model = vertexai.VertexAIGeminiPro1Vision()
95
- content = model._content_from_message(message)
96
- self.assertEqual(
97
- content,
98
- {
99
- 'role': 'user',
100
- 'parts': [
101
- {'text': 'This is an'},
102
- {
103
- 'inlineData': {
104
- 'data': base64.b64encode(example_image).decode(),
105
- 'mimeType': 'image/png',
106
- }
107
- },
108
- {'text': ', what is it?'},
109
- ],
110
- },
111
- )
112
-
113
26
  @mock.patch.object(vertexai.VertexAI, 'credentials', new=True)
114
27
  def test_project_and_location_check(self):
115
28
  with self.assertRaisesRegex(ValueError, 'Please specify `project`'):
@@ -126,87 +39,14 @@ class VertexAITest(unittest.TestCase):
126
39
 
127
40
  os.environ['VERTEXAI_PROJECT'] = 'abc'
128
41
  os.environ['VERTEXAI_LOCATION'] = 'us-central1'
129
- self.assertTrue(vertexai.VertexAIGeminiPro1()._api_initialized)
42
+ model = vertexai.VertexAIGeminiPro1()
43
+ self.assertTrue(model.model_id.startswith('VertexAI('))
44
+ self.assertIsNotNone(model.api_endpoint)
45
+ self.assertTrue(model._api_initialized)
46
+ self.assertIsNotNone(model._session)
130
47
  del os.environ['VERTEXAI_PROJECT']
131
48
  del os.environ['VERTEXAI_LOCATION']
132
49
 
133
- def test_generation_config(self):
134
- model = vertexai.VertexAIGeminiPro1()
135
- json_schema = {
136
- 'type': 'object',
137
- 'properties': {
138
- 'name': {'type': 'string'},
139
- },
140
- 'required': ['name'],
141
- 'title': 'Person',
142
- }
143
- actual = model._generation_config(
144
- lf.UserMessage('hi', json_schema=json_schema),
145
- lf.LMSamplingOptions(
146
- temperature=2.0,
147
- top_p=1.0,
148
- top_k=20,
149
- max_tokens=1024,
150
- stop=['\n'],
151
- ),
152
- )
153
- self.assertEqual(
154
- actual,
155
- dict(
156
- candidateCount=1,
157
- temperature=2.0,
158
- topP=1.0,
159
- topK=20,
160
- maxOutputTokens=1024,
161
- stopSequences=['\n'],
162
- responseLogprobs=False,
163
- logprobs=None,
164
- seed=None,
165
- responseMimeType='application/json',
166
- responseSchema={
167
- 'type': 'object',
168
- 'properties': {
169
- 'name': {'type': 'string'}
170
- },
171
- 'required': ['name'],
172
- 'title': 'Person',
173
- }
174
- ),
175
- )
176
- with self.assertRaisesRegex(
177
- ValueError, '`json_schema` must be a dict, got'
178
- ):
179
- model._generation_config(
180
- lf.UserMessage('hi', json_schema='not a dict'),
181
- lf.LMSamplingOptions(),
182
- )
183
-
184
- @mock.patch.object(vertexai.VertexAI, 'credentials', new=True)
185
- def test_call_model(self):
186
- with mock.patch('requests.Session.post') as mock_generate:
187
- mock_generate.side_effect = mock_requests_post
188
-
189
- lm = vertexai.VertexAIGeminiPro1_5_002(
190
- project='abc', location='us-central1'
191
- )
192
- r = lm(
193
- 'hello',
194
- temperature=2.0,
195
- top_p=1.0,
196
- top_k=20,
197
- max_tokens=1024,
198
- stop='\n',
199
- )
200
- self.assertEqual(
201
- r.text,
202
- (
203
- 'This is a response to hello with temperature=2.0, '
204
- 'top_p=1.0, top_k=20, max_tokens=1024, stop=\n.'
205
- ),
206
- )
207
- self.assertEqual(r.metadata.usage.prompt_tokens, 3)
208
- self.assertEqual(r.metadata.usage.completion_tokens, 4)
209
-
210
50
 
211
51
  if __name__ == '__main__':
212
52
  unittest.main()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: langfun
3
- Version: 0.1.2.dev202501050804
3
+ Version: 0.1.2.dev202501060804
4
4
  Summary: Langfun: Language as Functions.
5
5
  Home-page: https://github.com/google/langfun
6
6
  Author: Langfun Authors
@@ -30,7 +30,7 @@ Requires-Dist: jinja2>=3.1.2; extra == "all"
30
30
  Requires-Dist: requests>=2.31.0; extra == "all"
31
31
  Requires-Dist: termcolor==1.1.0; extra == "all"
32
32
  Requires-Dist: tqdm>=4.64.1; extra == "all"
33
- Requires-Dist: google-generativeai>=0.3.2; extra == "all"
33
+ Requires-Dist: google-auth>=2.16.0; extra == "all"
34
34
  Requires-Dist: python-magic>=0.4.27; extra == "all"
35
35
  Requires-Dist: python-docx>=0.8.11; extra == "all"
36
36
  Requires-Dist: pillow>=10.0.0; extra == "all"
@@ -39,12 +39,8 @@ Requires-Dist: pandas>=2.0.3; extra == "all"
39
39
  Provides-Extra: ui
40
40
  Requires-Dist: termcolor==1.1.0; extra == "ui"
41
41
  Requires-Dist: tqdm>=4.64.1; extra == "ui"
42
- Provides-Extra: llm
43
- Requires-Dist: google-generativeai>=0.3.2; extra == "llm"
44
- Provides-Extra: llm-google
45
- Requires-Dist: google-generativeai>=0.3.2; extra == "llm-google"
46
- Provides-Extra: llm-google-genai
47
- Requires-Dist: google-generativeai>=0.3.2; extra == "llm-google-genai"
42
+ Provides-Extra: vertexai
43
+ Requires-Dist: google-auth>=2.16.0; extra == "vertexai"
48
44
  Provides-Extra: mime
49
45
  Requires-Dist: python-magic>=0.4.27; extra == "mime"
50
46
  Requires-Dist: python-docx>=0.8.11; extra == "mime"
@@ -201,9 +197,7 @@ If you want to customize your installation, you can select specific features usi
201
197
  | Tag | Description |
202
198
  | ------------------- | ---------------------------------------- |
203
199
  | all | All Langfun features. |
204
- | llm | All supported LLMs. |
205
- | llm-google | All supported Google-powered LLMs. |
206
- | llm-google-genai | LLMs powered by Google Generative AI API |
200
+ | vertexai | VertexAI access. |
207
201
  | mime | All MIME supports. |
208
202
  | mime-auto | Automatic MIME type detection. |
209
203
  | mime-docx | DocX format support. |
@@ -212,9 +206,9 @@ If you want to customize your installation, you can select specific features usi
212
206
  | ui | UI enhancements |
213
207
 
214
208
 
215
- For example, to install a nightly build that includes Google-powered LLMs, full modality support, and UI enhancements, use:
209
+ For example, to install a nightly build that includes VertexAI access, full modality support, and UI enhancements, use:
216
210
  ```
217
- pip install langfun[llm-google,mime,ui] --pre
211
+ pip install langfun[vertexai,mime,ui] --pre
218
212
  ```
219
213
 
220
214
  *Disclaimer: this is not an officially supported Google product.*
@@ -73,19 +73,21 @@ langfun/core/eval/v2/progress.py,sha256=azZgssQgNdv3IgjKEaQBuGI5ucFDNbdi02P4z_nQ
73
73
  langfun/core/eval/v2/progress_test.py,sha256=YU7VHzmy5knPZwj9vpBN3rQQH2tukj9eKHkuBCI62h8,2540
74
74
  langfun/core/eval/v2/progress_tracking.py,sha256=l9fEkz4oP5McpZzf72Ua7PYm3lAWtRru7gRWNf8H0ms,6083
75
75
  langfun/core/eval/v2/progress_tracking_test.py,sha256=fouMVJkFJqHjbhQJngGLGCmA9x3n0dU4USI2dY163mg,2291
76
- langfun/core/eval/v2/reporting.py,sha256=KF4pE2H1qj3mJcgkv_c5YYFjrU-uJCk_-fuu891Olzs,8061
76
+ langfun/core/eval/v2/reporting.py,sha256=obYPQma5k4K3jxTmV8UXigJnQZvWfxUENBwoa-29JSk,8265
77
77
  langfun/core/eval/v2/reporting_test.py,sha256=UmYSAQvD3AIXsSyWQ-WD2uLtEISYpmBeoKY5u5Qwc8E,5696
78
78
  langfun/core/eval/v2/runners.py,sha256=DKEmSlGXjOXKWFdBhTpLy7tMsBHZHd1Brl3hWIngsSQ,15931
79
79
  langfun/core/eval/v2/runners_test.py,sha256=A37fKK2MvAVTiShsg_laluJzJ9AuAQn52k7HPbfD0Ks,11666
80
- langfun/core/llms/__init__.py,sha256=6mi0IKTNfq6kymZPlPGA2V7YF1xDLrBCPytojeFMMeA,6716
80
+ langfun/core/llms/__init__.py,sha256=hzddKJKI7NsqGXXyV3Q2jhB95A0TTpBIAhKrACICkk0,6519
81
81
  langfun/core/llms/anthropic.py,sha256=a5MmnFsBA0CbfvwzXT1v_0fqLRMrhUNdh1tx6469PQ4,14357
82
82
  langfun/core/llms/anthropic_test.py,sha256=-2U4kc_pgBM7wqxu8RuxzyHPGww1EAWqKUvN4PW8Btw,8058
83
83
  langfun/core/llms/compositional.py,sha256=csW_FLlgL-tpeyCOTVvfUQkMa_zCN5Y2I-YbSNuK27U,2872
84
84
  langfun/core/llms/compositional_test.py,sha256=4eTnOer-DncRKGaIJW2ZQQMLnt5r2R0UIx_DYOvGAQo,2027
85
85
  langfun/core/llms/fake.py,sha256=gCHBYBLvBCsC78HI1hpoqXCS-p1FMTgY1P1qh_sGBPk,3070
86
86
  langfun/core/llms/fake_test.py,sha256=2h13qkwEz_JR0mtUDPxdAhQo7MueXaFSwsD2DIRDW9g,7653
87
- langfun/core/llms/google_genai.py,sha256=3iAmLMcBXxkfiiI8BN0S6trKCfyfuajCIHIGpnCrtTg,11973
88
- langfun/core/llms/google_genai_test.py,sha256=zw14sgWmk0P_irHyb7vpPy1WAuLEE0PmyfiFElu03sA,7686
87
+ langfun/core/llms/gemini.py,sha256=tfM4vrt0WnvnrxRhWXZWh7Gp8dYYfMnSbi9uOstkSak,17399
88
+ langfun/core/llms/gemini_test.py,sha256=2ERhYWCJwnfDTQbCaZHFuB1TdWJFrOBS7yyCBInIdQk,6129
89
+ langfun/core/llms/google_genai.py,sha256=p19jTO3D-wNUIxDcLwypgGpTCjvwQxqxCMBoPjtaetM,3645
90
+ langfun/core/llms/google_genai_test.py,sha256=JZf_cbQ4GGGpwiQCLjFJn7V4jxBBqgZhIx91AzbGKVo,1250
89
91
  langfun/core/llms/groq.py,sha256=dCnR3eAECEKuKKAAj-PDTs8NRHl6CQPdf57m1f6a79U,10312
90
92
  langfun/core/llms/groq_test.py,sha256=GYF_Qtq5S1H1TrKH38t6_lkdroqT7v-joYLDKnmS9e0,5274
91
93
  langfun/core/llms/llama_cpp.py,sha256=9tXQntSCDtjTF3bnyJrAPCr4N6wycy5nXYvp9uduygE,2843
@@ -94,12 +96,12 @@ langfun/core/llms/openai.py,sha256=dLDVBB47nJ30XCwjJpAZMc55ZlZXB__PcfcICCRNuXQ,2
94
96
  langfun/core/llms/openai_test.py,sha256=kOWa1nf-nJvtYY10REUw5wojh3ZgfU8tRaCZ8wUgJbA,16623
95
97
  langfun/core/llms/rest.py,sha256=sWbYUV8S3SuOg9giq7xwD-xDRfaF7NP_ig7bI52-Rj4,3442
96
98
  langfun/core/llms/rest_test.py,sha256=NZ3Nf0XQVpT9kLP5cBVo_yBHLI7vWTYhWQxYEJVMGs4,3472
97
- langfun/core/llms/vertexai.py,sha256=EPPswgaTfPZQ_GGa_dWsqWPV9uRjCmIH2Iwgm1YXOqM,15377
98
- langfun/core/llms/vertexai_test.py,sha256=ffcA5yPecnQy_rhkuYAw_6o1iLW8AR8FgswmHt6aAys,6725
99
+ langfun/core/llms/vertexai.py,sha256=D9pC-zNRTZ4LDZeV_tjP_c8y3JaGQxTOXpp8VIthwCI,5420
100
+ langfun/core/llms/vertexai_test.py,sha256=KqQkJpjZvpqp-JmDK2yTYK_XyLIyizzb4EeTHfUV8sk,1779
99
101
  langfun/core/llms/cache/__init__.py,sha256=QAo3InUMDM_YpteNnVCSejI4zOsnjSMWKJKzkb3VY64,993
100
102
  langfun/core/llms/cache/base.py,sha256=rt3zwmyw0y9jsSGW-ZbV1vAfLxQ7_3AVk0l2EySlse4,3918
101
- langfun/core/llms/cache/in_memory.py,sha256=l6b-iU9OTfTRo9Zmg4VrQIuArs4cCJDOpXiEpvNocjo,5004
102
- langfun/core/llms/cache/in_memory_test.py,sha256=taebhky84-VbqwSA-96GZp-uBe5INYITVrM-NCsoE7E,10249
103
+ langfun/core/llms/cache/in_memory.py,sha256=i58oiQL28RDsq37dwqgVpC2mBETJjIEFS20yHiV5MKU,5185
104
+ langfun/core/llms/cache/in_memory_test.py,sha256=V2UPeu5co5vUwSkjekCH1B1iLm9qQKPaacvP6VW3GTg,10388
103
105
  langfun/core/memories/__init__.py,sha256=HpghfZ-w1NQqzJXBx8Lz0daRhB2rcy2r9Xm491SBhC4,773
104
106
  langfun/core/memories/conversation_history.py,sha256=c9amD8hCxGFiZuVAzkP0dOMWSp8L90uvwkOejjuBqO0,1835
105
107
  langfun/core/memories/conversation_history_test.py,sha256=AaW8aNoFjxNusanwJDV0r3384Mg0eAweGmPx5DIkM0Y,2052
@@ -146,8 +148,8 @@ langfun/core/templates/demonstration.py,sha256=vCrgYubdZM5Umqcgp8NUVGXgr4P_c-fik
146
148
  langfun/core/templates/demonstration_test.py,sha256=SafcDQ0WgI7pw05EmPI2S4v1t3ABKzup8jReCljHeK4,2162
147
149
  langfun/core/templates/selfplay.py,sha256=yhgrJbiYwq47TgzThmHrDQTF4nDrTI09CWGhuQPNv-s,2273
148
150
  langfun/core/templates/selfplay_test.py,sha256=Ot__1P1M8oJfoTp-M9-PQ6HUXqZKyMwvZ5f7yQ3yfyM,2326
149
- langfun-0.1.2.dev202501050804.dist-info/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
150
- langfun-0.1.2.dev202501050804.dist-info/METADATA,sha256=kNngwBqJlzR5sCW7VWtWMwZLA2uItkoRB8q1mXZe5ys,8281
151
- langfun-0.1.2.dev202501050804.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
152
- langfun-0.1.2.dev202501050804.dist-info/top_level.txt,sha256=RhlEkHxs1qtzmmtWSwYoLVJAc1YrbPtxQ52uh8Z9VvY,8
153
- langfun-0.1.2.dev202501050804.dist-info/RECORD,,
151
+ langfun-0.1.2.dev202501060804.dist-info/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
152
+ langfun-0.1.2.dev202501060804.dist-info/METADATA,sha256=3MbJlnp-TmGrXXb9jhRPLd7g86sm9b2zDXLWIWMkkZA,7941
153
+ langfun-0.1.2.dev202501060804.dist-info/WHEEL,sha256=A3WOREP4zgxI0fKrHUG8DC8013e3dK3n7a6HDbcEIwE,91
154
+ langfun-0.1.2.dev202501060804.dist-info/top_level.txt,sha256=RhlEkHxs1qtzmmtWSwYoLVJAc1YrbPtxQ52uh8Z9VvY,8
155
+ langfun-0.1.2.dev202501060804.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (75.6.0)
2
+ Generator: setuptools (75.7.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5