langfun 0.0.2.dev20240330__py3-none-any.whl → 0.0.2.dev20240511__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.

Files changed (59) hide show
  1. langfun/__init__.py +7 -0
  2. langfun/core/__init__.py +1 -0
  3. langfun/core/coding/python/correction.py +0 -7
  4. langfun/core/component.py +6 -0
  5. langfun/core/component_test.py +1 -0
  6. langfun/core/eval/__init__.py +15 -0
  7. langfun/core/eval/base.py +665 -95
  8. langfun/core/eval/base_test.py +224 -53
  9. langfun/core/eval/matching.py +48 -30
  10. langfun/core/eval/matching_test.py +25 -3
  11. langfun/core/eval/patching.py +130 -0
  12. langfun/core/eval/patching_test.py +170 -0
  13. langfun/core/eval/scoring.py +19 -10
  14. langfun/core/eval/scoring_test.py +21 -3
  15. langfun/core/langfunc.py +1 -22
  16. langfun/core/langfunc_test.py +10 -4
  17. langfun/core/language_model.py +130 -24
  18. langfun/core/language_model_test.py +249 -26
  19. langfun/core/llms/__init__.py +27 -2
  20. langfun/core/llms/anthropic.py +263 -0
  21. langfun/core/llms/anthropic_test.py +167 -0
  22. langfun/core/llms/cache/in_memory_test.py +37 -28
  23. langfun/core/llms/fake.py +34 -25
  24. langfun/core/llms/fake_test.py +122 -11
  25. langfun/core/llms/google_genai.py +8 -0
  26. langfun/core/llms/google_genai_test.py +8 -3
  27. langfun/core/llms/groq.py +260 -0
  28. langfun/core/llms/groq_test.py +170 -0
  29. langfun/core/llms/llama_cpp.py +3 -1
  30. langfun/core/llms/openai.py +100 -81
  31. langfun/core/llms/openai_test.py +287 -60
  32. langfun/core/llms/vertexai.py +291 -0
  33. langfun/core/llms/vertexai_test.py +233 -0
  34. langfun/core/modalities/image.py +1 -3
  35. langfun/core/modalities/mime.py +6 -0
  36. langfun/core/modalities/video.py +6 -5
  37. langfun/core/structured/__init__.py +5 -0
  38. langfun/core/structured/completion_test.py +2 -2
  39. langfun/core/structured/function_generation.py +245 -0
  40. langfun/core/structured/function_generation_test.py +329 -0
  41. langfun/core/structured/mapping.py +61 -3
  42. langfun/core/structured/mapping_test.py +17 -0
  43. langfun/core/structured/parsing_test.py +18 -13
  44. langfun/core/structured/prompting.py +61 -12
  45. langfun/core/structured/prompting_test.py +122 -12
  46. langfun/core/structured/schema.py +38 -6
  47. langfun/core/structured/schema_generation_test.py +2 -2
  48. langfun/core/structured/schema_test.py +36 -7
  49. langfun/core/structured/scoring.py +4 -1
  50. langfun/core/structured/scoring_test.py +6 -0
  51. langfun/core/template.py +147 -11
  52. langfun/core/template_test.py +75 -0
  53. langfun/core/templates/selfplay_test.py +6 -2
  54. {langfun-0.0.2.dev20240330.dist-info → langfun-0.0.2.dev20240511.dist-info}/METADATA +3 -2
  55. langfun-0.0.2.dev20240511.dist-info/RECORD +112 -0
  56. langfun-0.0.2.dev20240330.dist-info/RECORD +0 -102
  57. {langfun-0.0.2.dev20240330.dist-info → langfun-0.0.2.dev20240511.dist-info}/LICENSE +0 -0
  58. {langfun-0.0.2.dev20240330.dist-info → langfun-0.0.2.dev20240511.dist-info}/WHEEL +0 -0
  59. {langfun-0.0.2.dev20240330.dist-info → langfun-0.0.2.dev20240511.dist-info}/top_level.txt +0 -0
langfun/__init__.py CHANGED
@@ -33,7 +33,13 @@ complete = structured.complete
33
33
  score = structured.score
34
34
  generate_class = structured.generate_class
35
35
 
36
+ # Helper functions for input/output transformations based on
37
+ # `lf.query` (e.g. jax-on-beam could use these for batch processing)
38
+ query_prompt = structured.query_prompt
39
+ query_output = structured.query_output
40
+
36
41
  source_form = structured.source_form
42
+ function_gen = structured.function_gen
37
43
 
38
44
  from langfun.core import eval # pylint: disable=redefined-builtin
39
45
  from langfun.core import templates
@@ -54,6 +60,7 @@ Video = modalities.Video
54
60
  PDF = modalities.PDF
55
61
 
56
62
  # Error types.
63
+ MappingError = structured.MappingError
57
64
  SchemaError = structured.SchemaError
58
65
  JsonError = structured.JsonError
59
66
  CodeError = coding.CodeError
langfun/core/__init__.py CHANGED
@@ -99,6 +99,7 @@ from langfun.core.modality import ModalityRef
99
99
  from langfun.core.language_model import LanguageModel
100
100
  from langfun.core.language_model import LMSample
101
101
  from langfun.core.language_model import LMSamplingOptions
102
+ from langfun.core.language_model import LMSamplingUsage
102
103
  from langfun.core.language_model import LMSamplingResult
103
104
  from langfun.core.language_model import LMScoringResult
104
105
  from langfun.core.language_model import LMCache
@@ -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,
langfun/core/component.py CHANGED
@@ -210,6 +210,12 @@ def get_contextual_override(var_name: str) -> ContextualOverride | None:
210
210
  return _get_scoped_value(_global_tls, _CONTEXT_OVERRIDES, var_name)
211
211
 
212
212
 
213
+ def all_contextual_values() -> dict[str, Any]:
214
+ """Returns all contextual values provided from `lf.context` in scope."""
215
+ overrides = getattr(_global_tls, _CONTEXT_OVERRIDES, {})
216
+ return {k: v.value for k, v in overrides.items()}
217
+
218
+
213
219
  @contextlib.contextmanager
214
220
  def _contextual_scope(
215
221
  tls: threading.local, tls_key, **variables
@@ -84,6 +84,7 @@ class ComponentContextTest(unittest.TestCase):
84
84
  lf.get_contextual_override('y'),
85
85
  lf.ContextualOverride(3, cascade=False, override_attrs=False),
86
86
  )
87
+ self.assertEqual(lf.all_contextual_values(), dict(x=3, y=3, z=3))
87
88
 
88
89
  # Member attributes take precedence over `lf.context`.
89
90
  self.assertEqual(a1.x, 1)
@@ -16,6 +16,12 @@
16
16
  # pylint: disable=g-importing-member
17
17
  # pylint: disable=g-bad-import-order
18
18
 
19
+ from langfun.core.eval.base import register
20
+ from langfun.core.eval.base import registered_names
21
+ from langfun.core.eval.base import get_evaluation
22
+ from langfun.core.eval.base import get
23
+ from langfun.core.eval.base import run
24
+
19
25
  from langfun.core.eval.base import Evaluable
20
26
  from langfun.core.eval.base import Evaluation
21
27
  from langfun.core.eval.base import Suite
@@ -32,6 +38,15 @@ from langfun.core.eval.base import as_inputs
32
38
  from langfun.core.eval.matching import Matching
33
39
  from langfun.core.eval.scoring import Scoring
34
40
 
41
+ # Experiment patching.
42
+ from langfun.core.eval.patching import patch_member
43
+
44
+ from langfun.core.eval.patching import patch_lm
45
+ from langfun.core.eval.patching import patch_parsing_lm
46
+ from langfun.core.eval.patching import patch_inputs
47
+ from langfun.core.eval.patching import patch_prompt
48
+ from langfun.core.eval.patching import patch_schema_fn
35
49
 
50
+ # Placeholder for Google-internal imports.
36
51
  # pylint: enable=g-bad-import-order
37
52
  # pylint: enable=g-importing-member