PERDYPTO commited on
Update app/services/nim.py
Browse files- app/services/nim.py +17 -7
app/services/nim.py
CHANGED
|
@@ -4,8 +4,8 @@ import logging
|
|
| 4 |
import httpx
|
| 5 |
|
| 6 |
from app.config import settings
|
| 7 |
-
from app.prompts import JUDGEME_BASE_PROMPT, TONE_INSTRUCTIONS
|
| 8 |
-
from app.utils.validation import extract_and_validate_json
|
| 9 |
|
| 10 |
logger = logging.getLogger(__name__)
|
| 11 |
|
|
@@ -19,18 +19,20 @@ class NIMService:
|
|
| 19 |
def _build_messages(
|
| 20 |
self,
|
| 21 |
mode: str,
|
|
|
|
| 22 |
scenario: str | None,
|
| 23 |
tone: str,
|
| 24 |
image_b64: str | None,
|
| 25 |
image_mime: str = "image/png",
|
| 26 |
) -> list[dict]:
|
| 27 |
tone_instruction = TONE_INSTRUCTIONS.get(tone, TONE_INSTRUCTIONS["normal"])
|
|
|
|
| 28 |
user_content: list[dict] = []
|
| 29 |
|
| 30 |
if mode == "text":
|
| 31 |
user_content.append({
|
| 32 |
"type": "text",
|
| 33 |
-
"text": f"
|
| 34 |
})
|
| 35 |
|
| 36 |
elif mode == "image":
|
|
@@ -40,7 +42,11 @@ class NIMService:
|
|
| 40 |
})
|
| 41 |
user_content.append({
|
| 42 |
"type": "text",
|
| 43 |
-
"text":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
})
|
| 45 |
|
| 46 |
elif mode == "image_text":
|
|
@@ -50,7 +56,7 @@ class NIMService:
|
|
| 50 |
})
|
| 51 |
user_content.append({
|
| 52 |
"type": "text",
|
| 53 |
-
"text": f"
|
| 54 |
})
|
| 55 |
|
| 56 |
return [
|
|
@@ -61,12 +67,13 @@ class NIMService:
|
|
| 61 |
async def judge(
|
| 62 |
self,
|
| 63 |
mode: str,
|
|
|
|
| 64 |
scenario: str | None,
|
| 65 |
tone: str,
|
| 66 |
image_b64: str | None,
|
| 67 |
image_mime: str = "image/png",
|
| 68 |
) -> dict:
|
| 69 |
-
messages = self._build_messages(mode, scenario, tone, image_b64, image_mime)
|
| 70 |
|
| 71 |
payload = {
|
| 72 |
"model": self.model,
|
|
@@ -92,10 +99,13 @@ class NIMService:
|
|
| 92 |
raw_content = data["choices"][0]["message"]["content"]
|
| 93 |
logger.info("NIM raw response received (length=%d)", len(raw_content))
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
try:
|
| 96 |
return extract_and_validate_json(raw_content)
|
| 97 |
except ValueError as e:
|
| 98 |
-
# Log the actual model output so you can see refusals/truncation
|
| 99 |
logger.warning(
|
| 100 |
"NIM returned non-JSON response (length=%d): %.500s",
|
| 101 |
len(raw_content),
|
|
|
|
| 4 |
import httpx
|
| 5 |
|
| 6 |
from app.config import settings
|
| 7 |
+
from app.prompts import JUDGEME_BASE_PROMPT, JUDGE_MODE_INSTRUCTIONS, TONE_INSTRUCTIONS
|
| 8 |
+
from app.utils.validation import extract_and_validate_json, is_refusal
|
| 9 |
|
| 10 |
logger = logging.getLogger(__name__)
|
| 11 |
|
|
|
|
| 19 |
def _build_messages(
|
| 20 |
self,
|
| 21 |
mode: str,
|
| 22 |
+
judge_mode: str,
|
| 23 |
scenario: str | None,
|
| 24 |
tone: str,
|
| 25 |
image_b64: str | None,
|
| 26 |
image_mime: str = "image/png",
|
| 27 |
) -> list[dict]:
|
| 28 |
tone_instruction = TONE_INSTRUCTIONS.get(tone, TONE_INSTRUCTIONS["normal"])
|
| 29 |
+
mode_instruction = JUDGE_MODE_INSTRUCTIONS.get(judge_mode, JUDGE_MODE_INSTRUCTIONS["judge_me"])
|
| 30 |
user_content: list[dict] = []
|
| 31 |
|
| 32 |
if mode == "text":
|
| 33 |
user_content.append({
|
| 34 |
"type": "text",
|
| 35 |
+
"text": f"{mode_instruction}\n\nTone: {tone_instruction}\n\nScenario: {scenario}",
|
| 36 |
})
|
| 37 |
|
| 38 |
elif mode == "image":
|
|
|
|
| 42 |
})
|
| 43 |
user_content.append({
|
| 44 |
"type": "text",
|
| 45 |
+
"text": (
|
| 46 |
+
f"{mode_instruction}\n\nTone: {tone_instruction}\n\n"
|
| 47 |
+
"Give a lighthearted, low-stakes vibe check of the moment or situation shown in this photo. "
|
| 48 |
+
"Do not comment on the person's appearance, only on the situation/context visible."
|
| 49 |
+
),
|
| 50 |
})
|
| 51 |
|
| 52 |
elif mode == "image_text":
|
|
|
|
| 56 |
})
|
| 57 |
user_content.append({
|
| 58 |
"type": "text",
|
| 59 |
+
"text": f"{mode_instruction}\n\nTone: {tone_instruction}\n\nScenario: {scenario}",
|
| 60 |
})
|
| 61 |
|
| 62 |
return [
|
|
|
|
| 67 |
async def judge(
|
| 68 |
self,
|
| 69 |
mode: str,
|
| 70 |
+
judge_mode: str,
|
| 71 |
scenario: str | None,
|
| 72 |
tone: str,
|
| 73 |
image_b64: str | None,
|
| 74 |
image_mime: str = "image/png",
|
| 75 |
) -> dict:
|
| 76 |
+
messages = self._build_messages(mode, judge_mode, scenario, tone, image_b64, image_mime)
|
| 77 |
|
| 78 |
payload = {
|
| 79 |
"model": self.model,
|
|
|
|
| 99 |
raw_content = data["choices"][0]["message"]["content"]
|
| 100 |
logger.info("NIM raw response received (length=%d)", len(raw_content))
|
| 101 |
|
| 102 |
+
if is_refusal(raw_content):
|
| 103 |
+
logger.warning("NIM declined the request (length=%d): %s", len(raw_content), raw_content)
|
| 104 |
+
raise ValueError("REFUSED")
|
| 105 |
+
|
| 106 |
try:
|
| 107 |
return extract_and_validate_json(raw_content)
|
| 108 |
except ValueError as e:
|
|
|
|
| 109 |
logger.warning(
|
| 110 |
"NIM returned non-JSON response (length=%d): %.500s",
|
| 111 |
len(raw_content),
|