Skip to content

Commit 944f294

Browse files
Add Claude Code skill and AI Agents docs section
1 parent 0fd22db commit 944f294

2 files changed

Lines changed: 294 additions & 0 deletions

File tree

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
---
2+
name: physlibsearch
3+
description: Semantic search over Physlib, a formal Lean 4 library of physics theorems and definitions. Use when asked to find Lean 4 declarations related to physics or mathematics, look up formal proofs, retrieve theorem signatures, or browse the Physlib module hierarchy.
4+
---
5+
6+
# PhyslibSearch API Skill
7+
8+
PhyslibSearch is a semantic search engine over **Physlib**, a Lean 4 formal library of physics and mathematics. It lets you find theorems, definitions, lemmas, and instances by describing them in natural language — no Lean syntax required.
9+
10+
**Base URL**: `https://physlibsearch.net`
11+
12+
---
13+
14+
## When to use this skill
15+
16+
- The user asks to find a Lean 4 theorem, definition, or proof about a physics/math concept.
17+
- The user wants the formal statement (signature) of a known result (e.g. Newton's second law, Schrödinger equation).
18+
- The user wants to browse what Physlib covers in a specific area.
19+
- The user has a Lean 4 declaration name and wants its full record.
20+
21+
---
22+
23+
## Endpoints
24+
25+
### 1. Search — `POST /search`
26+
27+
The main endpoint. Submit one or more natural language queries, get ranked results.
28+
29+
**Request**
30+
```json
31+
{
32+
"query": ["Newton's second law"],
33+
"num_results": 10
34+
}
35+
```
36+
- `query`: array of strings (1–many queries); each runs independently and returns its own ranked list.
37+
- `num_results`: 1–150, default 10.
38+
39+
**Response**`list[list[QueryResult]]` (one list per query, ordered by relevance)
40+
```json
41+
[[
42+
{
43+
"result": {
44+
"module_name": ["Physlib", "Mechanics", "Newton"],
45+
"kind": "theorem",
46+
"name": ["Physlib", "Mechanics", "Newton", "secondLaw"],
47+
"signature": "theorem Physlib.Mechanics.Newton.secondLaw : ∀ (m F : ℝ), m > 0 → acceleration m F = F / m",
48+
"type": "Prop",
49+
"value": null,
50+
"docstring": null,
51+
"informal_name": "Newton's Second Law",
52+
"informal_description": "For a body of mass $m > 0$ and applied force $F$, the acceleration satisfies $a = F/m$."
53+
},
54+
"distance": 0.12
55+
}
56+
]]
57+
```
58+
59+
**Key fields**:
60+
- `distance` — cosine distance (lower = more relevant; 0 is perfect).
61+
- `informal_name` / `informal_description` — human-readable explanation (LaTeX math).
62+
- `signature` — the formal Lean 4 statement.
63+
- `name` — fully-qualified name as a string array (use with `/fetch`).
64+
- `kind``theorem`, `definition`, `lemma`, `instance`, `axiom`, etc.
65+
66+
**Example (curl)**
67+
```bash
68+
curl -s -X POST https://physlibsearch.net/search \
69+
-H "Content-Type: application/json" \
70+
-d '{"query": ["quantum harmonic oscillator energy levels"], "num_results": 5}'
71+
```
72+
73+
---
74+
75+
### 2. Query Expansion — `POST /expand`
76+
77+
Optionally call this **before** `/search` to improve results for technical or ambiguous queries. It uses HyDE (Hypothetical Document Embeddings): Gemini generates a plausible hypothetical Lean declaration, which is then used as the search vector.
78+
79+
**Request** — raw JSON string (the query itself, not an object)
80+
```json
81+
"Schrödinger equation for a free particle"
82+
```
83+
84+
**Response** — JSON string (expanded query or original on failure)
85+
```json
86+
"theorem Physlib.QuantumMechanics.FreeParticle.schrodinger : ..."
87+
```
88+
89+
**When to use**: For precise or highly technical queries (specific equation names, less common concepts). For broad natural-language queries, go straight to `/search`.
90+
91+
**Example flow**
92+
```bash
93+
# Step 1: expand
94+
EXPANDED=$(curl -s -X POST https://physlibsearch.net/expand \
95+
-H "Content-Type: application/json" \
96+
-d '"Schrödinger equation for a free particle"')
97+
98+
# Step 2: search with expanded query
99+
curl -s -X POST https://physlibsearch.net/search \
100+
-H "Content-Type: application/json" \
101+
-d "{\"query\": [$EXPANDED], \"num_results\": 5}"
102+
```
103+
104+
---
105+
106+
### 3. Fetch by Name — `POST /fetch`
107+
108+
Retrieve full records for known Lean declaration names. Use this when you already have a name from a prior search or from the user.
109+
110+
**Request**
111+
```json
112+
{
113+
"query": [
114+
["Physlib", "Mechanics", "Newton", "secondLaw"],
115+
["Physlib", "QuantumMechanics", "HarmonicOscillator", "energy"]
116+
]
117+
}
118+
```
119+
- Each entry is a `LeanName` — an array of strings forming the fully-qualified path.
120+
121+
**Response**`list[Record | null]` (null for names not found, order preserved)
122+
123+
**Example (curl)**
124+
```bash
125+
curl -s -X POST https://physlibsearch.net/fetch \
126+
-H "Content-Type: application/json" \
127+
-d '{"query": [["Physlib", "Mechanics", "Newton", "secondLaw"]]}'
128+
```
129+
130+
---
131+
132+
### 4. List Modules — `GET /modules`
133+
134+
Returns all top-level Physlib modules with a count of searchable declarations.
135+
136+
**Response**
137+
```json
138+
[
139+
{"name": ["Physlib", "Mechanics"], "count": 42},
140+
{"name": ["Physlib", "QuantumMechanics"], "count": 31}
141+
]
142+
```
143+
144+
**Example (curl)**
145+
```bash
146+
curl -s https://physlibsearch.net/modules
147+
```
148+
149+
---
150+
151+
### 5. Module Declarations — `POST /modules/declarations`
152+
153+
Returns all declarations in a specific module, ordered by source position.
154+
155+
**Request** — raw JSON array (the module name, not wrapped in an object)
156+
```json
157+
["Physlib", "Mechanics", "Newton"]
158+
```
159+
160+
**Response**`list[Record]` (same shape as search results, without `distance`)
161+
162+
**Example (curl)**
163+
```bash
164+
curl -s -X POST https://physlibsearch.net/modules/declarations \
165+
-H "Content-Type: application/json" \
166+
-d '["Physlib", "Mechanics", "Newton"]'
167+
```
168+
169+
---
170+
171+
## Best practices
172+
173+
### Write queries like you'd explain the concept to a physicist, not like Lean code
174+
175+
| Good | Avoid |
176+
|------|-------|
177+
| `"conservation of momentum"` | `"theorem momentum"` |
178+
| `"energy of a quantum harmonic oscillator"` | `"E_n = hbar omega (n + 1/2)"` |
179+
| `"Maxwell's equations in differential form"` | `"curl E = -dB/dt"` |
180+
| `"Euler-Lagrange equation"` | `"Lagrangian mechanics derivative"` |
181+
182+
### Batch multiple queries in one request
183+
184+
If you need to answer several related questions, send them together — each gets its own ranked list and it's a single round trip:
185+
```json
186+
{
187+
"query": [
188+
"kinetic energy theorem",
189+
"work-energy theorem",
190+
"conservation of mechanical energy"
191+
],
192+
"num_results": 5
193+
}
194+
```
195+
196+
### Use `/expand` for narrow technical queries
197+
198+
Good candidates for expansion: named equations (Schrödinger, Navier-Stokes, Boltzmann), specific physical constants, or queries where the first search returns low-relevance results (distance > 0.4).
199+
200+
### Interpret `distance` to judge relevance
201+
202+
| Distance | Interpretation |
203+
|----------|---------------|
204+
| < 0.15 | Strong match — likely exactly what was asked for |
205+
| 0.15–0.30 | Good match — closely related concept |
206+
| 0.30–0.45 | Partial match — topically related but may not be the right theorem |
207+
| > 0.45 | Weak match — consider rephrasing or using `/expand` |
208+
209+
### Show `informal_description` first, then `signature`
210+
211+
For human-facing answers, lead with `informal_description` (natural language + LaTeX) and offer the formal `signature` as supporting detail. Most users want to understand the result before reading Lean 4 syntax.
212+
213+
### Reconstruct the Lean import path from `name`
214+
215+
The `name` array maps directly to the Lean import path:
216+
- `["Physlib", "Mechanics", "Newton", "secondLaw"]``Physlib.Mechanics.Newton.secondLaw`
217+
- Users can reference this in their Lean 4 files with `import Physlib.Mechanics.Newton`
218+
219+
---
220+
221+
## Rate limits
222+
223+
| Endpoint | Limit |
224+
|----------|-------|
225+
| `/search` | 1 request/second |
226+
| `/fetch` | 10 requests/second |
227+
| `/expand` | 15 requests/minute |
228+
| `/modules` | 30 requests/minute |
229+
| `/modules/declarations` | 30 requests/minute |
230+
231+
For agents making multiple searches in a loop, add a short delay between `/search` calls or batch queries into a single request (preferred).
232+
233+
---
234+
235+
## Declaration kinds
236+
237+
Results can have these `kind` values:
238+
`theorem`, `definition`, `lemma`, `instance`, `axiom`, `structure`, `inductive`, `abbrev`, `opaque`, `example`, `proofWanted`, `classInductive`
239+
240+
For most physics queries, `theorem` and `definition` are the most common and useful.
241+
242+
---
243+
244+
## More
245+
246+
- **Live search**: https://physlibsearch.net
247+
- **API docs**: https://physlibsearch.net/docs
248+
- **Skill repo**: https://github.com/Kernel-Science/physlibsearch-skill
249+
- **Source**: https://github.com/Kernel-Science/physlibsearch

frontend/src/app/docs/page.tsx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export default function DocsPage() {
8484
["overview", "Overview"],
8585
["how-it-works", "How it works"],
8686
["api", "API Reference"],
87+
["ai-agents", "AI Agents"],
8788
["self-hosting", "Self-hosting"],
8889
["open-source", "Open source"],
8990
["next-steps", "Next steps"],
@@ -255,6 +256,50 @@ curl -s -X POST http://physlibsearch.net/search \\
255256
/>
256257
</Section>
257258

259+
<Section id="ai-agents" title="AI Agents">
260+
<p>
261+
PhyslibSearch ships a{" "}
262+
<a
263+
href="https://claude.ai/code"
264+
target="_blank"
265+
rel="noopener noreferrer"
266+
className="underline underline-offset-2 hover:text-foreground transition-colors"
267+
>
268+
Claude Code
269+
</a>{" "}
270+
skill that teaches any Claude agent how to call the API. Once installed, the agent can
271+
search Physlib, fetch declarations by name, and expand queries with HyDE — without any
272+
extra configuration.
273+
</p>
274+
<p className="text-foreground/50">Install the skill (one-time, any project):</p>
275+
<CodeBlock
276+
lang="bash"
277+
code={`mkdir -p ~/.claude/skills/physlibsearch && curl -fsSo ~/.claude/skills/physlibsearch/SKILL.md \\
278+
https://raw.githubusercontent.com/Kernel-Science/physlibsearch-skill/main/SKILL.md`}
279+
/>
280+
<p>
281+
Then invoke it in any Claude Code session with{" "}
282+
<code className="font-mono text-xs bg-foreground/8 px-1 py-0.5 rounded">/physlibsearch</code>,
283+
or just ask Claude to find a Lean theorem and it will load the skill automatically.
284+
</p>
285+
<p className="text-foreground/50">
286+
To use it in a custom agent or system prompt, paste the raw{" "}
287+
<code className="font-mono text-xs bg-foreground/8 px-1 py-0.5 rounded">SKILL.md</code>{" "}
288+
content directly into your agent&apos;s context. The skill covers all endpoints,
289+
example curl calls, query tips, and how to interpret the{" "}
290+
<code className="font-mono text-xs bg-foreground/8 px-1 py-0.5 rounded">distance</code>{" "}
291+
field.{" "}
292+
<a
293+
href="https://github.com/Kernel-Science/physlibsearch-skill"
294+
target="_blank"
295+
rel="noopener noreferrer"
296+
className="underline underline-offset-2 hover:text-foreground transition-colors"
297+
>
298+
View on GitHub ↗
299+
</a>
300+
</p>
301+
</Section>
302+
258303
<Section id="self-hosting" title="Self-hosting">
259304
<p>PhyslibSearch runs on three services:</p>
260305
<ul className="list-disc list-inside flex flex-col gap-1 pl-2">

0 commit comments

Comments
 (0)