langfun 0.1.2.dev202505080804__py3-none-any.whl → 0.1.2.dev202505100803__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.

Potentially problematic release.


This version of langfun might be problematic. Click here for more details.

@@ -17,6 +17,7 @@ import os
17
17
  import unittest
18
18
  from unittest import mock
19
19
 
20
+ from google import auth
20
21
  from google.auth import exceptions
21
22
  import langfun.core as lf
22
23
  from langfun.core.llms import rest
@@ -61,7 +62,11 @@ class VertexAITest(unittest.TestCase):
61
62
  lf.concurrent.RetryError,
62
63
  'Failed to refresh Google authentication credentials'
63
64
  ):
64
- with mock.patch.object(rest.REST, '_sample_single') as mock_sample_single:
65
+ with (
66
+ mock.patch.object(auth, 'default') as mock_auth,
67
+ mock.patch.object(rest.REST, '_sample_single') as mock_sample_single
68
+ ):
69
+ mock_auth.return_value = mock.MagicMock(), None
65
70
  mock_sample_single.side_effect = _auth_refresh_error
66
71
  model = vertexai.VertexAIGemini15Pro(
67
72
  project='abc', location='us-central1', max_attempts=1
@@ -325,7 +325,7 @@ def query(
325
325
 
326
326
  ```
327
327
  lf.query('1 + 1 = ?', int, lm=lf.llms.Gpt4Turbo())
328
-
328
+
329
329
  # Output: 2
330
330
  ```
331
331
 
@@ -349,7 +349,7 @@ def query(
349
349
 
350
350
  class Dog(Animal):
351
351
  pass
352
-
352
+
353
353
  class Entity(pg.Object):
354
354
  name: str
355
355
 
@@ -562,7 +562,10 @@ def query(
562
562
  output_message = lf.AIMessage(processed_text, source=output_message)
563
563
  else:
564
564
  # Query with structured output.
565
- output_message = LfQuery.from_protocol(protocol)(
565
+ query_cls = LfQuery.from_protocol(protocol)
566
+ if ':' not in protocol:
567
+ protocol = f'{protocol}:{query_cls.version}'
568
+ output_message = query_cls(
566
569
  input=(
567
570
  query_input.render(lm=lm)
568
571
  if isinstance(query_input, lf.Template)
@@ -572,7 +575,7 @@ def query(
572
575
  default=default,
573
576
  examples=examples,
574
577
  response_postprocess=response_postprocess,
575
- autofix=autofix if protocol == 'python' else 0,
578
+ autofix=autofix if protocol.startswith('python:') else 0,
576
579
  **kwargs,
577
580
  )(
578
581
  lm=lm,
@@ -605,6 +608,8 @@ def query(
605
608
  ),
606
609
  lm=pg.Ref(lm),
607
610
  examples=pg.Ref(examples) if examples else [],
611
+ protocol=protocol,
612
+ kwargs={k: pg.Ref(v) for k, v in kwargs.items()},
608
613
  lm_response=lf.AIMessage(output_message.text, metadata=metadata),
609
614
  usage_summary=usage_summary,
610
615
  start_time=start_time,
@@ -788,6 +793,14 @@ class QueryInvocation(pg.Object, pg.views.HtmlTreeView.Extension):
788
793
  list[mapping.MappingExample],
789
794
  'Fewshot exemplars for `lf.query`.'
790
795
  ]
796
+ protocol: Annotated[
797
+ str,
798
+ 'Protocol of `lf.query`.'
799
+ ] = 'python'
800
+ kwargs: Annotated[
801
+ dict[str, Any],
802
+ 'Kwargs of `lf.query`.'
803
+ ] = {}
791
804
  usage_summary: Annotated[
792
805
  lf.UsageSummary,
793
806
  'Usage summary for `lf.query`.'
@@ -803,13 +816,17 @@ class QueryInvocation(pg.Object, pg.views.HtmlTreeView.Extension):
803
816
 
804
817
  @functools.cached_property
805
818
  def lm_request(self) -> lf.Message:
806
- return query_prompt(self.input, self.schema, examples=self.examples or None)
819
+ return query_prompt(
820
+ self.input, self.schema, examples=self.examples or None,
821
+ protocol=self.protocol,
822
+ **self.kwargs
823
+ )
807
824
 
808
825
  @functools.cached_property
809
826
  def output(self) -> Any:
810
827
  """The output of `lf.query`. If it failed, returns the `MappingError`."""
811
828
  try:
812
- return query_output(self.lm_response, self.schema)
829
+ return query_output(self.lm_response, self.schema, protocol=self.protocol)
813
830
  except mapping.MappingError as e:
814
831
  return e
815
832
 
@@ -1321,6 +1321,26 @@ class QueryInvocationTest(unittest.TestCase):
1321
1321
  self.assertTrue(queries[0].has_error)
1322
1322
  self.assertIsInstance(queries[0].output, mapping.MappingError)
1323
1323
 
1324
+ def test_kwargs(self):
1325
+ lm = fake.StaticSequence([
1326
+ 'Activity(description="hi")',
1327
+ ])
1328
+ with querying.track_queries() as queries:
1329
+ querying.query(
1330
+ 'foo {{x}}',
1331
+ Activity,
1332
+ lm=lm,
1333
+ system_message='system message',
1334
+ x=1,
1335
+ )
1336
+ self.assertTrue(queries[0].protocol.startswith('python:'))
1337
+ self.assertEqual(
1338
+ list(queries[0].kwargs.keys()),
1339
+ ['x', 'metadata_system_message']
1340
+ )
1341
+ self.assertIn('foo 1', queries[0].lm_request.text)
1342
+ self.assertEqual(queries[0].lm_request.system_message, 'system message')
1343
+
1324
1344
  def test_to_html(self):
1325
1345
  lm = fake.StaticSequence([
1326
1346
  'Activity(description="hi")',
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: langfun
3
- Version: 0.1.2.dev202505080804
3
+ Version: 0.1.2.dev202505100803
4
4
  Summary: Langfun: Language as Functions.
5
5
  Home-page: https://github.com/google/langfun
6
6
  Author: Langfun Authors
@@ -107,7 +107,7 @@ langfun/core/llms/openai_test.py,sha256=gwuO6aoa296iM2welWV9ua4KF8gEVGsEPakgbtkW
107
107
  langfun/core/llms/rest.py,sha256=MCybcHApJcf49lubLnDzScN9Oc2IWY_JnMHIGdbDOuU,4474
108
108
  langfun/core/llms/rest_test.py,sha256=_zM7nV8DEVyoXNiQOnuwJ917mWjki0614H88rNmDboE,5020
109
109
  langfun/core/llms/vertexai.py,sha256=nPWwv1C-wVpWpE4Vyl4vJoAEMB519-XNzPjyLY4N3tM,18816
110
- langfun/core/llms/vertexai_test.py,sha256=0M4jsPOXGagdzPfEdJixmyLdhmmERePZWSFfTwnaYCQ,4875
110
+ langfun/core/llms/vertexai_test.py,sha256=_e-acnNBAf9C3WO6i1b2J_mhRzdDdYQTorD9hIVZKOg,5034
111
111
  langfun/core/llms/cache/__init__.py,sha256=QAo3InUMDM_YpteNnVCSejI4zOsnjSMWKJKzkb3VY64,993
112
112
  langfun/core/llms/cache/base.py,sha256=rt3zwmyw0y9jsSGW-ZbV1vAfLxQ7_3AVk0l2EySlse4,3918
113
113
  langfun/core/llms/cache/in_memory.py,sha256=i58oiQL28RDsq37dwqgVpC2mBETJjIEFS20yHiV5MKU,5185
@@ -137,8 +137,8 @@ langfun/core/structured/mapping.py,sha256=gxdcYQP9yqbDRtiJQ1RRAOrKHiCr0h6xBYLCRK
137
137
  langfun/core/structured/mapping_test.py,sha256=OntYvfDitAf0tAnzQty3YS90vyEn6FY1Mi93r_ViEk8,9594
138
138
  langfun/core/structured/parsing.py,sha256=MGvI7ypXlwfzr5XB8_TFU9Ei0_5reYqkWkv64eAy0EA,12015
139
139
  langfun/core/structured/parsing_test.py,sha256=V8Cj1tJK4Lxv_b0YQj6-2hzXZgnYNBa2JR7rOLRBKoQ,22346
140
- langfun/core/structured/querying.py,sha256=lHSPQ4UKRfhLXRjofG1geIGpQ_ImZf0dIvjXIwr_jNk,30480
141
- langfun/core/structured/querying_test.py,sha256=_npZ3ztaZc6VerP7nU_QTJscWGgBqiwTE02z_S3Ahd4,40197
140
+ langfun/core/structured/querying.py,sha256=v-C8cU4LNFPwVSVgfO5PFyYxKCTHHh6SdbaR5V5MKKs,30938
141
+ langfun/core/structured/querying_test.py,sha256=6jN3oOSpVEBIlISrCUJtZfY6sBHahF_EBRUEhNNlzW8,40797
142
142
  langfun/core/structured/schema.py,sha256=UTonddBx3hVr0Zhm_38jqd8khTSXWy-bkk8l1YOUdLA,28797
143
143
  langfun/core/structured/schema_generation.py,sha256=3AcuKvv3VOtKY5zMVqODrxfOuDxzoZtGeBxHlOWDOWw,5308
144
144
  langfun/core/structured/schema_generation_test.py,sha256=RM9s71kMNg2jTePwInkiW9fK1ACN37eyPeF8OII-0zw,2950
@@ -156,8 +156,8 @@ langfun/core/templates/demonstration.py,sha256=vCrgYubdZM5Umqcgp8NUVGXgr4P_c-fik
156
156
  langfun/core/templates/demonstration_test.py,sha256=SafcDQ0WgI7pw05EmPI2S4v1t3ABKzup8jReCljHeK4,2162
157
157
  langfun/core/templates/selfplay.py,sha256=yhgrJbiYwq47TgzThmHrDQTF4nDrTI09CWGhuQPNv-s,2273
158
158
  langfun/core/templates/selfplay_test.py,sha256=Ot__1P1M8oJfoTp-M9-PQ6HUXqZKyMwvZ5f7yQ3yfyM,2326
159
- langfun-0.1.2.dev202505080804.dist-info/licenses/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
160
- langfun-0.1.2.dev202505080804.dist-info/METADATA,sha256=CAqwJ8wOEhPMNBg-5-9XUOB8qWL3wuZJkycpxgcKugg,8178
161
- langfun-0.1.2.dev202505080804.dist-info/WHEEL,sha256=0CuiUZ_p9E4cD6NyLD6UG80LBXYyiSYZOKDm5lp32xk,91
162
- langfun-0.1.2.dev202505080804.dist-info/top_level.txt,sha256=RhlEkHxs1qtzmmtWSwYoLVJAc1YrbPtxQ52uh8Z9VvY,8
163
- langfun-0.1.2.dev202505080804.dist-info/RECORD,,
159
+ langfun-0.1.2.dev202505100803.dist-info/licenses/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
160
+ langfun-0.1.2.dev202505100803.dist-info/METADATA,sha256=M52b_XjFPeZNK9ZFRbXrVjBPlT9LyI3NThMyeLjKaQ4,8178
161
+ langfun-0.1.2.dev202505100803.dist-info/WHEEL,sha256=DnLRTWE75wApRYVsjgc6wsVswC54sMSJhAEd4xhDpBk,91
162
+ langfun-0.1.2.dev202505100803.dist-info/top_level.txt,sha256=RhlEkHxs1qtzmmtWSwYoLVJAc1YrbPtxQ52uh8Z9VvY,8
163
+ langfun-0.1.2.dev202505100803.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (80.3.1)
2
+ Generator: setuptools (80.4.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5