langfun 0.1.2.dev202410100804__py3-none-any.whl → 0.1.2.dev202410110804__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/core/template.py CHANGED
@@ -17,7 +17,7 @@ import contextlib
17
17
  import dataclasses
18
18
  import functools
19
19
  import inspect
20
- from typing import Annotated, Any, Callable, Iterator, Set, Tuple, Type, Union
20
+ from typing import Annotated, Any, Callable, Iterator, Sequence, Set, Tuple, Type, Union
21
21
 
22
22
  import jinja2
23
23
  from jinja2 import meta as jinja2_meta
@@ -526,6 +526,112 @@ class Template(
526
526
  return lfun
527
527
  return cls(template_str='{{input}}', input=value, **kwargs)
528
528
 
529
+ def _html_tree_view_content(
530
+ self,
531
+ *,
532
+ view: pg.views.HtmlTreeView,
533
+ root_path: pg.KeyPath,
534
+ **kwargs,
535
+ ):
536
+ def render_template_str():
537
+ return pg.Html.element(
538
+ 'div',
539
+ [
540
+ pg.Html.element('span', [self.template_str])
541
+ ],
542
+ css_class=['template-str'],
543
+ )
544
+
545
+ def render_fields():
546
+ def render_value_fn(value, *, root_path, **kwargs):
547
+ if isinstance(value, component.ContextualAttribute):
548
+ inferred = self.sym_inferred(root_path.key, pg.MISSING_VALUE)
549
+ if inferred != pg.MISSING_VALUE:
550
+ return pg.Html.element(
551
+ 'div',
552
+ [
553
+ view.render(inferred, root_path=root_path, **kwargs)
554
+ ],
555
+ css_class=['inferred-value'],
556
+ )
557
+ else:
558
+ return pg.Html.element(
559
+ 'span',
560
+ ['(external)'],
561
+ css_class=['contextual-variable'],
562
+ )
563
+ return view.render(
564
+ value, root_path=root_path, **kwargs
565
+ )
566
+ return pg.Html.element(
567
+ 'fieldset',
568
+ [
569
+ pg.Html.element('legend', ['Template Variables']),
570
+ view.complex_value(
571
+ self.sym_init_args,
572
+ name='fields',
573
+ root_path=root_path,
574
+ render_value_fn=render_value_fn,
575
+ exclude_keys=['template_str', 'clean'],
576
+ parent=self,
577
+ collapse_level=root_path.depth + 1,
578
+ ),
579
+ ],
580
+ css_class=['template-fields'],
581
+ )
582
+
583
+ return pg.Html.element(
584
+ 'div',
585
+ [
586
+ render_template_str(),
587
+ render_fields(),
588
+ ],
589
+ css_class=['complex_value'],
590
+ )
591
+
592
+ def _html_style(self) -> list[str]:
593
+ return super()._html_style() + [
594
+ """
595
+ /* Langfun Template styles. */
596
+ .template-str {
597
+ padding: 10px;
598
+ margin: 10px 5px 10px 5px;
599
+ font-style: italic;
600
+ font-size: 1.1em;
601
+ white-space: pre-wrap;
602
+ border: 1px solid #EEE;
603
+ border-radius: 5px;
604
+ background-color: #EEE;
605
+ color: #cc2986;
606
+ }
607
+ .template-fields {
608
+ margin: 0px 0px 5px 0px;
609
+ border: 1px solid #EEE;
610
+ padding: 5px;
611
+ }
612
+ .template-fields > legend {
613
+ font-size: 0.8em;
614
+ margin: 5px 0px 5px 0px;
615
+ }
616
+ .inferred-value::after {
617
+ content: ' (inferred)';
618
+ color: gray;
619
+ font-style: italic;
620
+ }
621
+ .contextual-variable {
622
+ margin: 0px 0px 0px 5px;
623
+ font-style: italic;
624
+ color: gray;
625
+ }
626
+ """
627
+ ]
628
+
629
+ # Additional CSS class to add to the root <details> element.
630
+ def _html_element_class(self) -> Sequence[str] | None:
631
+ return [
632
+ pg.object_utils.camel_to_snake(self.__class__.__name__, '-'),
633
+ 'lf-template'
634
+ ]
529
635
 
530
636
  # Register converter from str to LangFunc, therefore we can always
531
637
  # pass strs to attributes that accept LangFunc.
@@ -13,6 +13,7 @@
13
13
  # limitations under the License.
14
14
  """Template test."""
15
15
  import inspect
16
+ from typing import Any
16
17
  import unittest
17
18
 
18
19
  from langfun.core import component
@@ -552,5 +553,41 @@ class TemplateRenderEventTest(unittest.TestCase):
552
553
  self.assertEqual(render_stacks, [[l]])
553
554
 
554
555
 
556
+ class HtmlTest(unittest.TestCase):
557
+
558
+ def assert_html_content(self, html, expected):
559
+ expected = inspect.cleandoc(expected).strip()
560
+ actual = html.content.strip()
561
+ if actual != expected:
562
+ print(actual)
563
+ self.assertEqual(actual, expected)
564
+
565
+ def test_html(self):
566
+
567
+ class Foo(Template):
568
+ """Template Foo.
569
+
570
+ {{x}} + {{y}} = ?
571
+ """
572
+ x: Any
573
+ y: Any
574
+
575
+ class Bar(Template):
576
+ """Template Bar.
577
+
578
+ {{y}} + {{z}}
579
+ """
580
+ y: Any
581
+
582
+ self.assert_html_content(
583
+ Foo(x=Bar('{{y}} + {{z}}'), y=1).to_html(
584
+ enable_summary_tooltip=False,
585
+ ),
586
+ """
587
+ <details open class="pyglove foo lf-template"><summary><div class="summary_title">Foo(...)</div></summary><div class="complex_value"><div class="template-str"><span>{{x}} + {{y}} = ?</span></div><fieldset class="template-fields"><legend>Template Variables</legend><div class="complex_value foo lf-template"><table><tr><td><span class="object_key str">x</span><span class="tooltip key-path">x</span></td><td><div><details class="pyglove bar lf-template"><summary><div class="summary_title">Bar(...)</div></summary><div class="complex_value"><div class="template-str"><span>{{y}} + {{z}}</span></div><fieldset class="template-fields"><legend>Template Variables</legend><div class="complex_value bar lf-template"><table><tr><td><span class="object_key str">y</span><span class="tooltip key-path">x.y</span></td><td><div><div class="inferred-value"><span class="simple_value int">1</span></div></div></td></tr><tr><td><span class="object_key str">z</span><span class="tooltip key-path">x.z</span></td><td><div><span class="contextual-variable">(external)</span></div></td></tr></table></div></fieldset></div></details></div></td></tr><tr><td><span class="object_key str">y</span><span class="tooltip key-path">y</span></td><td><div><span class="simple_value int">1</span></div></td></tr></table></div></fieldset></div></details>
588
+ """
589
+ )
590
+
591
+
555
592
  if __name__ == '__main__':
556
593
  unittest.main()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: langfun
3
- Version: 0.1.2.dev202410100804
3
+ Version: 0.1.2.dev202410110804
4
4
  Summary: Langfun: Language as Functions.
5
5
  Home-page: https://github.com/google/langfun
6
6
  Author: Langfun Authors
@@ -10,11 +10,11 @@ langfun/core/langfunc.py,sha256=G50YgoVZ0y1GFw2ev41MlOqr6qa8YakbvNC0h_E0PiA,1114
10
10
  langfun/core/langfunc_test.py,sha256=ZLlj6ysNIWUlm0jcq6PbNUwdJ2XstW5QQT0L2s5GlGY,8753
11
11
  langfun/core/language_model.py,sha256=7z1Go39NvrP8dg52la2m56Acg3jsk-hA9m-27hUPorA,26302
12
12
  langfun/core/language_model_test.py,sha256=ebJ1vnaxKSKvlwi6v07yHjn91xMiDw2bQ9DBnyVorYw,23303
13
- langfun/core/logging.py,sha256=wYDuPuGeS1hyJBzjMQgquyfLw-TUVO-b7H68UA8X-Eg,4279
14
- langfun/core/logging_test.py,sha256=poSsNGKi6G9LWOcWnTY0BQjj0BtaQknH-NK6FcQrVT4,2152
13
+ langfun/core/logging.py,sha256=tW5z4mMFQkKc4H2yptYcRxVODhmoFUrXGYplPnfJFak,6940
14
+ langfun/core/logging_test.py,sha256=4YkRFb-3_a3ARsIrXtjphFvvA3nU8iBNC9WTwlkYC1A,4156
15
15
  langfun/core/memory.py,sha256=f-asN1F7Vehgdn_fK84v73GrEUOxRtaW934keutTKjk,2416
16
- langfun/core/message.py,sha256=7UOxNMA1Le0ZGkleryqwXcWojZ-l_hku5Sc58BsIOGw,18586
17
- langfun/core/message_test.py,sha256=2o4WvE-WL67OsVY-O5g__67OIJ73vvg6MuojSc2uVRs,12504
16
+ langfun/core/message.py,sha256=OZo2Zy-R31GIC-Vl5340huDUyBLWGHwz-y8jU0ANDRc,24030
17
+ langfun/core/message_test.py,sha256=e32yv8TShaSI75wyXIGU4nk7dvL-dmorKZ_XIvkjv1g,28484
18
18
  langfun/core/modality.py,sha256=hC0LF8EGCU2GJxTNbfzR441oOk2-HnHnAVWbwkLG0bI,4133
19
19
  langfun/core/modality_test.py,sha256=7SwhixFME2Q1sIXRgJx97EZFiIyC31A9NVr6_nDtFv4,2441
20
20
  langfun/core/natural_language.py,sha256=3ynSnaYQnjE60LIPK5fyMgdIjubnPYZwzGq4rWPeloE,1177
@@ -25,8 +25,8 @@ langfun/core/sampling.py,sha256=SCnS5PFJWNVxSKvSkSCNRUmruvScun8UcNN4gafuXcw,5866
25
25
  langfun/core/sampling_test.py,sha256=U7PANpMsl9E_pa4_Y4FzesSjcwg-u-LKHGCWSgv-8FY,3663
26
26
  langfun/core/subscription.py,sha256=euawEuSZP-BHydaT-AQpfYFL0m5pWPGcW0upFhrojqc,10930
27
27
  langfun/core/subscription_test.py,sha256=Y4ZdbZEwm83YNZBxHff0QR4QUa4rdaNXA3_jfIcArBo,8717
28
- langfun/core/template.py,sha256=AWHJwWjsPgf8r584_HOleBqgN90oKCpW8OMx-gdljuM,22635
29
- langfun/core/template_test.py,sha256=YCBa9YMYtc_8X2gNSJvvQKpSovZy5A4YgIniCvm-1Ys,15843
28
+ langfun/core/template.py,sha256=B3dIPVcLMjYUPlU7trM5FdHUrfhTLfgFUmJxyBo1PMU,25683
29
+ langfun/core/template_test.py,sha256=Xo_8iYWRwK6actfmEkajw1ZGmmyK4DOwo0g_Ohq4ZA0,17847
30
30
  langfun/core/text_formatting.py,sha256=d7t9vaY6aCn1dkfkikpNYnBy5E_i93vHbfyDWFclGZU,5284
31
31
  langfun/core/text_formatting_test.py,sha256=ck0Xzdd4YF4CtCUj7VE0GybfbAyKQ8p3xkM1FBGrqIk,2096
32
32
  langfun/core/coding/__init__.py,sha256=5utju_fwEsImaiftx4oXKl9FAM8p281k8-Esdh_-m1w,835
@@ -77,18 +77,18 @@ langfun/core/memories/__init__.py,sha256=HpghfZ-w1NQqzJXBx8Lz0daRhB2rcy2r9Xm491S
77
77
  langfun/core/memories/conversation_history.py,sha256=c9amD8hCxGFiZuVAzkP0dOMWSp8L90uvwkOejjuBqO0,1835
78
78
  langfun/core/memories/conversation_history_test.py,sha256=AaW8aNoFjxNusanwJDV0r3384Mg0eAweGmPx5DIkM0Y,2052
79
79
  langfun/core/modalities/__init__.py,sha256=F8P72IwFiTpEseTR2tYEJyQMlDW7fd9csvGJquLKJNg,1269
80
- langfun/core/modalities/audio.py,sha256=Qxo7bYjLKQ1gVJVomr9RqR2SvxY826QgXhTzzk437Sk,952
81
- langfun/core/modalities/audio_test.py,sha256=gWCB9h3FyrdGqro3ajBXqkw0lU0W1sBjOOq6wZbl7Fg,2027
82
- langfun/core/modalities/image.py,sha256=DyVV17Ii-vJmwA7Mo23B40m4VxZg_WXc0KPKN8Q4Mlc,1756
83
- langfun/core/modalities/image_test.py,sha256=A0aYblxpnKABDgm2OVPxF8iRq4qFVanM9TPcaewkAgA,3774
84
- langfun/core/modalities/mime.py,sha256=-BEIAcIGzPdPgwUZ2yqFj6NGsBYGCiqeNqIWVeaNnTI,6164
85
- langfun/core/modalities/mime_test.py,sha256=kmRiPP-3Py02NnKPu0oPexSIJ-MXaaE2UYY82ga0TV8,2913
86
- langfun/core/modalities/ms_office.py,sha256=_dUgUCfK-hsEMOjgA_q3hUgKC4n8iAufKy7Sh2Gs1Jo,3780
87
- langfun/core/modalities/ms_office_test.py,sha256=d_NZ0QU23NydenYZgNj6YxgO5ZYzjg-HCbglsVJGp04,87866
80
+ langfun/core/modalities/audio.py,sha256=qCrVCX690SG0ps-ZfOtNWvHn_CmdJsmxF7GySScWUqY,964
81
+ langfun/core/modalities/audio_test.py,sha256=yyGEBYqMXmNs_F2dEtj-PX8HE040vqh-YQppsvdxPw4,2025
82
+ langfun/core/modalities/image.py,sha256=ovcX8NLBNv8WzxAdhQ-u4VQfHVBkWijRj_oiNwhE9lk,1768
83
+ langfun/core/modalities/image_test.py,sha256=XMgtJXY75R5eo0CZ222D1QUy57_hESnttmCGWwDLt7k,3824
84
+ langfun/core/modalities/mime.py,sha256=BrhS-r_1455dSCoIX5hhan9EKVapq0tnO2AFf1aYplg,7371
85
+ langfun/core/modalities/mime_test.py,sha256=0LLptQfF71-WKStoWs3-CvWZSIRglCvgEuU_AwkN8bI,4382
86
+ langfun/core/modalities/ms_office.py,sha256=0PnM6W9SovVfJNd4XCpUfOcGBuvj4hlA1zqPjdsJNFM,3725
87
+ langfun/core/modalities/ms_office_test.py,sha256=rrDBx51ELAqRaUX6Y6tgg0k3tEFHOlRMfUpeULD7mlo,87868
88
88
  langfun/core/modalities/pdf.py,sha256=mfaeCbUA4JslFVTARiJh8hW7imvL4tLVw9gUhO5bAZA,727
89
- langfun/core/modalities/pdf_test.py,sha256=KE40zJD3Whe6ty2OULkp1J8jwLmB4ZjGXlGekluTP48,1952
90
- langfun/core/modalities/video.py,sha256=sKcXxbx9S1ERjH8yEzkbtySpcRJD40QiPIQiIBy-U5I,955
91
- langfun/core/modalities/video_test.py,sha256=GbsoefSeO7y8kCYhTtp4s9E3ah_eYrb6Z-MXpS01RFc,2046
89
+ langfun/core/modalities/pdf_test.py,sha256=ulZ0FbnlsU0wkrdckJ4ONZPTYRyMPO9Aob1UO6FXygk,1950
90
+ langfun/core/modalities/video.py,sha256=vI9apcHIHGyp90i34Srg7S3G6IBDtDCk8qiXhwRQmkw,967
91
+ langfun/core/modalities/video_test.py,sha256=7OXZoohKMYjt7vrJUdPb553HLyl1oBOKRgzBePFv68Q,2042
92
92
  langfun/core/structured/__init__.py,sha256=7EgI6pQIWnSNxhcIawBcSRDR8GPq5ytcmGxgPEjbxeA,3894
93
93
  langfun/core/structured/completion.py,sha256=cS2PjG7sqzDu5x0xoTk8RmNcoeX55iVwH38NTefkMHg,8108
94
94
  langfun/core/structured/completion_test.py,sha256=2mUzDMKGF_WGfTtsnfmfMDx97dkJ-98y8leen__qWLA,19281
@@ -96,13 +96,13 @@ langfun/core/structured/description.py,sha256=SXW4MJvshFjbR-0gw6rE21o6WXq12UlRXa
96
96
  langfun/core/structured/description_test.py,sha256=UtZGjSFUaQ6130t1E5tcL7ODu0xIefkapb53TbnqsK8,7362
97
97
  langfun/core/structured/function_generation.py,sha256=pFgS3vcRAWiuFBol2x5Eeip3XqoudONsOpeJpWyjT3s,7479
98
98
  langfun/core/structured/function_generation_test.py,sha256=ZJI-aaGgWWszn92u7h5IZ9Pl70N2DgAGGJrIxPzsvwg,10065
99
- langfun/core/structured/mapping.py,sha256=dKOCvIA_kCQ88KoCnP5k0iOe9xRt8WLC2kbT6qPqrd8,12016
100
- langfun/core/structured/mapping_test.py,sha256=zQoVx3kAD5oSm_OJAQA6q41NXLLyn8qs6CIVJgAoP_w,4489
99
+ langfun/core/structured/mapping.py,sha256=wEkubZ7-kXUZacgz4hEKvb_FumQA3emjEgwBuQA7Hbk,13052
100
+ langfun/core/structured/mapping_test.py,sha256=Oal51n0R2SMKCh7aoZnD_BYBnLLYrXE3cH_bjPu-jlY,8388
101
101
  langfun/core/structured/parsing.py,sha256=keoVqEfzAbdULh6GawWFsTQzU91MzJXYFZjXGXLaD8g,11492
102
102
  langfun/core/structured/parsing_test.py,sha256=34wDrXaQ-EYhJLfDL8mX9K53oQMSzh5pVYdKjnESmK8,20895
103
103
  langfun/core/structured/prompting.py,sha256=huwwh01AQQCwPBQESOMI_V1V5PZkVQ8C89Yjk67_4Uw,10677
104
104
  langfun/core/structured/prompting_test.py,sha256=P4SsnWKC1MuslxUVPd8rJQQrBbMD_WrItNWqqSc_EV8,26689
105
- langfun/core/structured/schema.py,sha256=zpK3UqHPgxaHWptMr4I4zd_f22Yn4IFxF655d4O_jmQ,27416
105
+ langfun/core/structured/schema.py,sha256=E3f49ej7XFiFd14SErcpgJI5WLm1aTLxL-PVs-UrmQE,28154
106
106
  langfun/core/structured/schema_generation.py,sha256=U3nRQsqmMZg_qIVDh2fiY3K4JLfsAL1LcKzIFP1iXFg,5316
107
107
  langfun/core/structured/schema_generation_test.py,sha256=RM9s71kMNg2jTePwInkiW9fK1ACN37eyPeF8OII-0zw,2950
108
108
  langfun/core/structured/schema_test.py,sha256=RjYhwTgktQgyqAjzLvo967nTiIK9KWgP-aNGg4e7ihE,25258
@@ -119,8 +119,8 @@ langfun/core/templates/demonstration.py,sha256=vCrgYubdZM5Umqcgp8NUVGXgr4P_c-fik
119
119
  langfun/core/templates/demonstration_test.py,sha256=SafcDQ0WgI7pw05EmPI2S4v1t3ABKzup8jReCljHeK4,2162
120
120
  langfun/core/templates/selfplay.py,sha256=yhgrJbiYwq47TgzThmHrDQTF4nDrTI09CWGhuQPNv-s,2273
121
121
  langfun/core/templates/selfplay_test.py,sha256=rBW2Qr8yi-aWYwoTwRR-n1peKyMX9QXPZXURjLgoiRs,2264
122
- langfun-0.1.2.dev202410100804.dist-info/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
123
- langfun-0.1.2.dev202410100804.dist-info/METADATA,sha256=keMqre48TJyYnoryatucsOkXaiPV1uOJZHe46MBi0cM,8890
124
- langfun-0.1.2.dev202410100804.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
125
- langfun-0.1.2.dev202410100804.dist-info/top_level.txt,sha256=RhlEkHxs1qtzmmtWSwYoLVJAc1YrbPtxQ52uh8Z9VvY,8
126
- langfun-0.1.2.dev202410100804.dist-info/RECORD,,
122
+ langfun-0.1.2.dev202410110804.dist-info/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
123
+ langfun-0.1.2.dev202410110804.dist-info/METADATA,sha256=jTVkWGOI_iFvCwSABP5qNdmRuQud4Hy77-uJcFkbtXA,8890
124
+ langfun-0.1.2.dev202410110804.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
125
+ langfun-0.1.2.dev202410110804.dist-info/top_level.txt,sha256=RhlEkHxs1qtzmmtWSwYoLVJAc1YrbPtxQ52uh8Z9VvY,8
126
+ langfun-0.1.2.dev202410110804.dist-info/RECORD,,