Raven-1/Dockerfile
Kevin Hu 82c76a82a9
feat(knowledge): read every format, show what the index holds, and let an agent search it (#489)
<img width="2318" height="1448" alt="image"
src="https://github.com/user-attachments/assets/bca71d69-4972-420c-b14e-c8cf74a17de9"
/>

## Summary

Everything a knowledge base could not read, and the pieces it cut them
into
saying what they are.

**Reading a PDF.** A PDF records glyphs at coordinates; whether they are
a
heading, a table cell or a running footer is inferred from how they
look. There
are now two readers. The geometric one infers structure from type size
and
position and needs nothing but the file. The other asks deepdoc's
models, ported
into `raven/knowledge/parser/deepdoc`: a layout model that says what
each region
is, a detector and recognizer that read a page carrying no text at all,
and a
table-structure model that rebuilds a grid from a picture of one. 
 Detecting boxes around text the file already spells
out exactly costs an inference pass per page to learn less, and
recognising it
costs accuracy.

The layout graph answers with a shape its own postprocessing
raises on, and its classes are DocLayout-YOLO's order rather than the
list
Both were established by running the model, and the label order is
pinned by a test.

**Tables go in as tables.** `<table>` is the only shape that keeps what
a grid
states -- which column a cell stood under, which cells are merged, which
row is
the header -- and it is the shape a model reads a table in, which is
what these
chunks are for. PDF, Word and PowerPoint all state their merges exactly,
and
they now survive as `colspan` and `rowspan`. The flat form this replaces
had to
repeat a header standing over two columns across both of them, because
there was
nowhere to record a span; it had no callers left afterwards and is gone.
Two
deviations from upstream, both about what the string is for: every
cell's text
is escaped, where upstream interpolates it raw and a cell holding a `<`
produces
markup that is no longer a table, and the span attributes are written
only where
there is a span, where upstream emits `<td  >` for every ordinary cell.

**A picture of where each chunk came from.** A chunk is a few hundred
words with
a page number on it, which is enough to cite and not enough to answer
whether
the parser read the page correctly. So every PDF chunk that knows where
it sits
carries one image: the regions it covers, cropped out of the rendered
pages and
stacked. here they are already structured in the parser's element spans,
so
the markers would only put text nobody wrote into the thing being
embedded.
The panel draws a chunk's tables as tables, parsed and
rebuilt as elements rather than injected: a person can rewrite any piece
from
that same panel, so what comes back from the store is user input however
it got
there.

**The page that reads them.** Upstream retired the knowledge page while
this
branch was out; it is rebuilt against the new architecture, and the half
that
never existed is the one a reader needs -- what a document actually
became once
it was cut and embedded. Documents sit in one-level folders and arrive
by four
routes: a file, a folder, a URL, a written note. Adding and indexing are
two
calls on the contract and nothing drains that queue on its own, so a
page that
stopped after the first left every upload saying Queued for good.

Opening a file takes the whole page: the document on the left, the
pieces the
index holds of it on the right. The left half is the file drawn as its
own
pages rather than framed by the browser's PDF viewer, which reads where
to open
once and answers to nothing afterwards -- and which nothing can be drawn
on.
Pages are rendered once and kept, and the region a piece was cut from is
washed
over the page it came from, placed in fractions of the page so the
picture's
own scale never enters the arithmetic. Clicking a piece takes the file
there;
double-clicking opens it for rewriting.

The pieces can be searched, filtered by state, turned on and off,
deleted,
rewritten and added to, one at a time or in a batch. Every one of those
was
already on the contract with no reader for it. A base's settings are
back and
fuller: how many pieces a search returns, how documents are cut, and how
much
prose comes with a table or a figure. The embedding model is a picker
while the
base holds no pieces and a stated fact once it does -- moving it then is
the
collection made again at the new width and every document re-cut, which
is not
a thing a panel does by being saved.

**An agent can read them.** The retrieval engine was there and nothing
could
reach it: no agent had a tool that opened a library. A reader now points
a
conversation at bases from the composer, the way they pin a working
directory,
and the selection rides in the same place -- the session's metadata,
bound
around the turn.

The tool takes a query and never a base. The scope is the reader's
choice, so a
model cannot reach material nobody offered it, and it is withheld where
nothing
was picked rather than offered and refused. A hit says which document
and page
it came from, because a hit a reader cannot place is a hit they cannot
check --
and the passages are pressable in the transcript, walking into the base,
the
document, the folder it is filed under and then the piece. A citation a
reader
cannot follow is a claim they have to take on trust, and the whole point
of
searching their own material is that they can go and look.

**The word segmenter and the resources.** `raven.core.Tokenizer` is
Infinity's
`rag_tokenizer`, ported: a forward and backward maximum match over a
dictionary
trie, with stemming and lemmatisation on the latin path. XGBoost is not
carried
over. None of the data lives in git -- the dictionary, the NLTK corpora
and the
ONNX graphs run to about a hundred megabytes, so `make fetch-resources`
on an
install and an optional Docker layer behind `RAVEN_RESOURCES` both point
at one
script. A checkout that could not reach the network still works, and
every
feature that reads this data says so when asked.

## Type

- [ ] Fix
- [x] Feature
- [ ] Docs
- [ ] CI / tooling
- [ ] Refactor
- [ ] Other

## Verification

Run on macOS against Python 3.12, with the deepdoc weights fetched (the
model
tests skip without them):

```
uv run pytest                        27450 passed, 111 skipped, 3 failed
npm run test --prefix ui-web          3276 passed
npm run test --prefix ui-tui          2106 passed
npm run type-check --prefix ui-web    clean (tsc)
npm run lint --prefix ui-web          clean (4 pre-existing hook warnings)
make lint-python                      clean (ruff check + format)
make lint-types                       clean (ty)
make lint-imports                     10 contracts kept, 0 broken
make check-source-language            clean
make check-large-files                clean
make build-ui                         built, both boot snapshots match
node ui-web/scripts/check-css.mjs     clean
node ui-web/scripts/check-class-namespace.mjs  clean
```

The three Python failures are not this branch's and were each confirmed
on the
base alone: one asks a host without LibreOffice to refuse a render and
this host
has LibreOffice installed, and two simulation cases fail on an
unmodified
checkout of the base commit.

The RPC contract was regenerated for both consumers and both match it
(`npm run gen --prefix ui-web`, `npm run gen:rpc --prefix ui-tui`, then
`gen:check`). The page's boot snapshots and region goldens were
refreshed where
the composer bar gained a button, and each records that button and
nothing else.

- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [x] User-facing docs or screenshots are updated when needed

## Risk

A PDF's tables, and a Word or PowerPoint file's, are now indexed as HTML
rather
than as header-prefixed lines. Chunk text changes shape for those
documents, so
a base indexed before this and searched after it holds both forms until
its
documents are reindexed; retrieval keeps working either way, because
both forms
are text. The same applies to how much prose a table or a figure carries
with
it: the setting now reaches as far as it says, where before it stopped
at the
nearest paragraph, so a chunk cut before this keeps the narrower context
until
its document is rebuilt. Rolling back is reverting the branch and
reindexing.

The deepdoc models are optional. An install that never ran `make
fetch-resources` reads PDFs geometrically, exactly as before, and says
so when
the models are asked for by name. Nothing here reaches the network at
request
time.

Chunk crops and rendered pages are written at index time and on first
view, both
under the state directory rather than in the repository, and both are
deleted
with their document. A page is immutable for the life of its document
id, so
the route serving them caches; the document route does not. All three
routes
take ids and no path, and the two image routes answer under the
strictest
sandbox in the tree.

What an agent may search is the reader's choice and nothing else. The
tool takes
no base argument, so a model cannot name one, and it is absent from the
schema
entirely for a conversation that named none -- a lane nobody pointed at
a base
is unchanged by this branch. The bases a session carries are stored
beside its
working directory and travel no further.

- [x] Security impact considered
- [x] Backward compatibility considered
- [x] Rollback path is clear for risky changes

## Related Issues

N/A

---------

Co-authored-by: Claude (claude-opus-5) <noreply@anthropic.com>
2026-10-12 19:05:16 +08:00

233 lines
10 KiB
Docker

# syntax=docker/dockerfile:1.7
# Raven in one image: the Python engine, the built web page, and the nginx that
# serves it.
#
# Why one image and not two. The engine binds its HTTP surface to 127.0.0.1
# (raven/cli/_gateway_page.py and raven/cli/serve_commands.py both call
# `web.TCPSite(runner, "127.0.0.1", port)`), so a reverse proxy in a second
# container has no address to reach it on. nginx therefore shares the engine's
# network namespace by living beside it, which is the same answer ragflow's
# image gives to the same shape. Splitting the two into separate services needs
# a bind address the engine does not offer yet.
#
# Build:
# docker build -t raven:local .
# docker build -t raven:local --build-arg RAVEN_EXTRAS="channels,tools,sandbox" .
#
# Run: see docker/docker-compose.yml.
ARG PYTHON_VERSION=3.12
ARG NODE_VERSION=22
# --- the page ---------------------------------------------------------------
# `make build-ui` in a stage: vite emits the island bundle, then ui-web/build.py
# assembles dist/index.html around it. build.py is stdlib-only but it is the
# second half of one build, so python3 comes along rather than the page being
# split across two stages.
FROM node:${NODE_VERSION}-bookworm-slim AS web
RUN apt-get update \
&& apt-get install -y --no-install-recommends python3 \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /build
COPY ui-web/package.json ui-web/package-lock.json ./ui-web/
RUN --mount=type=cache,target=/root/.npm npm ci --prefix ui-web
# i18n/messages.json is the catalogue build.py inlines; it is shared with the
# TUI and lives at the repo root, not under ui-web.
COPY ui-web ./ui-web
COPY i18n ./i18n
RUN npm run --prefix ui-web build && python3 ui-web/build.py
# --- the engine's dependencies ----------------------------------------------
FROM python:${PYTHON_VERSION}-slim AS deps
COPY --from=ghcr.io/astral-sh/uv:0.9 /uv /uvx /bin/
# Most of the tree resolves to manylinux wheels, but a compiler keeps a missing
# one a slow build instead of a failed image. Dropped with this stage.
RUN apt-get update \
&& apt-get install -y --no-install-recommends build-essential \
&& rm -rf /var/lib/apt/lists/*
ENV UV_COMPILE_BYTECODE=1 \
UV_LINK_MODE=copy \
UV_PYTHON_DOWNLOADS=never
WORKDIR /app
# Optional extras baked in. `channels` is what makes the gateway more than a
# web page; `tools` is the readability extractor. `sandbox`, `browser` and
# `eval` are opt-in -- each pulls a payload (boxlite, a browser binary, the
# huggingface client) that a default deployment does not open.
ARG RAVEN_EXTRAS="channels,tools"
# Dependencies before the project, so editing raven/ does not re-resolve the
# tree. uv reads the workspace from the members' manifests, so those three
# files come first too -- copying the members' sources here would put them back
# in the cache key this layer exists to avoid.
COPY pyproject.toml uv.lock README.md hatch_build.py ./
COPY plugins-dist/everos-memory/pyproject.toml ./plugins-dist/everos-memory/
COPY plugins-dist/ppt-engine/pyproject.toml ./plugins-dist/ppt-engine/
COPY plugins-dist/design-engine/pyproject.toml ./plugins-dist/design-engine/
RUN --mount=type=cache,target=/root/.cache/uv \
set -eu; \
flags=""; \
for extra in $(echo "${RAVEN_EXTRAS}" | tr ',' ' '); do flags="${flags} --extra ${extra}"; done; \
uv sync --frozen --no-install-project --no-dev ${flags}
# The project itself, editable on purpose: `agents/` and the packaged page are
# discovered beside the package the way a checkout finds them (see
# raven/agent/subagent/vendored_agents.py), so the image needs no wheel-time
# git tree and behaves like the environment the tests run in.
COPY . /app
RUN --mount=type=cache,target=/root/.cache/uv \
set -eu; \
flags=""; \
for extra in $(echo "${RAVEN_EXTRAS}" | tr ',' ' '); do flags="${flags} --extra ${extra}"; done; \
uv sync --frozen --no-dev ${flags}
# The weights and dictionaries that cannot live in git: the word dictionary the
# tokenizer segments Chinese with, the nltk corpora beside it, and the deepdoc
# vision models the PDF parser runs -- about 160 MB, fetched into the package
# directories their readers look in. Done here rather than at first use so the
# image is self-contained: a container that had to reach huggingface the first
# time somebody uploaded a PDF is one that fails in a deployment with no egress,
# long after the build that could have said so.
#
# RAVEN_RESOURCES=0 builds a smaller image without them. What it costs is
# Chinese segmentation and PDF layout; both report their own absence.
#
# What it also costs is the licence question: one of the four models,
# layout.onnx, stamps AGPL-3.0 into its own metadata. Baking it in here is the
# operator's own fetch inside a container, but an image that is then pushed to
# a registry is redistribution. NOTICES.md, "deepdoc models", has the detail.
ARG RAVEN_RESOURCES="1"
RUN --mount=type=cache,target=/root/.cache/uv \
set -eu; \
if [ "${RAVEN_RESOURCES}" = "1" ]; then \
uv run --frozen --no-dev python scripts/fetch_resources.py; \
fi
# The distributables beside the host wheel, discovered through the
# `raven.plugins` entry-point group. everos-memory by default: the gateway
# starts the everos memory server on boot, so without it memory is the one
# capability that silently does nothing. ppt-engine and design-engine are
# opt-in -- between them they add matplotlib, pymupdf and cairo.
ARG RAVEN_PLUGINS="everos-memory"
RUN --mount=type=cache,target=/root/.cache/uv \
set -eu; \
if [ -n "${RAVEN_PLUGINS}" ]; then \
paths=""; \
for plugin in $(echo "${RAVEN_PLUGINS}" | tr ',' ' '); do paths="${paths} ./plugins-dist/${plugin}"; done; \
uv pip install ${paths}; \
fi
# --- runtime ----------------------------------------------------------------
FROM python:${PYTHON_VERSION}-slim AS runtime
LABEL org.opencontainers.image.title="Raven" \
org.opencontainers.image.description="AI-native agent: web page, RPC gateway and IM channels in one container." \
org.opencontainers.image.licenses="Apache-2.0"
# nginx serves the page and proxies the engine. curl is the health check and
# the sign-in mint. git and ca-certificates are for the agent itself: the
# in-container sandbox backend is the container, so agent commands run in this
# filesystem and a workspace is usually a repository.
#
# procps is not optional here. The slim base ships no `ps`, and the everos
# memory server identifies processes with `ps -ww -p <pid> -o command=`
# (raven_everos/server.py). A missing binary is caught and read as "no match",
# so both callers get a wrong answer that looks like a normal one: the lock
# holder reads as absent, so a running server is invisible and a second one is
# started onto a lock it then dies on; and a stop declares STOPPED on its first
# poll and unlinks the pidfile while the process is still draining. `top`,
# `kill` and `free` come with the package, which is what an operator in
# `docker exec` was reaching for.
#
# lsof is the same module's lock-holder probe. Its port lookup falls back to
# /proc/net; the holder lookup has no fallback. ripgrep is what the agent's
# file search runs when it is there -- without it the tool degrades to a Python
# scan of the tree.
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
ca-certificates \
curl \
gettext-base \
git \
lsof \
nginx \
procps \
ripgrep \
tini \
&& rm -rf /var/lib/apt/lists/* \
&& rm -f /etc/nginx/sites-enabled/default
# LibreOffice, because the page cannot draw a .docx, .xlsx or .pptx and the
# gateway converts those to PDF to show them (raven/rpc/pdf_preview.py through
# raven/utils/office.py). Without it every office file in a knowledge base --
# and every deck -- answers the viewer with "LibreOffice is not installed",
# which is a hosted image telling its user to go and install something on it.
#
# The three app packages rather than the `libreoffice` metapackage: the filters
# for Writer, Calc and Impress formats are what a conversion needs, and the
# metapackage adds Base, Draw, Math and a JRE that no conversion here opens.
# --no-install-recommends holds that line.
#
# Fonts are not optional decoration. A slim image ships none, and LibreOffice
# renders every glyph it cannot find as a box -- so a Chinese document, which
# this project expects, would convert to a PDF of tofu. Noto CJK covers those
# and DejaVu the Latin text beside them.
#
# It is the largest thing in this image (roughly 500 MB with the fonts). Build
# with --build-arg RAVEN_OFFICE=0 for a deployment that will never preview an
# office file; the viewer then says what is missing and how to add it, which is
# the same message a host without it gives today.
ARG RAVEN_OFFICE=1
RUN if [ "${RAVEN_OFFICE}" = "1" ]; then \
apt-get update \
&& apt-get install -y --no-install-recommends \
fonts-dejavu-core \
fonts-noto-cjk \
libreoffice-calc \
libreoffice-impress \
libreoffice-writer \
&& rm -rf /var/lib/apt/lists/*; \
fi
# /data is the single persistence root: HOME and RAVEN_HOME both point into it,
# so config.json, the workspace, sessions, logs and the memory store land under
# one mountable volume.
ENV HOME=/data \
RAVEN_HOME=/data/.raven \
PATH="/app/.venv/bin:${PATH}" \
PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
RAVEN_PAGE_PORT=18792 \
RAVEN_DIST_ROOT=/app/ui-web/dist
WORKDIR /app
COPY --from=deps /app /app
COPY --from=web /build/ui-web/dist /app/ui-web/dist
COPY docker/nginx/nginx.conf docker/nginx/proxy.conf /etc/nginx/
COPY docker/nginx/raven.conf.template /etc/nginx/templates/raven.conf.template
COPY docker/entrypoint.sh /usr/local/bin/docker-entrypoint.sh
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
# Root, deliberately: nginx as packaged writes its pid and logs under paths a
# non-root user cannot take, and the agent's own tools install into /data. The
# container is the isolation boundary here -- do not also publish the engine's
# port straight to a network you do not trust.
VOLUME ["/data"]
EXPOSE 80
# tini reaps what the agent spawns: the engine launches ACP agents and the
# everos memory server as children, and PID 1 in a shell would leave them.
ENTRYPOINT ["/usr/bin/tini", "--", "/usr/local/bin/docker-entrypoint.sh"]
CMD ["run"]