import json from odoo import models OLLAMA_URL = 'http://192.168.2.10:11434/api/generate' OLLAMA_MODEL = 'llama3.1' class FlAiEngine(models.AbstractModel): """ Phase 5 — Full Ollama integration. Phase 1: Stub service model. This is an AbstractModel — not stored in the database. Used as a service class for AI analysis calls. """ _name = 'fl.ai.engine' _description = 'Family Law AI Analysis Engine (Ollama)' def analyze_case(self, case_id): """ Phase 5 entry point. Full workflow: 1. Rule-based issue tagging 2. Build case context JSON 3. Call Ollama (llama3.1) 4. Parse JSON response 5. Store fl.analysis record """ case = self.env['fl.case'].browse(case_id) analysis = self.env['fl.analysis'].create({ 'case_id': case.id, 'state': 'pending', 'model_used': OLLAMA_MODEL, 'plain_english_summary': ( 'AI analysis not yet implemented. ' 'Full analysis will be available in Phase 5.' ), 'plain_english_summary_es': ( 'El análisis de IA aún no está implementado. ' 'El análisis completo estará disponible en la Fase 5.' ), 'state': 'complete', }) return analysis def _call_ollama(self, prompt): """Call Ollama API and return parsed JSON response.""" try: import requests except ImportError: raise RuntimeError( 'requests library not available. ' 'Install with: pip install requests' ) response = requests.post( OLLAMA_URL, json={ 'model': OLLAMA_MODEL, 'prompt': prompt, 'stream': False, 'options': { 'temperature': 0.1, 'top_p': 0.9, 'num_predict': 2000, }, }, timeout=180, ) response.raise_for_status() raw = response.json().get('response', '{}').strip() # Strip markdown code fences if present if raw.startswith('```'): parts = raw.split('```') raw = parts[1] if len(parts) > 1 else raw if raw.startswith('json'): raw = raw[4:] return json.loads(raw.strip())