LLM model data generation
StructuredLLM
class in Starfish is designed to simplify the interaction with Large Language Models (LLMs) by providing structured outputs. It allows developers to define schemas for the expected output, making it easier to work with LLM responses in a type-safe manner. This class handles prompt rendering, LLM API calls, and response parsing, supporting both synchronous and asynchronous execution. src/starfish/llm/structured_llm.py:23-40
StructuredLLM
offers flexibility by supporting different LLM providers, dynamic prompts using Jinja2 templates, and automatic parsing of responses into structured data formats such as JSON schemas or Pydantic models. This approach ensures that the data returned by the LLM is consistent and easily usable within the application. src/starfish/llm/structured_llm.py:23-40
StructuredLLM
class comprises several key components that work together to provide structured LLM interactions. These include prompt management, LLM proxy calls, and response parsing.
PromptManager
class, which is responsible for rendering dynamic prompts using Jinja2 templates. The prompt can include variables that are passed during runtime, allowing for customized and context-aware LLM interactions. src/starfish/llm/structured_llm.py:103-107
The PromptManager
uses Jinja2 templates, which are rendered with the provided variables. It also handles the conversion of Python f-string-like syntax to Jinja2 syntax. src/starfish/llm/structured_llm.py:35-39, src/starfish/llm/prompt/prompt_loader.py:4
StructuredLLM
class uses a proxy to call the LLM API. Currently, it supports LiteLLM as the default proxy, which allows for integration with various LLM providers. The call_chat_model
function is used to make the API call and retrieve the raw response. src/starfish/llm/structured_llm.py:108, src/starfish/llm/proxy/litellm_adapter.py
StructuredLLM
class supports both JSON schemas and Pydantic models for defining the output structure. The JSONParser
and PydanticParser
classes are used to parse the response accordingly. src/starfish/llm/structured_llm.py:109-112, src/starfish/llm/parser/json_parser.py, src/starfish/llm/parser/pydantic_parser.py
StructuredLLM
class:
StructuredLLM
Classrun
, __call__
) and sync (run_sync
) execution methodsParameter | Type | Description |
---|---|---|
model_name | str | Name of the LLM model (e.g., ‘openai/gpt-4o-mini’) |
prompt | str | Template string in Jinja2 or Python f-string format |
model_kwargs | Optional[Dict[str, Any]] | Additional arguments to pass to the LLM |
output_schema | Optional[Union[List[Dict[str, Any]], Dict[str, Any], type]] | Schema for response parsing (JSON or Pydantic model) |
prompt_template | Optional[str] | Name of partial prompt template to wrap around the main prompt |
strict_parsing | bool | Whether to raise errors on parsing failures (default: False) |
type_check | bool | Whether to check field types against schema (default: False) |
Method | Description |
---|---|
__call__ | Async convenience wrapper for run() |
_prepare_prompt_inputs | Prepares prompt inputs, including schema instructions |
render_prompt | Renders the prompt template with provided parameters |
render_prompt_printable | Returns a printable version of the rendered prompt |
run | Main async method to execute the LLM with provided parameters |
run_sync | Synchronous version of run() (requires nest_asyncio in Jupyter) |
{variable}
are automatically converted to Jinja2 syntax {{ variable }}
process_list_input=True
in run methods to process list argumentsParameter | Description |
---|---|
T | The type of the parsed data (e.g., dict, Pydantic model, or custom type) |