mooncak397 Claude Sonnet 5.5 commited on
Commit ·
5f42268
1
Parent(s): 6a730b0
M1: one-node LangGraph chat from system prompt, CLI, Gemini model name as setting, fake-model tests
Browse files- app/cli.py +16 -0
- app/graph.py +18 -0
- app/llm.py +16 -0
- app/prompts.py +10 -0
- docs/adr/005-models-and-free-quota.md +9 -0
- pyproject.toml +1 -0
- tests/test_graph.py +37 -0
app/cli.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Usage: uv run python -m app.cli "Where does Christopher work?" """
|
| 2 |
+
import sys
|
| 3 |
+
|
| 4 |
+
from app.graph import build_graph
|
| 5 |
+
from app.llm import get_model
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def ask(graph, question: str) -> str:
|
| 9 |
+
result = graph.invoke({"messages": [("user", question)]})
|
| 10 |
+
return result["messages"][-1].text
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
if __name__ == "__main__":
|
| 14 |
+
if len(sys.argv) != 2:
|
| 15 |
+
raise SystemExit('Usage: python -m app.cli "<question>"')
|
| 16 |
+
print(ask(build_graph(get_model()), sys.argv[1]))
|
app/graph.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""M1: a LangGraph graph with one node that answers from the system prompt. M2 adds the router."""
|
| 2 |
+
from langchain_core.messages import SystemMessage
|
| 3 |
+
from langgraph.graph import END, START, MessagesState, StateGraph
|
| 4 |
+
|
| 5 |
+
from app.prompts import system_prompt
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def build_graph(model):
|
| 9 |
+
prompt = system_prompt()
|
| 10 |
+
|
| 11 |
+
def answer(state: MessagesState):
|
| 12 |
+
return {"messages": [model.invoke([SystemMessage(prompt), *state["messages"]])]}
|
| 13 |
+
|
| 14 |
+
g = StateGraph(MessagesState)
|
| 15 |
+
g.add_node("answer", answer)
|
| 16 |
+
g.add_edge(START, "answer")
|
| 17 |
+
g.add_edge("answer", END)
|
| 18 |
+
return g.compile()
|
app/llm.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""The one place that builds the chat model. Model name and key come from the environment."""
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
from langchain.chat_models import init_chat_model
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def get_model():
|
| 9 |
+
load_dotenv() # reads .env if it exists; real env vars win
|
| 10 |
+
name = os.environ.get("MODEL_APP")
|
| 11 |
+
if not name:
|
| 12 |
+
raise SystemExit("MODEL_APP is not set. Copy .env.example to .env.")
|
| 13 |
+
if not (os.environ.get("GEMINI_API_KEY") or os.environ.get("GOOGLE_API_KEY")):
|
| 14 |
+
raise SystemExit("GEMINI_API_KEY is not set. Put it in .env (see README).")
|
| 15 |
+
# temperature 0: the same question should give the same answer (plan section 6)
|
| 16 |
+
return init_chat_model(name, model_provider="google_genai", temperature=0)
|
app/prompts.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
|
| 3 |
+
ROOT = Path(__file__).resolve().parent.parent
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def system_prompt() -> str:
|
| 7 |
+
"""Rules first, then the profile. Both files are public in the repo."""
|
| 8 |
+
rules = (ROOT / "prompts/rules.md").read_text()
|
| 9 |
+
profile = (ROOT / "profile/profile.md").read_text()
|
| 10 |
+
return f"{rules}\n\n# Profile\n\n{profile}"
|
docs/adr/005-models-and-free-quota.md
CHANGED
|
@@ -12,3 +12,12 @@ Decision:
|
|
| 12 |
- When the daily cap is hit, the chat shows a polite "back tomorrow" message with Christopher's LinkedIn link. The contact form keeps working, because it does not use the model.
|
| 13 |
|
| 14 |
Consequences: the eval workflow runs only on pull requests that touch `prompts/`, `profile/`, `app/` or `evals/`, plus on release tags, so CI cannot drain the live quota. CI uses its own Gemini key (same project) so it can be revoked on its own.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
- When the daily cap is hit, the chat shows a polite "back tomorrow" message with Christopher's LinkedIn link. The contact form keeps working, because it does not use the model.
|
| 13 |
|
| 14 |
Consequences: the eval workflow runs only on pull requests that touch `prompts/`, `profile/`, `app/` or `evals/`, plus on release tags, so CI cannot drain the live quota. CI uses its own Gemini key (same project) so it can be revoked on its own.
|
| 15 |
+
|
| 16 |
+
## M1 check (2026-11-22)
|
| 17 |
+
|
| 18 |
+
Checked in the Google AI Studio docs (pricing and models pages):
|
| 19 |
+
- `gemini-3.5-flash-lite` is a current model. Free tier: input and output "free of charge". "Used to improve our products": yes on the free tier, no on the paid tier. This matches the privacy rule in this project.
|
| 20 |
+
- `gemini-3.1-flash-lite` is also current. Older `gemini-2.5-flash` is deprecated and routes to `gemini-3.5-flash-lite` from 2026-12-01.
|
| 21 |
+
- Gemma 4 has a free tier, "free of charge". The docs page does not list a Gemma model ID. Check the ID in AI Studio at M3, when the judge is built.
|
| 22 |
+
- Requests per day and per minute are not on the public docs pages. The rate-limit page says limits are shown in the AI Studio dashboard and "not guaranteed". The numbers above ("about 500 per day on Flash-Lite", "about 14,400 on Gemma 4") come from third-party sources and are not confirmed. TODO: read the real numbers from the dashboard (login needed) and write them here.
|
| 23 |
+
- `MODEL_APP` has no default in code. `.env.example` suggests `gemini-3.5-flash-lite`.
|
pyproject.toml
CHANGED
|
@@ -14,3 +14,4 @@ dev = ["pytest>=9", "pyyaml>=6"]
|
|
| 14 |
|
| 15 |
[tool.pytest.ini_options]
|
| 16 |
testpaths = ["tests"]
|
|
|
|
|
|
| 14 |
|
| 15 |
[tool.pytest.ini_options]
|
| 16 |
testpaths = ["tests"]
|
| 17 |
+
pythonpath = ["."]
|
tests/test_graph.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""M1 tests: no key, no network. A fake chat model stands in for Gemini."""
|
| 2 |
+
import pytest
|
| 3 |
+
from langchain_core.language_models.fake_chat_models import GenericFakeChatModel
|
| 4 |
+
from langchain_core.messages import AIMessage, SystemMessage
|
| 5 |
+
|
| 6 |
+
from app.cli import ask
|
| 7 |
+
from app.graph import build_graph
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class RecordingModel(GenericFakeChatModel):
|
| 11 |
+
seen: list = [] # the messages the graph sent to the model
|
| 12 |
+
|
| 13 |
+
def _generate(self, messages, *a, **k):
|
| 14 |
+
self.seen.append(messages)
|
| 15 |
+
return super()._generate(messages, *a, **k)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def test_graph_sends_rules_and_profile_then_returns_the_answer():
|
| 19 |
+
model = RecordingModel(messages=iter([AIMessage("He works at DATEV.")]), seen=[])
|
| 20 |
+
assert ask(build_graph(model), "Where does Christopher work?") == "He works at DATEV."
|
| 21 |
+
system, user = model.seen[0]
|
| 22 |
+
assert isinstance(system, SystemMessage)
|
| 23 |
+
assert "version: v1" in system.content and "DATEV" in system.content # rules + profile
|
| 24 |
+
assert user.content == "Where does Christopher work?"
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def test_get_model_fails_clearly_without_settings(monkeypatch):
|
| 28 |
+
from app import llm
|
| 29 |
+
|
| 30 |
+
monkeypatch.setattr(llm, "load_dotenv", lambda: None) # ignore a real .env
|
| 31 |
+
for v in ("MODEL_APP", "GEMINI_API_KEY", "GOOGLE_API_KEY"):
|
| 32 |
+
monkeypatch.delenv(v, raising=False)
|
| 33 |
+
with pytest.raises(SystemExit, match="MODEL_APP"):
|
| 34 |
+
llm.get_model()
|
| 35 |
+
monkeypatch.setenv("MODEL_APP", "x")
|
| 36 |
+
with pytest.raises(SystemExit, match="GEMINI_API_KEY"):
|
| 37 |
+
llm.get_model()
|