langfun 0.1.2.dev202412150804__py3-none-any.whl → 0.1.2.dev202412180804__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.
@@ -11,7 +11,7 @@
11
11
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
- """Symbolic query."""
14
+ """Query LLM for structured output."""
15
15
 
16
16
  import contextlib
17
17
  import functools
@@ -26,7 +26,7 @@ import pyglove as pg
26
26
 
27
27
 
28
28
  @lf.use_init_args(['schema', 'default', 'examples'])
29
- class QueryStructure(mapping.Mapping):
29
+ class _QueryStructure(mapping.Mapping):
30
30
  """Query an object out from a natural language text."""
31
31
 
32
32
  context_title = 'CONTEXT'
@@ -38,7 +38,7 @@ class QueryStructure(mapping.Mapping):
38
38
  ]
39
39
 
40
40
 
41
- class QueryStructureJson(QueryStructure):
41
+ class _QueryStructureJson(_QueryStructure):
42
42
  """Query a structured value using JSON as the protocol."""
43
43
 
44
44
  preamble = """
@@ -52,10 +52,10 @@ class QueryStructureJson(QueryStructure):
52
52
  1 + 1 =
53
53
 
54
54
  {{ schema_title }}:
55
- {"result": {"_type": "langfun.core.structured.prompting.Answer", "final_answer": int}}
55
+ {"result": {"_type": "langfun.core.structured.query.Answer", "final_answer": int}}
56
56
 
57
57
  {{ output_title}}:
58
- {"result": {"_type": "langfun.core.structured.prompting.Answer", "final_answer": 2}}
58
+ {"result": {"_type": "langfun.core.structured.query.Answer", "final_answer": 2}}
59
59
  """
60
60
 
61
61
  protocol = 'json'
@@ -63,7 +63,7 @@ class QueryStructureJson(QueryStructure):
63
63
  output_title = 'JSON'
64
64
 
65
65
 
66
- class QueryStructurePython(QueryStructure):
66
+ class _QueryStructurePython(_QueryStructure):
67
67
  """Query a structured value using Python as the protocol."""
68
68
 
69
69
  preamble = """
@@ -94,23 +94,22 @@ class QueryStructurePython(QueryStructure):
94
94
 
95
95
  def _query_structure_cls(
96
96
  protocol: schema_lib.SchemaProtocol,
97
- ) -> Type[QueryStructure]:
97
+ ) -> Type[_QueryStructure]:
98
98
  if protocol == 'json':
99
- return QueryStructureJson
99
+ return _QueryStructureJson
100
100
  elif protocol == 'python':
101
- return QueryStructurePython
101
+ return _QueryStructurePython
102
102
  else:
103
103
  raise ValueError(f'Unknown protocol: {protocol!r}.')
104
104
 
105
105
 
106
106
  def query(
107
107
  prompt: Union[str, lf.Template, Any],
108
- schema: Union[
109
- schema_lib.Schema, Type[Any], list[Type[Any]], dict[str, Any], None
110
- ] = None,
108
+ schema: schema_lib.SchemaType | None = None,
111
109
  default: Any = lf.RAISE_IF_HAS_ERROR,
112
110
  *,
113
- lm: lf.LanguageModel | None = None,
111
+ lm: lf.LanguageModel | list[lf.LanguageModel] | None = None,
112
+ num_samples: int | list[int] = 1,
114
113
  examples: list[mapping.MappingExample] | None = None,
115
114
  cache_seed: int | None = 0,
116
115
  response_postprocess: Callable[[str], str] | None = None,
@@ -121,76 +120,207 @@ def query(
121
120
  skip_lm: bool = False,
122
121
  **kwargs,
123
122
  ) -> Any:
124
- """Queries an language model for a (maybe) structured output.
123
+ """Query one or more language models for structured or unstructured outputs.
124
+
125
+ This is the primary API in Langfun for interacting with language models,
126
+ supporting natural language prompts, structured inputs, and multiple advanced
127
+ features.
128
+
129
+ Key Features:
130
+
131
+ - **Input**: Accepts natural language strings, structured inputs (e.g.,
132
+ `pg.Object`), and templates (`lf.Template`) with modality objects.
133
+
134
+ - **Output**: Returns structured outputs when `schema` is specified;
135
+ otherwise, outputs raw natural language (as a string).
136
+
137
+ - **Few-shot examples**: Supports structured few-shot examples with the
138
+ `examples` argument.
139
+
140
+ - **Multi-LM fan-out**: Sends queries to multiple language models with in
141
+ multiple samples in parallel, returning a list of outputs.
125
142
 
126
143
  Examples:
127
144
 
145
+ Case 1: Regular natural language-based LLM query:
146
+
147
+ ```
148
+ lf.query('1 + 1 = ?', lm=lf.llms.Gpt4Turbo())
149
+
150
+ # Outptut: '2'
151
+ ```
152
+
153
+ Case 2: Query with structured output.
154
+
155
+ ```
156
+ lf.query('1 + 1 = ?', int, lm=lf.llms.Gpt4Turbo())
157
+
158
+ # Output: 2
159
+ ```
160
+
161
+ Case 3: Query with structured input.
162
+
163
+ ```
164
+ class Sum(pg.Object):
165
+ a: int
166
+ b: int
167
+
168
+ lf.query(Sum(1, 1), int, lm=lf.llms.Gpt4Turbo())
169
+
170
+ # Output: 2
171
+ ```
172
+
173
+ Case 4: Query with input of mixed modalities.
174
+
128
175
  ```
129
- class FlightDuration:
130
- hours: int
131
- minutes: int
132
-
133
- class Flight(pg.Object):
134
- airline: str
135
- flight_number: str
136
- departure_airport_code: str
137
- arrival_airport_code: str
138
- departure_time: str
139
- arrival_time: str
140
- duration: FlightDuration
141
- stops: int
142
- price: float
143
-
144
- prompt = '''
145
- Information about flight UA2631.
146
- '''
147
-
148
- r = lf.query(prompt, Flight)
149
- assert isinstance(r, Flight)
150
- assert r.airline == 'United Airlines'
151
- assert r.departure_airport_code == 'SFO'
152
- assert r.duration.hour = 7
176
+ class Animal(pg.Object):
177
+ pass
178
+
179
+ class Dog(Animal):
180
+ pass
181
+
182
+ class Entity(pg.Object):
183
+ name: str
184
+
185
+ lf.query(
186
+ 'What is in this {{image}} and {{objects}}?'
187
+ list[Entity],
188
+ lm=lf.llms.Gpt4Turbo()
189
+ image=lf.Image(path='/path/to/a/airplane.png'),
190
+ objects=[Dog()],
191
+ )
192
+
193
+ # Output: [Entity(name='airplane'), Entity(name='dog')]
194
+ ```
195
+
196
+ Case 5: Query with structured few-shot examples.
197
+ ```
198
+ lf.query(
199
+ 'What is in this {{image}} and {{objects}}?'
200
+ list[Entity],
201
+ lm=lf.llms.Gpt4Turbo()
202
+ image=lf.Image(path='/path/to/a/dinasaur.png'),
203
+ objects=[Dog()],
204
+ examples=[
205
+ lf.MappingExample(
206
+ input=lf.Template(
207
+ 'What is the object near the house in this {{image}}?',
208
+ image=lf.Image(path='/path/to/image.png'),
209
+ ),
210
+ schema=Entity,
211
+ output=Entity('cat'),
212
+ ),
213
+ ],
214
+ )
215
+
216
+ # Output: [Entity(name='dinasaur'), Entity(name='dog')]
217
+ ```
218
+
219
+ Case 6: Multiple queries to multiple models.
220
+ ```
221
+ lf.query(
222
+ '1 + 1 = ?',
223
+ int,
224
+ lm=[
225
+ lf.llms.Gpt4Turbo(),
226
+ lf.llms.Gemini1_5Pro(),
227
+ ],
228
+ num_samples=[1, 2],
229
+ )
230
+ # Output: [2, 2, 2]
153
231
  ```
154
232
 
155
233
  Args:
156
- prompt: A str (may contain {{}} as template) as natural language input, or a
157
- `pg.Symbolic` object as structured input as prompt to LLM.
158
- schema: A type annotation as the schema for output object. If str (default),
159
- the response will be a str in natural language.
160
- default: The default value if parsing failed. If not specified, error will
161
- be raised.
162
- lm: The language model to use. If not specified, the language model from
163
- `lf.context` context manager will be used.
164
- examples: An optional list of fewshot examples for helping parsing. If None,
165
- the default one-shot example will be added.
166
- cache_seed: Seed for computing cache key. The cache key is determined by a
167
- tuple of (lm, prompt, cache seed). If None, cache will be disabled for the
168
- query even cache is configured by the LM.
169
- response_postprocess: An optional callable object to process the raw LM
170
- response before parsing it into the final output object. If None, the raw
171
- LM response will not be processed.
172
- autofix: Number of attempts to auto fix the generated code. If 0, autofix is
173
- disabled. Auto-fix is not supported for 'json' protocol.
174
- autofix_lm: The language model to use for autofix. If not specified, the
175
- `autofix_lm` from `lf.context` context manager will be used. Otherwise it
176
- will use `lm`.
177
- protocol: The protocol for schema/value representation. Applicable values
178
- are 'json' and 'python'. By default `python` will be used.
179
- returns_message: If True, returns `lf.Message` as the output, instead of
180
- returning the structured `message.result`.
181
- skip_lm: If True, returns the rendered prompt as a UserMessage object.
182
- otherwise return the LLM response based on the rendered prompt.
183
- **kwargs: Keyword arguments passed to render the prompt or configure the
184
- `lf.structured.Mapping` class. Notable kwargs are:
185
- - template_str: Change the root template for query.
186
- - preamble: Change the preamble for query.
187
- - mapping_template: Change the template for each mapping examle.
234
+ prompt: The input query. Can be:
235
+ - A natural language string (supports templating with `{{}}`),
236
+ - A `pg.Object` object for structured input,
237
+ - An `lf.Template` for mixed or template-based inputs.
238
+ schema: Type annotation or `lf.Schema` object for the expected output.
239
+ If `None` (default), the response will be a natural language string.
240
+ default: Default value to return if parsing fails. If not specified, an
241
+ error will be raised.
242
+ lm: The language model(s) to query. Can be:
243
+ - A single `LanguageModel`,
244
+ - A list of `LanguageModel`s for multi-model fan-out.
245
+ If `None`, the LM from `lf.context` will be used.
246
+ num_samples: Number of samples to generate. If a list is provided, its
247
+ length must match the number of models in `lm`.
248
+ examples: Few-shot examples to guide the model output. Defaults to `None`.
249
+ cache_seed: Seed for caching the query. Queries with the same
250
+ `(lm, prompt, cache_seed)` will use cached responses. If `None`,
251
+ caching is disabled.
252
+ response_postprocess: A post-processing function for the raw LM response.
253
+ If `None`, no post-processing occurs.
254
+ autofix: Number of attempts for auto-fixing code errors. Set to `0` to
255
+ disable auto-fixing. Not supported with the `'json'` protocol.
256
+ autofix_lm: The LM to use for auto-fixing. Defaults to the `autofix_lm`
257
+ from `lf.context` or the main `lm`.
258
+ protocol: Format for schema representation. Choices are `'json'` or
259
+ `'python'`. Default is `'python'`.
260
+ returns_message: If `True`, returns an `lf.Message` object instead of
261
+ the final parsed result.
262
+ skip_lm: If `True`, skips the LLM call and returns the rendered
263
+ prompt as a `UserMessage` object.
264
+ **kwargs: Additional keyword arguments for:
265
+ - Rendering templates (e.g., `template_str`, `preamble`),
266
+ - Configuring `lf.structured.Mapping`.
188
267
 
189
268
  Returns:
190
- The result based on the schema.
269
+ The result of the query:
270
+ - A single output or a list of outputs if multiple models/samples are used.
271
+ - Each output is a parsed object matching `schema`, an `lf.Message` (if
272
+ `returns_message=True`), or a natural language string (default).
191
273
  """
192
274
  # Internal usage logging.
193
275
 
276
+ # Multiple quries will be issued when `lm` is a list or `num_samples` is
277
+ # greater than 1.
278
+ if isinstance(lm, list) or num_samples != 1:
279
+ def _single_query(inputs):
280
+ lm, example_i = inputs
281
+ return query(
282
+ prompt,
283
+ schema,
284
+ default=default,
285
+ lm=lm,
286
+ examples=examples,
287
+ # Usually num_examples should not be large, so we multiple the user
288
+ # provided cache seed by 100 to avoid collision.
289
+ cache_seed=(
290
+ None if cache_seed is None else cache_seed * 100 + example_i
291
+ ),
292
+ response_postprocess=response_postprocess,
293
+ autofix=autofix,
294
+ autofix_lm=autofix_lm,
295
+ protocol=protocol,
296
+ returns_message=returns_message,
297
+ skip_lm=skip_lm,
298
+ **kwargs,
299
+ )
300
+ lm_list = lm if isinstance(lm, list) else [lm]
301
+ num_samples_list = (
302
+ num_samples if isinstance(num_samples, list)
303
+ else [num_samples] * len(lm_list)
304
+ )
305
+ assert len(lm_list) == len(num_samples_list), (
306
+ 'Expect the length of `num_samples` to be the same as the '
307
+ f'the length of `lm`. Got {num_samples} and {lm_list}.'
308
+ )
309
+ query_inputs = []
310
+ total_queries = 0
311
+ for lm, num_samples in zip(lm_list, num_samples_list):
312
+ query_inputs.extend([(lm, i) for i in range(num_samples)])
313
+ total_queries += num_samples
314
+
315
+ samples = []
316
+ for _, output, error in lf.concurrent_map(
317
+ _single_query, query_inputs, max_workers=max(64, total_queries),
318
+ ordered=True,
319
+ ):
320
+ if error is None:
321
+ samples.append(output)
322
+ return samples
323
+
194
324
  # Normalize query schema.
195
325
  # When `lf.query` is used for symbolic completion, schema is automatically
196
326
  # inferred when it is None.
@@ -280,11 +410,52 @@ def query(
280
410
  return output_message if returns_message else _result(output_message)
281
411
 
282
412
 
413
+ #
414
+ # Helper function for map-reduce style querying.
415
+ #
416
+
417
+
418
+ def query_and_reduce(
419
+ prompt: Union[str, lf.Template, Any],
420
+ schema: schema_lib.SchemaType | None = None,
421
+ *,
422
+ reduce: Callable[[list[Any]], Any],
423
+ lm: lf.LanguageModel | list[lf.LanguageModel] | None = None,
424
+ num_samples: int | list[int] = 1,
425
+ **kwargs,
426
+ ) -> Any:
427
+ """Issues multiple `lf.query` calls in parallel and reduce the outputs.
428
+
429
+ Args:
430
+ prompt: A str (may contain {{}} as template) as natural language input, or a
431
+ `pg.Symbolic` object as structured input as prompt to LLM.
432
+ schema: A type annotation as the schema for output object. If str (default),
433
+ the response will be a str in natural language.
434
+ reduce: A function to reduce the outputs of multiple `lf.query` calls. It
435
+ takes a list of outputs and returns the final object.
436
+ lm: The language model to use. If not specified, the language model from
437
+ `lf.context` context manager will be used.
438
+ num_samples: The number of samples to obtain from each language model being
439
+ requested. If a list is provided, it should have the same length as `lm`.
440
+ **kwargs: Additional arguments to pass to `lf.query`.
441
+
442
+ Returns:
443
+ The reduced output from multiple `lf.query` calls.
444
+ """
445
+ results = query(prompt, schema, lm=lm, num_samples=num_samples, **kwargs)
446
+ if isinstance(results, list):
447
+ results = reduce(results)
448
+ return results
449
+
450
+
451
+ #
452
+ # Functions for decomposing `lf.query` into pre-llm and post-llm operations.
453
+ #
454
+
455
+
283
456
  def query_prompt(
284
457
  prompt: Union[str, lf.Template, Any],
285
- schema: Union[
286
- schema_lib.Schema, Type[Any], list[Type[Any]], dict[str, Any], None
287
- ] = None,
458
+ schema: schema_lib.SchemaType | None = None,
288
459
  **kwargs,
289
460
  ) -> lf.Message:
290
461
  """Returns the final prompt sent to LLM for `lf.query`."""
@@ -295,9 +466,7 @@ def query_prompt(
295
466
 
296
467
  def query_output(
297
468
  response: Union[str, lf.Message],
298
- schema: Union[
299
- schema_lib.Schema, Type[Any], list[Type[Any]], dict[str, Any], None
300
- ],
469
+ schema: schema_lib.SchemaType | None = None,
301
470
  **kwargs,
302
471
  ) -> Any:
303
472
  """Returns the final output of `lf.query` from a provided LLM response."""
@@ -308,6 +477,11 @@ def query_output(
308
477
  )
309
478
 
310
479
 
480
+ #
481
+ # Functions for computing reward of an LLM response based on a mapping example.
482
+ #
483
+
484
+
311
485
  def query_reward(
312
486
  mapping_example: Union[str, mapping.MappingExample],
313
487
  response: Union[str, lf.Message],
@@ -362,6 +536,11 @@ def _reward_fn(cls) -> Callable[
362
536
  return _reward
363
537
 
364
538
 
539
+ #
540
+ # Functions for tracking `lf.query` invocations.
541
+ #
542
+
543
+
365
544
  class QueryInvocation(pg.Object, pg.views.HtmlTreeView.Extension):
366
545
  """A class to represent the invocation of `lf.query`."""
367
546