API Reference

Below is a comprehensive API Reference that documents each module's classes, functions, parameters, and usage examples. This reference is intended to help developers understand how to interact with and extend the framework.

1. Agent Core (agent.py)

Class: Agent

Description:
Orchestrates query processing by integrating configuration, memory management, LLM interactions, and tool execution.

Constructor:

Agent(config_path: str, llm_provider: str = "openai")
  • config_path: Path to the JSON configuration file.
  • llm_provider: Identifier for the LLM provider (e.g., "openai", "anthropic", "custom").

Key Methods:

_load_config(config_path: str) -> Dict[str, Any]
Reads and parses the configuration JSON file.

Usage Example:

config = agent._load_config("agent_config.json")

_load_tools(tool_names: List[str]) -> Dict[str, Dict[str, Any]]
Loads tool configuration files from the tools/ directory based on the list of tool names.

_execute_tool(tool_name: str, parameters: Dict[str, Any]) -> Any
Dynamically imports and executes a tool function with the provided parameters.

_create_system_prompt() -> str
Generates a system prompt using configuration data and tool information.

_format_tools_for_llm() -> List[Dict[str, Any]]
Formats tool configurations into the structure required by the LLM provider.

process_query(query: str) -> str
Main method to process a user query:

  • Builds message context (system prompt, conversation memory, and query).
  • Sends messages to the LLM.
  • Detects and executes tool calls.
  • Returns the final response.

Usage Example:

from agent import create_agent, run_agent
agent = create_agent("agent_config.json", llm_provider="openai")
response = run_agent(agent, "Calculate 3 * 5")
print("Agent Response:", response)

Helper Functions:

create_agent(config_path: str, llm_provider: str = "openai") -> Agent
Factory function to instantiate an Agent.

run_agent(agent: Agent, query: str) -> str
Executes the agent's query processing and returns the response.

2. Memory Management (memory.py)

Class: Memory

Description:
Manages basic conversation memory by storing a fixed number of recent messages.

Constructor:

Memory(max_items: int = 10, enabled: bool = True)
  • max_items: Maximum number of messages to retain.
  • enabled: Boolean flag to enable or disable memory.

Methods:

add(role: str, content: str) -> None
Adds a message with the given role and content to the memory.

Usage Example:

mem = Memory(max_items=10, enabled=True)
mem.add("user", "Hello!")

get_messages() -> List[Dict[str, str]]
Returns the list of stored messages.

clear() -> None
Clears all stored messages.

is_enabled() -> bool
Checks if memory is enabled.

set_enabled(enabled: bool) -> None
Enables or disables memory.

3. Semantic Memory (semantic_memory.py)

Class: SemanticMemory

Description:
Extends basic memory by indexing messages with embeddings using SentenceTransformers and FAISS for semantic retrieval.

Constructor:

SemanticMemory(max_items: int = 10, enabled: bool = True)

Methods:

add(role: str, content: str) -> None
Overrides Memory.add to compute and index the embedding of the content.

retrieve_similar(query: str, top_k: int = 3) -> List[Dict[str, Any]]
Retrieves messages semantically similar to the query using FAISS.

Usage Example:

from semantic_memory import SemanticMemory
sem_mem = SemanticMemory(max_items=10, enabled=True)
sem_mem.add("user", "Tell me a joke")
similar = sem_mem.retrieve_similar("I need something funny", top_k=1)
print(similar)

4. LLM Provider Integration (llm_provider.py)

Class: LLMProvider (Abstract Base Class)

Description:
Defines the interface for LLM provider implementations.

Methods:

format_tools(tools: Dict[str, Dict[str, Any]]) -> List[Dict[str, Any]]
Must be implemented by subclasses to format tools for the LLM.

get_response(messages: List[Dict[str, Any]], tools: Optional[List[Dict[str, Any]]]) -> str
Must be implemented to get a response from the LLM.

extract_tool_call(response_text: str) -> Optional[Dict[str, Any]]
Parses the LLM response for JSON formatted tool calls.

Concrete Implementations

  • OpenAIProvider
  • AnthropicProvider
  • CustomLLMProvider (Example)

Usage Example:

from llm_provider import get_llm_provider
llm = get_llm_provider("openai")
messages = [{"role": "system", "content": "You are helpful."},
            {"role": "user", "content": "What's the capital of France?"}]
response = llm.get_response(messages, tools=None)
print(response)

5. Tool Management (tool.py)

Class: ToolManager

Description:
Provides static methods to create and register tool configurations.

Static Methods:

create_tool_config(tool_name: str, description: str, function_path: str, parameters: Dict[str, Any], required_params: List[str] = None, keywords: List[str] = None) -> None
Creates a tool configuration file (JSON) in the tools/ directory.

Usage Example:

from tool import ToolManager
ToolManager.create_tool_config(
    tool_name="sample_tool",
    description="A sample tool.",
    function_path="tools.sample_tool.run",
    parameters={"param": {"type": "string", "description": "A sample parameter"}},
    required_params=["param"]
)

register_basic_tools() -> None
Registers basic tools (e.g., a calculator) by creating configurations and setting up directories.

setup_basic_config() -> None
Registers basic tools and creates an initial agent configuration file (agent_config.json).

6. Retrieval-Augmented Generation (rag.py)

Class: RetrievalAugmentedGeneration

Description:
Indexes external documents and retrieves relevant context using embeddings.

Methods:

index_documents(documents: List[str]) -> None
Processes and indexes a list of documents.

retrieve(query: str, top_k: int = 3) -> List[str]
Retrieves the top-k documents relevant to the query.

Usage Example:

from rag import RetrievalAugmentedGeneration
rag = RetrievalAugmentedGeneration()
documents = ["Document 1 text.", "Document 2 text.", "Document 3 text."]
rag.index_documents(documents)
results = rag.retrieve("Query text", top_k=2)
print(results)

7. Agent Communication (agent_communication.py)

Class: InterAgentCommunicator

Description:
Facilitates sending and receiving messages between agents.

Methods:

send_message(agent_id: str, message: Dict[str, Any]) -> None
Sends a message to another agent.

receive_messages() -> List[Any]
Retrieves all messages from the communication queue.

Usage Example:

from agent_communication import InterAgentCommunicator
comm = InterAgentCommunicator()
comm.send_message("AgentX", {"text": "Hello, AgentX!"})
msgs = comm.receive_messages()
print("Messages:", msgs)

8. Evaluation Framework (evaluation_framework.py)

Class: EvaluationFramework

Description:
Logs performance metrics and evaluates response times.

Methods:

log_metric(name: str, value: Any) -> None
Records a performance metric.

evaluate_response_time(start_time: float) -> float
Computes and logs the time taken for a process.

report() -> Dict[str, Any]
Returns a report of all logged metrics.

Usage Example:

import time
from evaluation_framework import EvaluationFramework
eval_fw = EvaluationFramework()
start = time.time()
time.sleep(0.5)
eval_fw.evaluate_response_time(start)
print("Metrics:", eval_fw.report())

9. Multimodal Support (multimodal_support.py)

Class: MultimodalProcessor

Description:
Provides basic support for processing images and audio.

Methods:

process_image(image_path: str) -> str
Processes an image and returns a description (e.g., dimensions).

process_audio(audio_path: str) -> str
Processes an audio file and returns a placeholder transcription or analysis.

Usage Example:

from multimodal_support import MultimodalProcessor
processor = MultimodalProcessor()
img_info = processor.process_image("path/to/image.jpg")
print("Image Info:", img_info)

10. Streaming Responses (streaming_responses.py)

Function: stream_response

Description:
A generator function that yields chunks of a response for streaming output.

Signature:

def stream_response(response: str, chunk_size: int = 20) -> Generator[str, None, None]
  • response: The complete response text.
  • chunk_size: The size of each chunk (default is 20 characters).

Usage Example:

from streaming_responses import stream_response
for chunk in stream_response("This is a long response that will be streamed.", chunk_size=10):
    print(chunk)

11. Type Safety (type_safety.py)

Functions:

validate_input(value: Any, expected_type: Type, param_name: str = "parameter") -> None
Validates that an input value is of the expected type; raises a TypeError if not.

validate_output(value: Any, expected_type: Type, param_name: str = "output") -> None
Validates that an output value is of the expected type; raises a TypeError if not.

Usage Example:

from type_safety import validate_input, validate_output
validate_input(42, int, "age")
validate_output("Hello", str, "greeting")
print("Type safety validation passed.")

Summary

This API Reference details each module, class, and function available in the framework along with usage examples. It is designed to help you quickly understand how to integrate and extend the framework's capabilities—from core agent functions to advanced features like semantic memory, RAG, and multimodal support.

If you have any questions or need further details about a specific API element, please let us know!