langdash.chains package#

Submodules#

langdash.chains.chains module#

Classes:

LDChain(ld, nodes[, args, returns])

Class used to represent language chains

LDChainCacheStore(_dict, _model, _model_kwargs)

LDChainCached(model, model_kwargs, **kwargs)

LDResult()

Class for storing the results of inference.

class langdash.chains.chains.LDChain(ld: Langdash, nodes: List[LDNode | str], args: Dict[str, Callable[[str], Any]] | Type = {}, returns: Dict[str, Callable[[str], Any]] | Type = {})[source]#

Bases: object

Class used to represent language chains

Methods:

arg_items()

Returns the item view of the argument dictionary.

arg_type(name)

Return the type of the argument.

cached(model, **model_kwargs)

Cache the chain for a specific model

call(ctx, **kwargs)

Returns data generated from the LLM within the specified session.

return_items()

Returns the item view of the return variable dictionary.

return_type(name)

Return the type of the return variable.

stream(ctx, **kwargs)

Stream data generated from the LLM within the specified session.

arg_items() → ItemsView[str, Callable[[str], Any]][source]#

Returns the item view of the argument dictionary.

arg_type(name: str) → Callable[[str], Any] | None[source]#

Return the type of the argument.

cached(model: str, **model_kwargs) → LDChainCached[source]#

Cache the chain for a specific model

Parameters:

model (str) – The model name

Returns:

The cached chain

call(ctx: str | LLMGenerationSession, **kwargs)[source]#

Returns data generated from the LLM within the specified session.

Parameters:
  • ctx (Union[str, "LLMGenerationSession"]) – The name of the model, or an existing LLM generation session.

  • args (LDNodeArgs) – Arguments to pass to the chain. This will be used by any argument or format nodes.

  • return_session (bool) – Whether or not to return the generation session after generation.

Returns:

The result, or a tuple with (result, session).

return_items() → ItemsView[str, Callable[[str], Any]][source]#

Returns the item view of the return variable dictionary.

return_type(name: str) → Callable[[str], Any] | None[source]#

Return the type of the return variable.

stream(ctx: str | LLMGenerationSession, **kwargs) → Generator[Response, None, None][source]#

Stream data generated from the LLM within the specified session.

Parameters:
  • session (Union[str, "LLMGenerationSession"]) – The name of the model, or an existing LLM generation session.

  • args (LDNodeArgs) – Arguments to pass to the chain. This will be used by any argument or format nodes.

Returns:

Generator yielding response events.

class langdash.chains.chains.LDChainCacheStore(_dict: collections.OrderedDict[FrozenSet[Tuple[str, Any]], langdash.chains.chains._LDChainCacheState], _model: str, _model_kwargs: dict)[source]#

Bases: object

class langdash.chains.chains.LDChainCached(model: str, model_kwargs: dict, **kwargs)[source]#

Bases: LDChain

Methods:

call(**kwargs)

Returns data generated from the LLM within the specified session.

load_cache_store(cache_store)

Loads the cache store from previous inference time.

save_cache_store()

Saves the cache store into an object.

stream(**kwargs)

Stream data generated from the LLM within the specified session.

call(**kwargs)[source]#

Returns data generated from the LLM within the specified session.

Parameters:
  • ctx (Union[str, "LLMGenerationSession"]) – The name of the model, or an existing LLM generation session.

  • args (LDNodeArgs) – Arguments to pass to the chain. This will be used by any argument or format nodes.

  • return_session (bool) – Whether or not to return the generation session after generation.

Returns:

The result, or a tuple with (result, session).

load_cache_store(cache_store: LDChainCacheStore)[source]#

Loads the cache store from previous inference time.

Warning

This function expects that the model data of the parent Langdash instance does not change across session. If it does, a UserWarning is emitted.

Parameters:

cache_store (LDChainCacheStore) – The cache store.

Raises:

ValueError – If the model name of the cache store doesn’t match.

save_cache_store() → LDChainCacheStore[source]#

Saves the cache store into an object.

stream(**kwargs)[source]#

Stream data generated from the LLM within the specified session.

Parameters:
  • session (Union[str, "LLMGenerationSession"]) – The name of the model, or an existing LLM generation session.

  • args (LDNodeArgs) – Arguments to pass to the chain. This will be used by any argument or format nodes.

Returns:

Generator yielding response events.

class langdash.chains.chains.LDResult[source]#

Bases: object

Class for storing the results of inference.

returns#

Mapping of return keys to return values

Type:

Dict[str, Any]

prompt_tokens#

Number of tokens injected to the language model

Type:

int

completion_tokens#

Number of tokens generated by the language model

Type:

int

langdash.chains.nodes module#

Classes:

LDArg(ld, arg[, padleft, padright])

Argument node

LDChoice(ld, returns, choices[, padleft, ...])

Choice node

LDFormatArg(ld, text)

Format argument node

LDNode(ld)

Base class for langdash nodes.

LDRepeat(ld, subchain, append_source, ...[, ...])

Repeat node

LDReturns(ld, returns, end[, padleft, ...])

Return node

LDText(ld, text[, add_special_tokens])

Constant text node

class langdash.chains.nodes.LDArg(ld: Langdash, arg: str, padleft: str = '', padright: str = '')[source]#

Bases: LDNode

Argument node

class langdash.chains.nodes.LDChoice(ld: Langdash, returns: str, choices: str | List[str], padleft: str = '', padright: str = '', argmax: bool = False, return_idx: bool = False)[source]#

Bases: LDNode

Choice node

class langdash.chains.nodes.LDFormatArg(ld: Langdash, text: str)[source]#

Bases: LDNode

Format argument node

class langdash.chains.nodes.LDNode(ld: Langdash)[source]#

Bases: object

Base class for langdash nodes.

class langdash.chains.nodes.LDRepeat(ld: Langdash, subchain: LDChain, append_source: str, append_target: str, end: str = '', end_threshold: int = 5, max_len: int = -1)[source]#

Bases: LDNode

Repeat node

class langdash.chains.nodes.LDReturns(ld: Langdash, returns: str, end: str | int | SpecialToken | List[str] | List[int] | None, padleft: str = '', infer_args: InferArgs | None = None, end_is_token: bool = False, logit_preprocessors: List[LogitPreprocessor] | None = None, maybe_from_args: bool = False)[source]#

Bases: LDNode

Return node

class langdash.chains.nodes.LDText(ld: Langdash, text: str | int | List[int] | SpecialToken, add_special_tokens: bool = False)[source]#

Bases: LDNode

Constant text node

langdash.chains.typing module#

Classes:

Mapping(mapping)

Helper type for mapping string into value.

class langdash.chains.typing.Mapping(mapping: Dict[str, Any])[source]#

Bases: object

Helper type for mapping string into value.

Module contents#