comms: normalize atlasbot replies
This commit is contained in:
parent
83b8e13661
commit
cd6eaff7cb
@ -16,7 +16,7 @@ spec:
|
|||||||
labels:
|
labels:
|
||||||
app: atlasbot
|
app: atlasbot
|
||||||
annotations:
|
annotations:
|
||||||
checksum/atlasbot-configmap: manual-atlasbot-4
|
checksum/atlasbot-configmap: manual-atlasbot-5
|
||||||
vault.hashicorp.com/agent-inject: "true"
|
vault.hashicorp.com/agent-inject: "true"
|
||||||
vault.hashicorp.com/role: "comms"
|
vault.hashicorp.com/role: "comms"
|
||||||
vault.hashicorp.com/agent-inject-secret-turn-secret: "kv/data/atlas/comms/turn-shared-secret"
|
vault.hashicorp.com/agent-inject-secret-turn-secret: "kv/data/atlas/comms/turn-shared-secret"
|
||||||
@ -75,6 +75,8 @@ spec:
|
|||||||
value: http://victoria-metrics-single-server.monitoring.svc.cluster.local:8428
|
value: http://victoria-metrics-single-server.monitoring.svc.cluster.local:8428
|
||||||
- name: BOT_USER
|
- name: BOT_USER
|
||||||
value: atlasbot
|
value: atlasbot
|
||||||
|
- name: BOT_MENTIONS
|
||||||
|
value: atlasbot
|
||||||
- name: OLLAMA_URL
|
- name: OLLAMA_URL
|
||||||
value: http://chat-ai-gateway.bstein-dev-home.svc.cluster.local/
|
value: http://chat-ai-gateway.bstein-dev-home.svc.cluster.local/
|
||||||
- name: OLLAMA_MODEL
|
- name: OLLAMA_MODEL
|
||||||
|
|||||||
@ -71,6 +71,8 @@ METRIC_HINT_WORDS = {
|
|||||||
"latency",
|
"latency",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CODE_FENCE_RE = re.compile(r"^```(?:json)?\\s*(.*?)\\s*```$", re.DOTALL)
|
||||||
|
|
||||||
def _tokens(text: str) -> list[str]:
|
def _tokens(text: str) -> list[str]:
|
||||||
toks = [t.lower() for t in TOKEN_RE.findall(text or "")]
|
toks = [t.lower() for t in TOKEN_RE.findall(text or "")]
|
||||||
return [t for t in toks if t not in STOPWORDS and len(t) >= 2]
|
return [t for t in toks if t not in STOPWORDS and len(t) >= 2]
|
||||||
@ -442,6 +444,35 @@ def vm_cluster_snapshot() -> str:
|
|||||||
parts.append(pr)
|
parts.append(pr)
|
||||||
return "\n".join(parts).strip()
|
return "\n".join(parts).strip()
|
||||||
|
|
||||||
|
def _strip_code_fence(text: str) -> str:
|
||||||
|
cleaned = (text or "").strip()
|
||||||
|
match = CODE_FENCE_RE.match(cleaned)
|
||||||
|
if match:
|
||||||
|
return match.group(1).strip()
|
||||||
|
return cleaned
|
||||||
|
|
||||||
|
def _normalize_reply(value: Any) -> str:
|
||||||
|
if isinstance(value, dict):
|
||||||
|
for key in ("content", "response", "reply", "message"):
|
||||||
|
if key in value:
|
||||||
|
return _normalize_reply(value[key])
|
||||||
|
for v in value.values():
|
||||||
|
if isinstance(v, (str, dict, list)):
|
||||||
|
return _normalize_reply(v)
|
||||||
|
return json.dumps(value, ensure_ascii=False)
|
||||||
|
if isinstance(value, list):
|
||||||
|
parts = [_normalize_reply(item) for item in value]
|
||||||
|
return " ".join(p for p in parts if p)
|
||||||
|
if value is None:
|
||||||
|
return ""
|
||||||
|
text = _strip_code_fence(str(value))
|
||||||
|
if text.startswith("{") and text.endswith("}"):
|
||||||
|
try:
|
||||||
|
return _normalize_reply(json.loads(text))
|
||||||
|
except Exception:
|
||||||
|
return text
|
||||||
|
return text
|
||||||
|
|
||||||
|
|
||||||
# Conversation state.
|
# Conversation state.
|
||||||
history = collections.defaultdict(list) # (room_id, sender|None) -> list[str] (short transcript)
|
history = collections.defaultdict(list) # (room_id, sender|None) -> list[str] (short transcript)
|
||||||
@ -511,7 +542,8 @@ def ollama_reply(hist_key, prompt: str, *, context: str) -> str:
|
|||||||
r = request.Request(OLLAMA_URL, data=json.dumps(payload).encode(), headers=headers)
|
r = request.Request(OLLAMA_URL, data=json.dumps(payload).encode(), headers=headers)
|
||||||
with request.urlopen(r, timeout=20) as resp:
|
with request.urlopen(r, timeout=20) as resp:
|
||||||
data = json.loads(resp.read().decode())
|
data = json.loads(resp.read().decode())
|
||||||
reply = data.get("message") or data.get("response") or data.get("reply") or "I'm here to help."
|
raw_reply = data.get("message") or data.get("response") or data.get("reply") or data
|
||||||
|
reply = _normalize_reply(raw_reply) or "I'm here to help."
|
||||||
history[hist_key].append(f"Atlas: {reply}")
|
history[hist_key].append(f"Atlas: {reply}")
|
||||||
return reply
|
return reply
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user