wedgecheck/app/llm_compat.py
caelum0x 31f2a18133 feat: deploy live, free LLM path via portfolio relay, fix missing ddgs dep
OpenRouter credits are still exhausted, but that's no longer the real
blocker: OPENAI_BASE_URL now points at this portfolio's free Cloudflare
Workers AI relay (ai-relay-worker) instead, which needed two real fixes to
work with gpt-researcher:

- app/llm_compat.py (opt-in via WEDGECHECK_NON_STREAMING_LLM=1): the relay
  always answers stream:true with one flat JSON body (no real SSE), which
  broke gpt-researcher's forced-streaming report writer ("No generation
  chunks were returned"). Patches ChatOpenAI.astream to fall back to a
  single ainvoke() wrapped as a one-chunk stream.
- EMBEDDING=custom:bge against the relay's new /v1/embeddings route (added
  to ai-relay-worker separately) instead of the paid
  openai:text-embedding-3-small default -- avoids a torch/sentence-
  transformers dependency entirely.

Live-verified end-to-end, repeatedly, against the real code path: the
existing cited-sources retry/fail-loud logic in app/brief.py correctly
discards an LLM-written report and raises WedgecheckResearchError whenever
the free duckduckgo retriever returns zero real sources on every attempt,
rather than ever serving an unsourced brief.

Also fixes a real deploy-time bug found live: gpt-researcher's duckduckgo
retriever lazily imports ddgs but doesn't declare it as a dependency --
requirements.txt (pinned from a dev .venv that happened to already have it
installed) was missing it, which 502'd in production until added.

Deployed to https://wedgecheck.ereboslab.com (Docker on the Oracle box).
2026-10-29 03:25:10 +03:00

57 lines
2.2 KiB
Python

"""Compatibility shim for OpenAI-compatible LLM endpoints that don't support
real SSE streaming.
gpt-researcher's report-writing step (gpt_researcher/actions/report_generation.py)
always calls the chat model with stream=True and consumes the response via
langchain's `ChatOpenAI.astream()`, which expects real `text/event-stream`
chunks. Pointed at a minimal relay that always returns one flat JSON body
regardless of the `stream` flag in the request (e.g. this portfolio's free
Cloudflare Workers AI relay, which forwards to Workers AI / Groq and shapes
a single `{choices: [...]}` response), `astream()` raises:
No generation chunks were returned
This is not a credits/auth problem -- it is a transport mismatch between
"client asked for SSE" and "server always answers with one JSON blob".
Enable with WEDGECHECK_NON_STREAMING_LLM=1 when pointing Wedgecheck's
OPENAI_BASE_URL at such an endpoint. This monkeypatches
`ChatOpenAI.astream` to fall back to a single `ainvoke()` call wrapped in a
one-chunk async generator, so gpt-researcher's streaming consumer still
gets a valid (if non-incremental) stream. Leave this unset for any real
streaming-capable provider (OpenAI, OpenRouter, Anthropic, etc) -- it is
strictly a workaround for non-streaming relays, not a general improvement.
"""
from __future__ import annotations
import os
def apply_if_enabled() -> None:
"""Patch ChatOpenAI.astream iff WEDGECHECK_NON_STREAMING_LLM is truthy.
Idempotent and safe to call multiple times (e.g. once per research
attempt) -- the patch is only ever installed once per process.
"""
if os.getenv("WEDGECHECK_NON_STREAMING_LLM", "").strip().lower() not in (
"1",
"true",
"yes",
):
return
from langchain_openai import ChatOpenAI
from langchain_core.messages import AIMessageChunk
if getattr(ChatOpenAI, "_wedgecheck_nonstreaming_patch", False):
return
original_ainvoke = ChatOpenAI.ainvoke
async def _astream_via_ainvoke(self, input, config=None, *, stop=None, **kwargs):
result = await original_ainvoke(self, input, config, stop=stop, **kwargs)
yield AIMessageChunk(content=result.content)
ChatOpenAI.astream = _astream_via_ainvoke
ChatOpenAI._wedgecheck_nonstreaming_patch = True