langfun 0.0.2.dev20240506__py3-none-any.whl → 0.0.2.dev20240507__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.
@@ -251,7 +251,7 @@ class Mapping(lf.LangFunc):
251
251
 
252
252
  {%- if example.schema -%}
253
253
  {{ schema_title }}:
254
- {{ example.schema_repr(protocol) | indent(2, True) }}
254
+ {{ example.schema_repr(protocol, include_methods=include_methods) | indent(2, True) }}
255
255
 
256
256
  {% endif -%}
257
257
 
@@ -279,6 +279,10 @@ class Mapping(lf.LangFunc):
279
279
  'The protocol for representing the schema and value.',
280
280
  ] = 'python'
281
281
 
282
+ include_methods: Annotated[
283
+ bool, 'If True, include method definitions in the schema.'
284
+ ] = False
285
+
282
286
  #
283
287
  # Other user-provided flags.
284
288
  #
@@ -113,6 +113,7 @@ def query(
113
113
  autofix: int = 0,
114
114
  autofix_lm: lf.LanguageModel | None = None,
115
115
  protocol: schema_lib.SchemaProtocol = 'python',
116
+ include_methods: bool = False,
116
117
  returns_message: bool = False,
117
118
  skip_lm: bool = False,
118
119
  **kwargs,
@@ -160,11 +161,11 @@ def query(
160
161
  examples: An optional list of fewshot examples for helping parsing. If None,
161
162
  the default one-shot example will be added.
162
163
  cache_seed: Seed for computing cache key. The cache key is determined by a
163
- tuple of (lm, prompt, cache seed). If None, cache will be disabled for
164
- the query even cache is configured by the LM.
164
+ tuple of (lm, prompt, cache seed). If None, cache will be disabled for the
165
+ query even cache is configured by the LM.
165
166
  response_postprocess: An optional callable object to process the raw LM
166
- response before parsing it into the final output object. If None, the
167
- raw LM response will not be processed.
167
+ response before parsing it into the final output object. If None, the raw
168
+ LM response will not be processed.
168
169
  autofix: Number of attempts to auto fix the generated code. If 0, autofix is
169
170
  disabled. Auto-fix is not supported for 'json' protocol.
170
171
  autofix_lm: The language model to use for autofix. If not specified, the
@@ -172,6 +173,8 @@ def query(
172
173
  will use `lm`.
173
174
  protocol: The protocol for schema/value representation. Applicable values
174
175
  are 'json' and 'python'. By default `python` will be used.
176
+ include_methods: If True, include method definitions in the output type
177
+ during prompting.
175
178
  returns_message: If True, returns `lf.Message` as the output, instead of
176
179
  returning the structured `message.result`.
177
180
  skip_lm: If True, returns the rendered prompt as a UserMessage object.
@@ -226,6 +229,7 @@ def query(
226
229
  schema=schema,
227
230
  default=default,
228
231
  examples=examples,
232
+ include_methods=include_methods,
229
233
  response_postprocess=response_postprocess,
230
234
  autofix=autofix if protocol == 'python' else 0,
231
235
  **kwargs,
@@ -301,6 +301,7 @@ class SchemaPythonRepr(SchemaRepr):
301
301
  schema: Schema,
302
302
  *,
303
303
  include_result_definition: bool = True,
304
+ include_methods: bool = False,
304
305
  markdown: bool = True,
305
306
  **kwargs,
306
307
  ) -> str:
@@ -308,7 +309,7 @@ class SchemaPythonRepr(SchemaRepr):
308
309
  if include_result_definition:
309
310
  ret += self.result_definition(schema)
310
311
  class_definition_str = self.class_definitions(
311
- schema, markdown=markdown, **kwargs
312
+ schema, markdown=markdown, include_methods=include_methods, **kwargs
312
313
  )
313
314
  if class_definition_str:
314
315
  ret += f'\n\n{class_definition_str}'
@@ -331,6 +332,7 @@ def class_definitions(
331
332
  classes: Sequence[Type[Any]],
332
333
  *,
333
334
  include_pg_object_as_base: bool = False,
335
+ include_methods: bool = False,
334
336
  strict: bool = False,
335
337
  markdown: bool = False,
336
338
  ) -> str | None:
@@ -346,6 +348,7 @@ def class_definitions(
346
348
  cls,
347
349
  strict=strict,
348
350
  include_pg_object_as_base=include_pg_object_as_base,
351
+ include_methods=include_methods,
349
352
  )
350
353
  )
351
354
  ret = def_str.getvalue()
@@ -355,7 +358,10 @@ def class_definitions(
355
358
 
356
359
 
357
360
  def class_definition(
358
- cls, strict: bool = False, include_pg_object_as_base: bool = False
361
+ cls,
362
+ strict: bool = False,
363
+ include_pg_object_as_base: bool = False,
364
+ include_methods: bool = False,
359
365
  ) -> str:
360
366
  """Returns the Python class definition."""
361
367
  out = io.StringIO()
@@ -383,6 +389,7 @@ def class_definition(
383
389
  out.write('\n')
384
390
  out.write(' """\n')
385
391
 
392
+ empty_class = True
386
393
  if schema.fields:
387
394
  for key, field in schema.items():
388
395
  if not isinstance(key, pg.typing.ConstStrKey):
@@ -401,11 +408,33 @@ def class_definition(
401
408
  out.write('\n')
402
409
  out.write(f' {field.key}: {annotation(field.value, strict=strict)}')
403
410
  out.write('\n')
404
- else:
411
+ empty_class = False
412
+
413
+ if include_methods:
414
+ for method in _iter_newly_defined_methods(cls):
415
+ out.write('\n')
416
+ out.write(
417
+ textwrap.indent(
418
+ inspect.cleandoc('\n' + inspect.getsource(method)), ' ' * 2)
419
+ )
420
+ out.write('\n')
421
+ empty_class = False
422
+
423
+ if empty_class:
405
424
  out.write(' pass\n')
406
425
  return out.getvalue()
407
426
 
408
427
 
428
+ def _iter_newly_defined_methods(cls):
429
+ names = set(dir(cls))
430
+ for base in cls.__bases__:
431
+ names -= set(dir(base))
432
+ for name in names:
433
+ attr = getattr(cls, name)
434
+ if callable(attr):
435
+ yield attr
436
+
437
+
409
438
  def annotation(
410
439
  vs: pg.typing.ValueSpec,
411
440
  annotate_optional: bool = True,
@@ -493,7 +522,8 @@ def annotation(
493
522
  class SchemaJsonRepr(SchemaRepr):
494
523
  """JSON-representation for a schema."""
495
524
 
496
- def repr(self, schema: Schema) -> str:
525
+ def repr(self, schema: Schema, **kwargs) -> str:
526
+ del kwargs
497
527
  out = io.StringIO()
498
528
  def _visit(node: Any) -> None:
499
529
  if isinstance(node, str):
@@ -461,6 +461,23 @@ class SchemaPythonReprTest(unittest.TestCase):
461
461
 
462
462
  self.assertEqual(schema_lib.class_definition(C), 'class C:\n x: str\n')
463
463
 
464
+ class D(pg.Object):
465
+ x: str
466
+ def __call__(self, y: int) -> int:
467
+ return len(self.x) + y
468
+
469
+ self.assertEqual(
470
+ schema_lib.class_definition(D, include_methods=True),
471
+ inspect.cleandoc(
472
+ """
473
+ class D:
474
+ x: str
475
+
476
+ def __call__(self, y: int) -> int:
477
+ return len(self.x) + y
478
+ """) + '\n'
479
+ )
480
+
464
481
  def test_repr(self):
465
482
  class Foo(pg.Object):
466
483
  x: int
@@ -477,13 +494,21 @@ class SchemaPythonReprTest(unittest.TestCase):
477
494
  class A(pg.Object):
478
495
  foo: Foo
479
496
 
497
+ def foo_value(self) -> int:
498
+ return self.foo.x
499
+
480
500
  class B(A):
481
501
  bar: Bar
482
502
  foo2: Foo
483
503
 
504
+ def bar_value(self) -> str:
505
+ return self.bar.y
506
+
484
507
  schema = schema_lib.Schema([B])
485
508
  self.assertEqual(
486
- schema_lib.SchemaPythonRepr().class_definitions(schema),
509
+ schema_lib.SchemaPythonRepr().class_definitions(
510
+ schema, include_methods=True
511
+ ),
487
512
  inspect.cleandoc('''
488
513
  class Foo:
489
514
  x: int
@@ -491,6 +516,9 @@ class SchemaPythonReprTest(unittest.TestCase):
491
516
  class A:
492
517
  foo: Foo
493
518
 
519
+ def foo_value(self) -> int:
520
+ return self.foo.x
521
+
494
522
  class Bar:
495
523
  """Class Bar."""
496
524
  y: str
@@ -503,6 +531,9 @@ class SchemaPythonReprTest(unittest.TestCase):
503
531
  foo: Foo
504
532
  bar: Bar
505
533
  foo2: Foo
534
+
535
+ def bar_value(self) -> str:
536
+ return self.bar.y
506
537
  ''') + '\n',
507
538
  )
508
539
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: langfun
3
- Version: 0.0.2.dev20240506
3
+ Version: 0.0.2.dev20240507
4
4
  Summary: Langfun: Language as Functions.
5
5
  Home-page: https://github.com/google/langfun
6
6
  Author: Langfun Authors
@@ -82,16 +82,16 @@ langfun/core/structured/description.py,sha256=SXW4MJvshFjbR-0gw6rE21o6WXq12UlRXa
82
82
  langfun/core/structured/description_test.py,sha256=UtZGjSFUaQ6130t1E5tcL7ODu0xIefkapb53TbnqsK8,7362
83
83
  langfun/core/structured/function_generation.py,sha256=pFgS3vcRAWiuFBol2x5Eeip3XqoudONsOpeJpWyjT3s,7479
84
84
  langfun/core/structured/function_generation_test.py,sha256=ZJI-aaGgWWszn92u7h5IZ9Pl70N2DgAGGJrIxPzsvwg,10065
85
- langfun/core/structured/mapping.py,sha256=Vq3bQZWi4iYjcVn8D2kvPXTAm9jrQ-_1ueHLbXtGRNQ,12112
85
+ langfun/core/structured/mapping.py,sha256=V2EI53KwhXxqcoH2ouhuei8aYWny0ml_FwMTiS7Zr9M,12253
86
86
  langfun/core/structured/mapping_test.py,sha256=PiXklMeIa8L6KtMi3ju7J9Y39gZy0hIGz-Oeq4A_7XE,3835
87
87
  langfun/core/structured/parsing.py,sha256=keoVqEfzAbdULh6GawWFsTQzU91MzJXYFZjXGXLaD8g,11492
88
88
  langfun/core/structured/parsing_test.py,sha256=34wDrXaQ-EYhJLfDL8mX9K53oQMSzh5pVYdKjnESmK8,20895
89
- langfun/core/structured/prompting.py,sha256=WICdX_figP2u98Hvg3BFSTF83nNKxM4x1STxkWe2_9Y,7925
89
+ langfun/core/structured/prompting.py,sha256=mzQwZBQi2RU3-W8RNdlr_tr_9G6UA2Z9QuowyzcC9ww,8099
90
90
  langfun/core/structured/prompting_test.py,sha256=vslaCAUikfwOvqsKzqs_oyEacrefFsr2SWSqu6OHi3w,20813
91
- langfun/core/structured/schema.py,sha256=mJXirgqx3N7SA9zBO_ISHrzcV-ZRshLhnMJyCcSjGjY,25057
91
+ langfun/core/structured/schema.py,sha256=Zy9y6Vq9DrFwcuP5o5VL_PvMCmzavF-nuDqyviBnaxk,25818
92
92
  langfun/core/structured/schema_generation.py,sha256=U3nRQsqmMZg_qIVDh2fiY3K4JLfsAL1LcKzIFP1iXFg,5316
93
93
  langfun/core/structured/schema_generation_test.py,sha256=RM9s71kMNg2jTePwInkiW9fK1ACN37eyPeF8OII-0zw,2950
94
- langfun/core/structured/schema_test.py,sha256=yw_Uo3xJC3JA9dBDjZdkQdBGPf04e7t1oT9SZTAiSdg,22360
94
+ langfun/core/structured/schema_test.py,sha256=NgQK1zGSliZVx_Af6gDBTqQxXRHvmAvGARv4dUs8IbI,23078
95
95
  langfun/core/structured/scoring.py,sha256=5DsMNrWKf98ZYCEkxA4-HvA62nMSNBs9DC5m8dYL7Cs,2442
96
96
  langfun/core/structured/scoring_test.py,sha256=39_dw6p_FkoqeUccO67yIqos-MccAWezoozS21i8mi0,1732
97
97
  langfun/core/templates/__init__.py,sha256=bO0eMsVJbi7sxEB2YlInKRQ2EVP-RyyKUwcD-8msuN4,927
@@ -103,8 +103,8 @@ langfun/core/templates/demonstration.py,sha256=vCrgYubdZM5Umqcgp8NUVGXgr4P_c-fik
103
103
  langfun/core/templates/demonstration_test.py,sha256=SafcDQ0WgI7pw05EmPI2S4v1t3ABKzup8jReCljHeK4,2162
104
104
  langfun/core/templates/selfplay.py,sha256=yhgrJbiYwq47TgzThmHrDQTF4nDrTI09CWGhuQPNv-s,2273
105
105
  langfun/core/templates/selfplay_test.py,sha256=DYVrkk7uNKCqJGEHH31HssU2BPuMItU1vJLzfcXIlYg,2156
106
- langfun-0.0.2.dev20240506.dist-info/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
107
- langfun-0.0.2.dev20240506.dist-info/METADATA,sha256=lzwASgqmdYxjw1o3ivVL4au5jvKsvY6dQXdfBxLqcec,3405
108
- langfun-0.0.2.dev20240506.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
109
- langfun-0.0.2.dev20240506.dist-info/top_level.txt,sha256=RhlEkHxs1qtzmmtWSwYoLVJAc1YrbPtxQ52uh8Z9VvY,8
110
- langfun-0.0.2.dev20240506.dist-info/RECORD,,
106
+ langfun-0.0.2.dev20240507.dist-info/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
107
+ langfun-0.0.2.dev20240507.dist-info/METADATA,sha256=kz0C5CzRBOml2nNCsUkvDY36x_49qo3HvwS-ifZY_sU,3405
108
+ langfun-0.0.2.dev20240507.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
109
+ langfun-0.0.2.dev20240507.dist-info/top_level.txt,sha256=RhlEkHxs1qtzmmtWSwYoLVJAc1YrbPtxQ52uh8Z9VvY,8
110
+ langfun-0.0.2.dev20240507.dist-info/RECORD,,