API Reference

Core Components

LLM

class yamllm.core.llm.LLM(config_path)[source]

Main LLM interface class for YAMLLM.

This class handles configuration loading and API interactions with language models.

Parameters:
  • config_path (str) – Path to YAML configuration file

  • api_key (str, optional) – API key for the LLM service

Examples

>>> llm = LLM("config.yaml")
>>> llm.api_key = "your-api-key"
>>> response = llm.query("Hello, world!")
__init__(config_path)[source]

Initialize the LLM instance with the given configuration path.

Parameters:

config_path (str) – Path to the YAML configuration file.

create_embedding(text)[source]

Create an embedding for the given text using OpenAI’s API.

Parameters:

text (str) – The text to create an embedding for.

Returns:

The embedding as bytes.

Return type:

bytes

Raises:

Exception – If there is an error creating the embedding.

find_similar_messages(query, k=5)[source]

Find messages similar to the query.

Parameters:
  • query (str) – The text to find similar messages for.

  • k (int) – Number of similar messages to return. Default is 5.

Returns:

List of similar messages with their metadata and similarity scores.

Return type:

List[Dict[str, Any]]

get_response(prompt, system_prompt=None)[source]

Send a query to the language model and get the response.

Parameters:
  • prompt (str) – The prompt to send to the model.

  • system_prompt (Optional[str]) – An optional system prompt to provide context.

Returns:

The response from the language model.

Return type:

str

Raises:

Exception – If there is an error getting the response from OpenAI.

load_config()[source]

Load configuration from YAML file.

Returns:

Parsed configuration.

Return type:

YamlLMConfig

Raises:
print_settings()[source]

Print the current settings of the LLM (Language Model).

This method outputs the following settings: - Model: The model being used. - Temperature: The temperature setting for the model. - Max Tokens: The maximum number of tokens. - Top P: The top-p sampling parameter. - Frequency Penalty: The frequency penalty parameter. - Presence Penalty: The presence penalty parameter. - Stop Sequences: The stop sequences used by the model. - System Prompt: The system prompt from the configuration context. - Max Context Length: The maximum context length. - Memory Enabled: Whether memory is enabled. - Memory Max Messages: The maximum number of messages in memory. - Output Format: The format of the output. - Output Stream: The output stream. - Tools Enabled: Whether tools are enabled. - Tools: The tools available. - Tools Timeout: The timeout setting for tools.

Return type:

None

query(prompt, system_prompt=None)[source]

Send a query to the language model.

Parameters:
  • prompt (str) – The prompt to send to the model.

  • system_prompt (Optional[str]) – An optional system prompt to provide context.

Returns:

The response from the language model.

Return type:

str

Raises:
  • ValueError – If API key is not initialized or invalid.

  • Exception – If there is an error during the query.

update_settings(**kwargs)[source]

Update the settings of the instance with the provided keyword arguments.

This method iterates over the provided keyword arguments and updates the instance attributes if they exist.

Parameters:
  • **kwargs (Dict[str, Any]) – Keyword arguments where the key is the

  • attribute. (attribute name and the value is the new value for that)

Return type:

None

Configuration

class yamllm.core.config.Config(**data)[source]

Configuration class for YAMLLM.

model

The name of the LLM model to use

Type:

str

temperature

Sampling temperature for text generation

Type:

float

max_tokens

Maximum number of tokens to generate

Type:

int

system_prompt

The system prompt to use

Type:

str

retry_attempts

Number of retry attempts for API calls

Type:

int

timeout

Timeout in seconds for API calls

Type:

int

api_key

API key for the LLM service

Type:

Optional[str]

additional_params

Additional model parameters

Type:

Dict[str, Any]

additional_params: Dict[str, Any]
api_key: Optional[str]
classmethod from_dict(config_dict)[source]

Create configuration from dictionary.

Parameters:

config_dict (Dict[str, Any]) – Configuration dictionary

Returns:

New configuration instance

Return type:

Config

max_tokens: int
model: str
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

retry_attempts: int
system_prompt: str
temperature: float
timeout: int
to_dict()[source]

Convert configuration to dictionary format.

Returns:

Configuration as a dictionary

Return type:

Dict[str, Any]

Memory Management

class yamllm.memory.conversation_store.ConversationStore(db_path='yamllm/memory/conversation_history.db')[source]

A class to manage conversation history stored in a SQLite database. .. attribute:: db_path

The path to the SQLite database file.

type:

str

db_exists() bool[source]

Check if the database file exists.

create_db() None[source]

Create the database and messages table if they don’t exist.

add_message(session_id

str, role: str, content: str) -> int: Add a message to the database and return its ID.

get_messages(session_id

str = None, limit: int = None) -> List[Dict[str, str]]: Retrieve messages from the database.

add_message(session_id, role, content)[source]

Add a message to the database and return its ID.

Parameters:
  • session_id (str) – The ID of the session to which the message belongs.

  • role (str) – The role of the sender (e.g., ‘user’, ‘assistant’).

  • content (str) – The content of the message.

Returns:

The ID of the newly added message in the database.

Return type:

int

create_db()[source]

Create the database and messages table if they don’t exist.

This method establishes a connection to the SQLite database specified by :rtype: None

self.db_path. It then creates a table named messages with the following columns if it does not already exist:

  • id: An integer primary key that auto-increments.

  • session_id: A text field that is not null.

  • role: A text field that is not null.

  • content: A text field that is not null.

  • timestamp: A datetime field that defaults to the current timestamp.

The connection to the database is closed after the table is created.

db_exists()[source]

Check if the database file exists

Return type:

bool

get_messages(session_id=None, limit=None)[source]

Retrieve messages from the database. :type session_id: str :param session_id: The session ID to filter messages by. Defaults to None. :type session_id: str, optional :type limit: int :param limit: The maximum number of messages to retrieve. Defaults to None. :type limit: int, optional

Returns:

A list of messages, where each message is represented as a dictionary

with ‘role’ and ‘content’ keys.

Return type:

List[Dict[str, str]]

class yamllm.memory.conversation_store.VectorStore(vector_dim=1536, store_path='yamllm/memory/vector_store')[source]
__init__(vector_dim=1536, store_path='yamllm/memory/vector_store')[source]

Initializes the ConversationStore object. :type vector_dim: int :param vector_dim: The dimensionality of the vectors to be stored. Default is 1536. :type vector_dim: int :type store_path: str :param store_path: The path to the directory where the vector store and metadata will be saved. Default is “yamllm/memory/vector_store”. :type store_path: str

vector_dim

The dimensionality of the vectors to be stored.

Type:

int

store_path

The path to the directory where the vector store and metadata will be saved.

Type:

str

index_path

The path to the FAISS index file.

Type:

str

metadata_path

The path to the metadata file.

Type:

str

index

The FAISS index for storing vectors.

Type:

faiss.Index

metadata

A list to store message metadata.

Type:

list

The constructor creates the directory if it doesn’t exist, and initializes or loads the FAISS index and metadata.

add_vector(vector, message_id, content, role)[source]

Adds a vector to the index and stores associated metadata. :type vector: List[float] :param vector: The vector to be added. :type vector: List[float] :type message_id: int :param message_id: The unique identifier for the message. :type message_id: int :type content: str :param content: The content of the message. :type content: str :type role: str :param role: The role associated with the message. :type role: str

Return type:

None

Returns:

None

search(query_vector, k=5)[source]

Search for the most similar items in the index based on the provided query vector. :type query_vector: List[float] :param query_vector: The query vector to search for similar items. :type query_vector: List[float] :type k: int :param k: The number of top similar items to return. Defaults to 5. :type k: int, optional

Returns:

A list of dictionaries containing the metadata of the most similar items and their similarity scores.

Return type:

List[Dict[str, Any]]