File size: 1,622 Bytes
ca4ed58
 
 
83fc25d
ca4ed58
 
 
 
 
 
 
 
1315e90
ca4ed58
 
 
 
 
 
 
 
 
1315e90
ca4ed58
 
1315e90
ca4ed58
1315e90
ca4ed58
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# app/core/ibm_sanity.py
import os, requests, json, numpy as np
from app.core.config import WATSONX_BASE_URL, WATSONX_PROJECT, WATSONX_API_KEY
from app.core.auth import get_ibm_iam_token

VERSION = os.getenv("IBM_API_VERSION", "2023-05-29")
EMB = os.getenv("IBM_EMBEDDINGS_MODEL_ID", "")
CLAIM = os.getenv("IBM_CLAIM_MODEL_ID", "")
VERIFY = os.getenv("IBM_VERIFIER_MODEL_ID", "")


def sanity_embeddings():
    tok = get_ibm_iam_token()
    r = requests.post(
        f"{WATSONX_BASE_URL.rstrip('/')}/ml/v1/text/embeddings?version={VERSION}",
        headers={"Authorization":f"Bearer {tok}","Accept":"application/json","Content-Type":"application/json"},
        json={"inputs":["probe"],"model_id":EMB,"project_id":WATSONX_PROJECT},
        timeout=60,
    )
    r.raise_for_status()
    j = r.json()
    items = j.get("data") or j.get("results") or []
    dim = len(items[0]["embedding"]) if items else 0
    return {"ok": True, "dim": dim}


def sanity_generation(model_id: str, prompt: str):
    tok = get_ibm_iam_token()
    r = requests.post(
        f"{WATSONX_BASE_URL.rstrip('/')}/ml/v1/text/generation?version={VERSION}",
        headers={"Authorization":f"Bearer {tok}","Accept":"application/json","Content-Type":"application/json"},
        json={
            "input": prompt,
            "model_id": model_id,
            "project_id": WATSONX_PROJECT,
            "parameters": {"decoding_method":"greedy","max_new_tokens":64}
        },
        timeout=90,
    )
    r.raise_for_status()
    txt = (r.json().get("results") or [{}])[0].get("generated_text","")
    return {"ok": True, "preview": txt[:120]}