File size: 2,257 Bytes
e684a60
 
6d508c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Models

from typing import Any, List, Dict, Optional
from langchain_community.chat_models import ChatOllama
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage

model_configurations: Dict[str, Dict[str, Any]] = {
    "qwen3:32b": {
        "type": "Ollama",
        "model_name": "qwen3:32b",
        "temperature": 0.3,
    },
    "mistral-small:24b": {
        "type": "Ollama",
        "model_name": "mistral-small:24b",
        "temperature": 0.3,
    },
    "deepseek-r1:32b": {
        "type": "Ollama",
        "model_name": "deepseek-r1:32b",
        "temperature": 0.3,
    }
}

class OllamaWithDebug(ChatOllama):
    def __init__(self, model, **kwargs):
        super().__init__(model=model, **kwargs)
        print(f"--- DEBUG INIT: Model {model} ---")
        print(f"Injected parameters: {kwargs}")
        print("Actual instance attributes:")
        for attr in ['temperature', 'top_p', 'base_url']:
            print(f"  - {attr}: {getattr(self, attr, 'UNDEFINED')}")
        print("-" * 40)

    def invoke(self, messages: Any, **kwargs):
        print(f"\n[DEBUG INVOKE] Sending to {self.model}")
        temp = getattr(self, 'temperature', 'N/A')
        print(f"Active config -> Temp: {temp}")

        preview = str(messages)[:150]
        print(f"Input preview: {preview}...")

        try:
            response = super().invoke(messages, **kwargs)
            print(f"[DEBUG RESPONSE] Success! Response length: {len(str(response.content))} chars")
            return response

        except Exception as e:
            print(f"!!! DEBUG ERROR !!! Ollama call failed: {e}")
            raise

def get_model_instance(model_key: str) -> Any:
    config = model_configurations.get(model_key)
    if config is None:
        raise ValueError(f"Model not configured: {model_key}")

    if config.get("type", "").lower() == "ollama":
        return OllamaWithDebug(
            model=config["model_name"],
            base_url="http://localhost:11434",
            temperature=config.get("temperature", 0.3),
            top_p=config.get("top_p"),
        )
    else:
        raise ValueError(f"Unsupported or missing model type: {config.get('type')}")