Query#

class langworks.Query#

A Query is a specialised Pypeworks Node wrapping a templatable prompt that may be passed to a LLM.

Fundamentals#

__init__(query: str, role: str | None = None, guidance: str | None = None, history: Thread | None = None, ignore_history: bool = False, context: dict | None = None, ignore_context: bool | Sequence[str] | None = None, include_in_output: Sequence[str] | None = None, middleware: Middleware | None = None, clean_multiline: bool | None = None, **kwargs)#

Sets up a Query object, wrapping a query stated by a human, as well as any guidance to pass to a LLM when prompted with this query. A Query object can be used standalone or in conjunction with a Langwork.

Parameters#

query

The query to prompt the LLM with, optionally formatted using Langworks’ static DSL.

role

The role of the agent stating this query, usually ‘user’, ‘system’ or ‘assistant’.

guidance

Template for the message to be generated, formatted using Langworks’ dynamic DSL.

history

Conversational history (thread) to prepend to the prompt when no such history is available from either the input or the Langwork.

ignore_history

Flag that signals whether any preceeding conversational history should be ignored or not. By default this history is taken into account. If not, effectively a new conversation is started, using the contents of history as specified for this node or the wrapping langwork.

context

Context to reference when filling in the templated parts of the query, guidance and history. In case the Langwork or the input also define a context, the available contexts are merged. When duplicate attributes are observed, the value is copied from the most specific context, i.e. input context over Query context, and Query context over Langwork context.

ignore_context

Controls how contexts passed by argument are handled. By default any context passed overwrites the keys set by Query and Langwork. However, when ignore_context is set, Query blocks this overwriting behaviour, instead maintaining the context as previously specified. When ignore_context is set to True none of the priorly specified keys may be overwritten. Alternatively, if a sequence of keys is passed, only those keys passed are protected from overwriting.

include_in_output

Controls what items from contexts processed are included in the output generated by Query.exec(). By default, when set to None, only a limited context is returned, consisting of the context gathered from the input and the result. However, optionally a list of keys may be specified of keys to include in the output, including those added through context.

middleware

The middleware used to connect to the LLM. If not defined, the Langwork’s middleware is used instead.

clean_multiline

Flag that controls whether or not Query automatically applies clean_multiline() to any query and guidance arguments passed. Overrides whatever value langworks.config.CLEAN_MULTILINE is set to.

Added in version 0.2.0.

kwargs

Any additional arguments to pass to the underlying Pypeworks Node.

Methods#

exec(input: Any | None = None, history: Thread | None = None, context: dict | None = None, params: SamplingParams | None = None, **kwargs) Args[[{'type': Thread, 'name': 'history'}, {'type': <class 'dict'>, 'name': 'context'}]]#

Takes the given history and context, combines it with the histories and contexts included as part of the specification of the Node and Langwork, and forwards it to the middleware, which feeds it to a LLM to generate a response. When a response can be required, a new thread - history - is drawn up, consisting of processed historic messages, as well as the message generated by the LLM. Alongside, a new context is returned, containing any named generated content as specificied by the DSL included in the node’s guidance.

Parameters#

input

Any anonymous input passed to this node, to be included in the context under the key ‘input’.

history

Conversational history (thread) to prepend to the prompt. This history supersedes any history specified by the Node or Langwork, unless either was instructed to ignore the histories provided by inputs or nodes (ignore_history=True).

context

Context used by Langwork’s DSL to fill in any templated parts of any queries, history or guidance included in the prompt passed to the LLM. In case the Node or Langwork also define a context, the available contexts are merged. When duplicate attributes are observed, the value is copied from the most specific context, i.e. input context over Query context, and Query context over Langwork context.

params

Sampling parameters, wrapped by a SamplingParams object, specifying how the LLM should select subsequent tokens.

kwargs

Any key-value arguments to be passed as additional context for use in Langworks’ DSL.