langfun 0.1.2.dev202411020804__tar.gz → 0.1.2.dev202411060804__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 (131) hide show
  1. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/PKG-INFO +1 -1
  2. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/component.py +7 -4
  3. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/llms/anthropic.py +11 -1
  4. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/llms/anthropic_test.py +21 -2
  5. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/logging.py +4 -4
  6. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/message.py +11 -7
  7. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/modality.py +1 -1
  8. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/structured/mapping.py +3 -1
  9. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/structured/schema.py +5 -2
  10. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/template.py +2 -1
  11. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun.egg-info/PKG-INFO +1 -1
  12. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/LICENSE +0 -0
  13. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/README.md +0 -0
  14. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/__init__.py +0 -0
  15. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/__init__.py +0 -0
  16. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/coding/__init__.py +0 -0
  17. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/coding/python/__init__.py +0 -0
  18. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/coding/python/correction.py +0 -0
  19. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/coding/python/correction_test.py +0 -0
  20. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/coding/python/errors.py +0 -0
  21. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/coding/python/errors_test.py +0 -0
  22. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/coding/python/execution.py +0 -0
  23. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/coding/python/execution_test.py +0 -0
  24. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/coding/python/generation.py +0 -0
  25. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/coding/python/generation_test.py +0 -0
  26. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/coding/python/parsing.py +0 -0
  27. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/coding/python/parsing_test.py +0 -0
  28. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/coding/python/permissions.py +0 -0
  29. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/coding/python/permissions_test.py +0 -0
  30. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/component_test.py +0 -0
  31. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/concurrent.py +0 -0
  32. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/concurrent_test.py +0 -0
  33. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/console.py +0 -0
  34. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/console_test.py +0 -0
  35. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/eval/__init__.py +0 -0
  36. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/eval/base.py +0 -0
  37. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/eval/base_test.py +0 -0
  38. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/eval/matching.py +0 -0
  39. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/eval/matching_test.py +0 -0
  40. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/eval/patching.py +0 -0
  41. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/eval/patching_test.py +0 -0
  42. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/eval/scoring.py +0 -0
  43. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/eval/scoring_test.py +0 -0
  44. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/langfunc.py +0 -0
  45. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/langfunc_test.py +0 -0
  46. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/language_model.py +0 -0
  47. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/language_model_test.py +0 -0
  48. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/llms/__init__.py +0 -0
  49. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/llms/cache/__init__.py +0 -0
  50. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/llms/cache/base.py +0 -0
  51. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/llms/cache/in_memory.py +0 -0
  52. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/llms/cache/in_memory_test.py +0 -0
  53. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/llms/fake.py +0 -0
  54. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/llms/fake_test.py +0 -0
  55. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/llms/google_genai.py +0 -0
  56. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/llms/google_genai_test.py +0 -0
  57. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/llms/groq.py +0 -0
  58. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/llms/groq_test.py +0 -0
  59. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/llms/llama_cpp.py +0 -0
  60. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/llms/llama_cpp_test.py +0 -0
  61. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/llms/openai.py +0 -0
  62. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/llms/openai_test.py +0 -0
  63. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/llms/rest.py +0 -0
  64. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/llms/rest_test.py +0 -0
  65. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/llms/vertexai.py +0 -0
  66. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/llms/vertexai_test.py +0 -0
  67. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/logging_test.py +0 -0
  68. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/memories/__init__.py +0 -0
  69. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/memories/conversation_history.py +0 -0
  70. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/memories/conversation_history_test.py +0 -0
  71. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/memory.py +0 -0
  72. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/message_test.py +0 -0
  73. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/modalities/__init__.py +0 -0
  74. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/modalities/audio.py +0 -0
  75. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/modalities/audio_test.py +0 -0
  76. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/modalities/image.py +0 -0
  77. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/modalities/image_test.py +0 -0
  78. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/modalities/mime.py +0 -0
  79. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/modalities/mime_test.py +0 -0
  80. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/modalities/ms_office.py +0 -0
  81. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/modalities/ms_office_test.py +0 -0
  82. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/modalities/pdf.py +0 -0
  83. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/modalities/pdf_test.py +0 -0
  84. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/modalities/video.py +0 -0
  85. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/modalities/video_test.py +0 -0
  86. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/modality_test.py +0 -0
  87. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/natural_language.py +0 -0
  88. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/natural_language_test.py +0 -0
  89. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/repr_utils.py +0 -0
  90. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/repr_utils_test.py +0 -0
  91. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/sampling.py +0 -0
  92. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/sampling_test.py +0 -0
  93. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/structured/__init__.py +0 -0
  94. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/structured/completion.py +0 -0
  95. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/structured/completion_test.py +0 -0
  96. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/structured/description.py +0 -0
  97. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/structured/description_test.py +0 -0
  98. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/structured/function_generation.py +0 -0
  99. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/structured/function_generation_test.py +0 -0
  100. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/structured/mapping_test.py +0 -0
  101. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/structured/parsing.py +0 -0
  102. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/structured/parsing_test.py +0 -0
  103. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/structured/prompting.py +0 -0
  104. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/structured/prompting_test.py +0 -0
  105. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/structured/schema_generation.py +0 -0
  106. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/structured/schema_generation_test.py +0 -0
  107. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/structured/schema_test.py +0 -0
  108. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/structured/scoring.py +0 -0
  109. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/structured/scoring_test.py +0 -0
  110. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/structured/tokenization.py +0 -0
  111. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/structured/tokenization_test.py +0 -0
  112. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/subscription.py +0 -0
  113. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/subscription_test.py +0 -0
  114. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/template_test.py +0 -0
  115. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/templates/__init__.py +0 -0
  116. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/templates/completion.py +0 -0
  117. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/templates/completion_test.py +0 -0
  118. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/templates/conversation.py +0 -0
  119. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/templates/conversation_test.py +0 -0
  120. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/templates/demonstration.py +0 -0
  121. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/templates/demonstration_test.py +0 -0
  122. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/templates/selfplay.py +0 -0
  123. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/templates/selfplay_test.py +0 -0
  124. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/text_formatting.py +0 -0
  125. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun/core/text_formatting_test.py +0 -0
  126. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun.egg-info/SOURCES.txt +0 -0
  127. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun.egg-info/dependency_links.txt +0 -0
  128. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun.egg-info/requires.txt +0 -0
  129. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/langfun.egg-info/top_level.txt +0 -0
  130. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/setup.cfg +0 -0
  131. {langfun-0.1.2.dev202411020804 → langfun-0.1.2.dev202411060804}/setup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: langfun
3
- Version: 0.1.2.dev202411020804
3
+ Version: 0.1.2.dev202411060804
4
4
  Summary: Langfun: Language as Functions.
5
5
  Home-page: https://github.com/google/langfun
6
6
  Author: Langfun Authors
@@ -253,7 +253,9 @@ def _get_scoped_value(
253
253
  return scoped_values.get(var_name, default)
254
254
 
255
255
 
256
- class ContextualAttribute(pg.symbolic.ValueFromParentChain):
256
+ class ContextualAttribute(
257
+ pg.symbolic.ValueFromParentChain, pg.views.HtmlTreeView.Extension
258
+ ):
257
259
  """Attributes whose values are inferred from the context of the component.
258
260
 
259
261
  Please see go/langfun-component#attribute-value-retrieval for details.
@@ -290,8 +292,8 @@ class ContextualAttribute(pg.symbolic.ValueFromParentChain):
290
292
  self,
291
293
  *,
292
294
  view: pg.views.HtmlTreeView,
293
- parent: Any,
294
- root_path: pg.KeyPath,
295
+ parent: Any = None,
296
+ root_path: pg.KeyPath | None = None,
295
297
  **kwargs,
296
298
  ) -> pg.Html:
297
299
  inferred_value = pg.MISSING_VALUE
@@ -301,7 +303,8 @@ class ContextualAttribute(pg.symbolic.ValueFromParentChain):
301
303
  if inferred_value is not pg.MISSING_VALUE:
302
304
  kwargs.pop('name', None)
303
305
  return view.render(
304
- inferred_value, parent=self, root_path=root_path + '<inferred>',
306
+ inferred_value, parent=self,
307
+ root_path=pg.KeyPath('<inferred>', root_path),
305
308
  **view.get_passthrough_kwargs(**kwargs)
306
309
  )
307
310
  return pg.Html.element(
@@ -141,6 +141,8 @@ class Anthropic(rest.REST):
141
141
  'x-api-key': self._api_key,
142
142
  'anthropic-version': self.api_version,
143
143
  'content-type': 'application/json',
144
+ # TODO(yifenglu): Remove beta flag once the feature is fully supported.
145
+ 'anthropic-beta': 'pdfs-2024-09-25',
144
146
  }
145
147
 
146
148
  @property
@@ -223,7 +225,6 @@ class Anthropic(rest.REST):
223
225
  if isinstance(chunk, str):
224
226
  item = dict(type='text', text=chunk)
225
227
  elif isinstance(chunk, lf_modalities.Image):
226
- # NOTE(daiyip): Anthropic only support image content instead of URL.
227
228
  item = dict(
228
229
  type='image',
229
230
  source=dict(
@@ -232,6 +233,15 @@ class Anthropic(rest.REST):
232
233
  data=base64.b64encode(chunk.to_bytes()).decode(),
233
234
  ),
234
235
  )
236
+ elif isinstance(chunk, lf_modalities.PDF):
237
+ item = dict(
238
+ type='document',
239
+ source=dict(
240
+ type='base64',
241
+ media_type=chunk.mime_type,
242
+ data=base64.b64encode(chunk.to_bytes()).decode(),
243
+ ),
244
+ )
235
245
  else:
236
246
  raise ValueError(f'Unsupported modality object: {chunk!r}.')
237
247
  content.append(item)
@@ -59,18 +59,30 @@ image_content = (
59
59
  b'\xdao\xd0|\x00\x00\x00\x00IEND\xaeB`\x82'
60
60
  )
61
61
 
62
+ pdf_content = (
63
+ b'%PDF-1.4\n1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n2 0 obj\n<<'
64
+ b' /Type /Pages /Count 1 /Kids [3 0 R] >>\nendobj\n3 0 obj\n<< /Type /Page'
65
+ b' /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R >>\nendobj\n4 0'
66
+ b' obj\n<< /Length 44 >>\nstream\nBT /F1 24 Tf 100 700 Td (Hello, PDF'
67
+ b' content!) Tj ET\nendstream\nendobj\n5 0 obj\n<< /Type /Font /Subtype'
68
+ b' /Type1 /BaseFont /Helvetica >>\nendobj\nxref\n0 6\n0000000000 65535 f'
69
+ b' \n0000000010 00000 n \n0000000079 00000 n \n0000000178 00000 n'
70
+ b' \n0000000278 00000 n \n0000000407 00000 n \ntrailer\n<< /Size 6 /Root 1'
71
+ b' 0 R >>\nstartxref\n517\n%%EOF'
72
+ )
73
+
62
74
 
63
75
  def mock_mm_requests_post(url: str, json: dict[str, Any], **kwargs):
64
76
  del url, kwargs
65
77
  v = json['messages'][0]['content'][0]
66
- image = lf_modalities.Image.from_bytes(base64.b64decode(v['source']['data']))
78
+ content = lf_modalities.Mime.from_bytes(base64.b64decode(v['source']['data']))
67
79
 
68
80
  response = requests.Response()
69
81
  response.status_code = 200
70
82
  response._content = pg.to_json_str({
71
83
  'content': [{
72
84
  'type': 'text',
73
- 'text': f'{v["type"]}: {image.mime_type}',
85
+ 'text': f'{v["type"]}: {content.mime_type}',
74
86
  }],
75
87
  'usage': {
76
88
  'input_tokens': 2,
@@ -146,6 +158,13 @@ class AnthropicTest(unittest.TestCase):
146
158
  response = lm(lf_modalities.Image.from_bytes(image_content), lm=lm)
147
159
  self.assertEqual(response.text, 'image: image/png')
148
160
 
161
+ def test_pdf_call(self):
162
+ with mock.patch('requests.Session.post') as mock_mm_request:
163
+ mock_mm_request.side_effect = mock_mm_requests_post
164
+ lm = anthropic.Claude3Haiku(api_key='fake_key')
165
+ response = lm(lf_modalities.PDF.from_bytes(pdf_content), lm=lm)
166
+ self.assertEqual(response.text, 'document: application/pdf')
167
+
149
168
  def test_call_errors(self):
150
169
  for status_code, error_type, error_message in [
151
170
  (429, 'rate_limit', 'Rate limit exceeded.'),
@@ -43,7 +43,7 @@ def get_log_level() -> LogLevel:
43
43
  return component.context_value('__event_log_level__', 'info')
44
44
 
45
45
 
46
- class LogEntry(pg.Object):
46
+ class LogEntry(pg.Object, pg.views.HtmlTreeView.Extension):
47
47
  """Event log entry."""
48
48
  time: datetime.datetime
49
49
  level: LogLevel
@@ -60,7 +60,7 @@ class LogEntry(pg.Object):
60
60
  title: str | pg.Html | None = None,
61
61
  max_summary_len_for_str: int = 80,
62
62
  **kwargs
63
- ) -> str:
63
+ ) -> pg.Html | None:
64
64
  if len(self.message) > max_summary_len_for_str:
65
65
  message = self.message[:max_summary_len_for_str] + '...'
66
66
  else:
@@ -89,7 +89,7 @@ class LogEntry(pg.Object):
89
89
  def _html_tree_view_content(
90
90
  self,
91
91
  view: pg.views.HtmlTreeView,
92
- root_path: pg.KeyPath,
92
+ root_path: pg.KeyPath | None = None,
93
93
  max_summary_len_for_str: int = 80,
94
94
  collapse_level: int | None = 1,
95
95
  extra_flags: dict[str, Any] | None = None,
@@ -118,7 +118,7 @@ class LogEntry(pg.Object):
118
118
  view.render(
119
119
  self.metadata,
120
120
  name='metadata',
121
- root_path=root_path + 'metadata',
121
+ root_path=pg.KeyPath('metadata', root_path),
122
122
  parent=self,
123
123
  collapse_level=view.get_collapse_level(
124
124
  (collapse_level, -1), collapse_log_metadata_level,
@@ -23,7 +23,11 @@ from langfun.core import natural_language
23
23
  import pyglove as pg
24
24
 
25
25
 
26
- class Message(natural_language.NaturalLanguageFormattable, pg.Object):
26
+ class Message(
27
+ natural_language.NaturalLanguageFormattable,
28
+ pg.Object,
29
+ pg.views.HtmlTreeView.Extension
30
+ ):
27
31
  """Message.
28
32
 
29
33
  ``Message`` is the protocol for users and the system to interact with
@@ -511,7 +515,7 @@ class Message(natural_language.NaturalLanguageFormattable, pg.Object):
511
515
  self,
512
516
  *,
513
517
  view: pg.views.HtmlTreeView,
514
- root_path: pg.KeyPath,
518
+ root_path: pg.KeyPath | None = None,
515
519
  collapse_level: int | None = None,
516
520
  extra_flags: dict[str, Any] | None = None,
517
521
  **kwargs,
@@ -582,7 +586,7 @@ class Message(natural_language.NaturalLanguageFormattable, pg.Object):
582
586
  s.write(s.escape(chunk))
583
587
  else:
584
588
  assert isinstance(chunk, modality.Modality), chunk
585
- child_path = root_path + 'metadata' + chunk.referred_name
589
+ child_path = pg.KeyPath(['metadata', chunk.referred_name], root_path)
586
590
  s.write(
587
591
  pg.Html.element(
588
592
  'div',
@@ -610,7 +614,7 @@ class Message(natural_language.NaturalLanguageFormattable, pg.Object):
610
614
  def render_result():
611
615
  if 'result' not in self.metadata:
612
616
  return None
613
- child_path = root_path + 'metadata' + 'result'
617
+ child_path = pg.KeyPath(['metadata', 'result'], root_path)
614
618
  return pg.Html.element(
615
619
  'div',
616
620
  [
@@ -632,7 +636,7 @@ class Message(natural_language.NaturalLanguageFormattable, pg.Object):
632
636
  def render_usage():
633
637
  if 'usage' not in self.metadata:
634
638
  return None
635
- child_path = root_path + 'metadata' + 'usage'
639
+ child_path = pg.KeyPath(['metadata', 'usage'], root_path)
636
640
  return pg.Html.element(
637
641
  'div',
638
642
  [
@@ -661,7 +665,7 @@ class Message(natural_language.NaturalLanguageFormattable, pg.Object):
661
665
  and not source.has_tag(source_tag)):
662
666
  source = source.source
663
667
  if source is not None:
664
- child_path = root_path + 'source'
668
+ child_path = pg.KeyPath('source', root_path)
665
669
  child_extra_flags = extra_flags.copy()
666
670
  child_extra_flags['collapse_source_message_level'] = (
667
671
  view.get_collapse_level(
@@ -684,7 +688,7 @@ class Message(natural_language.NaturalLanguageFormattable, pg.Object):
684
688
  def render_metadata():
685
689
  if not include_message_metadata:
686
690
  return None
687
- child_path = root_path + 'metadata'
691
+ child_path = pg.KeyPath('metadata', root_path)
688
692
  return pg.Html.element(
689
693
  'div',
690
694
  [
@@ -31,7 +31,7 @@ def format_modality_as_ref(enabled: bool = True) -> ContextManager[None]:
31
31
  )
32
32
 
33
33
 
34
- class Modality(component.Component):
34
+ class Modality(component.Component, pg.views.HtmlTreeView.Extension):
35
35
  """Base class for multimodal object."""
36
36
 
37
37
  REF_START = '<<[['
@@ -59,7 +59,9 @@ class MappingError(Exception): # pylint: disable=g-bad-exception-name
59
59
 
60
60
 
61
61
  @pg.use_init_args(['input', 'output', 'schema', 'context'])
62
- class MappingExample(lf.NaturalLanguageFormattable, lf.Component):
62
+ class MappingExample(lf.NaturalLanguageFormattable,
63
+ lf.Component,
64
+ pg.views.HtmlTreeView.Extension):
63
65
  """Mapping example between text, schema and structured value."""
64
66
 
65
67
  input: pg.typing.Annotated[
@@ -110,7 +110,11 @@ class SchemaError(Exception): # pylint: disable=g-bad-exception-name
110
110
  return r.getvalue()
111
111
 
112
112
 
113
- class Schema(lf.NaturalLanguageFormattable, pg.Object):
113
+ class Schema(
114
+ lf.NaturalLanguageFormattable,
115
+ pg.Object,
116
+ pg.views.HtmlTreeView.Extension
117
+ ):
114
118
  """Base class for structured data schema."""
115
119
 
116
120
  spec: pg.typing.Annotated[
@@ -193,7 +197,6 @@ class Schema(lf.NaturalLanguageFormattable, pg.Object):
193
197
  self,
194
198
  *,
195
199
  view: pg.views.HtmlTreeView,
196
- root_path: pg.KeyPath,
197
200
  **kwargs,
198
201
  ):
199
202
  return pg.Html.element(
@@ -47,6 +47,7 @@ class Template(
47
47
  natural_language.NaturalLanguageFormattable,
48
48
  component.Component,
49
49
  pg.typing.CustomTyping,
50
+ pg.views.HtmlTreeView.Extension
50
51
  ):
51
52
  """Langfun string template.
52
53
 
@@ -530,7 +531,7 @@ class Template(
530
531
  self,
531
532
  *,
532
533
  view: pg.views.HtmlTreeView,
533
- root_path: pg.KeyPath,
534
+ root_path: pg.KeyPath | None = None,
534
535
  collapse_level: int | None = None,
535
536
  extra_flags: dict[str, Any] | None = None,
536
537
  debug: bool = False,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: langfun
3
- Version: 0.1.2.dev202411020804
3
+ Version: 0.1.2.dev202411060804
4
4
  Summary: Langfun: Language as Functions.
5
5
  Home-page: https://github.com/google/langfun
6
6
  Author: Langfun Authors