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:
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.
- get_response(prompt, system_prompt=None)[source]
Send a query to the language model and get the response.
- load_config()[source]
Load configuration from YAML file.
- Returns:
Parsed configuration.
- Return type:
YamlLMConfig
- Raises:
FileNotFoundError – If config file is not found.
ValueError – If config file is empty or could not be parsed.
Exception – For any other unexpected errors.
- 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:
- query(prompt, system_prompt=None)[source]
Send a query to the language model.
- Parameters:
- Returns:
The response from the language model.
- Return type:
- Raises:
ValueError – If API key is not initialized or invalid.
Exception – If there is an error during the query.
Configuration
- class yamllm.core.config.Config(**data)[source]
Configuration class for YAMLLM.
- model
The name of the LLM model to use
- Type:
- temperature
Sampling temperature for text generation
- Type:
- max_tokens
Maximum number of tokens to generate
- Type:
- system_prompt
The system prompt to use
- Type:
- retry_attempts
Number of retry attempts for API calls
- Type:
- timeout
Timeout in seconds for API calls
- Type:
- api_key
API key for the LLM service
- Type:
Optional[str]
- additional_params
Additional model parameters
- Type:
Dict[str, Any]
- 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
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
- 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.
- 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.
- 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
- 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- index
The FAISS index for storing vectors.
- Type:
faiss.Index
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:
- 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]]