langfun 0.1.2.dev202501070804__py3-none-any.whl → 0.1.2.dev202501080804__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.
@@ -40,6 +40,7 @@ def run_with_correction(
40
40
  sandbox: bool | None = None,
41
41
  timeout: int | None = 5,
42
42
  returns_code: bool = False,
43
+ returns_stdout: bool = False,
43
44
  outputs_intermediate: bool = False,
44
45
  ) -> Any | tuple[Any, str]:
45
46
  """Correct code with a language model via self-play.
@@ -62,6 +63,7 @@ def run_with_correction(
62
63
  timeout. Applicable only when sandbox is set to True.
63
64
  returns_code: If True, the return value is a tuple of (result, final code).
64
65
  Otherwise the return value is the result only.
66
+ returns_stdout: If True, the stdout (a str) will be returned.
65
67
  outputs_intermediate: If True, intermediate output will be outputted as a
66
68
  dict, with the last line's value accessible by key '__result__'. Otherwise
67
69
  the value of the last line will be returned.
@@ -87,6 +89,7 @@ def run_with_correction(
87
89
  global_vars=global_vars,
88
90
  sandbox=sandbox,
89
91
  timeout=timeout,
92
+ returns_stdout=returns_stdout,
90
93
  outputs_intermediate=outputs_intermediate,
91
94
  )
92
95
  )
@@ -57,6 +57,7 @@ def evaluate(
57
57
  *,
58
58
  global_vars: dict[str, Any] | None = None,
59
59
  permission: permissions.CodePermission | None = None,
60
+ returns_stdout: bool = False,
60
61
  outputs_intermediate: bool = False,
61
62
  ) -> Any | dict[str, Any]:
62
63
  """Executes Python code.
@@ -71,14 +72,17 @@ def evaluate(
71
72
  global_vars: An optional dict as the globals that could be referenced by the
72
73
  code.
73
74
  permission: Permission for the Python code to run.
74
- outputs_intermediate: If True, intermediate output will be outputted as a
75
- dict, with the last line's value accessible by key '__result__'. Otherwise
76
- the value of the last line will be returned.
75
+ returns_stdout: If True, the stdout (a str) will be returned.
76
+ outputs_intermediate: Applicable when returns_stdout is False. If True,
77
+ intermediate output will be outputted as a dict, with the last line's
78
+ value accessible by key '__result__' and the std output accessible by
79
+ key '__stdout__'. Otherwise the value of the last line will be returned.
77
80
 
78
81
  Returns:
79
- The value of the last line of the code. Or a dict of variable name to
80
- their values if `outputs_intermediate` is set to True, with the final result
81
- accessible by key '__result__'.
82
+ The value of the last line of the code block. Or a dict of variable
83
+ names of all locals to their evaluated values as the output of the code to
84
+ run. The value for the last line can be accessed by key '__result__'. Or the
85
+ stdout as a str.
82
86
  """
83
87
  # Set up the permission and context.
84
88
  permission = permission or permissions.get_permission()
@@ -136,6 +140,8 @@ def evaluate(
136
140
  raise errors.CodeError(code, e) from e
137
141
  global_vars[RESULT_KEY] = list(global_vars.values())[-1]
138
142
 
143
+ if returns_stdout:
144
+ return stdout.getvalue()
139
145
  if outputs_intermediate:
140
146
  outputs = {}
141
147
  for k, v in global_vars.items():
@@ -258,6 +264,7 @@ def run(
258
264
  *,
259
265
  global_vars: dict[str, Any] | None = None,
260
266
  permission: permissions.CodePermission | None = None,
267
+ returns_stdout: bool = False,
261
268
  outputs_intermediate: bool = False,
262
269
  sandbox: bool | None = None,
263
270
  timeout: float | None = None,
@@ -273,9 +280,11 @@ def run(
273
280
  code: Python code to run.
274
281
  global_vars: An optional dict of
275
282
  permission: Permission for the Python code to run.
276
- outputs_intermediate: If True, all variables created as locals will be
277
- returned, with the final result accessible by key '__result__'. Otherwise
278
- only the final result will be returned.
283
+ returns_stdout: If True, the stdout (a str) will be returned.
284
+ outputs_intermediate: Applicable when returns_stdout is False. If True,
285
+ intermediate output will be outputted as a dict, with the last line's
286
+ value accessible by key '__result__' and the std output accessible by
287
+ key '__stdout__'. Otherwise the value of the last line will be returned.
279
288
  sandbox: If True, run code in sandbox; If False, run code in current
280
289
  process. If None, run in sandbox first, if the output could not be
281
290
  serialized and pass to current process, run the code again in current
@@ -285,7 +294,8 @@ def run(
285
294
  Returns:
286
295
  The value of the last line of the code block. Or a dict of variable
287
296
  names of all locals to their evaluated values as the output of the code to
288
- run. The value for the last line can be accessed by key '__result__'.
297
+ run. The value for the last line can be accessed by key '__result__'. Or the
298
+ stdout as a str.
289
299
 
290
300
  Raises:
291
301
  TimeoutError: If the execution time exceeds the timeout.
@@ -293,5 +303,6 @@ def run(
293
303
  """
294
304
  return call(
295
305
  evaluate, code=code, global_vars=global_vars, permission=permission,
296
- outputs_intermediate=outputs_intermediate,
297
- sandbox=sandbox, timeout=timeout)
306
+ returns_stdout=returns_stdout, outputs_intermediate=outputs_intermediate,
307
+ sandbox=sandbox, timeout=timeout
308
+ )
@@ -63,6 +63,15 @@ class EvaluateTest(unittest.TestCase):
63
63
  ),
64
64
  3,
65
65
  )
66
+ with self.assertRaisesRegex(errors.CodeError, 'ValueError'):
67
+ execution.evaluate(
68
+ """
69
+ def foo():
70
+ raise ValueError("intentional error")
71
+ foo()
72
+ """,
73
+ permission=permissions.CodePermission.ALL
74
+ )
66
75
 
67
76
  def test_class_def(self):
68
77
  ret = execution.evaluate(
@@ -102,16 +111,20 @@ class EvaluateTest(unittest.TestCase):
102
111
  self.assertIs(ret['__result__'], ret['bar'])
103
112
 
104
113
  def test_function_def_and_call(self):
105
- ret = execution.evaluate(
114
+ code = (
106
115
  """
107
116
  def foo(x, y):
108
117
  return x + y
109
118
 
110
119
  def bar(z):
120
+ print(f'z is {z}')
111
121
  return z + foo(z, z)
112
122
 
113
123
  bar(1)
114
- """,
124
+ """
125
+ )
126
+ ret = execution.evaluate(
127
+ code,
115
128
  permission=permissions.CodePermission.ALL,
116
129
  outputs_intermediate=True,
117
130
  )
@@ -119,6 +132,12 @@ class EvaluateTest(unittest.TestCase):
119
132
  list(ret.keys()), ['foo', 'bar', '__result__', '__stdout__']
120
133
  )
121
134
  self.assertEqual(ret['__result__'], 3)
135
+ ret = execution.evaluate(
136
+ code,
137
+ permission=permissions.CodePermission.ALL,
138
+ returns_stdout=True,
139
+ )
140
+ self.assertEqual(ret, 'z is 1\n')
122
141
 
123
142
  def test_complex(self):
124
143
  ret = execution.evaluate(
@@ -88,6 +88,8 @@ class PythonCode(pg.Object):
88
88
  sandbox: bool | None = None,
89
89
  timeout: int | None = 5,
90
90
  global_vars: dict[str, Any] | None = None,
91
+ returns_stdout: bool = False,
92
+ outputs_intermediate: bool = False,
91
93
  autofix: int = 3,
92
94
  autofix_lm: lf.LanguageModel | None = None,
93
95
  ) -> Any:
@@ -101,13 +103,22 @@ class PythonCode(pg.Object):
101
103
  timeout: Timeout in seconds. If None, there is no timeout. Applicable when
102
104
  sandbox is set to True.
103
105
  global_vars: Global variables that could be accessed from the source code.
106
+ returns_stdout: If True, the stdout (a str) will be returned.
107
+ outputs_intermediate: Applicable when returns_stdout is False. If True,
108
+ intermediate output will be outputted as a dict, with the last line's
109
+ value accessible by key '__result__' and the std output accessible by
110
+ key '__stdout__'. Otherwise the value of the last line will be returned.
104
111
  autofix: Number of attempts to auto fix the generated code. If 0, autofix
105
112
  is disabled.
106
113
  autofix_lm: Language model to be used. If not specified, it will try to
107
114
  use the `lm` under `lf.context`.
108
115
 
109
116
  Returns:
110
- The value of the last expression in the source code.
117
+ The value of the last expression in the source code. Or a dict of local
118
+ variable names defined in the source code to their values if
119
+ `outputs_intermediate` is set to True. The value for the last line can be
120
+ accessed by key '__result__'. Or the stdout as a str if `returns_stdout`
121
+ is set to True.
111
122
 
112
123
  Raises:
113
124
  TimeoutError: If `sandbox` is True and timeout has reached.
@@ -121,6 +132,8 @@ class PythonCode(pg.Object):
121
132
  max_attempts=autofix,
122
133
  lm=autofix_lm,
123
134
  returns_code=True,
135
+ returns_stdout=returns_stdout,
136
+ outputs_intermediate=outputs_intermediate,
124
137
  )
125
138
  self.rebind(source=updated_code)
126
139
  return result
@@ -158,18 +171,14 @@ class PythonCode(pg.Object):
158
171
  TimeoutError: If `sandbox` is True and timeout has reached.
159
172
  Exception: Any errors that the source code has raised.
160
173
  """
161
- result, updated_code = correction.run_with_correction(
162
- self.source,
163
- global_vars=global_vars,
174
+ return self(
164
175
  sandbox=sandbox,
165
176
  timeout=timeout,
177
+ global_vars=global_vars,
178
+ autofix=autofix,
179
+ autofix_lm=autofix_lm,
166
180
  outputs_intermediate=True,
167
- max_attempts=autofix,
168
- lm=autofix_lm,
169
- returns_code=True,
170
181
  )
171
- self.rebind(source=updated_code)
172
- return result
173
182
 
174
183
 
175
184
  class PythonFunction(pg.Object):
@@ -90,6 +90,8 @@ class VertexAI(gemini.Gemini):
90
90
  )
91
91
 
92
92
  self._project = project
93
+ self._location = location
94
+
93
95
  credentials = self.credentials
94
96
  if credentials is None:
95
97
  # Use default credentials.
@@ -114,9 +116,10 @@ class VertexAI(gemini.Gemini):
114
116
 
115
117
  @property
116
118
  def api_endpoint(self) -> str:
119
+ assert self._api_initialized
117
120
  return (
118
- f'https://{self.location}-aiplatform.googleapis.com/v1/projects/'
119
- f'{self.project}/locations/{self.location}/publishers/google/'
121
+ f'https://{self._location}-aiplatform.googleapis.com/v1/projects/'
122
+ f'{self._project}/locations/{self._location}/publishers/google/'
120
123
  f'models/{self.model}:generateContent'
121
124
  )
122
125
 
@@ -41,7 +41,7 @@ class VertexAITest(unittest.TestCase):
41
41
  os.environ['VERTEXAI_LOCATION'] = 'us-central1'
42
42
  model = vertexai.VertexAIGeminiPro1()
43
43
  self.assertTrue(model.model_id.startswith('VertexAI('))
44
- self.assertIsNotNone(model.api_endpoint)
44
+ self.assertIn('us-central1', model.api_endpoint)
45
45
  self.assertTrue(model._api_initialized)
46
46
  self.assertIsNotNone(model._session)
47
47
  del os.environ['VERTEXAI_PROJECT']
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: langfun
3
- Version: 0.1.2.dev202501070804
3
+ Version: 0.1.2.dev202501080804
4
4
  Summary: Langfun: Language as Functions.
5
5
  Home-page: https://github.com/google/langfun
6
6
  Author: Langfun Authors
@@ -34,13 +34,13 @@ langfun/core/agentic/action_eval_test.py,sha256=tRUkWmOE9p0rpNOq19xAY2oDEnYsEEyk
34
34
  langfun/core/agentic/action_test.py,sha256=Gu7P5XQvzqbKawn2jjyTpWaARzzhzO04KkC1TuBnUnw,4612
35
35
  langfun/core/coding/__init__.py,sha256=5utju_fwEsImaiftx4oXKl9FAM8p281k8-Esdh_-m1w,835
36
36
  langfun/core/coding/python/__init__.py,sha256=MJ-vubliz-ebrZH3OBRKBwMi0S9-FrhGCp8YQLR6_I4,1776
37
- langfun/core/coding/python/correction.py,sha256=cg5intTKlx7Lr4VGU-qc90_qF2nuat1mpIAcGj-JmNw,7018
37
+ langfun/core/coding/python/correction.py,sha256=xbnHFENQv0ibPzpAW8LFQ3TcbqzD0Y3I6nC1Dy2h_GI,7161
38
38
  langfun/core/coding/python/correction_test.py,sha256=qGxXuHaO32onF6cAoTfO1_sH_lM7-3dE9UqaaU8Myxs,4215
39
39
  langfun/core/coding/python/errors.py,sha256=fX3Du63uGm25YFXW9D-bV2gntTdTAX3hBFtAnRlmg14,3166
40
40
  langfun/core/coding/python/errors_test.py,sha256=_ZbWJCFIb-FkCK7K1zCuH8W3x_NFt-jNe3dfP8yqaD4,2323
41
- langfun/core/coding/python/execution.py,sha256=raZix62g2fwt6Lgykll2DFzkLlEjVqN9E73q0iaVdak,10185
42
- langfun/core/coding/python/execution_test.py,sha256=lExY6GMLeuCsCKXgM2KbAPJ6kqSlfHmz3RG0-dqfVcI,7197
43
- langfun/core/coding/python/generation.py,sha256=xivSeOKGN00HnG1TLuFhPfP-JyRuRrSELxVJW2ngqIQ,7750
41
+ langfun/core/coding/python/execution.py,sha256=vf8AzDcL_9xh-kpTKWywt9gV7Pc8KLghOHj6hrpiqS8,10762
42
+ langfun/core/coding/python/execution_test.py,sha256=xBsk5OZuuEY1_w7dxu25akL-NVHm02xjzyOQ2rPWhgY,7693
43
+ langfun/core/coding/python/generation.py,sha256=MqzoFdB3v7BtrAzqIMMyH8RvqaNJ6TSqZ9Xw-iPmPTI,8427
44
44
  langfun/core/coding/python/generation_test.py,sha256=54bgKr1DgzYFLoqR8bTn7Yjol0gPCuR6XvRltR4l6YM,2777
45
45
  langfun/core/coding/python/parsing.py,sha256=LMg8REP4VDY0YQjtPAGNAW4rKlMNdSXF8m19wMT9yrY,7128
46
46
  langfun/core/coding/python/parsing_test.py,sha256=9vAWF484kWIm6JZq8NFiMgKUDhXV-deRl1QMmNERfAA,7386
@@ -96,8 +96,8 @@ langfun/core/llms/openai.py,sha256=g5X_ySW-b2f0uRE8sb3_W1sAB-pWpKLNNoflBoRBwrc,2
96
96
  langfun/core/llms/openai_test.py,sha256=kOWa1nf-nJvtYY10REUw5wojh3ZgfU8tRaCZ8wUgJbA,16623
97
97
  langfun/core/llms/rest.py,sha256=sWbYUV8S3SuOg9giq7xwD-xDRfaF7NP_ig7bI52-Rj4,3442
98
98
  langfun/core/llms/rest_test.py,sha256=NZ3Nf0XQVpT9kLP5cBVo_yBHLI7vWTYhWQxYEJVMGs4,3472
99
- langfun/core/llms/vertexai.py,sha256=Nzcj-c98wWcU-JU8i_KrwOP_TZ9Hct9Y1l1Qa2OdkgY,5437
100
- langfun/core/llms/vertexai_test.py,sha256=KqQkJpjZvpqp-JmDK2yTYK_XyLIyizzb4EeTHfUV8sk,1779
99
+ langfun/core/llms/vertexai.py,sha256=MuwLPTJ6-9x2uRDCSM1_biPK6M76FFlL1ezf5OmobDA,5504
100
+ langfun/core/llms/vertexai_test.py,sha256=iXjmQs7TNiwcueoaRGpdp4KnASkDJaTP__Z9QroN8zQ,1787
101
101
  langfun/core/llms/cache/__init__.py,sha256=QAo3InUMDM_YpteNnVCSejI4zOsnjSMWKJKzkb3VY64,993
102
102
  langfun/core/llms/cache/base.py,sha256=rt3zwmyw0y9jsSGW-ZbV1vAfLxQ7_3AVk0l2EySlse4,3918
103
103
  langfun/core/llms/cache/in_memory.py,sha256=i58oiQL28RDsq37dwqgVpC2mBETJjIEFS20yHiV5MKU,5185
@@ -148,8 +148,8 @@ langfun/core/templates/demonstration.py,sha256=vCrgYubdZM5Umqcgp8NUVGXgr4P_c-fik
148
148
  langfun/core/templates/demonstration_test.py,sha256=SafcDQ0WgI7pw05EmPI2S4v1t3ABKzup8jReCljHeK4,2162
149
149
  langfun/core/templates/selfplay.py,sha256=yhgrJbiYwq47TgzThmHrDQTF4nDrTI09CWGhuQPNv-s,2273
150
150
  langfun/core/templates/selfplay_test.py,sha256=Ot__1P1M8oJfoTp-M9-PQ6HUXqZKyMwvZ5f7yQ3yfyM,2326
151
- langfun-0.1.2.dev202501070804.dist-info/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
152
- langfun-0.1.2.dev202501070804.dist-info/METADATA,sha256=UqLnA9hw4yPKiRkoG1TIiYiNkDQvDq7lGBROIDJhKaY,7941
153
- langfun-0.1.2.dev202501070804.dist-info/WHEEL,sha256=A3WOREP4zgxI0fKrHUG8DC8013e3dK3n7a6HDbcEIwE,91
154
- langfun-0.1.2.dev202501070804.dist-info/top_level.txt,sha256=RhlEkHxs1qtzmmtWSwYoLVJAc1YrbPtxQ52uh8Z9VvY,8
155
- langfun-0.1.2.dev202501070804.dist-info/RECORD,,
151
+ langfun-0.1.2.dev202501080804.dist-info/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
152
+ langfun-0.1.2.dev202501080804.dist-info/METADATA,sha256=nLlWl4_b1QuxJGdx2F9WQg7MUkf9t4qsqLxGupPJjqU,7941
153
+ langfun-0.1.2.dev202501080804.dist-info/WHEEL,sha256=A3WOREP4zgxI0fKrHUG8DC8013e3dK3n7a6HDbcEIwE,91
154
+ langfun-0.1.2.dev202501080804.dist-info/top_level.txt,sha256=RhlEkHxs1qtzmmtWSwYoLVJAc1YrbPtxQ52uh8Z9VvY,8
155
+ langfun-0.1.2.dev202501080804.dist-info/RECORD,,