langdash package#
Subpackages#
- langdash.chains package
- langdash.chat package
- langdash.classify package
- langdash.models package
- Submodules
- langdash.models.ctransformers module
CTransformersModelCTransformersSessionCTransformersSession.clone_state()CTransformersSession.context_lengthCTransformersSession.flush_token()CTransformersSession.get_vocab()CTransformersSession.next_token_logits()CTransformersSession.next_token_probs()CTransformersSession.set_state()CTransformersSession.tokenize()CTransformersSession.tokenize_special()
- langdash.models.llama_cpp module
- langdash.models.rwkv_cpp module
- langdash.models.sentence_transformers module
- langdash.models.transformers module
- Module contents
- langdash.search package
Submodules#
langdash.core module#
Classes:
|
Core Langdash instance. |
- class langdash.core.Langdash[source]#
Bases:
objectCore Langdash instance.
Methods:
arg(arg, **kwargs)Creates a new argument node with the specified argument.
chain(**kwargs)Chain a list of nodes together.
choice(returns, choices, **kwargs)Creates a new choice node with the specified choices.
format_args(text, **kwargs)Creates a format argument node.
model_from_type(type, *args, **kwargs)Create an instance of a builtin model with specified type name.
register_model(name, model)Register a new language model to the Langdash instance.
repeat(**kwargs)Creates a new repetition node that repeats a subchain.
returns(returns, end, **kwargs)Create a new return node for the specified return value.
session_for_model(model, **kwargs)Create a new session for a given model.
text(text, **kwargs)Creates a raw text node.
- arg(arg, **kwargs)[source]#
Creates a new argument node with the specified argument.
- Parameters:
arg (str) – The argument.
padleft (str) – The padding string to use for the left side of the argument.
padright (str) – The padding string to use for the right side of the argument.
- Returns:
The argument node.
- chain(**kwargs) LDChain[source]#
Chain a list of nodes together.
- Parameters:
nodes (List[Union["LDNode", str]]) – A list of nodes or constant text nodes (represented by strings) to chain together.
args (TypeDict) – A dictionary of argument types for the chain function.
returns (TypeDict) – A dictionary of return value types for the chain function.
- Returns:
The chain of nodes.
- choice(returns, choices, **kwargs)[source]#
Creates a new choice node with the specified choices.
- Parameters:
returns (str) – The name of the return value.
choices (Union[str, List[str]]) – Either the list of choice strings or the name of the argument containing the list.
padleft (str) – Left padding for every choice string. Defaults to empty string.
padright (str) – Right padding for every choice string. Defaults to empty string.
- Returns:
The choice node.
- format_args(text, **kwargs)[source]#
Creates a format argument node.
- Parameters:
text (str) – The format text.
- Returns:
The format text node.
- static model_from_type(type: str, *args, **kwargs) LLM[source]#
Create an instance of a builtin model with specified type name.
Additional arguments will be passed to the model constructor. Alternatively, a keyword argument with the name config of type LLMConfig can be passed to specify config parameters.
- Parameters:
type (str) – The type of the model.
- Returns:
The model.
- register_model(name: str, model: LLM)[source]#
Register a new language model to the Langdash instance.
- Parameters:
name (str) – The name of the model.
model (LLM) – The LLM object.
- repeat(**kwargs)[source]#
Creates a new repetition node that repeats a subchain.
- Parameters:
subchain (LDChain) – The subchain to be repeated.
append_source (str) – The return variable of the subchain, extracted into the parent chain’s append_target list.
append_target (str) – The append target variable of the parent chain.
end (str) – Token used to mark the end of the repetition. This token will not be injected as a prompt after the loop.
max_len (int) – Maximum number of repetitions. -1 means the chain will repeat until the next most likely token is end.
end_threshold (float) – Minimum probability of end token for the repetition to end.
- Returns:
The repeat node.
- returns(returns, end, **kwargs)[source]#
Create a new return node for the specified return value.
- Parameters:
returns (str) – The name of the return value.
end (InferEndSequence) – Where to stop the inference. See LLMGenerationSession#infer for more details.
padleft (str) –
The left padding value for the return. If the generated string starts with padleft then it will be stripped.
Defaults to empty string.
infer_args (Optional[InferArgs]) – Optional inference arguments for generation. Defaults to None.
end_is_token (bool) – If true, then the end string will be interpreted as a token. Defaults to False.
logit_preprocessors (Optional[List[LogitPreprocessor]]) – Optional list of logit preprocessor functions to be called before sampling. Defaults to None.
- Returns:
The return node.
- session_for_model(model: str, **kwargs) LLMSession[source]#
Create a new session for a given model.
- Parameters:
model (str) – The name of the model to be used.
default_infer_args (InferArgs) – Default arguments for the inference.
token_healing (bool) – Whether to enable token healing. Defaults to True.
event_handlers (Optional[LLMGenerationSessionEvents]) – Optional event handlers for prompt events. Defaults to None.
initial_prompt (Optional[Union[str, int, List[int]]]) – Optional initial prompt to be injected. Defaults to None.
- Returns:
The session object.
langdash.infer module#
Classes:
|
Data class for inference arguments. |
- class langdash.infer.InferArgs(min_new_tokens: int = 0, max_new_tokens: int = 512, temperature: float = 1.0, top_k: int = 0, top_p: float = 0.0, typical_mass: float = 0.0, max_rep_ctx: int = 64, rep_penalty: float = 1.0, rng: torch.Generator | None = None)[source]#
Bases:
objectData class for inference arguments.
Attributes:
Maximum number of new tokens to generate
Maximum number of tokens to look back for repetition penalty.
Minimum number of new tokens to generate.
Repetition penalty, applied to logits for every repeated token before sampling.
The random number generator (torch.Generator) to be used for token sampling.
Temperature
Top-K parameter.
Top-P parameter.
Typical mass parameter.
- max_new_tokens: int = 512#
Maximum number of new tokens to generate
- max_rep_ctx: int = 64#
Maximum number of tokens to look back for repetition penalty.
- min_new_tokens: int = 0#
Minimum number of new tokens to generate.
If this is set, the end argument passed to the session’s infer method will be interpreted as a single token. Special tokens count as an end token.
- rep_penalty: float = 1.0#
Repetition penalty, applied to logits for every repeated token before sampling.
- rng: torch.Generator | None = None#
The random number generator (torch.Generator) to be used for token sampling. Defaults to None for the global RNG.
- temperature: float = 1.0#
Temperature
- top_k: int = 0#
Top-K parameter. If set, generation uses top-K sampling method. Works with top_p.
- top_p: float = 0.0#
Top-P parameter. If set, generation uses top-P sampling method.
- typical_mass: float = 0.0#
Typical mass parameter. If set, generation will use typical sampling.
langdash.llm module#
Classes:
A language model class for generating embeddings. |
|
|
A language model class for inference. |
|
Flag representing the capabilities of a language model. |
- class langdash.llm.EmbeddingLLM[source]#
Bases:
LLM[T_LLMEmbeddingSession]A language model class for generating embeddings.
Attributes:
Returns the capability of the language model.
- property capability: LLMCapability#
Returns the capability of the language model.
- class langdash.llm.LLM[source]#
Bases:
Generic[T_LLMSession]A language model class for inference.
Attributes:
Returns the capability of the language model.
Methods:
session(*args, **kwargs)Create a new session for the given model.
- property capability: LLMCapability#
Returns the capability of the language model.
- session(*args, **kwargs) T_LLMSession[source]#
Create a new session for the given model.
- Parameters:
default_infer_args (InferArgs) – Default arguments for the inference.
token_healing (bool) – Whether to enable token healing. Defaults to True.
event_handlers (Optional[LLMGenerationSessionEvents]) – Event handlers for prompt events. Defaults to None.
- Returns:
A new session object.
langdash.llm_config module#
Classes:
|
Data class used to store unified LLM config. |
- class langdash.llm_config.LLMConfig(model: str | None = None, batch_size: int = -1, threads: int = -1, context_length: int = -1, gpu_layers: int = 0)[source]#
Bases:
objectData class used to store unified LLM config.
- model#
The name, or the path of the model. Defaults to None.
- Type:
Optional[str]
- batch_size#
The number of tokens per batch size for inference. Defaults to -1 for auto.
- Type:
int
- threads#
The number of threads used for inference. Defaults to -1 for auto.
- Type:
int
- context_length#
The number of tokens used for context. Defaults to -1 for auto.
- Type:
int
- gpu_layers#
The number of layers to run on GPU. Defaults to 0.
- Type:
int
langdash.llm_session module#
Classes:
|
Session for a language model that outputs an embedding for raw text. |
|
Generation session for a language model. |
|
Generation session for a language model that processes raw text. |
|
Base class for a session for a language model. |
|
A state class for a language model. |
- class langdash.llm_session.LLMEmbeddingSession(llm: T_LLM, ld: Langdash)[source]#
Bases:
LLMSession,Generic[T_LLM]Session for a language model that outputs an embedding for raw text.
Methods:
embed(documents)Infer the embedding of a list of text.
Attributes:
Returns the embedding size of the model.
- embed(documents: List[str]) torch.Tensor[source]#
Infer the embedding of a list of text.
- Parameters:
documents (List[str]) – The text to be embedded.
- Returns:
The embedding vector of the list of text.
- property embedding_size: int#
Returns the embedding size of the model.
- class langdash.llm_session.LLMGenerationSession(llm: T_LLM, ld: Langdash, default_infer_args: InferArgs = InferArgs(min_new_tokens=0, max_new_tokens=512, temperature=1.0, top_k=0, top_p=0.0, typical_mass=0.0, max_rep_ctx=64, rep_penalty=1.0, rng=None), token_healing: bool = True, event_handlers: LLMGenerationSessionEvents | None = None, initial_prompt: str | int | List[int] | None = None)[source]#
Bases:
LLMSession,Generic[T_LLM,T_LLMState]Generation session for a language model.
Methods:
clone()Clone the current session.
Clone the current state of the language model.
Returns a mapping of token strings to their respective ids.
infer([args])Infer the next tokens from the input sequence.
Returns the logits for next token.
Returns the probabilities for next token.
Returns a context manager that manages setting and unloading temporary state.
set_state(state)Set the state of the language model.
tokenize(text[, add_special_tokens])Tokenize the given text into a list of tokens.
tokenize_special(text)Tokenize the given text as a special token.
Attributes:
Returns the context length of the model, or zero for models that don't support one.
Default inference arguments.
Optional list of event handlers.
Whether token healing is enabled for this session.
Total number of tokens used.
- clone() LLMGenerationSession[source]#
Clone the current session.
- Returns:
A new generation session with the same arguments.
- clone_state() T_LLMState | None[source]#
Clone the current state of the language model.
- Returns:
The current state as an object.
- property context_length: int#
Returns the context length of the model, or zero for models that don’t support one.
- event_handlers: LLMGenerationSessionEvents | None#
Optional list of event handlers.
- get_vocab() Dict[str | bytes, int][source]#
Returns a mapping of token strings to their respective ids.
- infer(args: InferArgs | None = None, **kwargs) Generator[Response, None, None][source]#
Infer the next tokens from the input sequence.
- Parameters:
end (InferEndSequence) –
The end of the output sequence.
If is string:
if end_is_token is set to True then inference will stop when the generated text ends with end.
otherwise, inference will stop when the token end is encountered.
If is int, then inference will stop at the token with ID of end.
If is List[str] and end_is_token set to False, then inference will stop when the generated text ends with any string in end.
If is List[str] (with end_is_token set to True) or List[int], then inference will stop when token matches any in the list.
If set to None, the output sequence will be generated until the maximum number of tokens is reached.
Empty strings are interpreted as end of text token.
Warning
If end is interpreted as a token, then the token will not be injected into the session.
Defaults to None.
args (Optional[InferArgs]) – Optional inference parameters. Defaults to None.
end_is_token (bool) – If true, then the end string will be interpreted as a token. Defaults to False.
logit_preprocessors (Optional[List[Type[LogitPreprocessor]]]) – Optional list of logit preprocessor classes to be called before sampling. Defaults to None.
- Returns:
Generator yielding inference events.
- scratch_state() LLMGenerationSessionStateManager[source]#
Returns a context manager that manages setting and unloading temporary state.
- set_state(state: T_LLMState | None)[source]#
Set the state of the language model.
- Parameters:
state (Optional[T_LLMState]) – The state of the language model, or None to clear the state.
- token_healing: bool#
Whether token healing is enabled for this session.
- tokenize(text: str, add_special_tokens: bool = False) List[int][source]#
Tokenize the given text into a list of tokens.
- Parameters:
text (str) – The text to tokenize.
add_special_tokens (bool) – Whether to add special tokens to the output.
- Returns:
The list of tokens.
- tokenize_special(text: str) int | None[source]#
Tokenize the given text as a special token.
- Parameters:
text (str) – The text to tokenize.
- Returns:
The token id, or None if the text doesn’t correspond to a special token.
- tokens_used: int#
Total number of tokens used. Does not decrease unless session is reset.
- class langdash.llm_session.LLMGenerationSessionForRawText(*args, **kwargs)[source]#
Bases:
LLMGenerationSession,Generic[T_LLM,T_LLMState]Generation session for a language model that processes raw text.
Methods:
Flushes the previous token into the language model if healing is enabled.
next_token_probs(*args, **kwargs)Returns the probabilities for next token.
langdash.logit_preprocessors module#
Classes:
|
Base class for a logit preprocessor. |
- class langdash.logit_preprocessors.LogitPreprocessor(session: LLMGenerationSession)[source]#
Bases:
ABCBase class for a logit preprocessor.
langdash.response module#
Classes:
|
Choice response. |
|
Inference response. |
|
|
|
Injected response. |
|
Returns response. |
|
Subchain response. |
|
Base class for responses from language model. |
- class langdash.response.RespChoice(choice: int, tokens_counter: int)[source]#
Bases:
ResponseChoice response.
- class langdash.response.RespInfer(tokid: int, tokstr: str, running_infer: str)[source]#
Bases:
ResponseInference response. Will be emitted on every token generated.
- tokid#
Token ID
- Type:
int
- tokstr#
Token string representation
- Type:
str
- running_infer#
Current generated string
- Type:
str
- class langdash.response.RespInferEnd(running_infer: str, tokens_counter: int)[source]#
Bases:
Response
- class langdash.response.RespInject(tokens_counter: int)[source]#
Bases:
ResponseInjected response. Will be emitted every time new context is injected.
- tokens_counter#
Number of tokens injected to model
- Type:
int
- class langdash.response.RespReturns(key: str)[source]#
Bases:
ResponseReturns response. Acts as a header for return values.
- key#
Return key
- Type:
str
langdash.sampling module#
Functions:
|
Converts logit tensor to probability tensor using softmax. |
|
Sample from a distribution of tokens specified by logits. |
- langdash.sampling.logits_to_probs(logits: torch.Tensor)[source]#
Converts logit tensor to probability tensor using softmax.
- langdash.sampling.sample(logits: torch.Tensor, args: InferArgs, ctx: Sequence[int]) int[source]#
Sample from a distribution of tokens specified by logits.
- Parameters:
logits (torch.FloatTensor) – Logits to sample from.
args (InferArgs) – Sampling arguments.
ctx (Sequence[int]) – Sequence of tokens generated so far.
- Returns:
The token sampled.