langfun 0.0.2.dev20240330__py3-none-any.whl → 0.1.2.dev202501140804__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.
Files changed (145) hide show
  1. langfun/__init__.py +22 -2
  2. langfun/core/__init__.py +17 -5
  3. langfun/core/agentic/__init__.py +30 -0
  4. langfun/core/agentic/action.py +854 -0
  5. langfun/core/agentic/action_eval.py +150 -0
  6. langfun/core/agentic/action_eval_test.py +109 -0
  7. langfun/core/agentic/action_test.py +136 -0
  8. langfun/core/coding/python/__init__.py +5 -11
  9. langfun/core/coding/python/correction.py +37 -28
  10. langfun/core/coding/python/correction_test.py +29 -3
  11. langfun/core/coding/python/execution.py +40 -216
  12. langfun/core/coding/python/execution_test.py +29 -89
  13. langfun/core/coding/python/generation.py +21 -11
  14. langfun/core/coding/python/generation_test.py +2 -2
  15. langfun/core/coding/python/parsing.py +108 -193
  16. langfun/core/coding/python/parsing_test.py +2 -105
  17. langfun/core/component.py +69 -2
  18. langfun/core/component_test.py +54 -0
  19. langfun/core/concurrent.py +414 -117
  20. langfun/core/concurrent_test.py +111 -24
  21. langfun/core/console.py +18 -5
  22. langfun/core/console_test.py +17 -0
  23. langfun/core/eval/__init__.py +17 -0
  24. langfun/core/eval/base.py +767 -140
  25. langfun/core/eval/base_test.py +238 -53
  26. langfun/core/eval/matching.py +80 -76
  27. langfun/core/eval/matching_test.py +19 -9
  28. langfun/core/eval/patching.py +130 -0
  29. langfun/core/eval/patching_test.py +170 -0
  30. langfun/core/eval/scoring.py +37 -28
  31. langfun/core/eval/scoring_test.py +21 -3
  32. langfun/core/eval/v2/__init__.py +42 -0
  33. langfun/core/eval/v2/checkpointing.py +380 -0
  34. langfun/core/eval/v2/checkpointing_test.py +228 -0
  35. langfun/core/eval/v2/eval_test_helper.py +136 -0
  36. langfun/core/eval/v2/evaluation.py +725 -0
  37. langfun/core/eval/v2/evaluation_test.py +180 -0
  38. langfun/core/eval/v2/example.py +305 -0
  39. langfun/core/eval/v2/example_test.py +128 -0
  40. langfun/core/eval/v2/experiment.py +1048 -0
  41. langfun/core/eval/v2/experiment_test.py +433 -0
  42. langfun/core/eval/v2/metric_values.py +156 -0
  43. langfun/core/eval/v2/metric_values_test.py +80 -0
  44. langfun/core/eval/v2/metrics.py +357 -0
  45. langfun/core/eval/v2/metrics_test.py +203 -0
  46. langfun/core/eval/v2/progress.py +348 -0
  47. langfun/core/eval/v2/progress_test.py +82 -0
  48. langfun/core/eval/v2/progress_tracking.py +210 -0
  49. langfun/core/eval/v2/progress_tracking_test.py +66 -0
  50. langfun/core/eval/v2/reporting.py +270 -0
  51. langfun/core/eval/v2/reporting_test.py +158 -0
  52. langfun/core/eval/v2/runners.py +488 -0
  53. langfun/core/eval/v2/runners_test.py +334 -0
  54. langfun/core/langfunc.py +3 -21
  55. langfun/core/langfunc_test.py +26 -8
  56. langfun/core/language_model.py +686 -48
  57. langfun/core/language_model_test.py +681 -44
  58. langfun/core/llms/__init__.py +100 -12
  59. langfun/core/llms/anthropic.py +488 -0
  60. langfun/core/llms/anthropic_test.py +235 -0
  61. langfun/core/llms/cache/base.py +21 -2
  62. langfun/core/llms/cache/in_memory.py +13 -0
  63. langfun/core/llms/cache/in_memory_test.py +88 -28
  64. langfun/core/llms/compositional.py +101 -0
  65. langfun/core/llms/compositional_test.py +73 -0
  66. langfun/core/llms/deepseek.py +117 -0
  67. langfun/core/llms/deepseek_test.py +61 -0
  68. langfun/core/llms/fake.py +39 -26
  69. langfun/core/llms/fake_test.py +136 -11
  70. langfun/core/llms/gemini.py +507 -0
  71. langfun/core/llms/gemini_test.py +195 -0
  72. langfun/core/llms/google_genai.py +62 -218
  73. langfun/core/llms/google_genai_test.py +9 -197
  74. langfun/core/llms/groq.py +276 -0
  75. langfun/core/llms/groq_test.py +64 -0
  76. langfun/core/llms/llama_cpp.py +15 -40
  77. langfun/core/llms/llama_cpp_test.py +4 -30
  78. langfun/core/llms/openai.py +436 -226
  79. langfun/core/llms/openai_compatible.py +179 -0
  80. langfun/core/llms/openai_compatible_test.py +495 -0
  81. langfun/core/llms/openai_test.py +35 -174
  82. langfun/core/llms/rest.py +113 -0
  83. langfun/core/llms/rest_test.py +111 -0
  84. langfun/core/llms/vertexai.py +192 -0
  85. langfun/core/llms/vertexai_test.py +52 -0
  86. langfun/core/logging.py +284 -0
  87. langfun/core/logging_test.py +125 -0
  88. langfun/core/message.py +319 -9
  89. langfun/core/message_test.py +190 -13
  90. langfun/core/modalities/__init__.py +6 -2
  91. langfun/core/modalities/audio.py +30 -0
  92. langfun/core/modalities/audio_test.py +63 -0
  93. langfun/core/modalities/image.py +39 -20
  94. langfun/core/modalities/image_test.py +52 -9
  95. langfun/core/modalities/mime.py +206 -29
  96. langfun/core/modalities/mime_test.py +90 -9
  97. langfun/core/modalities/ms_office.py +117 -0
  98. langfun/core/modalities/ms_office_test.py +389 -0
  99. langfun/core/modalities/pdf.py +22 -0
  100. langfun/core/modalities/pdf_test.py +57 -0
  101. langfun/core/modalities/video.py +9 -23
  102. langfun/core/modalities/video_test.py +3 -3
  103. langfun/core/modality.py +26 -3
  104. langfun/core/modality_test.py +2 -2
  105. langfun/core/sampling.py +11 -11
  106. langfun/core/structured/__init__.py +15 -16
  107. langfun/core/structured/completion.py +32 -5
  108. langfun/core/structured/completion_test.py +9 -8
  109. langfun/core/structured/description.py +2 -2
  110. langfun/core/structured/description_test.py +3 -3
  111. langfun/core/structured/function_generation.py +278 -0
  112. langfun/core/structured/function_generation_test.py +399 -0
  113. langfun/core/structured/mapping.py +150 -46
  114. langfun/core/structured/mapping_test.py +105 -0
  115. langfun/core/structured/parsing.py +33 -21
  116. langfun/core/structured/parsing_test.py +71 -22
  117. langfun/core/structured/querying.py +746 -0
  118. langfun/core/structured/{prompting_test.py → querying_test.py} +545 -60
  119. langfun/core/structured/schema.py +208 -99
  120. langfun/core/structured/schema_generation.py +1 -1
  121. langfun/core/structured/schema_generation_test.py +2 -2
  122. langfun/core/structured/schema_test.py +133 -34
  123. langfun/core/structured/scoring.py +125 -19
  124. langfun/core/structured/scoring_test.py +30 -0
  125. langfun/core/structured/tokenization.py +64 -0
  126. langfun/core/structured/tokenization_test.py +48 -0
  127. langfun/core/template.py +240 -11
  128. langfun/core/template_test.py +146 -1
  129. langfun/core/templates/conversation.py +9 -0
  130. langfun/core/templates/conversation_test.py +4 -3
  131. langfun/core/templates/selfplay_test.py +14 -2
  132. langfun-0.1.2.dev202501140804.dist-info/METADATA +225 -0
  133. langfun-0.1.2.dev202501140804.dist-info/RECORD +153 -0
  134. {langfun-0.0.2.dev20240330.dist-info → langfun-0.1.2.dev202501140804.dist-info}/WHEEL +1 -1
  135. langfun/core/coding/python/errors.py +0 -108
  136. langfun/core/coding/python/errors_test.py +0 -99
  137. langfun/core/coding/python/permissions.py +0 -90
  138. langfun/core/coding/python/permissions_test.py +0 -86
  139. langfun/core/structured/prompting.py +0 -217
  140. langfun/core/text_formatting.py +0 -162
  141. langfun/core/text_formatting_test.py +0 -47
  142. langfun-0.0.2.dev20240330.dist-info/METADATA +0 -99
  143. langfun-0.0.2.dev20240330.dist-info/RECORD +0 -102
  144. {langfun-0.0.2.dev20240330.dist-info → langfun-0.1.2.dev202501140804.dist-info}/LICENSE +0 -0
  145. {langfun-0.0.2.dev20240330.dist-info → langfun-0.1.2.dev202501140804.dist-info}/top_level.txt +0 -0
@@ -17,11 +17,9 @@ import inspect
17
17
  import unittest
18
18
 
19
19
  import langfun.core as lf
20
- from langfun.core import coding
21
20
  from langfun.core.llms import fake
22
21
  from langfun.core.structured import mapping
23
22
  from langfun.core.structured import parsing
24
- from langfun.core.structured import schema as schema_lib
25
23
  import pyglove as pg
26
24
 
27
25
 
@@ -39,7 +37,7 @@ class Itinerary(pg.Object):
39
37
  class ParseStructurePythonTest(unittest.TestCase):
40
38
 
41
39
  def test_render_no_examples(self):
42
- l = parsing.ParseStructurePython(int)
40
+ l = parsing._ParseStructurePython(int)
43
41
  m = lf.AIMessage('12 / 6 + 2 = 4')
44
42
  self.assertEqual(
45
43
  l.render(input=m, context='Compute 12 / 6 + 2.').text,
@@ -64,7 +62,7 @@ class ParseStructurePythonTest(unittest.TestCase):
64
62
  )
65
63
 
66
64
  def test_render_no_context(self):
67
- l = parsing.ParseStructurePython(int)
65
+ l = parsing._ParseStructurePython(int)
68
66
  m = lf.AIMessage('12 / 6 + 2 = 4')
69
67
 
70
68
  self.assertEqual(
@@ -87,7 +85,7 @@ class ParseStructurePythonTest(unittest.TestCase):
87
85
  )
88
86
 
89
87
  def test_render(self):
90
- l = parsing.ParseStructurePython(
88
+ l = parsing._ParseStructurePython(
91
89
  int,
92
90
  examples=[
93
91
  mapping.MappingExample(
@@ -214,7 +212,7 @@ class ParseStructurePythonTest(unittest.TestCase):
214
212
  ),
215
213
  override_attrs=True,
216
214
  ):
217
- l = parsing.ParseStructurePython(
215
+ l = parsing._ParseStructurePython(
218
216
  [Itinerary],
219
217
  examples=[
220
218
  mapping.MappingExample(
@@ -255,7 +253,7 @@ class ParseStructurePythonTest(unittest.TestCase):
255
253
  override_attrs=True,
256
254
  ):
257
255
  with self.assertRaisesRegex(
258
- coding.CodeError,
256
+ mapping.MappingError,
259
257
  'name .* is not defined',
260
258
  ):
261
259
  parsing.parse('three', int)
@@ -280,13 +278,15 @@ class ParseStructurePythonTest(unittest.TestCase):
280
278
  ),
281
279
  1,
282
280
  )
281
+ r = parsing.parse(
282
+ 'the answer is 1', int, user_prompt='what is 0 + 1?', lm=lm,
283
+ returns_message=True
284
+ )
283
285
  self.assertEqual(
284
- parsing.parse(
285
- 'the answer is 1', int, user_prompt='what is 0 + 1?', lm=lm,
286
- returns_message=True
287
- ),
286
+ r,
288
287
  lf.AIMessage(
289
- '1', score=1.0, result=1, logprobs=None,
288
+ '1', score=1.0, result=1, logprobs=None, is_cached=False,
289
+ usage=lf.LMSamplingUsage(652, 1, 653),
290
290
  tags=['lm-response', 'lm-output', 'transformed']
291
291
  ),
292
292
  )
@@ -295,7 +295,7 @@ class ParseStructurePythonTest(unittest.TestCase):
295
295
  class ParseStructureJsonTest(unittest.TestCase):
296
296
 
297
297
  def test_render_no_examples(self):
298
- l = parsing.ParseStructureJson(int)
298
+ l = parsing._ParseStructureJson(int)
299
299
  m = lf.AIMessage('12 / 6 + 2 = 4')
300
300
  self.assertEqual(
301
301
  l.render(input=m, context='Compute 12 / 6 + 2.').text,
@@ -320,7 +320,7 @@ class ParseStructureJsonTest(unittest.TestCase):
320
320
  )
321
321
 
322
322
  def test_render_no_context(self):
323
- l = parsing.ParseStructureJson(int)
323
+ l = parsing._ParseStructureJson(int)
324
324
  m = lf.AIMessage('12 / 6 + 2 = 4')
325
325
 
326
326
  self.assertEqual(
@@ -343,7 +343,7 @@ class ParseStructureJsonTest(unittest.TestCase):
343
343
  )
344
344
 
345
345
  def test_render(self):
346
- l = parsing.ParseStructureJson(
346
+ l = parsing._ParseStructureJson(
347
347
  int,
348
348
  examples=[
349
349
  mapping.MappingExample(
@@ -504,7 +504,7 @@ class ParseStructureJsonTest(unittest.TestCase):
504
504
  override_attrs=True,
505
505
  ):
506
506
  message = lf.LangFunc(lm_input)()
507
- l = parsing.ParseStructureJson(
507
+ l = parsing._ParseStructureJson(
508
508
  [Itinerary],
509
509
  examples=[
510
510
  mapping.MappingExample(
@@ -544,7 +544,7 @@ class ParseStructureJsonTest(unittest.TestCase):
544
544
  override_attrs=True,
545
545
  ):
546
546
  with self.assertRaisesRegex(
547
- schema_lib.JsonError,
547
+ mapping.MappingError,
548
548
  'No JSON dict in the output',
549
549
  ):
550
550
  parsing.parse('three', int, protocol='json')
@@ -634,13 +634,19 @@ class CallTest(unittest.TestCase):
634
634
  )
635
635
 
636
636
  def test_call_with_returning_message(self):
637
+ r = parsing.call(
638
+ 'Compute 1 + 2', int, lm=fake.StaticSequence(['three', '3']),
639
+ returns_message=True
640
+ )
637
641
  self.assertEqual(
638
- parsing.call(
639
- 'Compute 1 + 2', int, lm=fake.StaticSequence(['three', '3']),
640
- returns_message=True
641
- ),
642
+ r,
642
643
  lf.AIMessage(
643
- '3', result=3, score=1.0, logprobs=None,
644
+ '3',
645
+ result=3,
646
+ score=1.0,
647
+ logprobs=None,
648
+ is_cached=False,
649
+ usage=lf.LMSamplingUsage(315, 1, 316),
644
650
  tags=['lm-response', 'lm-output', 'transformed']
645
651
  ),
646
652
  )
@@ -664,6 +670,49 @@ class CallTest(unittest.TestCase):
664
670
  3,
665
671
  )
666
672
 
673
+ def test_call_with_parsing_message_chaining(self):
674
+ output = parsing.call(
675
+ 'Compute 1 + 2',
676
+ int,
677
+ lm=fake.StaticSequence(['three']),
678
+ parsing_lm=fake.StaticSequence(['3']),
679
+ parsing_examples=[
680
+ mapping.MappingExample(
681
+ context='Multiple four and five',
682
+ input='twenty',
683
+ schema=int,
684
+ output=20,
685
+ )
686
+ ],
687
+ returns_message=True,
688
+ )
689
+ self.assertIn('parsing-lm-output', output.tags)
690
+ self.assertIn('parsing-lm-input', output.source.tags)
691
+ self.assertEqual(output.root.text, 'Compute 1 + 2')
692
+
693
+ def test_call_with_parsing_message_chaining_on_parsing_error(self):
694
+ try:
695
+ output = parsing.call(
696
+ 'Compute 1 + 2',
697
+ int,
698
+ lm=fake.StaticSequence(['three']),
699
+ parsing_lm=fake.StaticSequence(['abc']),
700
+ parsing_examples=[
701
+ mapping.MappingExample(
702
+ context='Multiple four and five',
703
+ input='twenty',
704
+ schema=int,
705
+ output=20,
706
+ )
707
+ ],
708
+ returns_message=True,
709
+ )
710
+ except mapping.MappingError as e:
711
+ output = e.lm_response
712
+ self.assertIn('parsing-lm-output', output.tags)
713
+ self.assertIn('parsing-lm-input', output.source.tags)
714
+ self.assertEqual(output.root.text, 'Compute 1 + 2')
715
+
667
716
  def test_call_with_autofix(self):
668
717
  lm = fake.StaticSequence(
669
718
  [