* add staging ios app build script * feat(api): add OpenTelemetry LGTM stack for local observability Wire OTel traces, metrics, and logs through a collector to Tempo, Prometheus, and Loki, with custom LLM instrumentation, dev compose overlay, Grafana provisioning, env templates, and development.sh auto-start. Co-authored-by: Cursor <cursoragent@cursor.com> * feat: expand observability, harden dev tooling, and fix expo staging UX Add business and LLM Prometheus metrics with Grafana dashboards, alerting, and a metrics verification script. Wire telemetry through adapters and core LLM paths, and document the local LGTM workflow. Fix development.sh for macOS bash 3.2, open Grafana and eval-web in Chrome, and repair eval-web auto-open (unbound EVAL_WEB_BROWSER_SCHEDULED). Merge internal-eval into the main dev script with improved compose handling. Require EXPO_PUBLIC_* at build time, improve iOS HTTP ATS for staging IPs, show memoir empty state instead of load errors when no chapters exist, and add jest env setup plus chapter list response normalization. Co-authored-by: Cursor <cursoragent@cursor.com> * chore: enable Grafana Assistant Cursor plugin Co-authored-by: Cursor <cursoragent@cursor.com> * fix: memoir empty state and repair withdrawn 0020_chapters_book_id stamp Show empty memoir UI when the chapter list succeeds with no items; treat auth/404 as non-fatal. Extend alembic revision repair so local dev DBs stamped with the removed 0020_chapters_book_id migration can roll back and upgrade to 0019. Co-authored-by: Cursor <cursoragent@cursor.com> --------- Co-authored-by: Kevin <kevin@brighteng.org> Co-authored-by: Cursor <cursoragent@cursor.com>
126 lines
4.0 KiB
Python
126 lines
4.0 KiB
Python
"""DeepSeek / OpenAI-compatible LLM adapter — implements LLMProvider port."""
|
|
|
|
from collections.abc import AsyncIterator
|
|
|
|
from langchain_openai import ChatOpenAI
|
|
|
|
from app.core.llm_telemetry import langchain_invoke_span, observe_astream
|
|
|
|
|
|
class DeepSeekLLMProvider:
|
|
"""LangChain-based LLM adapter for DeepSeek and OpenAI-compatible APIs.
|
|
|
|
`langchain_llm` 供 Agent / 任务同步调用;若需 JSON object 模式,请用
|
|
`app.core.langchain_llm.bind_json_object_mode` 或 `invoke_json_object` /
|
|
`ainvoke_json_object`(见 `api/docs/llm-json-mode.md`),勿使用已废弃的
|
|
`bind(model_kwargs={"response_format": ...})`。
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
api_key: str,
|
|
base_url: str = "https://api.deepseek.com",
|
|
model: str = "deepseek-v4-flash",
|
|
temperature: float = 0.7,
|
|
*,
|
|
extra_body: dict | None = None,
|
|
):
|
|
self._default_model = model
|
|
self._default_temperature = temperature
|
|
kwargs: dict = {
|
|
"temperature": temperature,
|
|
"model": model,
|
|
"api_key": api_key,
|
|
}
|
|
if extra_body:
|
|
kwargs["extra_body"] = extra_body
|
|
if base_url:
|
|
cleaned = base_url.rstrip("/")
|
|
for suffix in ("/v1/chat/completions", "/v1"):
|
|
if cleaned.endswith(suffix):
|
|
cleaned = cleaned[: -len(suffix)]
|
|
kwargs["base_url"] = cleaned
|
|
|
|
self._llm = ChatOpenAI(**kwargs)
|
|
|
|
@property
|
|
def langchain_llm(self) -> ChatOpenAI:
|
|
"""Expose underlying ChatOpenAI for LangChain agent interop (Phase 2 will remove)."""
|
|
return self._llm
|
|
|
|
async def complete(
|
|
self,
|
|
messages: list[dict],
|
|
*,
|
|
temperature: float | None = None,
|
|
model: str | None = None,
|
|
max_tokens: int | None = None,
|
|
) -> str:
|
|
llm = self._get_llm(temperature, model, max_tokens)
|
|
lc_messages = _to_langchain_messages(messages)
|
|
resolved_model = model or self._default_model
|
|
with langchain_invoke_span(
|
|
agent="deepseek.complete",
|
|
provider="deepseek",
|
|
model=resolved_model,
|
|
call_type="chat",
|
|
) as tel:
|
|
result = await llm.ainvoke(lc_messages)
|
|
tel["response"] = result
|
|
return str(result.content)
|
|
|
|
async def stream(
|
|
self,
|
|
messages: list[dict],
|
|
*,
|
|
temperature: float | None = None,
|
|
model: str | None = None,
|
|
max_tokens: int | None = None,
|
|
) -> AsyncIterator[str]:
|
|
llm = self._get_llm(temperature, model, max_tokens)
|
|
lc_messages = _to_langchain_messages(messages)
|
|
resolved_model = model or self._default_model
|
|
async for chunk in observe_astream(
|
|
llm,
|
|
lc_messages,
|
|
agent="deepseek.stream",
|
|
provider="deepseek",
|
|
model=resolved_model,
|
|
):
|
|
if chunk.content:
|
|
yield str(chunk.content)
|
|
|
|
def _get_llm(
|
|
self,
|
|
temperature: float | None,
|
|
model: str | None,
|
|
max_tokens: int | None = None,
|
|
):
|
|
if temperature is None and model is None and max_tokens is None:
|
|
return self._llm
|
|
kwargs: dict = {}
|
|
if temperature is not None:
|
|
kwargs["temperature"] = temperature
|
|
if model is not None:
|
|
kwargs["model"] = model
|
|
if max_tokens is not None:
|
|
kwargs["max_tokens"] = max_tokens
|
|
return self._llm.bind(**kwargs) if kwargs else self._llm
|
|
|
|
|
|
def _to_langchain_messages(messages: list[dict]) -> list:
|
|
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
|
|
|
|
mapping = {
|
|
"system": SystemMessage,
|
|
"human": HumanMessage,
|
|
"user": HumanMessage,
|
|
"ai": AIMessage,
|
|
"assistant": AIMessage,
|
|
}
|
|
result = []
|
|
for msg in messages:
|
|
cls = mapping.get(msg.get("role", ""), HumanMessage)
|
|
result.append(cls(content=msg.get("content", "")))
|
|
return result
|