langfun 0.0.2.dev20240609__py3-none-any.whl → 0.0.2.dev20240611__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.

@@ -30,7 +30,7 @@ class CompleteStructure(mapping.Mapping):
30
30
 
31
31
  mapping_template = lf.Template("""
32
32
  {{ input_title }}:
33
- {{ example.input_repr() | indent(2, True) }}
33
+ {{ example.input_repr(use_modality_ref=True) | indent(2, True) }}
34
34
 
35
35
  {%- if missing_type_dependencies(example.input) %}
36
36
 
@@ -45,13 +45,16 @@ class CompleteStructure(mapping.Mapping):
45
45
 
46
46
  {{ output_title }}:
47
47
  {%- if example.has_output %}
48
- {{ example.output_repr() | indent(2, True) }}
48
+ {{ example.output_repr(use_modality_ref=True) | indent(2, True) }}
49
49
  {% endif -%}
50
50
  """)
51
51
 
52
52
  input_title = 'INPUT_OBJECT'
53
53
  output_title = 'OUTPUT_OBJECT'
54
54
  schema_title = 'CLASS_DEFINITIONS'
55
+ modality_refs_title: Annotated[
56
+ str, 'The section title for modality refs.'
57
+ ] = 'MODALITY_REFERENCES'
55
58
 
56
59
  preamble = lf.LangFunc(
57
60
  """
@@ -148,6 +151,28 @@ class CompleteStructure(mapping.Mapping):
148
151
  pg.traverse(self.input, _visit)
149
152
  return context
150
153
 
154
+ #
155
+ # Helper methods for handling modalities.
156
+ #
157
+
158
+ def has_modality_refs(self, value: Any) -> bool:
159
+ """Returns true if the value has modalities."""
160
+ return not isinstance(value, lf.Modality) and pg.contains(
161
+ value, type=lf.Modality
162
+ )
163
+
164
+ def modalities(self, value: Any) -> dict[str, lf.Modality]:
165
+ return lf.Modality.from_value(value)
166
+
167
+ def modality_refs_repr(self, value: Any) -> str:
168
+ with lf.modality.format_modality_as_ref(True):
169
+ return pg.format(
170
+ self.modalities(value),
171
+ compact=False,
172
+ verbose=False,
173
+ python_format=True,
174
+ )
175
+
151
176
 
152
177
  def complete(
153
178
  input_value: pg.Symbolic,
@@ -107,7 +107,11 @@ class MappingExample(lf.NaturalLanguageFormattable, lf.Component):
107
107
 
108
108
  @classmethod
109
109
  def value_repr(
110
- cls, value: Any, protocol: schema_lib.SchemaProtocol = 'python', **kwargs
110
+ cls,
111
+ value: Any,
112
+ protocol: schema_lib.SchemaProtocol = 'python',
113
+ use_modality_ref: bool = False,
114
+ **kwargs
111
115
  ) -> str:
112
116
  if isinstance(value, str):
113
117
  return value
@@ -116,7 +120,7 @@ class MappingExample(lf.NaturalLanguageFormattable, lf.Component):
116
120
  return str(value)
117
121
 
118
122
  # Placehold modalities if they are present.
119
- if pg.contains(value, type=lf.Modality):
123
+ if use_modality_ref and pg.contains(value, type=lf.Modality):
120
124
  value = lf.ModalityRef.placehold(value)
121
125
  return schema_lib.value_repr(protocol).repr(value, **kwargs)
122
126
 
@@ -243,13 +247,7 @@ class Mapping(lf.LangFunc):
243
247
  {{ input_title }}:
244
248
  {{ example.input_repr(protocol, compact=False) | indent(2, True) }}
245
249
 
246
- {% if has_modality_refs(example.input) -%}
247
- {{ modality_refs_title }}:
248
- {{ modality_refs_repr(example.input) | indent(2, True) }}
249
-
250
- {% endif -%}
251
-
252
- {%- if example.schema -%}
250
+ {% if example.schema -%}
253
251
  {{ schema_title }}:
254
252
  {{ example.schema_repr(protocol) | indent(2, True) }}
255
253
 
@@ -270,10 +268,6 @@ class Mapping(lf.LangFunc):
270
268
 
271
269
  schema_title: Annotated[str, 'The section title for schema.'] = 'SCHEMA'
272
270
 
273
- modality_refs_title: Annotated[
274
- str, 'The section title for modality refs.'
275
- ] = 'MODALITY_REFERENCES'
276
-
277
271
  protocol: Annotated[
278
272
  schema_lib.SchemaProtocol,
279
273
  'The protocol for representing the schema and value.',
@@ -378,24 +372,3 @@ class Mapping(lf.LangFunc):
378
372
  """Gets additional symbol definitions besides schema as globals."""
379
373
  return {'ModalityRef': lf.modality.ModalityRef}
380
374
 
381
- #
382
- # Helper methods for handling modalities.
383
- #
384
-
385
- def has_modality_refs(self, value: Any) -> bool:
386
- """Returns true if the value has modalities."""
387
- return not isinstance(value, lf.Modality) and pg.contains(
388
- value, type=lf.Modality
389
- )
390
-
391
- def modalities(self, value: Any) -> dict[str, lf.Modality]:
392
- return lf.Modality.from_value(value)
393
-
394
- def modality_refs_repr(self, value: Any) -> str:
395
- with lf.modality.format_modality_as_ref(True):
396
- return pg.format(
397
- self.modalities(value),
398
- compact=False,
399
- verbose=False,
400
- python_format=True,
401
- )
@@ -41,13 +41,17 @@ class QueryTest(unittest.TestCase):
41
41
  self,
42
42
  prompt,
43
43
  schema,
44
+ examples: list[mapping.MappingExample] | None = None,
44
45
  *,
45
46
  expected_snippet: str,
46
47
  exact_match: bool = False,
47
48
  expected_modalities: int = 0,
48
49
  **kwargs,
49
50
  ):
50
- m = prompting.query(prompt, schema=schema, **kwargs, returns_message=True)
51
+ m = prompting.query(
52
+ prompt, schema=schema, examples=examples,
53
+ **kwargs, returns_message=True
54
+ )
51
55
  self.assertIsNotNone(m.lm_input)
52
56
  if exact_match:
53
57
  self.assertEqual(expected_snippet, m.lm_input.text)
@@ -265,24 +269,60 @@ class QueryTest(unittest.TestCase):
265
269
  INPUT_OBJECT:
266
270
  ```python
267
271
  [
268
- ModalityRef(
269
- name='input[0]'
270
- ),
271
- ModalityRef(
272
- name='input[1]'
273
- )
272
+ <<[[input[0]]]>>,
273
+ <<[[input[1]]]>>
274
274
  ]
275
275
  ```
276
-
277
- MODALITY_REFERENCES:
278
- {
279
- 'input[0]': <<[[input[0]]]>>,
280
- 'input[1]': <<[[input[1]]]>>
281
- }
282
276
  """),
283
277
  expected_modalities=2,
284
278
  )
285
279
 
280
+ def test_structure_with_modality_and_examples_to_structure_render(self):
281
+ lm = fake.StaticResponse('["cat", "mouse"]')
282
+ self.assert_render(
283
+ [
284
+ modalities.Image.from_bytes(b'cat_image'),
285
+ modalities.Image.from_bytes(b'mouse_image'),
286
+ ],
287
+ list[str],
288
+ examples=[
289
+ mapping.MappingExample(
290
+ input=[modalities.Image.from_bytes(b'dog_image')],
291
+ schema=list[str],
292
+ output=['dog'],
293
+ ),
294
+ ],
295
+ lm=lm,
296
+ expected_snippet=inspect.cleandoc("""
297
+ INPUT_OBJECT:
298
+ ```python
299
+ [
300
+ <<[[examples[0].input[0]]]>>
301
+ ]
302
+ ```
303
+
304
+ OUTPUT_TYPE:
305
+ list[str]
306
+
307
+ OUTPUT_OBJECT:
308
+ ```python
309
+ [
310
+ 'dog'
311
+ ]
312
+ ```
313
+
314
+
315
+ INPUT_OBJECT:
316
+ ```python
317
+ [
318
+ <<[[input[0]]]>>,
319
+ <<[[input[1]]]>>
320
+ ]
321
+ ```
322
+ """),
323
+ expected_modalities=3,
324
+ )
325
+
286
326
  def test_bad_protocol(self):
287
327
  with self.assertRaisesRegex(ValueError, 'Unknown protocol'):
288
328
  prompting.query('what is 1 + 1', int, protocol='text')
@@ -656,6 +656,8 @@ class ValuePythonRepr(ValueRepr):
656
656
  return object_code
657
657
  else:
658
658
  object_code = SchemaPythonRepr().result_definition(cls_schema)
659
+ elif isinstance(value, lf.Template):
660
+ return str(value)
659
661
  else:
660
662
  object_code = pg.format(
661
663
  value, compact=compact, verbose=verbose, python_format=True
@@ -18,6 +18,7 @@ import inspect
18
18
  import typing
19
19
  import unittest
20
20
 
21
+ import langfun.core as lf
21
22
  from langfun.core.llms import fake
22
23
  from langfun.core.structured import schema as schema_lib
23
24
  import pyglove as pg
@@ -694,6 +695,10 @@ class ValuePythonReprTest(unittest.TestCase):
694
695
  schema_lib.ValuePythonRepr().repr(1, schema_lib.Schema(int)),
695
696
  '```python\n1\n```'
696
697
  )
698
+ self.assertEqual(
699
+ schema_lib.ValuePythonRepr().repr(lf.Template('hi, {{a}}', a='foo')),
700
+ 'hi, foo'
701
+ )
697
702
  self.assertEqual(
698
703
  schema_lib.ValuePythonRepr().repr(
699
704
  A([Foo(1), Foo(2)], 'bar'), schema_lib.Schema(A), markdown=False,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: langfun
3
- Version: 0.0.2.dev20240609
3
+ Version: 0.0.2.dev20240611
4
4
  Summary: Langfun: Language as Functions.
5
5
  Home-page: https://github.com/google/langfun
6
6
  Author: Langfun Authors
@@ -90,22 +90,22 @@ langfun/core/modalities/pdf_test.py,sha256=KE40zJD3Whe6ty2OULkp1J8jwLmB4ZjGXlGek
90
90
  langfun/core/modalities/video.py,sha256=sKcXxbx9S1ERjH8yEzkbtySpcRJD40QiPIQiIBy-U5I,955
91
91
  langfun/core/modalities/video_test.py,sha256=GbsoefSeO7y8kCYhTtp4s9E3ah_eYrb6Z-MXpS01RFc,2046
92
92
  langfun/core/structured/__init__.py,sha256=yp60yeDSVlyT0ElmLwbpBHnQtk_JX5udnjG1UGcsXKA,3776
93
- langfun/core/structured/completion.py,sha256=RzWdHyaqKj-tj6mGwpHXk0s8YbM0UEHSpyT2axmj-o8,7343
93
+ langfun/core/structured/completion.py,sha256=cS2PjG7sqzDu5x0xoTk8RmNcoeX55iVwH38NTefkMHg,8108
94
94
  langfun/core/structured/completion_test.py,sha256=2mUzDMKGF_WGfTtsnfmfMDx97dkJ-98y8leen__qWLA,19281
95
95
  langfun/core/structured/description.py,sha256=SXW4MJvshFjbR-0gw6rE21o6WXq12UlRXawvDBXMZFA,5211
96
96
  langfun/core/structured/description_test.py,sha256=UtZGjSFUaQ6130t1E5tcL7ODu0xIefkapb53TbnqsK8,7362
97
97
  langfun/core/structured/function_generation.py,sha256=pFgS3vcRAWiuFBol2x5Eeip3XqoudONsOpeJpWyjT3s,7479
98
98
  langfun/core/structured/function_generation_test.py,sha256=ZJI-aaGgWWszn92u7h5IZ9Pl70N2DgAGGJrIxPzsvwg,10065
99
- langfun/core/structured/mapping.py,sha256=Vq3bQZWi4iYjcVn8D2kvPXTAm9jrQ-_1ueHLbXtGRNQ,12112
99
+ langfun/core/structured/mapping.py,sha256=QKbSnvOgut-sx2mZPjHJcdlDLxR8b3ZC16ZLWociwog,11298
100
100
  langfun/core/structured/mapping_test.py,sha256=PiXklMeIa8L6KtMi3ju7J9Y39gZy0hIGz-Oeq4A_7XE,3835
101
101
  langfun/core/structured/parsing.py,sha256=keoVqEfzAbdULh6GawWFsTQzU91MzJXYFZjXGXLaD8g,11492
102
102
  langfun/core/structured/parsing_test.py,sha256=34wDrXaQ-EYhJLfDL8mX9K53oQMSzh5pVYdKjnESmK8,20895
103
103
  langfun/core/structured/prompting.py,sha256=vjmJn7GMFkqkrEB6qRrYvjB78WVx_OhsIyMN8PTb-7o,8641
104
- langfun/core/structured/prompting_test.py,sha256=8orqsTVDpDb_7oFZtGHtV_zRU_j_9RaOGH1QR8IurIU,21756
105
- langfun/core/structured/schema.py,sha256=7s6g9HSGogdWXnsX3_Pd8w8Cf1tKeFGtbNwxIG6l7lA,27664
104
+ langfun/core/structured/prompting_test.py,sha256=W1FmbOfMXhBV8uZ3WN1fBxKLtdYngvR9l1JYo46HqJQ,22723
105
+ langfun/core/structured/schema.py,sha256=oiT4P4Q9pG-QOnFzxETN2EQZqNln8nG4zAJHxcmeX9U,27729
106
106
  langfun/core/structured/schema_generation.py,sha256=U3nRQsqmMZg_qIVDh2fiY3K4JLfsAL1LcKzIFP1iXFg,5316
107
107
  langfun/core/structured/schema_generation_test.py,sha256=RM9s71kMNg2jTePwInkiW9fK1ACN37eyPeF8OII-0zw,2950
108
- langfun/core/structured/schema_test.py,sha256=P1Bm-khjbAebW6IDjop5dv1ddRD4pqL83FdK1VmfZPw,25108
108
+ langfun/core/structured/schema_test.py,sha256=RjYhwTgktQgyqAjzLvo967nTiIK9KWgP-aNGg4e7ihE,25258
109
109
  langfun/core/structured/scoring.py,sha256=QyT1S8FkLtKICfUbh4AXoKK3YJ_rgejyk6TI2OtOa68,2751
110
110
  langfun/core/structured/scoring_test.py,sha256=39_dw6p_FkoqeUccO67yIqos-MccAWezoozS21i8mi0,1732
111
111
  langfun/core/templates/__init__.py,sha256=bO0eMsVJbi7sxEB2YlInKRQ2EVP-RyyKUwcD-8msuN4,927
@@ -117,8 +117,8 @@ langfun/core/templates/demonstration.py,sha256=vCrgYubdZM5Umqcgp8NUVGXgr4P_c-fik
117
117
  langfun/core/templates/demonstration_test.py,sha256=SafcDQ0WgI7pw05EmPI2S4v1t3ABKzup8jReCljHeK4,2162
118
118
  langfun/core/templates/selfplay.py,sha256=yhgrJbiYwq47TgzThmHrDQTF4nDrTI09CWGhuQPNv-s,2273
119
119
  langfun/core/templates/selfplay_test.py,sha256=rBW2Qr8yi-aWYwoTwRR-n1peKyMX9QXPZXURjLgoiRs,2264
120
- langfun-0.0.2.dev20240609.dist-info/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
121
- langfun-0.0.2.dev20240609.dist-info/METADATA,sha256=uyk0OoHbseBguRcn-EDVaDAun1goszVD5Dt1WO1BbZc,3550
122
- langfun-0.0.2.dev20240609.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
123
- langfun-0.0.2.dev20240609.dist-info/top_level.txt,sha256=RhlEkHxs1qtzmmtWSwYoLVJAc1YrbPtxQ52uh8Z9VvY,8
124
- langfun-0.0.2.dev20240609.dist-info/RECORD,,
120
+ langfun-0.0.2.dev20240611.dist-info/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
121
+ langfun-0.0.2.dev20240611.dist-info/METADATA,sha256=0L_yeRtnKYAVFwCrUnRakg8nE0W8iQsYlsOf0VKbg8Q,3550
122
+ langfun-0.0.2.dev20240611.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
123
+ langfun-0.0.2.dev20240611.dist-info/top_level.txt,sha256=RhlEkHxs1qtzmmtWSwYoLVJAc1YrbPtxQ52uh8Z9VvY,8
124
+ langfun-0.0.2.dev20240611.dist-info/RECORD,,