LLM Map¶
- class sycamore.transforms.base_llm.LLMMap(child: ~sycamore.plan_nodes.Node | None, prompt: ~sycamore.llms.prompts.prompts.SycamorePrompt, output_field: str, llm: ~sycamore.llms.llms.LLM, llm_mode: ~sycamore.llms.llms.LLMMode | None = None, iteration_var: str | None = None, validate: ~typing.Callable[[~sycamore.data.document.Document], bool] = <function LLMMap.<lambda>>, max_tries: int = 5, filter: ~typing.Callable[[~sycamore.data.document.Document], bool] = <function LLMMap.<lambda>>, **kwargs)[source]¶
Bases:
MapBatch
The LLMMap transform renders each Document in a docset into a prompt for an LLM, calls the LLM, and attaches the output to the document.
- Parameters:
child -- Child node in the sycamore execution graph
prompt -- The SycamorePrompt to use to render each document. Must implement the
render_document
method.output_field -- The name of the field in doc.properties in which to store the llm output
llm -- The llm to use for inference.
llm_mode -- How to call the llm - sync/async/batch. All LLMs do not necessarily implement all options.
iteration_var -- Name of the document property to increment with every invalid response. Default is None, which means no re-try.
validate -- Function to determine whether an LLM response is valid. Default is 'everything is valid'
max_tries -- Hard limit on the number of LLM calls per document. Default is 5
Example
prompt = EntityExtractorZeroShotGuidancePrompt.set(entity="title") docset.llm_map( prompt=prompt, output_field="title", llm=OpenAI(OpenAIModels.GPT_4O_MINI) )
- class sycamore.transforms.base_llm.LLMMapElements(child: ~sycamore.plan_nodes.Node | None, prompt: ~sycamore.llms.prompts.prompts.SycamorePrompt, output_field: str, llm: ~sycamore.llms.llms.LLM, llm_mode: ~sycamore.llms.llms.LLMMode | None = None, iteration_var: str | None = None, validate: ~typing.Callable[[~sycamore.data.element.Element], bool] = <function LLMMapElements.<lambda>>, max_tries: int = 5, filter: ~typing.Callable[[~sycamore.data.element.Element], bool] = <function LLMMapElements.<lambda>>, **kwargs)[source]¶
Bases:
MapBatch
The LLMMapElements transform renders each Element for each Document in a docset into a prompt for an LLM, calls the LLM, and attaches the output to the element.
- Parameters:
child -- Child node in the sycamore execution graph
prompt -- The SycamorePrompt to use to render each element. Must implement the
render_element
method.output_field -- The name of the field in elt.properties in which to store the llm output.
llm -- The llm to use for inference.
llm_mode -- How to call the llm - sync/async/batch. All LLMs do not necessarily implement all options.
iteration_var -- Name of the element property to increment with every invalid response. Default is None, which means no re-try.
validate -- Function to determine whether an LLM response is valid. Default is 'everything is valid'
max_tries -- Hard limit on the number of LLM calls per element. Default is 5
Example
prompt = TextSummarizerGuidancePrompt docset.llm_map_elements( prompt = prompt, output_field = "summary", llm = OpenAI(OpenAIModels.GPT_4O)