langtree.core

Submodules

Package Contents

Classes

Buffer

A class to represent a buffer with a fixed length.

Operator

A class to represent and process custom call and parse operations.

Prompt

A class to represent and process prompt templates.

VectorDatabase

Helper class that provides a standard way to create an ABC using

Functions

default_call(*args, **kwargs)

Default call function that returns the provided keyword arguments.

default_parse(output)

Default parse function that returns the provided output without modifications.

freeze(function, **top_kwargs)

Return a function with specific arguments frozen.

render_prompt(template, **kwargs)

Render a template string by substituting placeholders with provided keyword arguments.

class langtree.core.Buffer(length)[source]

A class to represent a buffer with a fixed length.

This buffer allows appending and extending its content while ensuring it never exceeds its defined length. If the buffer’s content exceeds its length, items are removed from the beginning.

property memory

Provide access to the buffer’s memory.

Returns:

The buffer’s memory content.

Return type:

list

append(item)[source]

Append an item to the buffer and ensure the buffer does not exceed its length.

Parameters:

item – The item to append.

extend(item_list)[source]

Extend the buffer with a list of items and ensure the buffer does not exceed its length.

Parameters:

item_list (list) – The list of items to extend the buffer with.

_shrink()[source]

Private method to shrink the buffer to its defined length by removing items from the beginning.

__add__(item)[source]

Handle addition operations with the buffer. Allows for adding a single item or a list of items.

Parameters:

item – An item or a list of items to add.

Returns:

The updated buffer.

Return type:

Buffer

__iadd__(item)[source]

Handle in-place addition operations with the buffer. Allows for adding a single item or a list of items.

Parameters:

item – An item or a list of items to add.

Returns:

The updated buffer.

Return type:

Buffer

__repr__()[source]

Return a representation of the buffer’s memory.

langtree.core.default_call(*args, **kwargs)[source]

Default call function that returns the provided keyword arguments.

Parameters:
  • *args – Variable length argument list.

  • **kwargs – Arbitrary keyword arguments.

Returns:

The provided keyword arguments.

Return type:

dict

langtree.core.default_parse(output)[source]

Default parse function that returns the provided output without modifications.

Parameters:

output – The output to parse.

Returns:

The unmodified output.

langtree.core.freeze(function, **top_kwargs)[source]

Return a function with specific arguments frozen.

Parameters:
  • function (callable) – The function to freeze arguments for.

  • **top_kwargs – Keyword arguments to freeze.

Returns:

The function with specific arguments frozen.

Return type:

callable

class langtree.core.Operator(call=default_call, parse=default_parse)[source]

Bases: object

A class to represent and process custom call and parse operations.

__call__(*args, **kwargs)[source]

Call the Operator’s call function and parse its result.

Parameters:
  • *args – Variable length argument list.

  • **kwargs – Arbitrary keyword arguments.

Returns:

The parsed result of the call function.

freeze_call(**kwargs)[source]

Freeze specific arguments for the Operator’s call function.

Parameters:

**kwargs – Keyword arguments to freeze.

freeze_parse(**kwargs)[source]

Freeze specific arguments for the Operator’s parse function.

Parameters:

**kwargs – Keyword arguments to freeze.

langtree.core.render_prompt(template, **kwargs)[source]

Render a template string by substituting placeholders with provided keyword arguments.

Parameters:
  • template (str) – The template string containing placeholders.

  • **kwargs – Keyword arguments representing placeholder-value pairs.

Returns:

The rendered string after substitutions.

Return type:

str

class langtree.core.Prompt(template)[source]

A class to represent and process prompt templates.

__call__(**kwargs)[source]

Render the prompt using the provided keyword arguments.

Parameters:

**kwargs – Keyword arguments for rendering the prompt.

Returns:

The rendered prompt.

Return type:

str

__add__(other)[source]

Handle concatenation of two Prompt objects or a Prompt object with a string.

Parameters:

other (Prompt, str) – Another Prompt object or a string to concatenate with.

Returns:

A new Prompt object with concatenated templates.

Return type:

Prompt

Raises:

TypeError – If the other object is neither a Prompt nor a string.

class langtree.core.VectorDatabase[source]

Bases: abc.ABC

Helper class that provides a standard way to create an ABC using inheritance.

abstract insert(vector: List[float], metadata: Any, **kwargs) None[source]

Insert a vector into the database with optional metadata and additional parameters.

Parameters:
  • vector (List[float]) – The vector to be inserted.

  • metadata (Any) – Optional metadata associated with the vector.

  • **kwargs – Additional parameters for insertion.

abstract query(vector: List[float], top_k: int, **kwargs) List[Any][source]

Query the database for the ‘top_k’ closest vectors to the provided vector.

Returns a list of matched vectors’ metadata. Accepts additional parameters.

Parameters:
  • vector (List[float]) – The vector used for querying.

  • top_k (int) – Number of closest vectors to return.

  • **kwargs – Additional parameters for querying.

Returns:

List of matched vectors’ metadata.

Return type:

List[Any]

__getattr__(name)[source]

Override Python’s default behavior when an attribute isn’t found.

This can be used to ‘forward’ method calls to the underlying database object.

Parameters:

name (str) – The name of the attribute.

Returns:

The method from the underlying database object.

Return type:

callable