papers-app 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- papers/__init__.py +2 -0
- papers/__main__.py +3 -0
- papers/agent/__init__.py +2 -0
- papers/agent/loop.py +295 -0
- papers/agent/mcp_server.py +65 -0
- papers/agent/tools.py +329 -0
- papers/ai/__init__.py +67 -0
- papers/ai/anthropic_api.py +53 -0
- papers/ai/claude_code.py +126 -0
- papers/ai/codex.py +66 -0
- papers/ai/errors.py +13 -0
- papers/ai/openai_compat.py +124 -0
- papers/ai/providers.py +112 -0
- papers/ai/streaming.py +111 -0
- papers/bibtex.py +28 -0
- papers/chat/__init__.py +1 -0
- papers/chat/answer.py +92 -0
- papers/chat/store.py +178 -0
- papers/cli.py +50 -0
- papers/config.py +52 -0
- papers/db.py +120 -0
- papers/figures.py +234 -0
- papers/folders.py +56 -0
- papers/index.py +96 -0
- papers/ingest.py +128 -0
- papers/keys.py +103 -0
- papers/library.py +208 -0
- papers/llm.py +428 -0
- papers/main.py +73 -0
- papers/notes.py +73 -0
- papers/pdf.py +116 -0
- papers/routes/__init__.py +4 -0
- papers/routes/chat.py +166 -0
- papers/routes/goals.py +65 -0
- papers/routes/learning.py +195 -0
- papers/routes/notes.py +92 -0
- papers/routes/papers.py +213 -0
- papers/routes/reading.py +114 -0
- papers/routes/recall.py +34 -0
- papers/routes/settings.py +133 -0
- papers/scholar.py +128 -0
- papers/static/css/base.css +109 -0
- papers/static/css/chat.css +241 -0
- papers/static/css/components.css +285 -0
- papers/static/css/goals.css +85 -0
- papers/static/css/library.css +142 -0
- papers/static/css/map.css +182 -0
- papers/static/css/paper.css +138 -0
- papers/static/css/reader.css +256 -0
- papers/static/css/recall.css +69 -0
- papers/static/css/settings.css +36 -0
- papers/static/css/tokens.css +129 -0
- papers/static/index.html +44 -0
- papers/static/js/chat/answer.js +130 -0
- papers/static/js/chat/canvas.js +24 -0
- papers/static/js/chat/conversation.js +345 -0
- papers/static/js/chat/render.js +230 -0
- papers/static/js/chat/stream.js +39 -0
- papers/static/js/chat/widgets.js +161 -0
- papers/static/js/components/combobox.js +91 -0
- papers/static/js/components/folderpicker.js +95 -0
- papers/static/js/core.js +358 -0
- papers/static/js/covers.js +74 -0
- papers/static/js/highlights.js +181 -0
- papers/static/js/main.js +115 -0
- papers/static/js/notes.js +160 -0
- papers/static/js/pages/goals.js +149 -0
- papers/static/js/pages/library.js +144 -0
- papers/static/js/pages/map.js +84 -0
- papers/static/js/pages/recall.js +70 -0
- papers/static/js/pages/settings.js +225 -0
- papers/static/js/pages/shared.js +46 -0
- papers/static/js/paper/figures.js +62 -0
- papers/static/js/paper/overview.js +291 -0
- papers/static/js/paper/reader/bar.js +50 -0
- papers/static/js/paper/reader/cite.js +36 -0
- papers/static/js/paper/reader/finish.js +54 -0
- papers/static/js/paper/reader/highlighter.js +132 -0
- papers/static/js/paper/reader/keys.js +30 -0
- papers/static/js/paper/reader/layout.js +69 -0
- papers/static/js/paper/reader/panel.js +105 -0
- papers/static/js/paper/reader/progress.js +71 -0
- papers/static/js/paper/reader/route.js +17 -0
- papers/static/js/paper/reader/selection.js +113 -0
- papers/static/js/paper/reader/state.js +22 -0
- papers/static/js/paper/reader/terms.js +85 -0
- papers/static/js/paper/reader/views.js +126 -0
- papers/static/js/paper/reader/vocab.js +122 -0
- papers/static/js/paper/reader/zoom.js +29 -0
- papers/static/js/paper/reader.js +92 -0
- papers/static/js/paper/review.js +109 -0
- papers/static/js/paper/shared.js +23 -0
- papers/static/js/pdfview.js +128 -0
- papers/static/js/readprefs.js +123 -0
- papers/static/js/speech.js +39 -0
- papers/static/js/textview.js +52 -0
- papers/static/js/theme.js +52 -0
- papers/static/js/tracker.js +97 -0
- papers/store.py +100 -0
- papers_app-0.2.0.dist-info/METADATA +138 -0
- papers_app-0.2.0.dist-info/RECORD +104 -0
- papers_app-0.2.0.dist-info/WHEEL +4 -0
- papers_app-0.2.0.dist-info/entry_points.txt +3 -0
- papers_app-0.2.0.dist-info/licenses/LICENSE +21 -0
papers/agent/tools.py
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
"""The tools the agent can use while it answers (Deep mode). Each is a plain function on the paper being read:
|
|
2
|
+
read tools look things up precisely, and (later) act tools add notes, words and highlights.
|
|
3
|
+
|
|
4
|
+
Every tool has a short label shown to the reader while it runs ("Reading §3.5"), a JSON schema for the model, and
|
|
5
|
+
returns a Result: text for the model, an optional image (a figure it can look at), and a one-line summary for the
|
|
6
|
+
steps list.
|
|
7
|
+
"""
|
|
8
|
+
import base64
|
|
9
|
+
import re
|
|
10
|
+
from dataclasses import dataclass, field
|
|
11
|
+
|
|
12
|
+
from papers import db, figures, index
|
|
13
|
+
from papers import notes as notes_db
|
|
14
|
+
from papers.store import get_paper
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@dataclass
|
|
18
|
+
class Result:
|
|
19
|
+
text: str
|
|
20
|
+
summary: str = ""
|
|
21
|
+
image: bytes | None = None # PNG
|
|
22
|
+
data: dict = field(default_factory=dict) # for act tools: what was done (for Undo)
|
|
23
|
+
|
|
24
|
+
def image_b64(self) -> str:
|
|
25
|
+
return base64.b64encode(self.image).decode() if self.image else ""
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
# ---------- Finding things in the paper's text ----------
|
|
29
|
+
NUMBERED = re.compile(r"(?:^|\s)(\d{1,2}(?:\.\d{1,2}){0,3})\s+(?=[A-Z])")
|
|
30
|
+
SMALL = {"and", "of", "the", "for", "with", "to", "in", "on", "a", "an", "by", "via", "vs", "from", "as", "at", "or"}
|
|
31
|
+
NOT_HEADINGS = ("Figure", "Table", "Fig", "Eq", "Equation", "Algorithm", "Published", "Under", "Preprint", "Proceedings", "arXiv")
|
|
32
|
+
OPENERS = {"We", "In", "Here", "This", "The", "Our", "It", "As", "For", "These", "That", "To", "On", "A", "An", "If", "When",
|
|
33
|
+
"Available", "Note", "See", "Then", "Thus", "However", "Since", "From", "With", "At", "By", "Each", "All"}
|
|
34
|
+
SMALL_CAPS = re.compile(r"\b([A-Z]) ([A-Z]{2,})\b") # "I NTRODUCTION": small capitals split by the PDF's text
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _title(after: str) -> str:
|
|
38
|
+
"""A heading's title from the text after its number: words until the next sentence starts ("Encoder and Decoder
|
|
39
|
+
Stacks | The encoder is composed of...", "Embeddings and Softmax | Similarly to the...")."""
|
|
40
|
+
words = SMALL_CAPS.sub(r"\1\2", after).split()[:14]
|
|
41
|
+
lower = lambda w: w[:1].islower() # noqa: E731
|
|
42
|
+
title = []
|
|
43
|
+
for i, w in enumerate(words):
|
|
44
|
+
nxt = words[i + 1] if i + 1 < len(words) else ""
|
|
45
|
+
then = words[i + 2] if i + 2 < len(words) else ""
|
|
46
|
+
if w[:1].isdigit():
|
|
47
|
+
break # the next heading's number
|
|
48
|
+
if lower(w) and w.lower() not in SMALL:
|
|
49
|
+
break
|
|
50
|
+
# a capitalised word that starts a sentence: "The encoder", "Similarly to the", "Instead of using"
|
|
51
|
+
if i and w[:1].isupper() and lower(nxt) and (nxt.lower() not in SMALL or lower(then)):
|
|
52
|
+
break
|
|
53
|
+
title.append(w)
|
|
54
|
+
if len(title) == 8 or w.endswith((".", ":")):
|
|
55
|
+
break
|
|
56
|
+
title = " ".join(title).rstrip(".:")
|
|
57
|
+
return title.title() if title.isupper() and len(title) > 3 else title # "INTRODUCTION" → "Introduction"
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def sections(text: str) -> list[dict]:
|
|
61
|
+
"""The numbered sections found in the text, in order: [{number, title, start}]. Each must follow the one before
|
|
62
|
+
(3.1 after 3, 3.2 after 3.1.4, 4 after 3.2), so page numbers, dates and figure numbers are left out, and so are
|
|
63
|
+
running headers ("3 Published as a conference paper...") and sentences after a page number ("6 We show")."""
|
|
64
|
+
found, last = [], (0,)
|
|
65
|
+
for m in NUMBERED.finditer(text):
|
|
66
|
+
number = tuple(int(x) for x in m.group(1).split("."))
|
|
67
|
+
# what may come next: a first subsection, or the next number at any level, allowing a few missed headings
|
|
68
|
+
# (up to 4 within a section, 2 between top-level ones; a page number like 12 after 7 is too far)
|
|
69
|
+
nexts = {last + (1,)} | {last[:k] + (last[k] + d,) for k in range(len(last)) for d in range(1, 3 if k == 0 else 5)}
|
|
70
|
+
if number not in nexts:
|
|
71
|
+
continue
|
|
72
|
+
title = _title(text[m.end():m.end() + 200])
|
|
73
|
+
words = title.split()
|
|
74
|
+
if not title or words[0].rstrip(".:") in NOT_HEADINGS or title.endswith(","):
|
|
75
|
+
continue
|
|
76
|
+
if words[0] in OPENERS and (len(words) == 1 or words[1][:1].islower()):
|
|
77
|
+
continue # "6 We show...", "7 In this...": a sentence after a page number, not a heading
|
|
78
|
+
found.append({"number": ".".join(map(str, number)), "title": title, "start": m.start(1)})
|
|
79
|
+
last = number
|
|
80
|
+
return found
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def chunks(text: str, size: int = 900) -> list[dict]:
|
|
84
|
+
"""The text in overlapping pieces, each knowing its section."""
|
|
85
|
+
secs = sections(text)
|
|
86
|
+
out = []
|
|
87
|
+
for start in range(0, max(len(text), 1), size - 150):
|
|
88
|
+
piece = text[start:start + size]
|
|
89
|
+
if not piece.strip():
|
|
90
|
+
continue
|
|
91
|
+
sec = next((s for s in reversed(secs) if s["start"] <= start + 100), None)
|
|
92
|
+
out.append({"start": start, "text": piece, "section": f"§{sec['number']} {sec['title']}" if sec else ""})
|
|
93
|
+
return out
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _words(s: str) -> list[str]:
|
|
97
|
+
return [w for w in re.findall(r"[a-z0-9]+", s.lower()) if len(w) > 2]
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
# ---------- Read tools ----------
|
|
101
|
+
def search_paper(paper: dict, query: str, limit: int = 5) -> Result:
|
|
102
|
+
text = paper.get("text") or ""
|
|
103
|
+
if not text:
|
|
104
|
+
return Result("This paper has no text (no PDF was added).", "No text to search")
|
|
105
|
+
terms = set(_words(query))
|
|
106
|
+
scored = []
|
|
107
|
+
for c in chunks(text):
|
|
108
|
+
words = _words(c["text"])
|
|
109
|
+
score = sum(words.count(t) for t in terms) + 3 * sum(1 for t in terms if t in words)
|
|
110
|
+
if score:
|
|
111
|
+
scored.append((score, c))
|
|
112
|
+
scored.sort(key=lambda x: -x[0])
|
|
113
|
+
best = [c for _, c in scored[:limit]]
|
|
114
|
+
if not best:
|
|
115
|
+
return Result(f'Nothing in the paper matches "{query}".', f"Searched “{query}”: nothing")
|
|
116
|
+
body = "\n\n".join(f"[{c['section'] or 'start of the paper'}, about {round(100 * c['start'] / len(text))}% in]\n{c['text']}" for c in best)
|
|
117
|
+
return Result(body, f"Searched “{query}”")
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def read_section(paper: dict, section: str) -> Result:
|
|
121
|
+
text = paper.get("text") or ""
|
|
122
|
+
secs = sections(text)
|
|
123
|
+
want = section.strip().lstrip("§").strip().lower()
|
|
124
|
+
hit = next((s for s in secs if s["number"] == want.split()[0]), None) if want and want[0].isdigit() else None
|
|
125
|
+
hit = hit or next((s for s in secs if want and want in s["title"].lower()), None)
|
|
126
|
+
if not hit:
|
|
127
|
+
names = ", ".join(f"{s['number']} {s['title']}" for s in secs[:40])
|
|
128
|
+
return Result(f'No section "{section}". The sections are: {names or "(none found)"}', f"Looked for §{section}")
|
|
129
|
+
depth = hit["number"].count(".")
|
|
130
|
+
after = next((s for s in secs if s["start"] > hit["start"] and s["number"].count(".") <= depth), None)
|
|
131
|
+
body = text[hit["start"]:after["start"] if after else hit["start"] + 8000][:8000]
|
|
132
|
+
return Result(body, f"Read §{hit['number']} {hit['title']}")
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _figure(paper: dict, kind: str, number: int) -> dict | None:
|
|
136
|
+
return next((f for f in figures.for_paper(paper["id"]) if f["kind"] == kind and f["number"] == number), None)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def look_at_figure(paper: dict, number: int, kind: str = "figure") -> Result:
|
|
140
|
+
kind = "table" if kind.lower().startswith("tab") else "figure"
|
|
141
|
+
name = "Table" if kind == "table" else "Figure"
|
|
142
|
+
f = _figure(paper, kind, int(number))
|
|
143
|
+
if not f:
|
|
144
|
+
have = ", ".join(f"{'Table' if x['kind'] == 'table' else 'Figure'} {x['number']}" for x in figures.for_paper(paper["id"]))
|
|
145
|
+
return Result(f"{name} {number} wasn't found in the PDF. Found: {have or 'none'}.", f"Looked for {name} {number}")
|
|
146
|
+
image = (figures.folder(paper["id"]) / figures.image_name(kind, f["number"])).read_bytes()
|
|
147
|
+
text = paper.get("text") or ""
|
|
148
|
+
mentions = [m.start() for m in re.finditer(rf"\b{name}\s+{number}\b", text)][:3]
|
|
149
|
+
around = "\n…\n".join(text[max(0, i - 300):i + 400] for i in mentions)
|
|
150
|
+
return Result(f"{f['caption']} (page {f['page']}).\nWhere the paper mentions it:\n{around or '(not mentioned in the text)'}",
|
|
151
|
+
f"Looked at {name} {number}", image=image)
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def search_library(paper: dict, idea: str) -> Result:
|
|
155
|
+
found = [c for c in index.candidates(idea, limit=6) if c["id"] != paper["id"]]
|
|
156
|
+
if not found:
|
|
157
|
+
return Result("No other paper in the library matches.", f"Searched your library for “{idea}”")
|
|
158
|
+
body = "\n\n".join(f"{c['title']}: {c.get('snippet') or c.get('abstract', '')[:300]}" for c in found)
|
|
159
|
+
return Result(body, f"Searched your library for “{idea}”")
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def get_notes(paper: dict) -> Result:
|
|
163
|
+
items = notes_db.list_for(paper["id"])
|
|
164
|
+
if not items:
|
|
165
|
+
return Result("The reader has no notes or highlights on this paper.", "Read your notes: none")
|
|
166
|
+
body = "\n".join(f"- {'Highlight' if n['kind'] == 'highlight' else 'Note'}: {n['quote'] or ''} {n['body'] or ''}".strip() for n in items)
|
|
167
|
+
return Result(body, f"Read your notes ({len(items)})")
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def get_vocabulary(paper: dict) -> Result:
|
|
171
|
+
words = paper.get("glossary") or []
|
|
172
|
+
if not words:
|
|
173
|
+
return Result("No vocabulary list for this paper yet.", "Read the word list: empty")
|
|
174
|
+
return Result("\n".join(f"- {w['term']}: {w['meaning']}" for w in words), f"Read the word list ({len(words)})")
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
# ---------- Act tools: they change the reader's library, so each says what it did and can be undone ----------
|
|
178
|
+
def _notes_changed(paper_id: int) -> None:
|
|
179
|
+
from papers import library # late import: library pulls in a lot
|
|
180
|
+
library.refresh_notes_text(paper_id)
|
|
181
|
+
library.sync_paper(paper_id, follow_parent=False)
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def save_note(paper: dict, text: str, quote: str = "") -> Result:
|
|
185
|
+
text = text.strip()
|
|
186
|
+
if not text:
|
|
187
|
+
return Result("A note needs some text.", "Didn't save an empty note")
|
|
188
|
+
note = notes_db.add(paper["id"], kind="note", body=text[:6000], quote=quote.strip()[:2000], source="ai",
|
|
189
|
+
position=_position(paper, quote))
|
|
190
|
+
_notes_changed(paper["id"])
|
|
191
|
+
short = text if len(text) <= 60 else text[:60].rsplit(" ", 1)[0] + "…"
|
|
192
|
+
return Result("Saved the note.", f"Saved a note: “{short}”", data={"kind": "note", "id": note["id"], "label": f"Saved a note: “{short}”"})
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def highlight(paper: dict, quote: str, color: str = "yellow") -> Result:
|
|
196
|
+
quote = re.sub(r"\s+", " ", quote).strip()
|
|
197
|
+
text = re.sub(r"\s+", " ", paper.get("text") or "")
|
|
198
|
+
if len(quote) < 3 or quote.lower() not in text.lower():
|
|
199
|
+
return Result("That exact text isn't in the paper; quote it exactly, as it appears.", "Couldn't find the passage to highlight")
|
|
200
|
+
color = color if color in notes_db.COLORS else "yellow"
|
|
201
|
+
note = notes_db.add(paper["id"], kind="highlight", quote=quote[:2000], color=color, source="ai", position=_position(paper, quote))
|
|
202
|
+
_notes_changed(paper["id"])
|
|
203
|
+
short = quote if len(quote) <= 50 else quote[:50].rsplit(" ", 1)[0] + "…"
|
|
204
|
+
return Result("Highlighted it.", f"Highlighted “{short}”", data={"kind": "highlight", "id": note["id"], "label": f"Highlighted “{short}”"})
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def save_word(paper: dict, term: str, meaning: str, kind: str = "technical", example: str = "", translation: str = "") -> Result:
|
|
208
|
+
import json
|
|
209
|
+
|
|
210
|
+
from papers.store import update_paper
|
|
211
|
+
term = term.strip()
|
|
212
|
+
words = paper.get("glossary") or []
|
|
213
|
+
if not term or any(w["term"].lower() == term.lower() for w in words):
|
|
214
|
+
return Result(f"“{term}” is already in the word list.", f"“{term}” was already in the word list")
|
|
215
|
+
word = {"term": term, "kind": "english" if kind == "english" else "technical", "meaning": meaning.strip(),
|
|
216
|
+
"example": example.strip(), "translation": translation.strip(), "source": "ai"}
|
|
217
|
+
update_paper(paper["id"], glossary=json.dumps([*words, word]))
|
|
218
|
+
return Result(f"Added “{term}” to the word list.", f"Added “{term}” to the word list",
|
|
219
|
+
data={"kind": "word", "term": term, "label": f"Added “{term}” to the word list"})
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def add_paper(paper: dict, title: str) -> Result:
|
|
223
|
+
import asyncio
|
|
224
|
+
import threading
|
|
225
|
+
|
|
226
|
+
from fastapi import BackgroundTasks
|
|
227
|
+
|
|
228
|
+
from papers.routes.papers import PaperIn, create_paper
|
|
229
|
+
from papers.store import find_existing
|
|
230
|
+
title = title.strip()
|
|
231
|
+
if existing := find_existing({"title": title}):
|
|
232
|
+
return Result(f"“{title}” is already in the library (paper {existing}).", f"“{title}” was already in your library")
|
|
233
|
+
tasks = BackgroundTasks()
|
|
234
|
+
added = create_paper(PaperIn(title=title, source_paper_id=paper["id"]), tasks)
|
|
235
|
+
threading.Thread(target=lambda: asyncio.run(tasks()), daemon=True).start() # the AI reads it ahead, in the background
|
|
236
|
+
return Result(f"Added “{added['title']}” to the library, linked from this paper on the Map.",
|
|
237
|
+
f"Added “{added['title']}” to your library",
|
|
238
|
+
data={"kind": "paper", "id": added["id"], "label": f"Added “{added['title']}” to your library"})
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def _position(paper: dict, quote: str) -> float:
|
|
242
|
+
"""Where a quote is in the paper (0 to 1), so a note made by the AI sits in the right place in the list."""
|
|
243
|
+
text = paper.get("text") or ""
|
|
244
|
+
at = text.lower().find(quote.strip().lower()[:60]) if quote.strip() else -1
|
|
245
|
+
return round(at / len(text), 4) if at >= 0 and text else 0
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
# ---------- The registry ----------
|
|
249
|
+
def _schema(props: dict, required: list[str]) -> dict:
|
|
250
|
+
return {"type": "object", "properties": props, "required": required, "additionalProperties": False}
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
TOOLS = {
|
|
254
|
+
"search_paper": {
|
|
255
|
+
"fn": search_paper, "label": "Searching the paper",
|
|
256
|
+
"description": "Find passages in this paper about something (keywords). Returns the passages with their section.",
|
|
257
|
+
"schema": _schema({"query": {"type": "string", "description": "a few keywords"}}, ["query"]),
|
|
258
|
+
},
|
|
259
|
+
"read_section": {
|
|
260
|
+
"fn": read_section, "label": "Reading a section",
|
|
261
|
+
"description": "Read one section of the paper in full, by number ('3.5') or title. Lists the sections if not found.",
|
|
262
|
+
"schema": _schema({"section": {"type": "string"}}, ["section"]),
|
|
263
|
+
},
|
|
264
|
+
"look_at_figure": {
|
|
265
|
+
"fn": look_at_figure, "label": "Looking at a figure",
|
|
266
|
+
"description": "See one of the paper's figures or tables (the image, its caption and where the text mentions it).",
|
|
267
|
+
"schema": _schema({"number": {"type": "integer"}, "kind": {"type": "string", "enum": ["figure", "table"]}}, ["number", "kind"]),
|
|
268
|
+
},
|
|
269
|
+
"search_library": {
|
|
270
|
+
"fn": search_library, "label": "Searching your library",
|
|
271
|
+
"description": "Find the reader's other papers about an idea, to connect this paper to what they read before.",
|
|
272
|
+
"schema": _schema({"idea": {"type": "string"}}, ["idea"]),
|
|
273
|
+
},
|
|
274
|
+
"get_notes": {
|
|
275
|
+
"fn": get_notes, "label": "Reading your notes",
|
|
276
|
+
"description": "The reader's notes and highlights on this paper.", "schema": _schema({}, []),
|
|
277
|
+
},
|
|
278
|
+
"get_vocabulary": {
|
|
279
|
+
"fn": get_vocabulary, "label": "Reading the word list",
|
|
280
|
+
"description": "The paper's hard words with their meanings (what the reader is learning).", "schema": _schema({}, []),
|
|
281
|
+
},
|
|
282
|
+
# Act tools: only when the reader asks for it (or clearly wants it); each can be undone.
|
|
283
|
+
"save_note": {
|
|
284
|
+
"fn": save_note, "label": "Saving a note", "act": True,
|
|
285
|
+
"description": "Save a note on this paper for the reader (only when they ask, e.g. 'note that', 'save this'). "
|
|
286
|
+
"Optionally the passage it's about, quoted exactly.",
|
|
287
|
+
"schema": _schema({"text": {"type": "string"}, "quote": {"type": "string"}}, ["text", "quote"]),
|
|
288
|
+
},
|
|
289
|
+
"highlight": {
|
|
290
|
+
"fn": highlight, "label": "Highlighting", "act": True,
|
|
291
|
+
"description": "Highlight a passage of the paper for the reader (only when they ask). Quote it exactly as it appears.",
|
|
292
|
+
"schema": _schema({"quote": {"type": "string"}, "color": {"type": "string", "enum": list(notes_db.COLORS)}}, ["quote", "color"]),
|
|
293
|
+
},
|
|
294
|
+
"save_word": {
|
|
295
|
+
"fn": save_word, "label": "Adding a word", "act": True,
|
|
296
|
+
"description": "Add a word or phrase to the paper's word list, with its meaning here in simple English (when the "
|
|
297
|
+
"reader asks, or wants to learn it). kind: 'technical' or 'english' (a general English word).",
|
|
298
|
+
"schema": _schema({"term": {"type": "string"}, "meaning": {"type": "string"},
|
|
299
|
+
"kind": {"type": "string", "enum": ["technical", "english"]}, "example": {"type": "string"},
|
|
300
|
+
"translation": {"type": "string"}}, ["term", "meaning", "kind", "example", "translation"]),
|
|
301
|
+
},
|
|
302
|
+
"add_paper": {
|
|
303
|
+
"fn": add_paper, "label": "Adding a paper", "act": True,
|
|
304
|
+
"description": "Add a paper this one cites (or that the reader names) to their library, linked from this paper on "
|
|
305
|
+
"their Map. Only when the reader asks to read or add it. Give its exact title.",
|
|
306
|
+
"schema": _schema({"title": {"type": "string"}}, ["title"]),
|
|
307
|
+
},
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
def run(name: str, paper_id: int, args: dict) -> Result:
|
|
312
|
+
"""Run a tool by name; errors come back as text for the model, not exceptions."""
|
|
313
|
+
tool = TOOLS.get(name)
|
|
314
|
+
if not tool:
|
|
315
|
+
return Result(f"There is no tool called {name}.", f"Unknown tool {name}")
|
|
316
|
+
try:
|
|
317
|
+
paper = get_paper(paper_id)
|
|
318
|
+
return tool["fn"](paper, **{k: v for k, v in (args or {}).items() if k in tool["schema"]["properties"]})
|
|
319
|
+
except Exception as e: # the model sees what went wrong and can carry on
|
|
320
|
+
return Result(f"The tool failed: {e}", f"{tool['label']}: failed")
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
def specs() -> list[dict]:
|
|
324
|
+
return [{"name": n, "description": t["description"], "schema": t["schema"]} for n, t in TOOLS.items()]
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
def setting_vision() -> bool:
|
|
328
|
+
"""Whether to send figure images to OpenAI-compatible models (many local and free ones can't take images)."""
|
|
329
|
+
return db.setting("ai_vision") == "on"
|
papers/ai/__init__.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"""Talking to AI models. The prompts themselves (skim, explain, quiz, ...) are in papers/llm.py; this package
|
|
2
|
+
only gets a prompt to the model the user chose in Settings and its answer back.
|
|
3
|
+
|
|
4
|
+
providers.py the list of providers, which one is chosen, its key/model/address, is it ready
|
|
5
|
+
anthropic_api.py the Anthropic API (an API key)
|
|
6
|
+
claude_code.py a Claude subscription, through the official Claude Code CLI
|
|
7
|
+
codex.py a ChatGPT subscription, through the official Codex CLI
|
|
8
|
+
openai_compat.py everything else: OpenAI, Gemini, Mistral, Groq, OpenRouter, Ollama, LM Studio, ...
|
|
9
|
+
|
|
10
|
+
To add a provider that speaks the OpenAI chat API, add a line to PROVIDERS. For one that doesn't, add a
|
|
11
|
+
module with `parse()` and `stream()` like the others and a branch in the two functions below.
|
|
12
|
+
"""
|
|
13
|
+
from pydantic import BaseModel
|
|
14
|
+
|
|
15
|
+
from papers.ai import anthropic_api, claude_code, codex, openai_compat
|
|
16
|
+
from papers.ai.errors import AIError
|
|
17
|
+
from papers.ai.providers import (
|
|
18
|
+
BY_ID,
|
|
19
|
+
PROVIDERS,
|
|
20
|
+
ai_name,
|
|
21
|
+
backend,
|
|
22
|
+
claude_binary,
|
|
23
|
+
codex_binary,
|
|
24
|
+
configured,
|
|
25
|
+
openai_config,
|
|
26
|
+
provider,
|
|
27
|
+
setting,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
__all__ = ["AIError", "BY_ID", "PROVIDERS", "ai_name", "backend", "claude_binary", "codex_binary", "configured",
|
|
31
|
+
"list_models", "openai_config", "parse", "provider", "setting", "stream"]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def parse(prompt: str, schema: type[BaseModel], system: str = "", effort: str = "medium") -> BaseModel:
|
|
35
|
+
"""Ask the chosen model and get its answer as `schema` (structured output)."""
|
|
36
|
+
kind = backend()
|
|
37
|
+
if kind == "openai":
|
|
38
|
+
return openai_compat.parse(prompt, schema, system)
|
|
39
|
+
if kind == "codex":
|
|
40
|
+
return schema.model_validate(codex.run(prompt, system, schema.model_json_schema()))
|
|
41
|
+
if kind == "claude-code":
|
|
42
|
+
return schema.model_validate(claude_code.run(prompt, system, schema.model_json_schema(), effort))
|
|
43
|
+
return anthropic_api.parse(prompt, schema, system, effort)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def stream(system: list[dict], content: str | list[dict]):
|
|
47
|
+
"""The chosen model's answer as it is written: str pieces for the answer, Thinking pieces for its reasoning.
|
|
48
|
+
`content` is the question, or the conversation so far ([{role, content}], ending with the question).
|
|
49
|
+
|
|
50
|
+
`system` is a list of Anthropic-style text blocks; the paper goes in a cached block so asking
|
|
51
|
+
again about the same paper is cheap with the API. Other backends get the blocks joined."""
|
|
52
|
+
kind = backend()
|
|
53
|
+
joined = "\n\n".join(block["text"] for block in system)
|
|
54
|
+
if kind == "claude-code":
|
|
55
|
+
return claude_code.stream(content, joined)
|
|
56
|
+
if kind == "openai":
|
|
57
|
+
return openai_compat.stream(joined, content)
|
|
58
|
+
if kind == "codex":
|
|
59
|
+
return codex.stream(joined, content)
|
|
60
|
+
return anthropic_api.stream(system, content)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def list_models() -> list[str]:
|
|
64
|
+
"""The models the chosen provider offers, to pick from in Settings."""
|
|
65
|
+
if backend() == "api":
|
|
66
|
+
return anthropic_api.list_models()
|
|
67
|
+
return openai_compat.list_models()
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""The Anthropic API, with a key saved in Settings or ANTHROPIC_API_KEY (pay per use)."""
|
|
2
|
+
import anthropic
|
|
3
|
+
from pydantic import BaseModel
|
|
4
|
+
|
|
5
|
+
from papers.ai import providers
|
|
6
|
+
from papers.ai.errors import AIError
|
|
7
|
+
from papers.ai.streaming import as_messages
|
|
8
|
+
|
|
9
|
+
NOT_SET_UP = "Claude is not set up. In Settings, choose your Claude subscription, or set ANTHROPIC_API_KEY and restart."
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def client() -> anthropic.Anthropic:
|
|
13
|
+
# A key saved in Settings wins; otherwise the SDK uses ANTHROPIC_API_KEY.
|
|
14
|
+
return anthropic.Anthropic(api_key=providers.provider_setting("ai_key", "anthropic") or None)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def request(method: str, **kwargs):
|
|
18
|
+
try:
|
|
19
|
+
response = getattr(client().messages, method)(model=providers.claude_model(), max_tokens=16000, **kwargs)
|
|
20
|
+
except TypeError as e:
|
|
21
|
+
raise AIError(NOT_SET_UP) from e
|
|
22
|
+
except anthropic.APIError as e:
|
|
23
|
+
raise AIError(f"Claude request failed: {e}") from e
|
|
24
|
+
if response.stop_reason == "refusal":
|
|
25
|
+
raise AIError("The model declined this request.")
|
|
26
|
+
return response
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def parse(prompt: str, schema: type[BaseModel], system: str = "", effort: str = "medium") -> BaseModel:
|
|
30
|
+
return request("parse", system=system, output_config={"effort": effort},
|
|
31
|
+
messages=[{"role": "user", "content": prompt}], output_format=schema).parsed_output
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def stream(system: list[dict], content: str | list[dict], effort: str = "medium"):
|
|
35
|
+
"""The answer, piece by piece."""
|
|
36
|
+
try:
|
|
37
|
+
with client().messages.stream(model=providers.claude_model(), max_tokens=16000, system=system,
|
|
38
|
+
output_config={"effort": effort},
|
|
39
|
+
messages=as_messages(content)) as answer:
|
|
40
|
+
yield from answer.text_stream
|
|
41
|
+
if answer.get_final_message().stop_reason == "refusal":
|
|
42
|
+
raise AIError("The model declined this request.")
|
|
43
|
+
except TypeError as e:
|
|
44
|
+
raise AIError(NOT_SET_UP) from e
|
|
45
|
+
except anthropic.APIError as e:
|
|
46
|
+
raise AIError(f"Claude request failed: {e}") from e
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def list_models() -> list[str]:
|
|
50
|
+
try:
|
|
51
|
+
return sorted(m.id for m in client().models.list(limit=100))
|
|
52
|
+
except Exception as e: # the SDK's own errors (no key, refused key)
|
|
53
|
+
raise AIError(f"Couldn't list the models: {e}") from e
|
papers/ai/claude_code.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"""A Claude subscription (Pro, Max), through the official Claude Code CLI (`claude -p`) on this computer.
|
|
2
|
+
|
|
3
|
+
The CLI signs in with the user's own account and usage counts toward their plan. The app only runs the
|
|
4
|
+
unmodified CLI, with no tools, and never reads or passes on its credentials.
|
|
5
|
+
"""
|
|
6
|
+
import json
|
|
7
|
+
import os
|
|
8
|
+
import re
|
|
9
|
+
import subprocess
|
|
10
|
+
import tempfile
|
|
11
|
+
|
|
12
|
+
from papers.ai import providers
|
|
13
|
+
from papers.ai.errors import AIError
|
|
14
|
+
from papers.ai.streaming import Thinking, as_transcript
|
|
15
|
+
|
|
16
|
+
NOT_INSTALLED = ("Claude Code isn't installed. Install it (claude.com/claude-code), run `claude` once "
|
|
17
|
+
"and sign in with your Claude account, then try again.")
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _error(message: str | None) -> AIError:
|
|
21
|
+
message = message or "unknown error"
|
|
22
|
+
if re.search(r"auth|log ?in|oauth|credential", message, re.IGNORECASE):
|
|
23
|
+
return AIError("Claude Code isn't signed in. Open a terminal, run `claude`, type /login and sign in "
|
|
24
|
+
f"with your Claude account, then try again. ({message})")
|
|
25
|
+
return AIError(f"Claude Code: {message}")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _system_file(system: str) -> str:
|
|
29
|
+
# Long instructions (they contain the whole paper) go through a file: Windows caps a command
|
|
30
|
+
# line at ~32k characters ([WinError 206]).
|
|
31
|
+
with tempfile.NamedTemporaryFile("w", suffix=".txt", encoding="utf-8", delete=False) as f:
|
|
32
|
+
f.write(system)
|
|
33
|
+
return f.name
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _env() -> dict:
|
|
37
|
+
# Without these, Claude Code would bill an API key instead of the subscription.
|
|
38
|
+
return {k: v for k, v in os.environ.items() if k not in ("ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN")}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _args(binary: str, output: str, effort: str) -> list[str]:
|
|
42
|
+
return [binary, "-p", "--output-format", output, "--tools", "", "--strict-mcp-config",
|
|
43
|
+
"--no-session-persistence", "--model", providers.claude_model(), "--effort", effort]
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def run(prompt: str, system: str = "", schema: dict | None = None, effort: str = "medium"):
|
|
47
|
+
"""One non-interactive turn with no tools; returns parsed JSON (with a schema) or text."""
|
|
48
|
+
binary = providers.claude_binary()
|
|
49
|
+
if not binary:
|
|
50
|
+
raise AIError(NOT_INSTALLED)
|
|
51
|
+
args = _args(binary, "json", effort)
|
|
52
|
+
system_file = _system_file(system) if system else None
|
|
53
|
+
if system_file:
|
|
54
|
+
args += ["--system-prompt-file", system_file]
|
|
55
|
+
if schema:
|
|
56
|
+
args += ["--json-schema", json.dumps(schema)]
|
|
57
|
+
try:
|
|
58
|
+
done = subprocess.run(args, input=prompt, capture_output=True, text=True, encoding="utf-8",
|
|
59
|
+
env=_env(), cwd=tempfile.gettempdir(), timeout=900)
|
|
60
|
+
except subprocess.TimeoutExpired as e:
|
|
61
|
+
raise AIError("Claude Code took too long to answer.") from e
|
|
62
|
+
except OSError as e:
|
|
63
|
+
raise AIError(f"Couldn't start Claude Code: {e}") from e
|
|
64
|
+
finally:
|
|
65
|
+
if system_file:
|
|
66
|
+
os.unlink(system_file)
|
|
67
|
+
try:
|
|
68
|
+
out = json.loads(done.stdout)
|
|
69
|
+
except json.JSONDecodeError as e:
|
|
70
|
+
raise AIError(f"Claude Code failed: {(done.stderr or done.stdout).strip()[:300]}") from e
|
|
71
|
+
if out.get("is_error"):
|
|
72
|
+
raise _error(out.get("result"))
|
|
73
|
+
if not schema:
|
|
74
|
+
return out.get("result", "")
|
|
75
|
+
if out.get("structured_output") is not None:
|
|
76
|
+
return out["structured_output"]
|
|
77
|
+
text = out.get("result", "")
|
|
78
|
+
try:
|
|
79
|
+
return json.loads(text[text.index("{"): text.rindex("}") + 1])
|
|
80
|
+
except ValueError as e:
|
|
81
|
+
raise AIError("Claude Code didn't return the expected format. Try again.") from e
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def stream(prompt: str | list[dict], system: str = "", effort: str = "medium"):
|
|
85
|
+
"""A turn whose text is passed on piece by piece, as Claude writes it (its thinking as Thinking pieces)."""
|
|
86
|
+
prompt = as_transcript(prompt)
|
|
87
|
+
binary = providers.claude_binary()
|
|
88
|
+
if not binary:
|
|
89
|
+
raise AIError(NOT_INSTALLED)
|
|
90
|
+
args = _args(binary, "stream-json", effort) + ["--verbose", "--include-partial-messages"]
|
|
91
|
+
system_file = _system_file(system) if system else None
|
|
92
|
+
if system_file:
|
|
93
|
+
args += ["--system-prompt-file", system_file]
|
|
94
|
+
try:
|
|
95
|
+
proc = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
|
96
|
+
text=True, encoding="utf-8", env=_env(), cwd=tempfile.gettempdir())
|
|
97
|
+
except OSError as e:
|
|
98
|
+
if system_file:
|
|
99
|
+
os.unlink(system_file)
|
|
100
|
+
raise AIError(f"Couldn't start Claude Code: {e}") from e
|
|
101
|
+
wrote = False
|
|
102
|
+
try:
|
|
103
|
+
proc.stdin.write(prompt)
|
|
104
|
+
proc.stdin.close()
|
|
105
|
+
for line in proc.stdout:
|
|
106
|
+
try:
|
|
107
|
+
event = json.loads(line)
|
|
108
|
+
except json.JSONDecodeError:
|
|
109
|
+
continue
|
|
110
|
+
if event.get("type") == "stream_event":
|
|
111
|
+
e = event.get("event") or {}
|
|
112
|
+
delta = e.get("delta") or {}
|
|
113
|
+
if e.get("type") == "content_block_delta" and delta.get("type") == "text_delta":
|
|
114
|
+
wrote = True
|
|
115
|
+
yield delta["text"]
|
|
116
|
+
elif e.get("type") == "content_block_delta" and delta.get("type") == "thinking_delta":
|
|
117
|
+
yield Thinking(delta.get("thinking", ""))
|
|
118
|
+
elif event.get("type") == "result" and event.get("is_error"):
|
|
119
|
+
raise _error(event.get("result"))
|
|
120
|
+
if proc.wait(timeout=60) and not wrote:
|
|
121
|
+
raise AIError(f"Claude Code failed: {proc.stderr.read().strip()[:300]}")
|
|
122
|
+
finally:
|
|
123
|
+
if proc.poll() is None:
|
|
124
|
+
proc.kill()
|
|
125
|
+
if system_file:
|
|
126
|
+
os.unlink(system_file)
|
papers/ai/codex.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""A ChatGPT subscription (Plus, Pro), through OpenAI's official Codex CLI (`codex exec`), signed in with the
|
|
2
|
+
user's own account. It runs read-only with nothing it can change."""
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
import re
|
|
6
|
+
import shutil
|
|
7
|
+
import subprocess
|
|
8
|
+
import tempfile
|
|
9
|
+
|
|
10
|
+
from papers.ai import providers
|
|
11
|
+
from papers.ai.errors import AIError, json_in
|
|
12
|
+
from papers.ai.streaming import as_transcript
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def strict(schema: dict) -> dict:
|
|
16
|
+
"""The JSON Schema form strict structured output wants: every property required, nothing extra."""
|
|
17
|
+
if isinstance(schema, dict):
|
|
18
|
+
schema = {k: strict(v) for k, v in schema.items() if k not in ("default", "title")}
|
|
19
|
+
if schema.get("type") == "object" and "properties" in schema:
|
|
20
|
+
schema["additionalProperties"] = False
|
|
21
|
+
schema["required"] = list(schema["properties"])
|
|
22
|
+
elif isinstance(schema, list):
|
|
23
|
+
schema = [strict(v) for v in schema]
|
|
24
|
+
return schema
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def run(prompt: str, system: str = "", schema: dict | None = None) -> str | dict:
|
|
28
|
+
"""One `codex exec` run; the prompt goes in on stdin. Returns parsed JSON (with a schema) or text."""
|
|
29
|
+
binary = providers.codex_binary()
|
|
30
|
+
if not binary:
|
|
31
|
+
raise AIError("Codex isn't installed. Install it (npm install -g @openai/codex), run `codex login` "
|
|
32
|
+
"and sign in with your ChatGPT account, then try again.")
|
|
33
|
+
work = tempfile.mkdtemp(prefix="papers-codex-")
|
|
34
|
+
out_file = os.path.join(work, "answer.txt")
|
|
35
|
+
args = [binary, "exec", "-", "--skip-git-repo-check", "--sandbox", "read-only", "--ephemeral", "-o", out_file]
|
|
36
|
+
if model := providers.provider_setting("ai_model", "chatgpt-plan"):
|
|
37
|
+
args += ["--model", model]
|
|
38
|
+
if schema:
|
|
39
|
+
schema_file = os.path.join(work, "schema.json")
|
|
40
|
+
with open(schema_file, "w", encoding="utf-8") as f:
|
|
41
|
+
json.dump(strict(schema), f)
|
|
42
|
+
args += ["--output-schema", schema_file]
|
|
43
|
+
# Codex has no system prompt option; the instructions lead the prompt. Without these variables it
|
|
44
|
+
# uses the ChatGPT sign-in instead of billing an API key.
|
|
45
|
+
env = {k: v for k, v in os.environ.items() if k not in ("OPENAI_API_KEY", "CODEX_API_KEY")}
|
|
46
|
+
try:
|
|
47
|
+
done = subprocess.run(args, input=(f"{system}\n\n---\n\n{prompt}" if system else prompt), capture_output=True,
|
|
48
|
+
text=True, encoding="utf-8", env=env, cwd=work, timeout=900)
|
|
49
|
+
if done.returncode != 0:
|
|
50
|
+
message = (done.stderr or done.stdout or "").strip()[-400:]
|
|
51
|
+
if re.search(r"log ?in|auth|sign in|credential", message, re.IGNORECASE):
|
|
52
|
+
raise AIError(f"Codex isn't signed in. Open a terminal, run `codex login` and sign in with your ChatGPT account. ({message})")
|
|
53
|
+
raise AIError(f"Codex failed: {message or 'no details'}")
|
|
54
|
+
text = open(out_file, encoding="utf-8").read() if os.path.exists(out_file) else done.stdout
|
|
55
|
+
except subprocess.TimeoutExpired as e:
|
|
56
|
+
raise AIError("Codex took too long to answer.") from e
|
|
57
|
+
except OSError as e:
|
|
58
|
+
raise AIError(f"Couldn't start Codex: {e}") from e
|
|
59
|
+
finally:
|
|
60
|
+
shutil.rmtree(work, ignore_errors=True)
|
|
61
|
+
return json_in(text) if schema else text.strip()
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def stream(system: str, content: str | list[dict]):
|
|
65
|
+
"""Codex gives the answer only once it's complete, so it arrives in one piece."""
|
|
66
|
+
yield run(as_transcript(content), system)
|
papers/ai/errors.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import json
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class AIError(Exception):
|
|
5
|
+
"""Anything that kept the model from answering; the message is shown to the user as is."""
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def json_in(text: str) -> dict:
|
|
9
|
+
"""The JSON object inside a model's text answer (models sometimes wrap it in prose or code fences)."""
|
|
10
|
+
try:
|
|
11
|
+
return json.loads(text[text.index("{"): text.rindex("}") + 1])
|
|
12
|
+
except ValueError as e:
|
|
13
|
+
raise AIError("The model didn't answer in the expected format. Try again, or choose a stronger model.") from e
|