langfun 0.0.2.dev20240403__tar.gz → 0.0.2.dev20240404__tar.gz

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 (107) hide show
  1. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/PKG-INFO +1 -1
  2. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/coding/python/correction.py +0 -7
  3. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/modalities/video.py +5 -2
  4. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/structured/mapping.py +18 -1
  5. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/structured/prompting.py +10 -1
  6. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/structured/prompting_test.py +17 -0
  7. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun.egg-info/PKG-INFO +1 -1
  8. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/LICENSE +0 -0
  9. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/README.md +0 -0
  10. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/__init__.py +0 -0
  11. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/__init__.py +0 -0
  12. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/coding/__init__.py +0 -0
  13. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/coding/python/__init__.py +0 -0
  14. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/coding/python/correction_test.py +0 -0
  15. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/coding/python/errors.py +0 -0
  16. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/coding/python/errors_test.py +0 -0
  17. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/coding/python/execution.py +0 -0
  18. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/coding/python/execution_test.py +0 -0
  19. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/coding/python/generation.py +0 -0
  20. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/coding/python/generation_test.py +0 -0
  21. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/coding/python/parsing.py +0 -0
  22. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/coding/python/parsing_test.py +0 -0
  23. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/coding/python/permissions.py +0 -0
  24. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/coding/python/permissions_test.py +0 -0
  25. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/component.py +0 -0
  26. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/component_test.py +0 -0
  27. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/concurrent.py +0 -0
  28. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/concurrent_test.py +0 -0
  29. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/console.py +0 -0
  30. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/console_test.py +0 -0
  31. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/eval/__init__.py +0 -0
  32. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/eval/base.py +0 -0
  33. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/eval/base_test.py +0 -0
  34. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/eval/matching.py +0 -0
  35. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/eval/matching_test.py +0 -0
  36. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/eval/scoring.py +0 -0
  37. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/eval/scoring_test.py +0 -0
  38. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/langfunc.py +0 -0
  39. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/langfunc_test.py +0 -0
  40. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/language_model.py +0 -0
  41. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/language_model_test.py +0 -0
  42. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/llms/__init__.py +0 -0
  43. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/llms/cache/__init__.py +0 -0
  44. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/llms/cache/base.py +0 -0
  45. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/llms/cache/in_memory.py +0 -0
  46. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/llms/cache/in_memory_test.py +0 -0
  47. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/llms/fake.py +0 -0
  48. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/llms/fake_test.py +0 -0
  49. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/llms/google_genai.py +0 -0
  50. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/llms/google_genai_test.py +0 -0
  51. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/llms/llama_cpp.py +0 -0
  52. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/llms/llama_cpp_test.py +0 -0
  53. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/llms/openai.py +0 -0
  54. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/llms/openai_test.py +0 -0
  55. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/memories/__init__.py +0 -0
  56. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/memories/conversation_history.py +0 -0
  57. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/memories/conversation_history_test.py +0 -0
  58. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/memory.py +0 -0
  59. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/message.py +0 -0
  60. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/message_test.py +0 -0
  61. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/modalities/__init__.py +0 -0
  62. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/modalities/image.py +0 -0
  63. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/modalities/image_test.py +0 -0
  64. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/modalities/mime.py +0 -0
  65. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/modalities/mime_test.py +0 -0
  66. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/modalities/video_test.py +0 -0
  67. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/modality.py +0 -0
  68. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/modality_test.py +0 -0
  69. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/natural_language.py +0 -0
  70. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/natural_language_test.py +0 -0
  71. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/sampling.py +0 -0
  72. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/sampling_test.py +0 -0
  73. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/structured/__init__.py +0 -0
  74. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/structured/completion.py +0 -0
  75. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/structured/completion_test.py +0 -0
  76. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/structured/description.py +0 -0
  77. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/structured/description_test.py +0 -0
  78. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/structured/mapping_test.py +0 -0
  79. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/structured/parsing.py +0 -0
  80. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/structured/parsing_test.py +0 -0
  81. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/structured/schema.py +0 -0
  82. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/structured/schema_generation.py +0 -0
  83. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/structured/schema_generation_test.py +0 -0
  84. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/structured/schema_test.py +0 -0
  85. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/structured/scoring.py +0 -0
  86. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/structured/scoring_test.py +0 -0
  87. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/subscription.py +0 -0
  88. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/subscription_test.py +0 -0
  89. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/template.py +0 -0
  90. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/template_test.py +0 -0
  91. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/templates/__init__.py +0 -0
  92. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/templates/completion.py +0 -0
  93. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/templates/completion_test.py +0 -0
  94. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/templates/conversation.py +0 -0
  95. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/templates/conversation_test.py +0 -0
  96. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/templates/demonstration.py +0 -0
  97. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/templates/demonstration_test.py +0 -0
  98. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/templates/selfplay.py +0 -0
  99. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/templates/selfplay_test.py +0 -0
  100. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/text_formatting.py +0 -0
  101. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun/core/text_formatting_test.py +0 -0
  102. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun.egg-info/SOURCES.txt +0 -0
  103. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun.egg-info/dependency_links.txt +0 -0
  104. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun.egg-info/requires.txt +0 -0
  105. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/langfun.egg-info/top_level.txt +0 -0
  106. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/setup.cfg +0 -0
  107. {langfun-0.0.2.dev20240403 → langfun-0.0.2.dev20240404}/setup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: langfun
3
- Version: 0.0.2.dev20240403
3
+ Version: 0.0.2.dev20240404
4
4
  Summary: Langfun: Language as Functions.
5
5
  Home-page: https://github.com/google/langfun
6
6
  Author: Langfun Authors
@@ -12,7 +12,6 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
  """Python code error correction."""
15
- import re
16
15
  from typing import Any
17
16
  import langfun.core as lf
18
17
  from langfun.core.coding.python import errors
@@ -31,11 +30,6 @@ class CorrectedCode(pg.Object):
31
30
  corrected_code: str
32
31
 
33
32
 
34
- def remove_docstrings(code):
35
- pattern = re.compile(r"(def .+?:\s*?)('''|\"\"\")((.|\s)*?)(\2)", re.DOTALL)
36
- return pattern.sub(r"\1", code)
37
-
38
-
39
33
  def run_with_correction(
40
34
  code: str,
41
35
  error: str | None = None,
@@ -86,7 +80,6 @@ def run_with_correction(
86
80
  # pytype: enable=import-error
87
81
  # pylint: enable=g-import-not-at-top
88
82
 
89
- code = remove_docstrings(code)
90
83
  if max_attempts == 0:
91
84
  result = execution.run(
92
85
  code,
@@ -15,9 +15,7 @@
15
15
 
16
16
  import base64
17
17
  from typing import cast
18
-
19
18
  from langfun.core.modalities import mime
20
- import magic
21
19
 
22
20
 
23
21
  class Video(mime.MimeType):
@@ -29,6 +27,11 @@ class Video(mime.MimeType):
29
27
 
30
28
  @property
31
29
  def mime_type(self) -> str:
30
+ # TODO(daiyip): after cl/619658455, LaunchPad binaries cannot import `magic`
31
+ # correctly. This is to mitigate the issue for major Langfun users who do
32
+ # not use Video. We shall move this import out once the issue is fixed.
33
+ import magic # pylint: disable=g-import-not-at-top
34
+
32
35
  video_mime_type = magic.from_buffer(self.to_bytes(), mime=True)
33
36
  if 'video/' not in video_mime_type:
34
37
  raise ValueError(f'Not a video: {video_mime_type!r}.')
@@ -14,7 +14,7 @@
14
14
  """The base of symbolic mapping methods."""
15
15
 
16
16
  import io
17
- from typing import Annotated, Any
17
+ from typing import Annotated, Any, Callable
18
18
  import langfun.core as lf
19
19
  from langfun.core.structured import schema as schema_lib
20
20
  import pyglove as pg
@@ -278,6 +278,14 @@ class Mapping(lf.LangFunc):
278
278
  ),
279
279
  ] = lf.RAISE_IF_HAS_ERROR
280
280
 
281
+ response_postprocess: Annotated[
282
+ Callable[[str], str] | None,
283
+ (
284
+ 'A callable object that post process the raw LLM response before '
285
+ 'parsing it into the output Python object.'
286
+ )
287
+ ] = None
288
+
281
289
  #
282
290
  # Key methods for implementing specific mappings.
283
291
  #
@@ -296,6 +304,7 @@ class Mapping(lf.LangFunc):
296
304
  def transform_output(self, lm_output: lf.Message) -> lf.Message:
297
305
  """Transforms LM response into structure if schema is present."""
298
306
  try:
307
+ lm_output = self.postprocess_response(lm_output)
299
308
  lm_output.result = self.postprocess_result(self.parse_result(lm_output))
300
309
  except Exception as e: # pylint: disable=broad-exception-caught
301
310
  if self.default == lf.RAISE_IF_HAS_ERROR:
@@ -316,6 +325,14 @@ class Mapping(lf.LangFunc):
316
325
  autofix_lm=self.autofix_lm or self.lm,
317
326
  )
318
327
 
328
+ def postprocess_response(self, response: lf.Message) -> lf.Message:
329
+ """Post process LLM response."""
330
+ if self.response_postprocess is not None:
331
+ postprocessed_text = self.response_postprocess(response.text)
332
+ if postprocessed_text != response.text:
333
+ return lf.AIMessage(postprocessed_text, source=response)
334
+ return response
335
+
319
336
  def postprocess_result(self, result: Any) -> Any:
320
337
  """Post process structured output."""
321
338
  return result
@@ -13,7 +13,7 @@
13
13
  # limitations under the License.
14
14
  """Symbolic query."""
15
15
 
16
- from typing import Any, Type, Union
16
+ from typing import Any, Callable, Type, Union
17
17
 
18
18
  import langfun.core as lf
19
19
  from langfun.core.structured import mapping
@@ -107,6 +107,7 @@ def query(
107
107
  lm: lf.LanguageModel | None = None,
108
108
  examples: list[mapping.MappingExample] | None = None,
109
109
  cache_seed: int | None = 0,
110
+ response_postprocess: Callable[[str], str] | None = None,
110
111
  autofix: int = 0,
111
112
  autofix_lm: lf.LanguageModel | None = None,
112
113
  protocol: schema_lib.SchemaProtocol = 'python',
@@ -159,6 +160,9 @@ def query(
159
160
  cache_seed: Seed for computing cache key. The cache key is determined by a
160
161
  tuple of (lm, prompt, cache seed). If None, cache will be disabled for
161
162
  the query even cache is configured by the LM.
163
+ response_postprocess: An optional callable object to process the raw LM
164
+ response before parsing it into the final output object. If None, the
165
+ raw LM response will not be processed.
162
166
  autofix: Number of attempts to auto fix the generated code. If 0, autofix is
163
167
  disabled. Auto-fix is not supported for 'json' protocol.
164
168
  autofix_lm: The language model to use for autofix. If not specified, the
@@ -188,6 +192,10 @@ def query(
188
192
  output = lf.LangFunc.from_value(prompt, **kwargs)(
189
193
  lm=lm, cache_seed=cache_seed, skip_lm=skip_lm
190
194
  )
195
+ if response_postprocess:
196
+ processed_text = response_postprocess(output.text)
197
+ if processed_text != output.text:
198
+ output = lf.AIMessage(processed_text, source=output)
191
199
  return output if returns_message else output.text
192
200
 
193
201
  # Query with structured output.
@@ -206,6 +214,7 @@ def query(
206
214
  schema=schema,
207
215
  default=default,
208
216
  examples=examples,
217
+ response_postprocess=response_postprocess,
209
218
  autofix=autofix if protocol == 'python' else 0,
210
219
  **kwargs,
211
220
  )(
@@ -436,6 +436,23 @@ class QueryStructurePythonTest(unittest.TestCase):
436
436
  ])
437
437
  self.assertEqual(prompting.query('what is 1 + 0', int, lm=lm, autofix=3), 1)
438
438
 
439
+ def test_response_postprocess(self):
440
+ with lf.context(
441
+ lm=fake.StaticResponse('<!-- some comment-->\n3'),
442
+ override_attrs=True,
443
+ ):
444
+ self.assertEqual(
445
+ prompting.query(
446
+ 'Compute 1 + 2', response_postprocess=lambda x: x.split('\n')[1]),
447
+ '3'
448
+ )
449
+ self.assertEqual(
450
+ prompting.query(
451
+ 'Compute 1 + 2', int,
452
+ response_postprocess=lambda x: x.split('\n')[1]),
453
+ 3
454
+ )
455
+
439
456
 
440
457
  class QueryStructureJsonTest(unittest.TestCase):
441
458
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: langfun
3
- Version: 0.0.2.dev20240403
3
+ Version: 0.0.2.dev20240404
4
4
  Summary: Langfun: Language as Functions.
5
5
  Home-page: https://github.com/google/langfun
6
6
  Author: Langfun Authors