langfun 0.0.2.dev20240422__py3-none-any.whl → 0.0.2.dev20240425__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.
- langfun/__init__.py +1 -0
- langfun/core/component.py +6 -0
- langfun/core/component_test.py +1 -0
- langfun/core/eval/__init__.py +2 -0
- langfun/core/eval/base.py +175 -17
- langfun/core/eval/base_test.py +34 -6
- langfun/core/eval/matching.py +18 -1
- langfun/core/eval/matching_test.py +2 -1
- langfun/core/eval/scoring.py +11 -1
- langfun/core/eval/scoring_test.py +2 -1
- langfun/core/language_model.py +14 -0
- langfun/core/language_model_test.py +32 -0
- langfun/core/llms/anthropic.py +36 -22
- langfun/core/llms/anthropic_test.py +7 -7
- langfun/core/llms/groq.py +27 -18
- langfun/core/llms/groq_test.py +5 -5
- langfun/core/llms/openai.py +55 -50
- langfun/core/llms/openai_test.py +3 -3
- langfun/core/structured/__init__.py +1 -0
- langfun/core/structured/completion_test.py +1 -2
- langfun/core/structured/mapping.py +38 -1
- langfun/core/structured/mapping_test.py +17 -0
- langfun/core/structured/parsing_test.py +2 -4
- langfun/core/structured/prompting_test.py +2 -4
- langfun/core/structured/schema_generation_test.py +2 -2
- langfun/core/template.py +26 -8
- langfun/core/template_test.py +9 -0
- {langfun-0.0.2.dev20240422.dist-info → langfun-0.0.2.dev20240425.dist-info}/METADATA +3 -2
- {langfun-0.0.2.dev20240422.dist-info → langfun-0.0.2.dev20240425.dist-info}/RECORD +32 -32
- {langfun-0.0.2.dev20240422.dist-info → langfun-0.0.2.dev20240425.dist-info}/LICENSE +0 -0
- {langfun-0.0.2.dev20240422.dist-info → langfun-0.0.2.dev20240425.dist-info}/WHEEL +0 -0
- {langfun-0.0.2.dev20240422.dist-info → langfun-0.0.2.dev20240425.dist-info}/top_level.txt +0 -0
langfun/core/template.py
CHANGED
@@ -38,6 +38,10 @@ NO_TEMPLATE_DOCSTR_SIGN = 'THIS IS NOT A TEMPLATE'
|
|
38
38
|
_TLS_RENDER_STACK = '_template_render_stack'
|
39
39
|
_TLS_RENDER_RESULT_CACHE = '_template_render_result_cache'
|
40
40
|
|
41
|
+
# The prefix for fields or contextual attributes to be treated as additional
|
42
|
+
# metadata for rendered message.
|
43
|
+
_ADDITIONAL_METADATA_PREFIX = 'metadata_'
|
44
|
+
|
41
45
|
|
42
46
|
class Template(
|
43
47
|
natural_language.NaturalLanguageFormattable,
|
@@ -303,19 +307,19 @@ class Template(
|
|
303
307
|
with modality.format_modality_as_ref():
|
304
308
|
rendered_text = self._template.render(**inputs)
|
305
309
|
|
310
|
+
# Carry additional metadata.
|
311
|
+
metadata = self.additional_metadata()
|
312
|
+
|
306
313
|
if self.clean:
|
307
314
|
rendered_text = rendered_text.strip()
|
308
315
|
|
309
|
-
|
310
|
-
|
311
|
-
text=rendered_text,
|
312
|
-
metadata={
|
313
|
-
k: pg.Ref(v)
|
314
|
-
for k, v in inputs.items()
|
315
|
-
if not inspect.ismethod(v)
|
316
|
-
},
|
316
|
+
metadata.update(
|
317
|
+
{k: pg.Ref(v) for k, v in inputs.items() if not inspect.ismethod(v)}
|
317
318
|
)
|
318
319
|
|
320
|
+
# Fill the variables for rendering the template as metadata.
|
321
|
+
message = message_cls(text=rendered_text, metadata=metadata)
|
322
|
+
|
319
323
|
# Tag input as rendered message.
|
320
324
|
message.tag(message_lib.Message.TAG_RENDERED)
|
321
325
|
|
@@ -340,6 +344,20 @@ class Template(
|
|
340
344
|
top = pg.object_utils.thread_local_pop(_TLS_RENDER_STACK)
|
341
345
|
assert top is self, (top, self)
|
342
346
|
|
347
|
+
def additional_metadata(self) -> dict[str, Any]:
|
348
|
+
"""Returns additional metadta to be carried in the rendered message."""
|
349
|
+
metadata = {}
|
350
|
+
# Carry metadata from `lf.context`.
|
351
|
+
for k, v in component.all_contextual_values().items():
|
352
|
+
if k.startswith(_ADDITIONAL_METADATA_PREFIX):
|
353
|
+
metadata[k.removeprefix(_ADDITIONAL_METADATA_PREFIX)] = v
|
354
|
+
|
355
|
+
# Carry metadata from fields.
|
356
|
+
for k, v in self.sym_init_args.items():
|
357
|
+
if k.startswith(_ADDITIONAL_METADATA_PREFIX):
|
358
|
+
metadata[k.removeprefix(_ADDITIONAL_METADATA_PREFIX)] = v
|
359
|
+
return metadata
|
360
|
+
|
343
361
|
#
|
344
362
|
# Implements `pg.typing.CustomTyping`.
|
345
363
|
#
|
langfun/core/template_test.py
CHANGED
@@ -16,6 +16,7 @@ import inspect
|
|
16
16
|
import unittest
|
17
17
|
|
18
18
|
from langfun.core import component
|
19
|
+
from langfun.core import message as message_lib
|
19
20
|
from langfun.core import modality
|
20
21
|
from langfun.core import subscription
|
21
22
|
from langfun.core.template import Template
|
@@ -427,6 +428,14 @@ class RenderTest(unittest.TestCase):
|
|
427
428
|
# Test len.
|
428
429
|
self.assert_partial(Template('Hello {{len(x)}}'), 'Hello {{len(x)}}')
|
429
430
|
|
431
|
+
def test_additional_metadata(self):
|
432
|
+
t = Template('hi', metadata_weights=1.0, y=2)
|
433
|
+
self.assertEqual(t.render(), message_lib.UserMessage('hi', weights=1.0))
|
434
|
+
|
435
|
+
t = Template('hi')
|
436
|
+
with component.context(metadata_weights=1.0, y=2):
|
437
|
+
self.assertEqual(t.render(), message_lib.UserMessage('hi', weights=1.0))
|
438
|
+
|
430
439
|
|
431
440
|
class TemplateRenderEventTest(unittest.TestCase):
|
432
441
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: langfun
|
3
|
-
Version: 0.0.2.
|
3
|
+
Version: 0.0.2.dev20240425
|
4
4
|
Summary: Langfun: Language as Functions.
|
5
5
|
Home-page: https://github.com/google/langfun
|
6
6
|
Author: Langfun Authors
|
@@ -21,10 +21,11 @@ Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
21
|
Classifier: Topic :: Software Development :: Libraries
|
22
22
|
Description-Content-Type: text/markdown
|
23
23
|
License-File: LICENSE
|
24
|
+
Requires-Dist: absl-py >=1.0.0
|
24
25
|
Requires-Dist: google-generativeai >=0.3.2
|
25
26
|
Requires-Dist: jinja2 >=3.1.2
|
26
27
|
Requires-Dist: openai ==0.27.2
|
27
|
-
Requires-Dist: pyglove >=0.4.5.
|
28
|
+
Requires-Dist: pyglove >=0.4.5.dev20240423
|
28
29
|
Requires-Dist: python-magic >=0.4.27
|
29
30
|
Requires-Dist: requests >=2.31.0
|
30
31
|
Requires-Dist: termcolor ==1.1.0
|
@@ -1,15 +1,15 @@
|
|
1
|
-
langfun/__init__.py,sha256=
|
1
|
+
langfun/__init__.py,sha256=zh-EYTCLxkUAIc2zMo3Lye46_CrMrhwO_GwBHZspUvE,1919
|
2
2
|
langfun/core/__init__.py,sha256=6QEuXOZ9BXxm6TjpaMXuLwUBTYO3pkFDqn9QVBXyyPQ,4248
|
3
|
-
langfun/core/component.py,sha256=
|
4
|
-
langfun/core/component_test.py,sha256=
|
3
|
+
langfun/core/component.py,sha256=oxesbC0BoE_TbtxwW5x-BAZWxZyyJbuPiX5S38RqCv0,9909
|
4
|
+
langfun/core/component_test.py,sha256=uR-_Sz_42Jxc5qzLIB-f5_pXmNwnC01Xlbv5NOQSeSU,8021
|
5
5
|
langfun/core/concurrent.py,sha256=TRc49pJ3HQro2kb5FtcWkHjhBm8UcgE8RJybU5cU3-0,24537
|
6
6
|
langfun/core/concurrent_test.py,sha256=mwFMZhDUdppnDr7vDSTwcbMHwrdsIoKJwRYNtl4ZWL4,15185
|
7
7
|
langfun/core/console.py,sha256=bk5rNPNm9rMGW5YT2HixxU04p2umnoabn5SDz6Dqe88,2317
|
8
8
|
langfun/core/console_test.py,sha256=5SYJdxpJGLgdSSQqqMPoA1X6jpsLD8rgcyk-EgI65oE,1077
|
9
9
|
langfun/core/langfunc.py,sha256=WXdTc3QsmGD_n80KD9dFRr5MHpGZ9E_y_Rhtk4t9-3w,11852
|
10
10
|
langfun/core/langfunc_test.py,sha256=sQaKuZpGGmG80GRifhbxkj7nfzQLJKj4Vuw5y1s1K3U,8378
|
11
|
-
langfun/core/language_model.py,sha256=
|
12
|
-
langfun/core/language_model_test.py,sha256=
|
11
|
+
langfun/core/language_model.py,sha256=mJfQ_Zqq9IyVyZUdYMQ1BPrpo4Gn8yxDJb_RghQFP_I,18911
|
12
|
+
langfun/core/language_model_test.py,sha256=oWQjnyiJugSpHJKda-qLaSvmbm1sx_v-ZXrHvw_kNk4,14172
|
13
13
|
langfun/core/memory.py,sha256=f-asN1F7Vehgdn_fK84v73GrEUOxRtaW934keutTKjk,2416
|
14
14
|
langfun/core/message.py,sha256=QhvV9t5qaryPcruyxxcXi3gm9QDInkSldwTtK6sVJ3c,15734
|
15
15
|
langfun/core/message_test.py,sha256=Z23pUM5vPnDrYkIIibe2KL73D5HKur_awI0ut_EQFQA,9501
|
@@ -21,8 +21,8 @@ langfun/core/sampling.py,sha256=vygWvgC8MFw0_AKNSmz-ywMXJYWf8cl0tI8QycvAmyI,5795
|
|
21
21
|
langfun/core/sampling_test.py,sha256=U7PANpMsl9E_pa4_Y4FzesSjcwg-u-LKHGCWSgv-8FY,3663
|
22
22
|
langfun/core/subscription.py,sha256=euawEuSZP-BHydaT-AQpfYFL0m5pWPGcW0upFhrojqc,10930
|
23
23
|
langfun/core/subscription_test.py,sha256=Y4ZdbZEwm83YNZBxHff0QR4QUa4rdaNXA3_jfIcArBo,8717
|
24
|
-
langfun/core/template.py,sha256=
|
25
|
-
langfun/core/template_test.py,sha256=
|
24
|
+
langfun/core/template.py,sha256=dr3tZCbXH2qWzigO_EFVHe0GDnnCu58Tru5Mvlzin4o,18447
|
25
|
+
langfun/core/template_test.py,sha256=xty7PgdNhGpw7ZRZ6QGwhKZWG6dyRgI16Lg3p7IMLJg,13944
|
26
26
|
langfun/core/text_formatting.py,sha256=ytjj7opnRJ6w-pkglL2CZUyfYDXLpNf65E42LBb31gc,5158
|
27
27
|
langfun/core/text_formatting_test.py,sha256=nyKC6tn2L4hPJiqQHgxcbQsJJi4A4Nbj8FiO8iT6B80,1514
|
28
28
|
langfun/core/coding/__init__.py,sha256=5utju_fwEsImaiftx4oXKl9FAM8p281k8-Esdh_-m1w,835
|
@@ -39,26 +39,26 @@ langfun/core/coding/python/parsing.py,sha256=uyvI1c5OLZhMVK2Oltkl3oJxSLlG0wadlpQ
|
|
39
39
|
langfun/core/coding/python/parsing_test.py,sha256=9vAWF484kWIm6JZq8NFiMgKUDhXV-deRl1QMmNERfAA,7386
|
40
40
|
langfun/core/coding/python/permissions.py,sha256=1QWGHvzL8MM0Ok_auQ9tURqZHtdOfJaDpBzZ29GUE-c,2544
|
41
41
|
langfun/core/coding/python/permissions_test.py,sha256=w5EDb8QxpxgJyZkojyzVWQvDfg366zn99-g__6TbPQ0,2699
|
42
|
-
langfun/core/eval/__init__.py,sha256=
|
43
|
-
langfun/core/eval/base.py,sha256=
|
44
|
-
langfun/core/eval/base_test.py,sha256=
|
45
|
-
langfun/core/eval/matching.py,sha256=
|
46
|
-
langfun/core/eval/matching_test.py,sha256=
|
47
|
-
langfun/core/eval/scoring.py,sha256=
|
48
|
-
langfun/core/eval/scoring_test.py,sha256=
|
42
|
+
langfun/core/eval/__init__.py,sha256=NSmPe2lxdxFoY4h8VkNyONPAFtOTUpK9WhmZRaqUgiI,1335
|
43
|
+
langfun/core/eval/base.py,sha256=_XUc8KivNzgn39sJ0H5W-o_37yXrTBt_X_hKkckLCiA,60239
|
44
|
+
langfun/core/eval/base_test.py,sha256=g3lRp2dcq411cLYHpn8spI4feyv2nOccs5PlFBwav3g,22512
|
45
|
+
langfun/core/eval/matching.py,sha256=Ks-L9vyMNDj4R8zFczzByT_4DK2wAFatyCZupdHzx_g,9932
|
46
|
+
langfun/core/eval/matching_test.py,sha256=5Qs9ETaLoyNcJ43f-_bK2Bfe--2Y3U79DnSA55-l6pc,4932
|
47
|
+
langfun/core/eval/scoring.py,sha256=A3y6HMcmpREQPqUD-WtImYOb2jG-23WpcUO2-WGhel0,6360
|
48
|
+
langfun/core/eval/scoring_test.py,sha256=vxJR-2rBghUDUOCLTIMd6M3i1F8xDhA-U45wuBHVfc0,4058
|
49
49
|
langfun/core/llms/__init__.py,sha256=1bPg1QI8duOZCYINm-jWi094x0JtLmsk4KX60qIC_gs,3245
|
50
|
-
langfun/core/llms/anthropic.py,sha256=
|
51
|
-
langfun/core/llms/anthropic_test.py,sha256=
|
50
|
+
langfun/core/llms/anthropic.py,sha256=7W9YdPN3SlAFhAIQlihMkrpo7tTY_4NvD0KIlCrqcsk,8505
|
51
|
+
langfun/core/llms/anthropic_test.py,sha256=TMM30myyEhwF99Le4RvJEXOn8RYl0q1FRkt9Q9nl1jk,5540
|
52
52
|
langfun/core/llms/fake.py,sha256=b-Xk5IPTbUt-elsyzd_i3n1tqzc_kgETXrEvgJruSMk,2824
|
53
53
|
langfun/core/llms/fake_test.py,sha256=ZlDQgL41EX3eYTfBQNp2nB2LciqCmtoHgCsGvW4XhwI,4184
|
54
54
|
langfun/core/llms/google_genai.py,sha256=n8zyJwh9UCTgb6-8LyvmjVNFGZQ4-zfzZ0ulkhHAnR8,8624
|
55
55
|
langfun/core/llms/google_genai_test.py,sha256=_UcGTfl16-aDUlEWFC2W2F8y9jPUs53RBYA6MOCpGXw,7525
|
56
|
-
langfun/core/llms/groq.py,sha256=
|
57
|
-
langfun/core/llms/groq_test.py,sha256=
|
56
|
+
langfun/core/llms/groq.py,sha256=NaGItVL_pkOpqPpI4bPGU27xLFRoaeizZ49v2s-4ERs,7844
|
57
|
+
langfun/core/llms/groq_test.py,sha256=M6GtlrsOvDun_j-sR8cPh4W_moHWZNSTiThu3kuwbbc,5281
|
58
58
|
langfun/core/llms/llama_cpp.py,sha256=Y_KkMUf3Xfac49koMUtUslKl3h-HWp3-ntq7Jaa3bdo,2385
|
59
59
|
langfun/core/llms/llama_cpp_test.py,sha256=ZxC6defGd_HX9SFRU9U4cJiQnBKundbOrchbXuC1Z2M,1683
|
60
|
-
langfun/core/llms/openai.py,sha256=
|
61
|
-
langfun/core/llms/openai_test.py,sha256=
|
60
|
+
langfun/core/llms/openai.py,sha256=06nPhmw0zIA5Zqv3eqsrZtYLHnKwW7N8yt3LlFUFVpI,13247
|
61
|
+
langfun/core/llms/openai_test.py,sha256=Yt_W6k8YXpT3bs0JroARofCGmn_Uq3u61LmZxqWS2DQ,8272
|
62
62
|
langfun/core/llms/cache/__init__.py,sha256=QAo3InUMDM_YpteNnVCSejI4zOsnjSMWKJKzkb3VY64,993
|
63
63
|
langfun/core/llms/cache/base.py,sha256=cFfYvOIUae842pncqCAsRvqXCk2AnAsRYVx0mcIoAeY,3338
|
64
64
|
langfun/core/llms/cache/in_memory.py,sha256=YfFyJEhLs73cUiB0ZfhMxYpdE8Iuxxw-dvMFwGHTSHw,4742
|
@@ -73,22 +73,22 @@ langfun/core/modalities/mime.py,sha256=wVfaYflhGz1W4v3m972rAplW3OGOFtjFpHDYIaUD5
|
|
73
73
|
langfun/core/modalities/mime_test.py,sha256=cVHxRvJ1QXC1SVhBmWkJdWGpL9Xl0UNfTQq6j0OGGL4,1881
|
74
74
|
langfun/core/modalities/video.py,sha256=25M4XsNG5XEWRy57LYT_a6_aMURMPAgC41B3weEXFsY,1747
|
75
75
|
langfun/core/modalities/video_test.py,sha256=jYuI2m8S8zDCAVBPEUbbpP205dXAht90A2_PHWo4-r8,2039
|
76
|
-
langfun/core/structured/__init__.py,sha256=
|
76
|
+
langfun/core/structured/__init__.py,sha256=zO6mdApZgWy6d2i3s_FWrjHS_-7qWnase0VRq0KhKn0,3589
|
77
77
|
langfun/core/structured/completion.py,sha256=skBxt6V_fv2TBUKnzFgnPMbVY8HSYn8sY04MLok2yvs,7299
|
78
|
-
langfun/core/structured/completion_test.py,sha256=
|
78
|
+
langfun/core/structured/completion_test.py,sha256=MYxEzeScC3gFVujvrMMboBF5nh-QiVLwGgqAV3oaFUQ,19273
|
79
79
|
langfun/core/structured/description.py,sha256=SXW4MJvshFjbR-0gw6rE21o6WXq12UlRXawvDBXMZFA,5211
|
80
80
|
langfun/core/structured/description_test.py,sha256=UtZGjSFUaQ6130t1E5tcL7ODu0xIefkapb53TbnqsK8,7362
|
81
81
|
langfun/core/structured/function_generation.py,sha256=pFgS3vcRAWiuFBol2x5Eeip3XqoudONsOpeJpWyjT3s,7479
|
82
82
|
langfun/core/structured/function_generation_test.py,sha256=ZJI-aaGgWWszn92u7h5IZ9Pl70N2DgAGGJrIxPzsvwg,10065
|
83
|
-
langfun/core/structured/mapping.py,sha256=
|
84
|
-
langfun/core/structured/mapping_test.py,sha256=
|
83
|
+
langfun/core/structured/mapping.py,sha256=Vq3bQZWi4iYjcVn8D2kvPXTAm9jrQ-_1ueHLbXtGRNQ,12112
|
84
|
+
langfun/core/structured/mapping_test.py,sha256=PiXklMeIa8L6KtMi3ju7J9Y39gZy0hIGz-Oeq4A_7XE,3835
|
85
85
|
langfun/core/structured/parsing.py,sha256=keoVqEfzAbdULh6GawWFsTQzU91MzJXYFZjXGXLaD8g,11492
|
86
|
-
langfun/core/structured/parsing_test.py,sha256=
|
86
|
+
langfun/core/structured/parsing_test.py,sha256=34wDrXaQ-EYhJLfDL8mX9K53oQMSzh5pVYdKjnESmK8,20895
|
87
87
|
langfun/core/structured/prompting.py,sha256=mOmCWNVMnBk4rI7KBlEm5kmusPXoAKiWcohhzaw-s2o,7427
|
88
|
-
langfun/core/structured/prompting_test.py,sha256=
|
88
|
+
langfun/core/structured/prompting_test.py,sha256=1Ik-RWs2fixpUTUKoN7FfJX48hDaVScZ-E1CB7J7F3Y,19860
|
89
89
|
langfun/core/structured/schema.py,sha256=mJXirgqx3N7SA9zBO_ISHrzcV-ZRshLhnMJyCcSjGjY,25057
|
90
90
|
langfun/core/structured/schema_generation.py,sha256=U3nRQsqmMZg_qIVDh2fiY3K4JLfsAL1LcKzIFP1iXFg,5316
|
91
|
-
langfun/core/structured/schema_generation_test.py,sha256=
|
91
|
+
langfun/core/structured/schema_generation_test.py,sha256=RM9s71kMNg2jTePwInkiW9fK1ACN37eyPeF8OII-0zw,2950
|
92
92
|
langfun/core/structured/schema_test.py,sha256=yw_Uo3xJC3JA9dBDjZdkQdBGPf04e7t1oT9SZTAiSdg,22360
|
93
93
|
langfun/core/structured/scoring.py,sha256=a3vfGnqf-DOWjD07MF54GCZTO_R1RTxTDVPzerXnU0s,2325
|
94
94
|
langfun/core/structured/scoring_test.py,sha256=TznLMl0x9QxzmhHz_3Vr44VOXuvFnUSeRQVhu33W5cA,1437
|
@@ -101,8 +101,8 @@ langfun/core/templates/demonstration.py,sha256=vCrgYubdZM5Umqcgp8NUVGXgr4P_c-fik
|
|
101
101
|
langfun/core/templates/demonstration_test.py,sha256=SafcDQ0WgI7pw05EmPI2S4v1t3ABKzup8jReCljHeK4,2162
|
102
102
|
langfun/core/templates/selfplay.py,sha256=yhgrJbiYwq47TgzThmHrDQTF4nDrTI09CWGhuQPNv-s,2273
|
103
103
|
langfun/core/templates/selfplay_test.py,sha256=DYVrkk7uNKCqJGEHH31HssU2BPuMItU1vJLzfcXIlYg,2156
|
104
|
-
langfun-0.0.2.
|
105
|
-
langfun-0.0.2.
|
106
|
-
langfun-0.0.2.
|
107
|
-
langfun-0.0.2.
|
108
|
-
langfun-0.0.2.
|
104
|
+
langfun-0.0.2.dev20240425.dist-info/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
|
105
|
+
langfun-0.0.2.dev20240425.dist-info/METADATA,sha256=OZw2iKQkUx9uF-tFzARKjqCekvCCVYMXL00JvSpRxVE,3436
|
106
|
+
langfun-0.0.2.dev20240425.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
107
|
+
langfun-0.0.2.dev20240425.dist-info/top_level.txt,sha256=RhlEkHxs1qtzmmtWSwYoLVJAc1YrbPtxQ52uh8Z9VvY,8
|
108
|
+
langfun-0.0.2.dev20240425.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|