Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ API 容器会在进程启动前按知识后端准备索引。`bm25` 模式根据
| --- | --- |
| `MEDGUIDE_MODE` | 外部模型与共享存储设为 `production`;单实例本地推理使用独立 `local` 配置 |
| `MEDGUIDE_LOCAL_LLM_BASE_URL` / `MEDGUIDE_LOCAL_LLM_MODEL` | `local` 模式专用的本机模型地址和模型 ID,不继承 `OPENAI_*` |
| `MEDGUIDE_LOCAL_LLM_PROVIDER` | 本地推理服务类型:`llama.cpp`(默认,发送 `chat_template_kwargs.enable_thinking=false`)或 `ollama`(发送 `reasoning_effort=none`);非法值启动即报错 |
| `MEDGUIDE_LOCAL_LLM_TIMEOUT_SECONDS` / `MEDGUIDE_LOCAL_LLM_MAX_TOKENS` | 本地生成预算,默认 120 秒 / 512 token |
| `MEDGUIDE_DOMAIN` | 已解析到部署主机的公网域名;TLS 网关启动时校验,缺失或非法则失败关闭 |
| `MEDGUIDE_HTTP_PORT` / `MEDGUIDE_HTTPS_PORT` | TLS 网关对外发布端口,默认 80/443 |
Expand Down
40 changes: 38 additions & 2 deletions backend/app/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class InvalidCredentialsError(AuthError):
"""Raised for an unknown username or an incorrect password."""


class UnknownUserError(AuthError):
"""Raised when a session is requested for a username that is not registered."""


class InvalidUsernameError(AuthError):
"""Raised when a username cannot be represented safely."""

Expand Down Expand Up @@ -323,6 +327,16 @@ def verify_credentials(self, username: str, password: str) -> str:

def create_session(self, username: str) -> tuple[str, int]:
normalized = normalize_username(username)
with self._connect() as connection:
row = connection.execute(
"SELECT 1 FROM users WHERE username = ?",
(normalized,),
).fetchone()
if row is None:
# Check first so a missing user is reported as such, instead of
# surfacing as a foreign-key IntegrityError retried as a
# "could not allocate a unique authentication token" failure.
raise UnknownUserError("user does not exist")
now = int(self._clock())
expires_at = now + self.session_ttl_seconds
for _ in range(3):
Expand All @@ -338,7 +352,13 @@ def create_session(self, username: str) -> tuple[str, int]:
(self._token_hash(token), normalized, now, expires_at),
)
return token, expires_at
except sqlite3.IntegrityError:
except sqlite3.IntegrityError as exc:
# Only a token-hash collision is worth retrying; any other
# constraint failure (e.g. a foreign key, if the user was
# deleted between the check above and the insert) must
# surface immediately instead of being misreported.
if not _is_unique_constraint_error(exc):
raise
continue
raise RuntimeError("could not allocate a unique authentication token")

Expand Down Expand Up @@ -593,6 +613,16 @@ def create_session(self, username: str) -> tuple[str, int]:
from sqlalchemy.exc import IntegrityError

normalized = normalize_username(username)
with self.engine.connect() as connection:
row = connection.execute(
text("SELECT 1 FROM medguide_users WHERE username = :username"),
{"username": normalized},
).first()
if row is None:
# Check first so a missing user is reported as such, instead of
# surfacing as a foreign-key IntegrityError retried as a
# "could not allocate a unique authentication token" failure.
raise UnknownUserError("user does not exist")
now = int(self._clock())
expires_at = now + self.session_ttl_seconds
for _ in range(3):
Expand All @@ -617,7 +647,13 @@ def create_session(self, username: str) -> tuple[str, int]:
},
)
return token, expires_at
except IntegrityError:
except IntegrityError as exc:
# Only a token-hash collision is worth retrying; any other
# constraint failure (e.g. a foreign key, if the user was
# deleted between the check above and the insert) must
# surface immediately instead of being misreported.
if not _is_unique_constraint_error(exc):
raise
continue
raise RuntimeError("could not allocate a unique authentication token")

Expand Down
14 changes: 11 additions & 3 deletions backend/app/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def _embedding_timeout() -> float:
return 15.0


_EMBEDDING_ENDPOINT_SUFFIXES = ("/embeddings", "/chat/completions", "/chaat/completions")
_EMBEDDING_ENDPOINT_SUFFIXES = ("/embeddings", "/chat/completions")


def _normalize_embedding_base_url(value: str | None) -> str | None:
Expand All @@ -299,11 +299,19 @@ def _normalize_embedding_base_url(value: str | None) -> str | None:
if not raw:
return None
parsed = urlsplit(raw)
if not parsed.scheme or not parsed.netloc:
if parsed.scheme.lower() not in {"http", "https"} or not parsed.netloc:
raise ValueError("Embedding base URL must include an HTTP(S) host")
path = parsed.path.rstrip("/")
for suffix in _EMBEDDING_ENDPOINT_SUFFIXES:
if path.lower().endswith(suffix):
path = path[: -len(suffix)].rstrip("/")
break
return urlunsplit((parsed.scheme, parsed.netloc, path, parsed.query, parsed.fragment)).rstrip("/")
else:
# See _normalize_base_url: a mistyped endpoint must fail closed
# instead of being silently truncated to a wrong base URL.
if path.lower().endswith("/completions"):
raise ValueError("Embedding base URL looks like a mistyped completion endpoint")
# Query strings and fragments are never part of a base URL; drop them
# instead of forwarding them (and any credentials they carry) with
# every request.
return urlunsplit((parsed.scheme, parsed.netloc, path, "", "")).rstrip("/")
132 changes: 122 additions & 10 deletions backend/app/infra.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,47 @@ class RedisSessionAdapter:

_UPSERT_FEEDBACK_SCRIPT = """
local previous = redis.call('HGET', KEYS[1], ARGV[1])
if previous == ARGV[2] then
redis.call('EXPIRE', KEYS[1], ARGV[4])
redis.call('EXPIRE', KEYS[2], ARGV[4])
local stored_comment = redis.call('HGET', KEYS[1], ARGV[1] .. ':comment')
local comment = ARGV[3]
local comment_changed = comment ~= '' and stored_comment ~= comment
if previous == ARGV[2] and not comment_changed then
redis.call('EXPIRE', KEYS[1], ARGV[6])
redis.call('EXPIRE', KEYS[2], ARGV[6])
return 0
end
if previous == 'up' or previous == 'down' then
redis.call('HINCRBY', KEYS[2], 'feedback_' .. previous, -1)
if previous ~= ARGV[2] then
redis.call('HINCRBY', KEYS[2], 'feedback_' .. previous, -1)
redis.call('HINCRBY', KEYS[2], 'feedback_' .. ARGV[2], 1)
end
else
redis.call('HINCRBY', KEYS[2], 'feedback_' .. ARGV[2], 1)
end
redis.call('HSET', KEYS[1], ARGV[1], ARGV[2])
redis.call('HINCRBY', KEYS[2], 'feedback_' .. ARGV[2], 1)
if ARGV[3] == '1' and not previous then
redis.call('HINCRBY', KEYS[2], 'feedback_commented', 1)
if comment_changed then
redis.call('HSET', KEYS[1], ARGV[1] .. ':comment', comment)
redis.call('HSET', KEYS[1], ARGV[1] .. ':session_id', ARGV[4])
redis.call('HSET', KEYS[1], ARGV[1] .. ':message_id', ARGV[5])
if not stored_comment then
redis.call('HINCRBY', KEYS[2], 'feedback_commented', 1)
end
end
redis.call('EXPIRE', KEYS[1], ARGV[4])
redis.call('EXPIRE', KEYS[2], ARGV[4])
local seq = redis.call('HINCRBY', KEYS[2], 'feedback_comment_seq', 1)
redis.call('HSET', KEYS[1], ARGV[1] .. ':seq', seq)
redis.call('EXPIRE', KEYS[1], ARGV[6])
redis.call('EXPIRE', KEYS[2], ARGV[6])
return 1
"""

_REMOVE_FEEDBACK_SCRIPT = """
local previous = redis.call('HGET', KEYS[1], ARGV[1])
if previous ~= 'up' and previous ~= 'down' then
return 0
end
redis.call('HDEL', KEYS[1], ARGV[1])
redis.call('HINCRBY', KEYS[2], 'feedback_' .. previous, -1)
redis.call('EXPIRE', KEYS[1], ARGV[2])
redis.call('EXPIRE', KEYS[2], ARGV[2])
return 1
"""

Expand Down Expand Up @@ -542,21 +568,107 @@ def upsert_feedback(
if not callable(evaluator):
return None
key = hashlib.sha256(f"{session_id}\x00{message_id}".encode("utf-8")).hexdigest()
# A blank comment is no comment: send an empty string so the
# script leaves any stored text untouched (issue #21).
text = comment.strip() if isinstance(comment, str) else ""
changed = evaluator(
self._UPSERT_FEEDBACK_SCRIPT,
2,
self.metrics_feedback_key,
self.metrics_key,
key,
rating,
"1" if comment else "0",
text,
session_id,
message_id,
self.ttl_seconds,
)
return bool(int(changed))
except Exception as exc:
self._mark_unavailable(exc)
return None

def remove_feedback(
self,
session_id: str,
message_id: str,
) -> bool | None:
if not self.ensure_ready() or self.client is None:
return None
try:
evaluator = getattr(self.client, "eval", None)
if not callable(evaluator):
return None
key = hashlib.sha256(f"{session_id}\x00{message_id}".encode("utf-8")).hexdigest()
changed = evaluator(
self._REMOVE_FEEDBACK_SCRIPT,
2,
self.metrics_feedback_key,
self.metrics_key,
key,
self.ttl_seconds,
)
return bool(int(changed))
except Exception as exc:
self._mark_unavailable(exc)
return None

def list_feedback_comments(self) -> list[dict[str, Any]] | None:
"""Return stored feedback comments, oldest update first.

The feedback hash stores one record per message under its opaque
key: the rating itself, plus `:comment`, `:session_id`,
`:message_id` and `:seq` fields. Records without a comment are
not returned.
"""
if not self.ensure_ready() or self.client is None:
return None
try:
hgetall = getattr(self.client, "hgetall", None)
if not callable(hgetall):
return None
raw = hgetall(self.metrics_feedback_key)
if not raw:
return []
values: dict[str, str] = {}
for key, value in raw.items():
if isinstance(key, bytes):
key = key.decode("utf-8")
if isinstance(value, bytes):
value = value.decode("utf-8")
values[str(key)] = str(value)
records: dict[str, dict[str, Any]] = {}
for field, value in values.items():
for suffix in (":comment", ":session_id", ":message_id", ":seq"):
if field.endswith(suffix):
records.setdefault(field[: -len(suffix)], {})[suffix[1:]] = value
break
else:
records.setdefault(field, {})["rating"] = value
comments: list[dict[str, Any]] = []
for record in records.values():
text = record.get("comment")
if not text:
continue
try:
seq = int(record.get("seq", 0))
except (TypeError, ValueError):
seq = 0
comments.append(
{
"session_id": str(record.get("session_id", "")),
"message_id": str(record.get("message_id", "")),
"rating": str(record.get("rating", "")),
"comment": str(text),
"seq": seq,
}
)
comments.sort(key=lambda item: item["seq"])
return comments
except Exception as exc:
self._mark_unavailable(exc)
return None

def metrics_snapshot(self) -> dict[str, Any] | None:
if not self.ensure_ready() or self.client is None:
return None
Expand Down
8 changes: 7 additions & 1 deletion backend/app/ingestion.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,13 @@ def _contains_nested_credentials(cls, value: str) -> bool:
break
decoded = next_value
normalized = unicodedata.normalize("NFKC", decoded)
for match in re.finditer(r"(?:^|[?&;\s])([^=&?#\s]+)\s*=", normalized):
# A nested URL carrying userinfo (scheme://user:password@host)
# hides credentials that no key=value scan would ever see.
if re.search(r"://[^/\s]*@", normalized):
return True
# `#` and `/` are delimiters too: a credential smuggled in as
# `.../x%23token=secret` or `.../x/token=secret` must be caught.
for match in re.finditer(r"(?:^|[?&;#/\s])([^=&?#\s]+)\s*=", normalized):
if cls._sensitive_query_key(match.group(1)):
return True
return False
Expand Down
42 changes: 35 additions & 7 deletions backend/app/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ def __init__(
mode = os.getenv("MEDGUIDE_MODE", "production").strip().lower()
self.local = mode == "local"
self.required = mode not in {"offline", "test"}
# The local reasoning toggle is provider-specific: llama.cpp honours
# chat_template_kwargs.enable_thinking=false, while Ollama ignores
# it and needs reasoning_effort="none" (issue #1). An unknown
# provider fails closed at startup instead of silently sending the
# wrong toggle to the local model.
self.local_llm_provider = self._read_local_provider() if self.local else None
configured_model = (
os.getenv("MEDGUIDE_LOCAL_LLM_MODEL", "")
if self.local else (model if model is not None else os.getenv("OPENAI_MODEL", ""))
Expand Down Expand Up @@ -192,6 +198,13 @@ def _read_max_tokens() -> int:
except ValueError:
return 1600

@staticmethod
def _read_local_provider() -> str:
provider = os.getenv("MEDGUIDE_LOCAL_LLM_PROVIDER", "llama.cpp").strip().lower()
if provider not in {"llama.cpp", "ollama"}:
raise ValueError("MEDGUIDE_LOCAL_LLM_PROVIDER must be llama.cpp or ollama")
return provider

@staticmethod
def _read_local_timeout() -> float:
try:
Expand All @@ -208,10 +221,15 @@ def _read_local_max_tokens() -> int:
return 512

def _local_request_options(self) -> dict[str, Any]:
return (
{"extra_body": {"chat_template_kwargs": {"enable_thinking": False}}}
if getattr(self, "local", False) else {}
)
if not getattr(self, "local", False):
return {}
# Shared by the readiness probe and generate, so one provider
# choice covers both. Ollama ignores llama.cpp's thinking toggle
# and spends the probe budget on reasoning instead, leaving an
# empty message body (issue #1).
if getattr(self, "local_llm_provider", "llama.cpp") == "ollama":
return {"extra_body": {"reasoning_effort": "none"}}
return {"extra_body": {"chat_template_kwargs": {"enable_thinking": False}}}

def generate(self, query: str, risk: str, context: str) -> str | None:
if not self.ensure_ready():
Expand Down Expand Up @@ -291,7 +309,7 @@ async def aclose(self) -> None:
self.close()


_COMPLETION_ENDPOINT_SUFFIXES = ("/chat/completions", "/chaat/completions")
_COMPLETION_ENDPOINT_SUFFIXES = ("/chat/completions",)


def _normalize_local_base_url(value: str) -> str:
Expand Down Expand Up @@ -332,11 +350,21 @@ def _normalize_base_url(value: str | None) -> str | None:
if not raw:
return None
parsed = urlsplit(raw)
if not parsed.scheme or not parsed.netloc:
if parsed.scheme.lower() not in {"http", "https"} or not parsed.netloc:
raise ValueError("OpenAI base URL must include an HTTP(S) host")
path = parsed.path.rstrip("/")
for suffix in _COMPLETION_ENDPOINT_SUFFIXES:
if path.lower().endswith(suffix):
path = path[: -len(suffix)].rstrip("/")
break
return urlunsplit((parsed.scheme, parsed.netloc, path, parsed.query, parsed.fragment)).rstrip("/")
else:
# A path ending in some other ".../completions" is almost certainly
# a mistyped endpoint (e.g. "/chaat/completions"). Fail closed
# instead of silently truncating it to a base URL that then hits
# the wrong endpoint.
if path.lower().endswith("/completions"):
raise ValueError("OpenAI base URL looks like a mistyped completion endpoint")
# Query strings and fragments are never part of a base URL; drop them
# instead of forwarding them (and any credentials they carry) with
# every request.
return urlunsplit((parsed.scheme, parsed.netloc, path, "", "")).rstrip("/")
Loading