LLM chat client namespace.
The OLLMchat namespace provides a complete client library for interacting with Ollama API and OpenAI-compatible REST interfaces. It handles chat conversations, tool calling, streaming responses, history management, and configuration without GTK dependencies, allowing use in both GUI and non-GUI contexts. The namespace includes Client for API communication, Call and Response classes for API operations, Message for chat messages, Tool system for function calling, History for session persistence, Settings for configuration, and Prompt system for agent-based conversations.
* Separation of Concerns: Core logic without GUI dependencies * Tool Integration: Extensible function-calling system with permissions * Streaming Support: Real-time response streaming * History Persistence: Automatic session management * Flexible Configuration: Multiple connection support * Agent System: Pluggable prompt generation
var connection = new Settings.Connection() {
url = "http://127.0.0.1:11434/api"
};
var client = new Client(connection) {
model = "llama3.2"
};
var response = yield client.chat("Hello, how are you?");
print(response.message.content);
var client = new Client(connection) {
model = "llama3.2"
};
// Add a tool
var read_file_tool = new Tools.ReadFile(client);
client.addTool(read_file_tool);
// Chat will automatically use tools when needed
var response = yield client.chat("Read the file README.md");
client.stream = true;
client.message_created.connect((msg, content) => {
if (msg.is_content && msg.is_stream) {
print(content.chat_content);
}
});
yield client.chat("Tell me a story");
var manager = new History.Manager(history_dir, db, client, config);
// Create new session
var session = yield manager.new_session();
// Switch to existing session
yield manager.switch_to_session(existing_session);
// Save current session
yield manager.save_session();
2. Model Selection: Set model from Config2's usage map if available
3. Tool Registration: Add tools before starting chat conversations
4. Permission Checking: Implement ChatPermission.Provider for tool security
5. Error Handling: Wrap API calls in try-catch blocks
6. Streaming: Use message_created signal for real-time updates
7. Session Management: Use History.Manager for persistent conversations