File size: 14,348 Bytes
914512c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df5dcd7
 
914512c
 
df5dcd7
914512c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
"""bioai.agent.ollama_client -- thin local Ollama API client.

Drop-in replacement for the Fireworks client that talks to a local Ollama
server (https://ollama.com) instead of a remote API. This eliminates the
need for any cloud LLM credits: Ollama runs on the same machine as the
pipeline (laptop, AMD cloud VM, or Docker container) and serves the Llama
3.2 3B model locally.

Setup (one-time):
    1. Install Ollama:  curl -fsSL https://ollama.com/install.sh | sh
    2. Pull the model:  ollama pull llama3.2:3b
    3. Start the server: ollama serve   (or it auto-starts on demand)

The client uses ``requests`` only (no ollama-python SDK dependency -- keeps
the install small and portable). Caches every response to
``<project_root>/.ollama_cache/`` keyed by ``sha256(prompt)`` with a 24-hour
TTL so demo runs don't re-spend compute when the prompts are identical.

If Ollama is unreachable (e.g., server not running, model not pulled), the
constructor raises a clear ``RuntimeError`` -- the orchestrator catches this
and falls back to a degraded-mode response so the demo still runs end-to-end
without a working Ollama server.

Env vars (all optional):
    OLLAMA_HOST       Override the Ollama server URL (default http://localhost:11434)
    OLLAMA_MODEL      Override the model name (default llama3.2:3b)
"""

from __future__ import annotations

import hashlib
import json
import os
import time
from pathlib import Path
from typing import Dict, List, Optional

import requests

# --------------------------------------------------------------------------- #
# Constants
# --------------------------------------------------------------------------- #
DEFAULT_OLLAMA_HOST = os.environ.get("OLLAMA_HOST", "http://localhost:11434")
DEFAULT_MODEL = os.environ.get("OLLAMA_MODEL", "llama3.2:3b")
CACHE_TTL_SECONDS = 24 * 60 * 60  # 24 hours

# Portable cache dir (resolved from bioai.paths so it's not hardcoded)
from bioai.paths import CACHE_DIR as _PROJECT_CACHE_DIR  # noqa: E402
CACHE_DIR = _PROJECT_CACHE_DIR.parent / ".ollama_cache"


# --------------------------------------------------------------------------- #
# OllamaClient
# --------------------------------------------------------------------------- #
class OllamaClient:
    """Local Ollama chat-completions client with disk caching.

    Same API surface as the old FireworksClient so it's a drop-in replacement.

    Parameters
    ----------
    model:
        Ollama model tag. Defaults to ``llama3.2:3b``.
    host:
        Ollama server URL. Defaults to ``OLLAMA_HOST`` env var or
        ``http://localhost:11434``.
    cache_dir:
        Where to store cached responses.
    cache_ttl:
        Cache time-to-live in seconds (default 24 hours).
    timeout:
        HTTP timeout per request, in seconds. Local Ollama is fast for 3B
        but cold-start of the model can take 10-15s, so default is 120.
    """

    def __init__(
        self,
        model: Optional[str] = None,
        host: Optional[str] = None,
        cache_dir: Path | str = CACHE_DIR,
        cache_ttl: int = CACHE_TTL_SECONDS,
        timeout: int = 120,
    ):
        self.model = model or DEFAULT_MODEL
        self.host = (host or DEFAULT_OLLAMA_HOST).rstrip("/")
        self.cache_dir = Path(cache_dir)
        self.cache_dir.mkdir(parents=True, exist_ok=True)
        self.cache_ttl = cache_ttl
        self.timeout = timeout
        # Reuse a session for connection pooling across calls.
        self._session = requests.Session()
        # Probe the server once at construction; raise if unreachable so the
        # orchestrator can fall back to degraded mode.
        self._verify_server()

    def _verify_server(self) -> None:
        """Hit ``GET /api/tags`` to confirm Ollama is running and the model is available.

        Raises RuntimeError with a helpful message on failure.
        Uses a short timeout (3s) so the demo doesn't hang when Ollama isn't running
        (e.g., on HuggingFace Spaces where Ollama can't be installed).
        """
        try:
            resp = self._session.get(f"{self.host}/api/tags", timeout=3)
            if resp.status_code != 200:
                raise RuntimeError(
                    f"Ollama server at {self.host} returned HTTP {resp.status_code}. "
                    f"Is `ollama serve` running?"
                )
            data = resp.json()
            available = {m.get("name", "") for m in data.get("models", [])}
            # Some Ollama versions append :latest, some don't. Check both.
            candidates = {self.model, self.model.split(":")[0], self.model + ":latest"}
            if not (available & candidates):
                # Don't hard-fail: the model might be pullable on demand. Just warn.
                print(
                    f"[ollama] WARNING: model '{self.model}' not found in available models "
                    f"({sorted(available)[:5]}...). Attempting to use it anyway -- "
                    f"run `ollama pull {self.model}` if the call fails."
                )
        except requests.exceptions.ConnectionError as e:
            raise RuntimeError(
                f"Cannot reach Ollama server at {self.host}. "
                f"Start it with `ollama serve` or set OLLAMA_HOST env var. "
                f"Original error: {e}"
            ) from e
        except Exception as e:
            raise RuntimeError(
                f"Ollama server probe failed: {type(e).__name__}: {e}"
            ) from e

    # ------------------------------------------------------------------ #
    # Low-level chat
    # ------------------------------------------------------------------ #
    def chat(
        self,
        messages: List[Dict[str, str]],
        temperature: float = 0.7,
        max_tokens: int = 2048,
    ) -> str:
        """Send a chat-completions request. Returns the assistant message text.

        ``messages`` is the standard OpenAI-style list of
        ``{"role": ..., "content": ...}`` dicts.

        Uses Ollama's OpenAI-compatible endpoint ``/v1/chat/completions`` so
        the request/response shape matches what we used for Fireworks.
        """
        cache_key = self._cache_key(messages, temperature, max_tokens)
        cached = self._cache_get(cache_key)
        if cached is not None:
            print(
                f"[ollama] CACHE HIT  model={self.model} "
                f"prompt_len={sum(len(m['content']) for m in messages)} "
                f"response_len={len(cached)}"
            )
            return cached

        # Use the OpenAI-compatible endpoint (Ollama supports both /api/chat
        # and /v1/chat/completions; the latter matches Fireworks' shape so we
        # don't need to change the response parsing).
        payload = {
            "model": self.model,
            "messages": messages,
            "temperature": temperature,
            "max_tokens": max_tokens,
            "stream": False,
        }
        prompt_len = sum(len(m["content"]) for m in messages)
        print(f"[ollama] API CALL  model={self.model} prompt_len={prompt_len}")
        t0 = time.time()
        resp = self._session.post(
            f"{self.host}/v1/chat/completions",
            data=json.dumps(payload),
            timeout=self.timeout,
            headers={"Content-Type": "application/json"},
        )
        dt = time.time() - t0
        if resp.status_code != 200:
            raise RuntimeError(
                f"Ollama API returned {resp.status_code}: {resp.text[:500]}"
            )
        data = resp.json()
        text = (
            data.get("choices", [{}])[0]
            .get("message", {})
            .get("content", "")
        )
        # Cache and log
        self._cache_set(cache_key, text)
        print(
            f"[ollama] API OK   model={self.model} "
            f"response_len={len(text)} elapsed={dt:.2f}s cached=False"
        )
        return text

    # ------------------------------------------------------------------ #
    # High-level helpers (identical to FireworksClient)
    # ------------------------------------------------------------------ #
    def parse_pest_report(self, user_text: str) -> Dict:
        """Extract pest species, crop, severity, location from free text.

        Returns a dict with keys ``pest_species``, ``crop``, ``severity``,
        ``location``. On parse failure, returns a dict with ``_raw`` set to
        the raw model output and best-effort defaults.
        """
        system_prompt = (
            "You are an agricultural pest identification assistant. "
            "Extract structured data from the user's pest report and return "
            "STRICT JSON ONLY (no markdown fences, no commentary) with keys: "
            '"pest_species" (string, MUST be the scientific name in lowercase '
            'with underscores, e.g. "nilaparvata_lugens" for brown planthopper, '
            '"spodoptera_frugiperda" for fall armyworm, "schistocerca_gregaria" '
            'for desert locust, "chilo_suppressalis" for striped stem borer, '
            '"myzus_persicae" for peach-potato aphid, '
            '"leptinotarsa_decemlineata" for Colorado potato beetle, '
            '"bemisia_tabaci" for tobacco whitefly), '
            '"crop" (string), "severity" (one of '
            '"low","moderate","high","severe"), "location" (string), '
            '"notes" (string, optional). If a field is unknown, use null.'
        )
        messages = [
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": user_text},
        ]
        raw = self.chat(messages, temperature=0.1, max_tokens=512)
        try:
            parsed = json.loads(raw)
        except json.JSONDecodeError:
            # Try to find a JSON block in the response.
            import re
            m = re.search(r"\{.*\}", raw, re.DOTALL)
            if m:
                try:
                    parsed = json.loads(m.group(0))
                except json.JSONDecodeError:
                    parsed = {}
            else:
                parsed = {}
        # Ensure all expected keys exist
        for key in ("pest_species", "crop", "severity", "location"):
            parsed.setdefault(key, None)
        parsed["_raw"] = raw
        return parsed

    def generate_safety_card(
        self,
        sirna_seq: str,
        offtarget_risks: Dict[str, float],
        half_life_hours: float,
    ) -> str:
        """Generate a markdown safety card for one siRNA candidate."""
        ot_str = "\n".join(
            f"  - {sp}: {risk:.3f}" for sp, risk in offtarget_risks.items()
        ) or "  (no off-target hits detected)"
        system_prompt = (
            "You are a regulatory toxicology writer. Produce a concise "
            "markdown SAFETY CARD for a dsRNA-based biopesticide siRNA "
            "candidate. Use only the data provided. Do not invent numbers. "
            "Sections: Sequence, Off-Target Profile, Environmental Fate, "
            "Overall Risk Tier (low/moderate/high). Keep it under 200 words."
        )
        user_prompt = (
            f"siRNA sequence (21 nt): {sirna_seq}\n\n"
            f"Off-target risks per species (0..1, fraction of 21-mers hit):\n{ot_str}\n\n"
            f"Predicted environmental half-life: {half_life_hours:.2f} hours\n"
        )
        messages = [
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": user_prompt},
        ]
        return self.chat(messages, temperature=0.3, max_tokens=800)

    def generate_regulatory_memo(
        self,
        pest_species: str,
        candidates: List[Dict],
    ) -> str:
        """Generate an EPA-style regulatory memo summarising the top candidates.

        Each candidate dict should contain at least ``sirna_seq``,
        ``efficacy``, ``offtarget_max``, ``half_life_hours``, ``final_score``.
        """
        cand_lines = []
        for i, c in enumerate(candidates, start=1):
            cand_lines.append(
                f"  {i}. {c.get('sirna_seq', '?')}  "
                f"efficacy={c.get('efficacy', 0):.3f}  "
                f"offtarget_max={c.get('offtarget_max', 0):.3f}  "
                f"half_life={c.get('half_life_hours', 0):.2f}h  "
                f"score={c.get('final_score', 0):.3f}"
            )
        cand_block = "\n".join(cand_lines) or "  (no candidates provided)"

        system_prompt = (
            "You are an EPA FIFRA regulatory affairs consultant. Produce a "
            "concise markdown MEMO (under 400 words) recommending whether "
            "the listed dsRNA biopesticide candidates are suitable for an "
            "experimental use permit against the named pest. Sections: "
            "Pest & Crop, Candidate Summary, Risk Assessment, Recommendation. "
            "Be conservative; if any candidate has high off-target risk or "
            "very short half-life, flag it."
        )
        user_prompt = (
            f"Pest species: {pest_species}\n\n"
            f"Top candidates (sorted by final_score):\n{cand_block}\n"
        )
        messages = [
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": user_prompt},
        ]
        return self.chat(messages, temperature=0.3, max_tokens=1500)

    # ------------------------------------------------------------------ #
    # Cache helpers (identical to FireworksClient)
    # ------------------------------------------------------------------ #
    def _cache_key(
        self,
        messages: List[Dict[str, str]],
        temperature: float,
        max_tokens: int,
    ) -> str:
        blob = json.dumps(
            {"model": self.model, "messages": messages,
             "temperature": temperature, "max_tokens": max_tokens},
            sort_keys=True,
        )
        return hashlib.sha256(blob.encode("utf-8")).hexdigest()

    def _cache_get(self, key: str) -> Optional[str]:
        path = self.cache_dir / f"{key}.txt"
        if not path.exists():
            return None
        age = time.time() - path.stat().st_mtime
        if age > self.cache_ttl:
            return None
        return path.read_text(encoding="utf-8")

    def _cache_set(self, key: str, value: str) -> None:
        path = self.cache_dir / f"{key}.txt"
        path.write_text(value, encoding="utf-8")