mindxagent_ollama_chat.md · 8.7 KB

MindXAgent Ollama Chat Integration

Overview

MindXAgent now includes persistent Ollama chat capabilities with dynamic model discovery and adaptation. This enables mindXagent to:

Architecture

Components

  1. OllamaChatManager (agents/core/ollama_chat_manager.py)
- Persistent connection management - Dynamic model discovery (periodic refresh every 5 minutes) - Chat conversation history management - Model selection and adaptation - Automatic reconnection on failures

  1. MindXAgent Integration (agents/core/mindXagent.py)
- chat_with_ollama() - Main chat interface - get_available_ollama_models() - List available models - select_ollama_model() - Select best model for task - get_ollama_conversation_history() - Get conversation history - clear_ollama_conversation() - Clear conversation history

Features

1. Persistent Connections

2. Dynamic Model Discovery

3. Chat Functionality

4. Model Selection

Usage

Basic Chat

# Get mindXagent instance
mindx_agent = await MindXAgent.get_instance()

Chat with default model

result = await mindx_agent.chat_with_ollama( message="Analyze the current system state and suggest improvements", temperature=0.7, max_tokens=2000 )

if result.get("success"): print(f"Response: {result['content']}") print(f"Model: {result['model']}") print(f"Latency: {result['latency']:.2f}s")

Chat with Specific Model

result = await mindx_agent.chat_with_ollama(
    message="Explain quantum computing",
    model="mistral-nemo:latest",
    temperature=0.5
)

Chat with System Prompt

result = await mindx_agent.chat_with_ollama(
    message="What should I do next?",
    system_prompt="You are a helpful AI assistant specialized in software development.",
    conversation_id="dev_assistant"
)

Get Available Models

models = await mindx_agent.get_available_ollama_models()
for model in models:
    print(f"Model: {model['name']}")
    print(f"  Size: {model.get('size', 'unknown')}")
    print(f"  Parameters: {model.get('details', {}).get('parameter_size', 'unknown')}")

Select Best Model for Task

# Select best model for reasoning
model = await mindx_agent.select_ollama_model(
    task_type="reasoning",
    preferred_models=["mistral-nemo:latest", "deepseek-r1:latest"]
)

Get Conversation History

history = mindx_agent.get_ollama_conversation_history("dev_assistant")
for msg in history:
    print(f"{msg['role']}: {msg['content'][:100]}...")

Clear Conversation

mindx_agent.clear_ollama_conversation("dev_assistant")

Configuration

Environment Variables

# Ollama server URL
export MINDX_LLM__OLLAMA__BASE_URL="http://10.0.0.155:18080"

Config File

{
  "llm": {
    "ollama": {
      "base_url": "http://10.0.0.155:18080"
    }
  }
}

OllamaChatManager Parameters

Model Discovery

Automatic Discovery

Models are automatically discovered:

New Model Detection

When new models are discovered:

Model Capabilities

Each model's capabilities are tracked:

Conversation Management

Conversation IDs

History Storage

History Structure

{
  "sessions": {
    "mindx_meta_agent_default": [
      {"role": "user", "content": "Hello"},
      {"role": "assistant", "content": "Hi! How can I help?"}
    ]
  },
  "last_saved": "2026-01-17T22:00:00"
}

Task-Based Model Selection

Task Types

Selection Logic

  1. Check preferred models (if provided)
  2. Match task keywords to model names
  3. Prefer smaller models (avoid 30b+ for speed)
  4. Fallback to first available model

Example

# For reasoning task
model = await mindx_agent.select_ollama_model("reasoning")

Prefers: nemo, reasoning, thinking, deepseek models

For coding task

model = await mindx_agent.select_ollama_model("coding")

Prefers: code, codellama, deepseek-coder models

Error Handling

Connection Errors

Model Errors

Timeout Handling

Integration with MindXAgent

Initialization

OllamaChatManager is automatically initialized during mindXagent's _async_init():

Memory Integration

All chat interactions are logged to MemoryAgent:

Thinking Process

Chat requests are logged to thinking process:

Best Practices

  1. Use Conversation IDs: Maintain separate conversations for different contexts
  2. Select Appropriate Models: Use task-based selection for best results
  3. Monitor Model Discovery: Check for new models periodically
  4. Clear Old Conversations: Free memory by clearing unused conversations
  5. Use System Prompts: Provide context for better responses
  6. Adjust Temperature: Lower for factual, higher for creative

Example: Complete Chat Session

# Initialize
mindx_agent = await MindXAgent.get_instance()

Discover models

models = await mindx_agent.get_available_ollama_models() print(f"Available models: {[m['name'] for m in models]}")

Select best model for reasoning

model = await mindx_agent.select_ollama_model("reasoning") print(f"Selected model: {model}")

Start conversation

result = await mindx_agent.chat_with_ollama( message="What are the key principles of self-improving AI systems?", model=model, conversation_id="ai_discussion", system_prompt="You are an expert in AI systems and self-improvement.", temperature=0.7 )

Continue conversation

result = await mindx_agent.chat_with_ollama( message="How can these principles be applied to mindX?", conversation_id="ai_discussion" )

View history

history = mindx_agent.get_ollama_conversation_history("ai_discussion") print(f"Conversation has {len(history)} messages")

Clear when done

mindx_agent.clear_ollama_conversation("ai_discussion")

Future Enhancements


All DocumentsDocument IndexThe Book of mindXImprovement JournalAPI Reference