LeXtract.Resolver (lextract v0.1.2)

View Source

Parses LLM output into Extraction structs.

Handles conversion from JSON/YAML format to structured Extraction records, including attribute extraction and index parsing.

Supported Formats

List of extractions directly:

[
  {
    "entity": "aspirin",
    "entity_index": 0,
    "entity_attributes": {"dosage": "81mg"}
  }
]

Extractions key:

extractions:
  - medication: "aspirin"
    medication_index: 0
    medication_attributes:
      dosage: "81mg"
      frequency: "daily"

Multiple extraction classes per item:

[
  {
    "person": "John Doe",
    "organization": "Acme Corp",
    "person_index": 0,
    "organization_index": 1
  }
]

Examples

iex> json = ~s([{"person": "John Doe", "person_index": 0}])
iex> {:ok, [extraction]} = LeXtract.Resolver.resolve(json, :json)
iex> extraction.extraction_class
"Person"
iex> extraction.extraction_text
"John Doe"
iex> extraction.extraction_index
0

Summary

Functions

Resolves LLM text output into a list of Extraction structs.

Resolves from already-parsed data (map or list).

Types

resolve_result()

@type resolve_result() :: {:ok, [LeXtract.Extraction.t()]} | {:error, Exception.t()}

Functions

resolve(text, format)

Resolves LLM text output into a list of Extraction structs.

Automatically parses the text in the specified format and converts the structured data into Extraction records.

Examples

iex> json = ~s([{"person": "John Doe", "person_index": 0}])
iex> {:ok, extractions} = LeXtract.Resolver.resolve(json, :json)
iex> length(extractions)
1

iex> yaml = "- medication: aspirin\n  medication_index: 0"
iex> {:ok, extractions} = LeXtract.Resolver.resolve(yaml, :yaml)
iex> hd(extractions).extraction_class
"Medication"

resolve_parsed(parsed_data)

@spec resolve_parsed(term()) :: resolve_result()

Resolves from already-parsed data (map or list).

Useful when data has been parsed elsewhere.

Examples

iex> data = [%{"person" => "Jane", "person_index" => 1}]
iex> {:ok, [extraction]} = LeXtract.Resolver.resolve_parsed(data)
iex> extraction.extraction_text
"Jane"

iex> data = %{"extractions" => [%{"entity" => "test"}]}
iex> {:ok, [extraction]} = LeXtract.Resolver.resolve_parsed(data)
iex> extraction.extraction_class
"Entity"