langfun 0.0.2.dev20240429__py3-none-any.whl → 0.1.2.dev202501140804__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 (144) hide show
  1. langfun/__init__.py +20 -2
  2. langfun/core/__init__.py +16 -5
  3. langfun/core/agentic/__init__.py +30 -0
  4. langfun/core/agentic/action.py +854 -0
  5. langfun/core/agentic/action_eval.py +150 -0
  6. langfun/core/agentic/action_eval_test.py +109 -0
  7. langfun/core/agentic/action_test.py +136 -0
  8. langfun/core/coding/python/__init__.py +5 -11
  9. langfun/core/coding/python/correction.py +37 -21
  10. langfun/core/coding/python/correction_test.py +29 -3
  11. langfun/core/coding/python/execution.py +40 -216
  12. langfun/core/coding/python/execution_test.py +29 -89
  13. langfun/core/coding/python/generation.py +21 -11
  14. langfun/core/coding/python/generation_test.py +2 -2
  15. langfun/core/coding/python/parsing.py +108 -193
  16. langfun/core/coding/python/parsing_test.py +2 -105
  17. langfun/core/component.py +63 -2
  18. langfun/core/component_test.py +53 -0
  19. langfun/core/concurrent.py +414 -117
  20. langfun/core/concurrent_test.py +111 -24
  21. langfun/core/console.py +18 -5
  22. langfun/core/console_test.py +17 -0
  23. langfun/core/eval/__init__.py +16 -1
  24. langfun/core/eval/base.py +622 -174
  25. langfun/core/eval/base_test.py +200 -54
  26. langfun/core/eval/matching.py +63 -76
  27. langfun/core/eval/matching_test.py +17 -8
  28. langfun/core/eval/patching.py +130 -0
  29. langfun/core/eval/patching_test.py +170 -0
  30. langfun/core/eval/scoring.py +26 -26
  31. langfun/core/eval/scoring_test.py +19 -2
  32. langfun/core/eval/v2/__init__.py +42 -0
  33. langfun/core/eval/v2/checkpointing.py +380 -0
  34. langfun/core/eval/v2/checkpointing_test.py +228 -0
  35. langfun/core/eval/v2/eval_test_helper.py +136 -0
  36. langfun/core/eval/v2/evaluation.py +725 -0
  37. langfun/core/eval/v2/evaluation_test.py +180 -0
  38. langfun/core/eval/v2/example.py +305 -0
  39. langfun/core/eval/v2/example_test.py +128 -0
  40. langfun/core/eval/v2/experiment.py +1048 -0
  41. langfun/core/eval/v2/experiment_test.py +433 -0
  42. langfun/core/eval/v2/metric_values.py +156 -0
  43. langfun/core/eval/v2/metric_values_test.py +80 -0
  44. langfun/core/eval/v2/metrics.py +357 -0
  45. langfun/core/eval/v2/metrics_test.py +203 -0
  46. langfun/core/eval/v2/progress.py +348 -0
  47. langfun/core/eval/v2/progress_test.py +82 -0
  48. langfun/core/eval/v2/progress_tracking.py +210 -0
  49. langfun/core/eval/v2/progress_tracking_test.py +66 -0
  50. langfun/core/eval/v2/reporting.py +270 -0
  51. langfun/core/eval/v2/reporting_test.py +158 -0
  52. langfun/core/eval/v2/runners.py +488 -0
  53. langfun/core/eval/v2/runners_test.py +334 -0
  54. langfun/core/langfunc.py +4 -17
  55. langfun/core/langfunc_test.py +22 -6
  56. langfun/core/language_model.py +577 -39
  57. langfun/core/language_model_test.py +470 -56
  58. langfun/core/llms/__init__.py +87 -16
  59. langfun/core/llms/anthropic.py +312 -87
  60. langfun/core/llms/anthropic_test.py +71 -3
  61. langfun/core/llms/cache/base.py +21 -2
  62. langfun/core/llms/cache/in_memory.py +13 -0
  63. langfun/core/llms/cache/in_memory_test.py +53 -2
  64. langfun/core/llms/compositional.py +101 -0
  65. langfun/core/llms/compositional_test.py +73 -0
  66. langfun/core/llms/deepseek.py +117 -0
  67. langfun/core/llms/deepseek_test.py +61 -0
  68. langfun/core/llms/fake.py +11 -7
  69. langfun/core/llms/fake_test.py +14 -0
  70. langfun/core/llms/gemini.py +507 -0
  71. langfun/core/llms/gemini_test.py +195 -0
  72. langfun/core/llms/google_genai.py +62 -218
  73. langfun/core/llms/google_genai_test.py +9 -202
  74. langfun/core/llms/groq.py +160 -144
  75. langfun/core/llms/groq_test.py +31 -137
  76. langfun/core/llms/llama_cpp.py +15 -42
  77. langfun/core/llms/llama_cpp_test.py +4 -30
  78. langfun/core/llms/openai.py +395 -203
  79. langfun/core/llms/openai_compatible.py +179 -0
  80. langfun/core/llms/openai_compatible_test.py +495 -0
  81. langfun/core/llms/openai_test.py +30 -395
  82. langfun/core/llms/rest.py +113 -0
  83. langfun/core/llms/rest_test.py +111 -0
  84. langfun/core/llms/vertexai.py +192 -0
  85. langfun/core/llms/vertexai_test.py +52 -0
  86. langfun/core/logging.py +284 -0
  87. langfun/core/logging_test.py +125 -0
  88. langfun/core/message.py +319 -9
  89. langfun/core/message_test.py +190 -13
  90. langfun/core/modalities/__init__.py +6 -2
  91. langfun/core/modalities/audio.py +30 -0
  92. langfun/core/modalities/audio_test.py +63 -0
  93. langfun/core/modalities/image.py +39 -20
  94. langfun/core/modalities/image_test.py +52 -9
  95. langfun/core/modalities/mime.py +206 -29
  96. langfun/core/modalities/mime_test.py +90 -9
  97. langfun/core/modalities/ms_office.py +117 -0
  98. langfun/core/modalities/ms_office_test.py +389 -0
  99. langfun/core/modalities/pdf.py +22 -0
  100. langfun/core/modalities/pdf_test.py +57 -0
  101. langfun/core/modalities/video.py +9 -26
  102. langfun/core/modalities/video_test.py +3 -3
  103. langfun/core/modality.py +26 -3
  104. langfun/core/modality_test.py +2 -2
  105. langfun/core/sampling.py +11 -11
  106. langfun/core/structured/__init__.py +12 -16
  107. langfun/core/structured/completion.py +32 -5
  108. langfun/core/structured/completion_test.py +7 -6
  109. langfun/core/structured/description.py +2 -2
  110. langfun/core/structured/description_test.py +3 -3
  111. langfun/core/structured/function_generation.py +60 -27
  112. langfun/core/structured/function_generation_test.py +72 -2
  113. langfun/core/structured/mapping.py +97 -47
  114. langfun/core/structured/mapping_test.py +90 -2
  115. langfun/core/structured/parsing.py +33 -21
  116. langfun/core/structured/parsing_test.py +53 -9
  117. langfun/core/structured/querying.py +746 -0
  118. langfun/core/structured/{prompting_test.py → querying_test.py} +469 -51
  119. langfun/core/structured/schema.py +204 -97
  120. langfun/core/structured/schema_generation.py +1 -1
  121. langfun/core/structured/schema_test.py +130 -29
  122. langfun/core/structured/scoring.py +125 -19
  123. langfun/core/structured/scoring_test.py +30 -0
  124. langfun/core/structured/tokenization.py +64 -0
  125. langfun/core/structured/tokenization_test.py +48 -0
  126. langfun/core/template.py +115 -1
  127. langfun/core/template_test.py +71 -1
  128. langfun/core/templates/conversation.py +9 -0
  129. langfun/core/templates/conversation_test.py +4 -3
  130. langfun/core/templates/selfplay_test.py +10 -2
  131. langfun-0.1.2.dev202501140804.dist-info/METADATA +225 -0
  132. langfun-0.1.2.dev202501140804.dist-info/RECORD +153 -0
  133. {langfun-0.0.2.dev20240429.dist-info → langfun-0.1.2.dev202501140804.dist-info}/WHEEL +1 -1
  134. langfun/core/coding/python/errors.py +0 -108
  135. langfun/core/coding/python/errors_test.py +0 -99
  136. langfun/core/coding/python/permissions.py +0 -90
  137. langfun/core/coding/python/permissions_test.py +0 -86
  138. langfun/core/structured/prompting.py +0 -238
  139. langfun/core/text_formatting.py +0 -162
  140. langfun/core/text_formatting_test.py +0 -47
  141. langfun-0.0.2.dev20240429.dist-info/METADATA +0 -100
  142. langfun-0.0.2.dev20240429.dist-info/RECORD +0 -108
  143. {langfun-0.0.2.dev20240429.dist-info → langfun-0.1.2.dev202501140804.dist-info}/LICENSE +0 -0
  144. {langfun-0.0.2.dev20240429.dist-info → langfun-0.1.2.dev202501140804.dist-info}/top_level.txt +0 -0
@@ -1,238 +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
- """Symbolic query."""
15
-
16
- from typing import Any, Callable, Type, Union
17
-
18
- import langfun.core as lf
19
- from langfun.core.structured import mapping
20
- from langfun.core.structured import schema as schema_lib
21
- import pyglove as pg
22
-
23
-
24
- @lf.use_init_args(['schema', 'default', 'examples'])
25
- class QueryStructure(mapping.Mapping):
26
- """Query an object out from a natural language text."""
27
-
28
- context_title = 'CONTEXT'
29
- input_title = 'INPUT_OBJECT'
30
-
31
- # Mark schema as required.
32
- schema: pg.typing.Annotated[
33
- schema_lib.schema_spec(), 'Required schema for parsing.'
34
- ]
35
-
36
-
37
- class QueryStructureJson(QueryStructure):
38
- """Query a structured value using JSON as the protocol."""
39
-
40
- preamble = """
41
- Please respond to the last {{ input_title }} with {{ output_title}} according to {{ schema_title }}:
42
-
43
- INSTRUCTIONS:
44
- 1. If the schema has `_type`, carry it over to the JSON output.
45
- 2. If a field from the schema cannot be extracted from the response, use null as the JSON value.
46
-
47
- {{ input_title }}:
48
- 1 + 1 =
49
-
50
- {{ schema_title }}:
51
- {"result": {"_type": "langfun.core.structured.prompting.Answer", "final_answer": int}}
52
-
53
- {{ output_title}}:
54
- {"result": {"_type": "langfun.core.structured.prompting.Answer", "final_answer": 2}}
55
- """
56
-
57
- protocol = 'json'
58
- schema_title = 'SCHEMA'
59
- output_title = 'JSON'
60
-
61
-
62
- class QueryStructurePython(QueryStructure):
63
- """Query a structured value using Python as the protocol."""
64
-
65
- preamble = """
66
- Please respond to the last {{ input_title }} with {{ output_title }} according to {{ schema_title }}.
67
-
68
- {{ input_title }}:
69
- 1 + 1 =
70
-
71
- {{ schema_title }}:
72
- Answer
73
-
74
- ```python
75
- class Answer:
76
- final_answer: int
77
- ```
78
-
79
- {{ output_title }}:
80
- ```python
81
- Answer(
82
- final_answer=2
83
- )
84
- ```
85
- """
86
- protocol = 'python'
87
- schema_title = 'OUTPUT_TYPE'
88
- output_title = 'OUTPUT_OBJECT'
89
-
90
-
91
- def _query_structure_cls(
92
- protocol: schema_lib.SchemaProtocol,
93
- ) -> Type[QueryStructure]:
94
- if protocol == 'json':
95
- return QueryStructureJson
96
- elif protocol == 'python':
97
- return QueryStructurePython
98
- else:
99
- raise ValueError(f'Unknown protocol: {protocol!r}.')
100
-
101
-
102
- def query(
103
- prompt: Union[str, pg.Symbolic],
104
- schema: Union[
105
- schema_lib.Schema, Type[Any], list[Type[Any]], dict[str, Any], None
106
- ] = None,
107
- default: Any = lf.RAISE_IF_HAS_ERROR,
108
- *,
109
- lm: lf.LanguageModel | None = None,
110
- examples: list[mapping.MappingExample] | None = None,
111
- cache_seed: int | None = 0,
112
- response_postprocess: Callable[[str], str] | None = None,
113
- autofix: int = 0,
114
- autofix_lm: lf.LanguageModel | None = None,
115
- protocol: schema_lib.SchemaProtocol = 'python',
116
- returns_message: bool = False,
117
- skip_lm: bool = False,
118
- **kwargs,
119
- ) -> Any:
120
- """Parse a natural langugage message based on schema.
121
-
122
- Examples:
123
-
124
- ```
125
- class FlightDuration:
126
- hours: int
127
- minutes: int
128
-
129
- class Flight(pg.Object):
130
- airline: str
131
- flight_number: str
132
- departure_airport_code: str
133
- arrival_airport_code: str
134
- departure_time: str
135
- arrival_time: str
136
- duration: FlightDuration
137
- stops: int
138
- price: float
139
-
140
- prompt = '''
141
- Information about flight UA2631.
142
- '''
143
-
144
- r = lf.query(prompt, Flight)
145
- assert isinstance(r, Flight)
146
- assert r.airline == 'United Airlines'
147
- assert r.departure_airport_code == 'SFO'
148
- assert r.duration.hour = 7
149
- ```
150
-
151
- Args:
152
- prompt: A str (may contain {{}} as template) as natural language input, or a
153
- `pg.Symbolic` object as structured input as prompt to LLM.
154
- schema: A type annotation as the schema for output object. If str (default),
155
- the response will be a str in natural language.
156
- default: The default value if parsing failed. If not specified, error will
157
- be raised.
158
- lm: The language model to use. If not specified, the language model from
159
- `lf.context` context manager will be used.
160
- examples: An optional list of fewshot examples for helping parsing. If None,
161
- the default one-shot example will be added.
162
- cache_seed: Seed for computing cache key. The cache key is determined by a
163
- tuple of (lm, prompt, cache seed). If None, cache will be disabled for
164
- the query even cache is configured by the LM.
165
- response_postprocess: An optional callable object to process the raw LM
166
- response before parsing it into the final output object. If None, the
167
- raw LM response will not be processed.
168
- autofix: Number of attempts to auto fix the generated code. If 0, autofix is
169
- disabled. Auto-fix is not supported for 'json' protocol.
170
- autofix_lm: The language model to use for autofix. If not specified, the
171
- `autofix_lm` from `lf.context` context manager will be used. Otherwise it
172
- will use `lm`.
173
- protocol: The protocol for schema/value representation. Applicable values
174
- are 'json' and 'python'. By default `python` will be used.
175
- returns_message: If True, returns `lf.Message` as the output, instead of
176
- returning the structured `message.result`.
177
- skip_lm: If True, returns the rendered prompt as a UserMessage object.
178
- otherwise return the LLM response based on the rendered prompt.
179
- **kwargs: Keyword arguments passed to render the prompt or configure the
180
- `lf.structured.Mapping` class. Notable kwargs are:
181
- - template_str: Change the root template for query.
182
- - preamble: Change the preamble for query.
183
- - mapping_template: Change the template for each mapping examle.
184
-
185
- Returns:
186
- The result based on the schema.
187
- """
188
- # Internal usage logging.
189
-
190
- # When `lf.query` is used for symbolic completion, schema is automatically
191
- # inferred when it is None.
192
- if isinstance(prompt, pg.Symbolic) and prompt.sym_partial and schema is None:
193
- schema = prompt.__class__
194
-
195
- if schema in (None, str):
196
- # Query with natural language output.
197
- output = lf.LangFunc.from_value(prompt, **kwargs)(
198
- lm=lm, cache_seed=cache_seed, skip_lm=skip_lm
199
- )
200
- if response_postprocess:
201
- processed_text = response_postprocess(output.text)
202
- if processed_text != output.text:
203
- output = lf.AIMessage(processed_text, source=output)
204
- return output if returns_message else output.text
205
-
206
- # Query with structured output.
207
- prompt_kwargs = kwargs.copy()
208
-
209
- # NOTE(daiyip): when `template_str` is passed in, it's intended to modify the
210
- # QueryStructure template string. Therefore, we pop out the argument for
211
- # prompt rendering.
212
- prompt_kwargs.pop('template_str', None)
213
-
214
- if isinstance(prompt, str):
215
- prompt = lf.Template(prompt, **prompt_kwargs)
216
- elif isinstance(prompt, lf.Template):
217
- prompt = prompt.rebind(**prompt_kwargs, raise_on_no_change=False)
218
-
219
- if isinstance(prompt, lf.Template):
220
- prompt = prompt.render(lm=lm)
221
- else:
222
- prompt = schema_lib.mark_missing(prompt)
223
-
224
- output = _query_structure_cls(protocol)(
225
- input=prompt,
226
- schema=schema,
227
- default=default,
228
- examples=examples,
229
- response_postprocess=response_postprocess,
230
- autofix=autofix if protocol == 'python' else 0,
231
- **kwargs,
232
- )(
233
- lm=lm,
234
- autofix_lm=autofix_lm or lm,
235
- cache_seed=cache_seed,
236
- skip_lm=skip_lm,
237
- )
238
- return output if returns_message else output.result
@@ -1,162 +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
- import termcolor
20
-
21
-
22
- # Regular expression for ANSI color characters.
23
- _ANSI_COLOR_REGEX = re.compile(r'\x1b\[[0-9;]*m')
24
-
25
-
26
- def decolored(text: str) -> str:
27
- """Return the de-colored string that may contains ANSI color characters."""
28
- return re.sub(_ANSI_COLOR_REGEX, '', text)
29
-
30
-
31
- def colored(
32
- text: str,
33
- color: str | None = None,
34
- background: str | None = None,
35
- styles: list[str] | None = None
36
- ) -> str:
37
- """Returns the colored text with ANSI color characters.
38
-
39
- Args:
40
- text: A string that may or may not already has ANSI color characters.
41
- color: A string for text colors. Applicable values are:
42
- 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'.
43
- background: A string for background colors. Applicable values are:
44
- 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'.
45
- styles: A list of strings for applying styles on the text.
46
- Applicable values are:
47
- 'bold', 'dark', 'underline', 'blink', 'reverse', 'concealed'.
48
-
49
- Returns:
50
- A string with ANSI color characters embracing the entire text.
51
- """
52
- return termcolor.colored(
53
- text,
54
- color=color,
55
- on_color=('on_' + background) if background else None,
56
- attrs=styles)
57
-
58
-
59
- def colored_template(
60
- text: str,
61
- expression_color: str | None = 'white',
62
- expression_background: str | None = 'blue',
63
- expression_styles: list[str] | None = None,
64
- statement_color: str | None = 'red',
65
- statement_background: str | None = None,
66
- statement_styles: list[str] | None = None,
67
- comment_color: str | None = 'green',
68
- comment_background: str | None = None,
69
- comment_styles: list[str] | None = None,
70
- ) -> str:
71
- """Returns colored (maybe) Jinja2 template string."""
72
- text = color_text_blocks(
73
- text, '{{', '}}',
74
- color=expression_color,
75
- background=expression_background,
76
- styles=expression_styles)
77
-
78
- text = color_text_blocks(
79
- text, '{%', '%}',
80
- color=statement_color,
81
- background=statement_background,
82
- styles=statement_styles)
83
-
84
- text = color_text_blocks(
85
- text, '{#', '#}',
86
- color=comment_color,
87
- background=comment_background,
88
- styles=comment_styles)
89
-
90
- return text
91
-
92
-
93
- def color_text_blocks(
94
- text: str,
95
- block_start: str,
96
- block_end: str,
97
- color: str | None = None,
98
- background: str | None = None,
99
- styles: list[str] | None = None
100
- ) -> str:
101
- """Apply colors to text blocks.
102
-
103
- Args:
104
- text: A string that may or may not already has ANSI color characters.
105
- block_start: A string that signals the start of a block. E.g. '{{'
106
- block_end: A string that signals the end of a block. E.g. '}}'.
107
- color: A string for text colors. Applicable values are:
108
- 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'.
109
- background: A string for background colors. Applicable values are:
110
- 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'.
111
- styles: A list of strings for applying styles on the text.
112
- Applicable values are:
113
- 'bold', 'dark', 'underline', 'blink', 'reverse', 'concealed'.
114
-
115
- Returns:
116
- A string with ANSI color characters embracing the matched text blocks.
117
- """
118
- if not color and not background and not styles:
119
- return text
120
-
121
- string_buffer = io.StringIO()
122
- start_index = 0
123
- end_index = 0
124
- previous_color = None
125
-
126
- def write_nonblock_text(text: str, previous_color: str | None):
127
- if previous_color:
128
- string_buffer.write(previous_color)
129
- string_buffer.write(text)
130
-
131
- while start_index < len(text):
132
- start_index = text.find(block_start, end_index)
133
- if start_index == -1:
134
- write_nonblock_text(text[end_index:], previous_color)
135
- break
136
-
137
- # Deal with text since last block.
138
- since_last_block = text[end_index:start_index]
139
- write_nonblock_text(since_last_block, previous_color)
140
- colors = re.findall(_ANSI_COLOR_REGEX, since_last_block)
141
- if colors:
142
- previous_color = colors[-1]
143
-
144
- # Match block.
145
- end_index = text.find(block_end, start_index + len(block_start))
146
- if end_index == -1:
147
- write_nonblock_text(text[start_index:], previous_color)
148
- break
149
- end_index += len(block_end)
150
-
151
- # Write block text.
152
- block = text[start_index:end_index]
153
- colored_block = colored(
154
- block, color=color, background=background, styles=styles)
155
- string_buffer.write(colored_block)
156
- return string_buffer.getvalue()
157
-
158
-
159
- def colored_print(value: Any):
160
- """Prints text with color."""
161
- print(colored_template(str(value)))
162
-
@@ -1,47 +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
-
46
- if __name__ == '__main__':
47
- unittest.main()
@@ -1,100 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: langfun
3
- Version: 0.0.2.dev20240429
4
- Summary: Langfun: Language as Functions.
5
- Home-page: https://github.com/google/langfun
6
- Author: Langfun Authors
7
- Author-email: langfun-authors@google.com
8
- License: Apache License 2.0
9
- Keywords: llm generative-ai machine-learning
10
- Classifier: Development Status :: 3 - Alpha
11
- Classifier: Intended Audience :: Developers
12
- Classifier: Intended Audience :: Education
13
- Classifier: Intended Audience :: Science/Research
14
- Classifier: License :: OSI Approved :: Apache Software License
15
- Classifier: Programming Language :: Python :: 3
16
- Classifier: Programming Language :: Python :: 3.10
17
- Classifier: Programming Language :: Python :: 3.11
18
- Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
19
- Classifier: Topic :: Scientific/Engineering :: Human Machine Interfaces
20
- Classifier: Topic :: Software Development :: Libraries :: Python Modules
21
- Classifier: Topic :: Software Development :: Libraries
22
- Description-Content-Type: text/markdown
23
- License-File: LICENSE
24
- Requires-Dist: absl-py >=1.0.0
25
- Requires-Dist: google-generativeai >=0.3.2
26
- Requires-Dist: jinja2 >=3.1.2
27
- Requires-Dist: openai ==0.27.2
28
- Requires-Dist: pyglove >=0.4.5.dev20240423
29
- Requires-Dist: python-magic >=0.4.27
30
- Requires-Dist: requests >=2.31.0
31
- Requires-Dist: termcolor ==1.1.0
32
- Requires-Dist: tqdm >=4.64.1
33
-
34
- <div align="center">
35
- <img src="https://raw.githubusercontent.com/google/langfun/main/docs/_static/logo.svg" width="520px" alt="logo"></img>
36
- </div>
37
-
38
- # Langfun
39
-
40
- [![PyPI version](https://badge.fury.io/py/langfun.svg)](https://badge.fury.io/py/langfun)
41
- [![codecov](https://codecov.io/gh/google/langfun/branch/main/graph/badge.svg)](https://codecov.io/gh/google/langfun)
42
- ![pytest](https://github.com/google/langfun/actions/workflows/ci.yaml/badge.svg)
43
-
44
- [**Installation**](#install) | [**Getting started**](#hello-world)
45
-
46
- ## What is Langfun
47
-
48
- Langfun is a Python library that aims to make language models (LM) fun
49
- to work with. Its design enables a programming model that flows naturally,
50
- resembling the human thought process. It emphasizes the reuse and combination of
51
- language pieces to form prompts, thereby accelerating innovation. In contrast to
52
- other LM frameworks, which feed program-generated data into the LM, langfun
53
- takes a distinct approach: It starts with natural language, allowing for
54
- seamless interactions between language and program logic, and concludes with
55
- natural language and optional structured output. Consequently, langfun can
56
- aptly be described as Language as functions, capturing the core of its
57
- methodology.
58
-
59
- ## Install
60
-
61
- ```
62
- pip install langfun
63
- ```
64
-
65
- Or install nightly build with:
66
-
67
- ```
68
- pip install langfun --pre
69
- ```
70
-
71
- ## Hello World
72
-
73
- ```python
74
- import langfun as lf
75
-
76
- class NumericAnswerExtractor(lf.LangFunc):
77
- """Numeric answer extractor.
78
-
79
- Here is my question:
80
- {{question}}
81
-
82
- Here is the response:
83
- {{question()}}
84
-
85
- Can you help me extract a number from the response as the answer to my
86
- question? Your response should only contain a number in numeric form.
87
- If the answer is not a number or you cannot extract it, respond with UNKNOWN.
88
- """
89
- output_transform = lf.transforms.Match('\d+').to_int()
90
-
91
- l = NumericAnswerExtractor()
92
-
93
- with lf.context(lm=lf.llms.Gpt35(debug=True)):
94
- r = l(question=lf.LangFunc('What is result of {{x}} plus {{y}}?'),
95
- x='one',
96
- y='two')
97
- print('Result:', r.result)
98
- ```
99
-
100
- *Disclaimer: this is not an officially supported Google product.*
@@ -1,108 +0,0 @@
1
- langfun/__init__.py,sha256=zh-EYTCLxkUAIc2zMo3Lye46_CrMrhwO_GwBHZspUvE,1919
2
- langfun/core/__init__.py,sha256=6QEuXOZ9BXxm6TjpaMXuLwUBTYO3pkFDqn9QVBXyyPQ,4248
3
- langfun/core/component.py,sha256=oxesbC0BoE_TbtxwW5x-BAZWxZyyJbuPiX5S38RqCv0,9909
4
- langfun/core/component_test.py,sha256=uR-_Sz_42Jxc5qzLIB-f5_pXmNwnC01Xlbv5NOQSeSU,8021
5
- langfun/core/concurrent.py,sha256=TRc49pJ3HQro2kb5FtcWkHjhBm8UcgE8RJybU5cU3-0,24537
6
- langfun/core/concurrent_test.py,sha256=mwFMZhDUdppnDr7vDSTwcbMHwrdsIoKJwRYNtl4ZWL4,15185
7
- langfun/core/console.py,sha256=bk5rNPNm9rMGW5YT2HixxU04p2umnoabn5SDz6Dqe88,2317
8
- langfun/core/console_test.py,sha256=5SYJdxpJGLgdSSQqqMPoA1X6jpsLD8rgcyk-EgI65oE,1077
9
- langfun/core/langfunc.py,sha256=bRujJfH4iTwKFtFxQf745uJkfltuFnPfOGLuP8ydcr4,11646
10
- langfun/core/langfunc_test.py,sha256=sQaKuZpGGmG80GRifhbxkj7nfzQLJKj4Vuw5y1s1K3U,8378
11
- langfun/core/language_model.py,sha256=dypSV3kr6BLC7hsvV1_QOiqrHUHWtOjQfyFqH79WZmU,20052
12
- langfun/core/language_model_test.py,sha256=T-itu7Li2smv2dkru0C0neCs2W4VJXlNTYahXU6jF54,19548
13
- langfun/core/memory.py,sha256=f-asN1F7Vehgdn_fK84v73GrEUOxRtaW934keutTKjk,2416
14
- langfun/core/message.py,sha256=QhvV9t5qaryPcruyxxcXi3gm9QDInkSldwTtK6sVJ3c,15734
15
- langfun/core/message_test.py,sha256=Z23pUM5vPnDrYkIIibe2KL73D5HKur_awI0ut_EQFQA,9501
16
- langfun/core/modality.py,sha256=HHFBRFbYfS95ZAoDHNM6rsZYgFFiBJkDYmVnteOH_9Q,3374
17
- langfun/core/modality_test.py,sha256=6OUYzEo-AnX-WPjisI3jVU_cgx_JpfDyuGRbUmkoHU0,2396
18
- langfun/core/natural_language.py,sha256=3ynSnaYQnjE60LIPK5fyMgdIjubnPYZwzGq4rWPeloE,1177
19
- langfun/core/natural_language_test.py,sha256=LHGU_1ytbkGuSZQFIFP7vP3dBlcY4-A12fT6dbjUA0E,1424
20
- langfun/core/sampling.py,sha256=vygWvgC8MFw0_AKNSmz-ywMXJYWf8cl0tI8QycvAmyI,5795
21
- langfun/core/sampling_test.py,sha256=U7PANpMsl9E_pa4_Y4FzesSjcwg-u-LKHGCWSgv-8FY,3663
22
- langfun/core/subscription.py,sha256=euawEuSZP-BHydaT-AQpfYFL0m5pWPGcW0upFhrojqc,10930
23
- langfun/core/subscription_test.py,sha256=Y4ZdbZEwm83YNZBxHff0QR4QUa4rdaNXA3_jfIcArBo,8717
24
- langfun/core/template.py,sha256=FZByYq6mhVDjT4HJ3yY-_TUZ13BiURzTJSKLw6QyLY4,21462
25
- langfun/core/template_test.py,sha256=Mbv0dFjboGCVvbDkHD-HacZnlCi8Ku2Hpf2UjdwGSNo,15464
26
- langfun/core/text_formatting.py,sha256=ytjj7opnRJ6w-pkglL2CZUyfYDXLpNf65E42LBb31gc,5158
27
- langfun/core/text_formatting_test.py,sha256=nyKC6tn2L4hPJiqQHgxcbQsJJi4A4Nbj8FiO8iT6B80,1514
28
- langfun/core/coding/__init__.py,sha256=5utju_fwEsImaiftx4oXKl9FAM8p281k8-Esdh_-m1w,835
29
- langfun/core/coding/python/__init__.py,sha256=MJ-vubliz-ebrZH3OBRKBwMi0S9-FrhGCp8YQLR6_I4,1776
30
- langfun/core/coding/python/correction.py,sha256=a2aFUt9ocbXTCR6Z6OGNjQZDI1LfU0PBkSe7hJB8dEM,6589
31
- langfun/core/coding/python/correction_test.py,sha256=yLqmQ9BPORsnREkrS10PnljEaLR3BoydTVeT3OGoqfU,3507
32
- langfun/core/coding/python/errors.py,sha256=fX3Du63uGm25YFXW9D-bV2gntTdTAX3hBFtAnRlmg14,3166
33
- langfun/core/coding/python/errors_test.py,sha256=_ZbWJCFIb-FkCK7K1zCuH8W3x_NFt-jNe3dfP8yqaD4,2323
34
- langfun/core/coding/python/execution.py,sha256=egbM--NlV4Ya-Yr7VQdLY-Jx92C8lq3dsMc09ojtPxU,10096
35
- langfun/core/coding/python/execution_test.py,sha256=lExY6GMLeuCsCKXgM2KbAPJ6kqSlfHmz3RG0-dqfVcI,7197
36
- langfun/core/coding/python/generation.py,sha256=xivSeOKGN00HnG1TLuFhPfP-JyRuRrSELxVJW2ngqIQ,7750
37
- langfun/core/coding/python/generation_test.py,sha256=54bgKr1DgzYFLoqR8bTn7Yjol0gPCuR6XvRltR4l6YM,2777
38
- langfun/core/coding/python/parsing.py,sha256=uyvI1c5OLZhMVK2Oltkl3oJxSLlG0wadlpQcYPEE0-U,7097
39
- langfun/core/coding/python/parsing_test.py,sha256=9vAWF484kWIm6JZq8NFiMgKUDhXV-deRl1QMmNERfAA,7386
40
- langfun/core/coding/python/permissions.py,sha256=1QWGHvzL8MM0Ok_auQ9tURqZHtdOfJaDpBzZ29GUE-c,2544
41
- langfun/core/coding/python/permissions_test.py,sha256=w5EDb8QxpxgJyZkojyzVWQvDfg366zn99-g__6TbPQ0,2699
42
- langfun/core/eval/__init__.py,sha256=NSmPe2lxdxFoY4h8VkNyONPAFtOTUpK9WhmZRaqUgiI,1335
43
- langfun/core/eval/base.py,sha256=1svQoZ0C2DGCVLvr0Qt0TcrlJKtJptdoOBVAxkxnHoU,60264
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
- langfun/core/llms/__init__.py,sha256=1bPg1QI8duOZCYINm-jWi094x0JtLmsk4KX60qIC_gs,3245
50
- langfun/core/llms/anthropic.py,sha256=7W9YdPN3SlAFhAIQlihMkrpo7tTY_4NvD0KIlCrqcsk,8505
51
- langfun/core/llms/anthropic_test.py,sha256=TMM30myyEhwF99Le4RvJEXOn8RYl0q1FRkt9Q9nl1jk,5540
52
- langfun/core/llms/fake.py,sha256=b-Xk5IPTbUt-elsyzd_i3n1tqzc_kgETXrEvgJruSMk,2824
53
- langfun/core/llms/fake_test.py,sha256=ipKfdOcuqVcJ8lDXVpnBVb9HHG0hAVkFkMoHpWjC2cI,7212
54
- langfun/core/llms/google_genai.py,sha256=n8zyJwh9UCTgb6-8LyvmjVNFGZQ4-zfzZ0ulkhHAnR8,8624
55
- langfun/core/llms/google_genai_test.py,sha256=_UcGTfl16-aDUlEWFC2W2F8y9jPUs53RBYA6MOCpGXw,7525
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
- langfun/core/llms/llama_cpp.py,sha256=Y_KkMUf3Xfac49koMUtUslKl3h-HWp3-ntq7Jaa3bdo,2385
59
- langfun/core/llms/llama_cpp_test.py,sha256=ZxC6defGd_HX9SFRU9U4cJiQnBKundbOrchbXuC1Z2M,1683
60
- langfun/core/llms/openai.py,sha256=06nPhmw0zIA5Zqv3eqsrZtYLHnKwW7N8yt3LlFUFVpI,13247
61
- langfun/core/llms/openai_test.py,sha256=MiLqBaYliAkWVEwOBmX3HTj_eAuWLv77q8-I3VyVEBU,14841
62
- langfun/core/llms/cache/__init__.py,sha256=QAo3InUMDM_YpteNnVCSejI4zOsnjSMWKJKzkb3VY64,993
63
- langfun/core/llms/cache/base.py,sha256=cFfYvOIUae842pncqCAsRvqXCk2AnAsRYVx0mcIoAeY,3338
64
- langfun/core/llms/cache/in_memory.py,sha256=YfFyJEhLs73cUiB0ZfhMxYpdE8Iuxxw-dvMFwGHTSHw,4742
65
- langfun/core/llms/cache/in_memory_test.py,sha256=D-n26h__rVXQO51WRFhRfq5sw1oifRLx2SvCQWuNEm8,8747
66
- langfun/core/memories/__init__.py,sha256=HpghfZ-w1NQqzJXBx8Lz0daRhB2rcy2r9Xm491SBhC4,773
67
- langfun/core/memories/conversation_history.py,sha256=c9amD8hCxGFiZuVAzkP0dOMWSp8L90uvwkOejjuBqO0,1835
68
- langfun/core/memories/conversation_history_test.py,sha256=AaW8aNoFjxNusanwJDV0r3384Mg0eAweGmPx5DIkM0Y,2052
69
- langfun/core/modalities/__init__.py,sha256=ldCbs1HHAHAJECNu19vppA0sWEidI40xBs4W1F_YOlo,1073
70
- langfun/core/modalities/image.py,sha256=zNpLHwJi6PJMeeAcVQG6vn0oYIbilTuJq6xu-TvrArM,1358
71
- langfun/core/modalities/image_test.py,sha256=YxDRvC49Bjwyyndd_P7y6XjyS7dOft0Zewwxk-7q4kE,2301
72
- langfun/core/modalities/mime.py,sha256=wVfaYflhGz1W4v3m972rAplW3OGOFtjFpHDYIaUD5D0,2238
73
- langfun/core/modalities/mime_test.py,sha256=cVHxRvJ1QXC1SVhBmWkJdWGpL9Xl0UNfTQq6j0OGGL4,1881
74
- langfun/core/modalities/video.py,sha256=25M4XsNG5XEWRy57LYT_a6_aMURMPAgC41B3weEXFsY,1747
75
- langfun/core/modalities/video_test.py,sha256=jYuI2m8S8zDCAVBPEUbbpP205dXAht90A2_PHWo4-r8,2039
76
- langfun/core/structured/__init__.py,sha256=zO6mdApZgWy6d2i3s_FWrjHS_-7qWnase0VRq0KhKn0,3589
77
- langfun/core/structured/completion.py,sha256=skBxt6V_fv2TBUKnzFgnPMbVY8HSYn8sY04MLok2yvs,7299
78
- langfun/core/structured/completion_test.py,sha256=MYxEzeScC3gFVujvrMMboBF5nh-QiVLwGgqAV3oaFUQ,19273
79
- langfun/core/structured/description.py,sha256=SXW4MJvshFjbR-0gw6rE21o6WXq12UlRXawvDBXMZFA,5211
80
- langfun/core/structured/description_test.py,sha256=UtZGjSFUaQ6130t1E5tcL7ODu0xIefkapb53TbnqsK8,7362
81
- langfun/core/structured/function_generation.py,sha256=pFgS3vcRAWiuFBol2x5Eeip3XqoudONsOpeJpWyjT3s,7479
82
- langfun/core/structured/function_generation_test.py,sha256=ZJI-aaGgWWszn92u7h5IZ9Pl70N2DgAGGJrIxPzsvwg,10065
83
- langfun/core/structured/mapping.py,sha256=Vq3bQZWi4iYjcVn8D2kvPXTAm9jrQ-_1ueHLbXtGRNQ,12112
84
- langfun/core/structured/mapping_test.py,sha256=PiXklMeIa8L6KtMi3ju7J9Y39gZy0hIGz-Oeq4A_7XE,3835
85
- langfun/core/structured/parsing.py,sha256=keoVqEfzAbdULh6GawWFsTQzU91MzJXYFZjXGXLaD8g,11492
86
- langfun/core/structured/parsing_test.py,sha256=34wDrXaQ-EYhJLfDL8mX9K53oQMSzh5pVYdKjnESmK8,20895
87
- langfun/core/structured/prompting.py,sha256=WICdX_figP2u98Hvg3BFSTF83nNKxM4x1STxkWe2_9Y,7925
88
- langfun/core/structured/prompting_test.py,sha256=vslaCAUikfwOvqsKzqs_oyEacrefFsr2SWSqu6OHi3w,20813
89
- langfun/core/structured/schema.py,sha256=mJXirgqx3N7SA9zBO_ISHrzcV-ZRshLhnMJyCcSjGjY,25057
90
- langfun/core/structured/schema_generation.py,sha256=U3nRQsqmMZg_qIVDh2fiY3K4JLfsAL1LcKzIFP1iXFg,5316
91
- langfun/core/structured/schema_generation_test.py,sha256=RM9s71kMNg2jTePwInkiW9fK1ACN37eyPeF8OII-0zw,2950
92
- langfun/core/structured/schema_test.py,sha256=yw_Uo3xJC3JA9dBDjZdkQdBGPf04e7t1oT9SZTAiSdg,22360
93
- langfun/core/structured/scoring.py,sha256=a3vfGnqf-DOWjD07MF54GCZTO_R1RTxTDVPzerXnU0s,2325
94
- langfun/core/structured/scoring_test.py,sha256=TznLMl0x9QxzmhHz_3Vr44VOXuvFnUSeRQVhu33W5cA,1437
95
- langfun/core/templates/__init__.py,sha256=bO0eMsVJbi7sxEB2YlInKRQ2EVP-RyyKUwcD-8msuN4,927
96
- langfun/core/templates/completion.py,sha256=mUqZHOEV3ag6-A08XghpeEltcrBvCDxXP004eDDfeag,1931
97
- langfun/core/templates/completion_test.py,sha256=vGnjnM38UHyVDUyaUYtmp20s9KBGOdbPVsX-H-ET11E,1636
98
- langfun/core/templates/conversation.py,sha256=HVih0atzCFpYbE-2bmO1VewhKGyKYAoCty-IZYCjf00,2589
99
- langfun/core/templates/conversation_test.py,sha256=RryYyIhfc34dLWOs6GfPQ8HU8mXpKGL87UgVkysfIMo,3911
100
- langfun/core/templates/demonstration.py,sha256=vCrgYubdZM5Umqcgp8NUVGXgr4P_c-fikKhwhzwhpKI,1460
101
- langfun/core/templates/demonstration_test.py,sha256=SafcDQ0WgI7pw05EmPI2S4v1t3ABKzup8jReCljHeK4,2162
102
- langfun/core/templates/selfplay.py,sha256=yhgrJbiYwq47TgzThmHrDQTF4nDrTI09CWGhuQPNv-s,2273
103
- langfun/core/templates/selfplay_test.py,sha256=DYVrkk7uNKCqJGEHH31HssU2BPuMItU1vJLzfcXIlYg,2156
104
- langfun-0.0.2.dev20240429.dist-info/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
105
- langfun-0.0.2.dev20240429.dist-info/METADATA,sha256=2ilR8AAbFugi7GfU5Szd9nOmkThPTNsTrOCOseGc7gQ,3436
106
- langfun-0.0.2.dev20240429.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
107
- langfun-0.0.2.dev20240429.dist-info/top_level.txt,sha256=RhlEkHxs1qtzmmtWSwYoLVJAc1YrbPtxQ52uh8Z9VvY,8
108
- langfun-0.0.2.dev20240429.dist-info/RECORD,,