langfun 0.1.2.dev202501050804__py3-none-any.whl → 0.1.2.dev202501090804__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.
Files changed (34) hide show
  1. langfun/core/__init__.py +0 -5
  2. langfun/core/coding/python/correction.py +4 -3
  3. langfun/core/coding/python/errors.py +10 -9
  4. langfun/core/coding/python/execution.py +23 -12
  5. langfun/core/coding/python/execution_test.py +21 -2
  6. langfun/core/coding/python/generation.py +18 -9
  7. langfun/core/concurrent.py +2 -3
  8. langfun/core/console.py +8 -3
  9. langfun/core/eval/base.py +2 -3
  10. langfun/core/eval/v2/reporting.py +15 -6
  11. langfun/core/language_model.py +7 -4
  12. langfun/core/language_model_test.py +15 -0
  13. langfun/core/llms/__init__.py +25 -26
  14. langfun/core/llms/cache/in_memory.py +6 -0
  15. langfun/core/llms/cache/in_memory_test.py +5 -0
  16. langfun/core/llms/deepseek.py +261 -0
  17. langfun/core/llms/deepseek_test.py +438 -0
  18. langfun/core/llms/gemini.py +507 -0
  19. langfun/core/llms/gemini_test.py +195 -0
  20. langfun/core/llms/google_genai.py +46 -320
  21. langfun/core/llms/google_genai_test.py +9 -204
  22. langfun/core/llms/openai.py +5 -0
  23. langfun/core/llms/vertexai.py +31 -359
  24. langfun/core/llms/vertexai_test.py +6 -166
  25. langfun/core/structured/mapping.py +13 -13
  26. langfun/core/structured/mapping_test.py +2 -2
  27. langfun/core/structured/schema.py +16 -8
  28. {langfun-0.1.2.dev202501050804.dist-info → langfun-0.1.2.dev202501090804.dist-info}/METADATA +19 -14
  29. {langfun-0.1.2.dev202501050804.dist-info → langfun-0.1.2.dev202501090804.dist-info}/RECORD +32 -30
  30. {langfun-0.1.2.dev202501050804.dist-info → langfun-0.1.2.dev202501090804.dist-info}/WHEEL +1 -1
  31. langfun/core/text_formatting.py +0 -168
  32. langfun/core/text_formatting_test.py +0 -65
  33. {langfun-0.1.2.dev202501050804.dist-info → langfun-0.1.2.dev202501090804.dist-info}/LICENSE +0 -0
  34. {langfun-0.1.2.dev202501050804.dist-info → langfun-0.1.2.dev202501090804.dist-info}/top_level.txt +0 -0
@@ -1,168 +0,0 @@
1
- # Copyright 2023 The Langfun Authors
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
- """Utility library for LM input/output formatting."""
15
-
16
- import io
17
- import re
18
- from typing import Any
19
-
20
- try:
21
- import termcolor # pylint: disable=g-import-not-at-top
22
- except ImportError:
23
- termcolor = None
24
-
25
-
26
- # Regular expression for ANSI color characters.
27
- _ANSI_COLOR_REGEX = re.compile(r'\x1b\[[0-9;]*m')
28
-
29
-
30
- def decolored(text: str) -> str:
31
- """Return the de-colored string that may contains ANSI color characters."""
32
- return re.sub(_ANSI_COLOR_REGEX, '', text)
33
-
34
-
35
- def colored(
36
- text: str,
37
- color: str | None = None,
38
- background: str | None = None,
39
- styles: list[str] | None = None
40
- ) -> str:
41
- """Returns the colored text with ANSI color characters.
42
-
43
- Args:
44
- text: A string that may or may not already has ANSI color characters.
45
- color: A string for text colors. Applicable values are:
46
- 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'.
47
- background: A string for background colors. Applicable values are:
48
- 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'.
49
- styles: A list of strings for applying styles on the text.
50
- Applicable values are:
51
- 'bold', 'dark', 'underline', 'blink', 'reverse', 'concealed'.
52
-
53
- Returns:
54
- A string with ANSI color characters embracing the entire text.
55
- """
56
- if not termcolor:
57
- return text
58
- return termcolor.colored(
59
- text,
60
- color=color,
61
- on_color=('on_' + background) if background else None,
62
- attrs=styles)
63
-
64
-
65
- def colored_template(
66
- text: str,
67
- expression_color: str | None = 'white',
68
- expression_background: str | None = 'blue',
69
- expression_styles: list[str] | None = None,
70
- statement_color: str | None = 'red',
71
- statement_background: str | None = None,
72
- statement_styles: list[str] | None = None,
73
- comment_color: str | None = 'green',
74
- comment_background: str | None = None,
75
- comment_styles: list[str] | None = None,
76
- ) -> str:
77
- """Returns colored (maybe) Jinja2 template string."""
78
- text = color_text_blocks(
79
- text, '{{', '}}',
80
- color=expression_color,
81
- background=expression_background,
82
- styles=expression_styles)
83
-
84
- text = color_text_blocks(
85
- text, '{%', '%}',
86
- color=statement_color,
87
- background=statement_background,
88
- styles=statement_styles)
89
-
90
- text = color_text_blocks(
91
- text, '{#', '#}',
92
- color=comment_color,
93
- background=comment_background,
94
- styles=comment_styles)
95
-
96
- return text
97
-
98
-
99
- def color_text_blocks(
100
- text: str,
101
- block_start: str,
102
- block_end: str,
103
- color: str | None = None,
104
- background: str | None = None,
105
- styles: list[str] | None = None
106
- ) -> str:
107
- """Apply colors to text blocks.
108
-
109
- Args:
110
- text: A string that may or may not already has ANSI color characters.
111
- block_start: A string that signals the start of a block. E.g. '{{'
112
- block_end: A string that signals the end of a block. E.g. '}}'.
113
- color: A string for text colors. Applicable values are:
114
- 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'.
115
- background: A string for background colors. Applicable values are:
116
- 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'.
117
- styles: A list of strings for applying styles on the text.
118
- Applicable values are:
119
- 'bold', 'dark', 'underline', 'blink', 'reverse', 'concealed'.
120
-
121
- Returns:
122
- A string with ANSI color characters embracing the matched text blocks.
123
- """
124
- if not color and not background and not styles:
125
- return text
126
-
127
- string_buffer = io.StringIO()
128
- start_index = 0
129
- end_index = 0
130
- previous_color = None
131
-
132
- def write_nonblock_text(text: str, previous_color: str | None):
133
- if previous_color:
134
- string_buffer.write(previous_color)
135
- string_buffer.write(text)
136
-
137
- while start_index < len(text):
138
- start_index = text.find(block_start, end_index)
139
- if start_index == -1:
140
- write_nonblock_text(text[end_index:], previous_color)
141
- break
142
-
143
- # Deal with text since last block.
144
- since_last_block = text[end_index:start_index]
145
- write_nonblock_text(since_last_block, previous_color)
146
- colors = re.findall(_ANSI_COLOR_REGEX, since_last_block)
147
- if colors:
148
- previous_color = colors[-1]
149
-
150
- # Match block.
151
- end_index = text.find(block_end, start_index + len(block_start))
152
- if end_index == -1:
153
- write_nonblock_text(text[start_index:], previous_color)
154
- break
155
- end_index += len(block_end)
156
-
157
- # Write block text.
158
- block = text[start_index:end_index]
159
- colored_block = colored(
160
- block, color=color, background=background, styles=styles)
161
- string_buffer.write(colored_block)
162
- return string_buffer.getvalue()
163
-
164
-
165
- def colored_print(value: Any):
166
- """Prints text with color."""
167
- print(colored_template(str(value)))
168
-
@@ -1,65 +0,0 @@
1
- # Copyright 2023 The Langfun Authors
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
- """Tests for text formatting."""
15
-
16
- import inspect
17
- import unittest
18
- from langfun.core import text_formatting
19
-
20
-
21
- class TextFormattingTest(unittest.TestCase):
22
-
23
- def test_colored_template(self):
24
- original_text = inspect.cleandoc("""
25
- Hi {{ foo }}
26
- {# print x if x is present #}
27
- {% if x %}
28
- {{ x }}
29
- {% endif %}
30
- """)
31
-
32
- colored_text = text_formatting.colored_template(
33
- text_formatting.colored(original_text, color='blue')
34
- )
35
- self.assertEqual(
36
- colored_text,
37
- '\x1b[34mHi \x1b[44m\x1b[37m{{ foo }}\x1b[0m\x1b[34m\n'
38
- '\x1b[32m{# print x if x is present #}\x1b[0m\x1b[34m\n'
39
- '\x1b[31m{% if x %}\x1b[0m\x1b[34m\n'
40
- '\x1b[44m\x1b[37m{{ x }}\x1b[0m\x1b[34m\n'
41
- '\x1b[31m{% endif %}\x1b[0m\x1b[34m\x1b[0m'
42
- )
43
- self.assertEqual(text_formatting.decolored(colored_text), original_text)
44
-
45
- def test_colored_without_termcolor(self):
46
- termcolor = text_formatting.termcolor
47
- text_formatting.termcolor = None
48
- original_text = inspect.cleandoc("""
49
- Hi {{ foo }}
50
- {# print x if x is present #}
51
- {% if x %}
52
- {{ x }}
53
- {% endif %}
54
- """)
55
-
56
- colored_text = text_formatting.colored_template(
57
- text_formatting.colored(original_text, color='blue')
58
- )
59
- self.assertEqual(colored_text, original_text)
60
- self.assertEqual(text_formatting.decolored(colored_text), original_text)
61
- text_formatting.termcolor = termcolor
62
-
63
-
64
- if __name__ == '__main__':
65
- unittest.main()