compass.plugin.one_shot.components.SchemaBasedTextExtractor#
- class SchemaBasedTextExtractor(llm_service, usage_tracker=None, **kwargs)[source]#
Bases:
SchemaOutputLLMCaller,BaseTextExtractorSchema-based text extractor
- Parameters:
llm_service (
Service) – LLM service used for queries.usage_tracker (
UsageTracker, optional) – Optional tracker instance to monitor token usage during LLM calls. By default,None.**kwargs –
Keyword arguments to be passed to the underlying service processing function (i.e.
llm_service.call(**kwargs)). Should not contain the following keys:usage_sub_label
messages
These arguments are provided by this caller object.
Methods
call(sys_msg, content, response_format[, ...])Call LLM for structured data retrieval
Attributes
Identifier for text ingested by this class
Validation output schema
Identifier for final text extracted by this class
Extraction schema
Task description to show in progress bar
ID to use for this extraction for linking with LLM configs
Iterable of parsers provided by this extractor
- property parsers#
Iterable of parsers provided by this extractor
- Yields:
name (
str) – Name describing the type of text output by the parser.parser (
callable()) – Async function that takes atext_chunksinput and outputs parsed text.
- TASK_DESCRIPTION = 'Condensing text for extraction'#
Task description to show in progress bar
- TASK_ID = 'text_extraction'#
ID to use for this extraction for linking with LLM configs
- async call(sys_msg, content, response_format, usage_sub_label=LLMUsageCategory.DEFAULT)#
Call LLM for structured data retrieval
- Parameters:
sys_msg (
str) – The LLM system message. If this text does not contain the instruction text “Return your answer as a dictionary in JSON format”, it will be added.content (
str) – LLM call content (typically some text to extract info from).usage_sub_label (
str, optional) – Label to store token usage under. By default,"default".response_format (
dict) –Dictionary specifying the expected response format. This will be passed to the underlying LLM service (e.g. OpenAI) and should be formatted according to that service’s specifications for structured output. For example, for OpenAI GPT models, this should be a dictionary with the following keys:
type: Should be set to “json_schema” to indicate that the expected output is structured JSON.
json_schema: A dictionary specifying the expected JSON schema of the output. This should include the following keys:
name: A string name for this response format (e.g. “extracted_features”).
strict: A boolean indicating whether the LLM should strictly adhere to the provided schema (i.e. not include any keys not specified in the schema). If True, the LLM will be instructed to only include keys specified in the schema field. If False, the LLM may include additional keys not specified in the schema field.
schema: A dictionary specifying the expected JSON schema of the output. This should be formatted according to JSON Schema specifications, and should define the expected structure of the output JSON object. For example, it may specify that the output should be an object with certain required properties, and the expected data types of those properties.
- Returns:
dict– Dictionary containing the LLM-extracted features. Dictionary may be empty if there was an error during the LLM call.