-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
884 lines (725 loc) · 32.4 KB
/
server.py
File metadata and controls
884 lines (725 loc) · 32.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
"""
AcademicAI OpenAI-kompatibler Proxy Server
Exponiert AcademicAI als OpenAI-kompatible API auf Port 11435.
OpenClaw (und andere Tools) können ihn wie jeden OpenAI-kompatiblen Provider nutzen.
Start:
py server.py
Endpoints:
GET /v1/models → Modell-Liste
POST /v1/chat/completions → Chat Completion (inkl. Streaming-Emulation)
GET /health → Health Check
"""
import os
import sys
import time
import uuid
import json
import logging
import re
import asyncio
import threading
from datetime import datetime, timezone
from pathlib import Path
from typing import Optional
import httpx
sys.path.insert(0, os.path.dirname(__file__))
from dotenv import load_dotenv
load_dotenv()
from fastapi import FastAPI, HTTPException, Request, Depends
from fastapi.responses import StreamingResponse, JSONResponse
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from starlette.concurrency import run_in_threadpool
import uvicorn
import academicai
from academicai.auth import get_base_url, get_headers
from academicai.tool_emulation import (
inject_tools_into_messages,
parse_tool_calls,
extract_respond_content,
format_arbitrary_json_as_codeblock,
format_arbitrary_json_for_humans,
build_tool_calls_response,
build_tool_calls_sse_chunks,
)
def _extract_text_content(msg_content) -> str:
"""Normalisiert OpenAI-Message-Content zu Plain-Text."""
if isinstance(msg_content, str):
return msg_content
if isinstance(msg_content, list):
parts = []
for item in msg_content:
if isinstance(item, dict) and item.get("type") == "text":
parts.append(item.get("text", ""))
return "\n".join(parts)
return ""
def _last_user_text(messages: list) -> str:
"""Liefert den letzten User-Text aus den Original-Messages."""
for m in reversed(messages or []):
if m.get("role") == "user":
return _extract_text_content(m.get("content"))
return ""
def _apply_post_tool_guard(messages: list, has_tools: bool) -> list:
"""
Stabilisiert den Follow-up-Schritt nach einem Tool-Result.
- Erfolgreiches Tool-Result: finale Antwort bevorzugen.
- Fehlerhaftes Tool-Result: Erfolg NICHT behaupten, sondern korrigierten
Tool-Call auslösen oder Fehler transparent melden.
"""
if not has_tools or not messages:
return messages
last = messages[-1] or {}
if last.get("role") != "tool":
return messages
tool_text = _extract_text_content(last.get("content")).lower()
has_error = any(tok in tool_text for tok in ["error:", "cannot parse", "failed", "not found", "exception"])
if has_error:
guard_text = (
"TOOL_RESULT_ERROR: The latest tool result contains an error. "
"Do NOT claim success. Either issue a corrected tool_call, or explain the failure clearly. "
"For mailbox envelope search, keep options before query, e.g. envelope list -s 50 \"from alerts@example.com\"."
)
else:
guard_text = (
"NO_FURTHER_TOOL_CALLS: You already received tool results. "
"Now produce the final user-facing answer. "
"Call another tool only if the latest tool result is clearly missing required data."
)
return [{"role": "system", "content": guard_text}] + messages
def _score_topic_match(user_text: str, topics: list) -> int:
txt = (user_text or "").lower()
score = 0
for t in (topics or []):
tok = str(t).strip().lower()
if tok and tok in txt:
score += 1
return score
def _load_skill_snippets() -> list:
try:
p = Path(SKILL_SNIPPETS_FILE)
if not p.exists():
return []
data = json.loads(p.read_text(encoding="utf-8"))
return data if isinstance(data, list) else []
except Exception as e:
log.warning(f"skill snippets load failed: {e}")
return []
def _inject_skill_snippet_context(messages: list, user_text: str) -> list:
"""Injiziert passende Skill-Snippets als kurze System-Message."""
if not ENABLE_SKILL_SNIPPETS:
return messages
snippets = _load_skill_snippets()
if not snippets:
return messages
scored = []
for s in snippets:
score = _score_topic_match(user_text, s.get("topics", []))
if score > 0 and s.get("snippet"):
scored.append((score, s))
if not scored:
return messages
scored.sort(key=lambda x: x[0], reverse=True)
selected = [s for _, s in scored[: max(1, SKILL_SNIPPETS_MAX)]]
selected_ids = [str(s.get("id", "snippet")) for s in selected]
log.info(f"skill snippet injection: selected_ids={selected_ids}")
parts = [
"SKILL CONTEXT (retrieved): Use this operational guidance when deciding tool calls."
]
for s in selected:
sid = s.get("id", "snippet")
parts.append(f"[{sid}] {s.get('snippet', '').strip()}")
msg = {"role": "system", "content": "\n\n".join(parts)}
return [msg] + messages
def _save_skill_snippets(snippets: list) -> None:
p = Path(SKILL_SNIPPETS_FILE)
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text(json.dumps(snippets, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
def _extract_learning_topics(user_text: str, limit: int) -> list[str]:
"""Leitet einfache Themen-Keywords aus User-Text ab (Variante 1, ohne Embeddings)."""
if not user_text:
return []
stopwords = {
"aber", "alle", "alles", "auch", "bitte", "dann", "dass", "deine", "deinen", "deiner",
"dem", "den", "der", "des", "die", "ein", "eine", "einer", "eines", "es", "für", "gibt",
"haben", "hier", "ich", "ihr", "ihre", "ihren", "ist", "kann", "können", "mal", "mit",
"nach", "noch", "oder", "schon", "sehr", "sind", "so", "und", "uns", "von", "was", "wie",
"wir", "wird", "wurde", "you", "your", "from", "that", "this", "have", "just", "tool",
}
words = re.findall(r"[a-zA-Z0-9äöüÄÖÜß_-]+", user_text.lower())
ranked = []
seen = set()
for w in words:
if len(w) < max(2, AUTO_SKILL_MIN_TOPIC_LEN):
continue
if w in stopwords:
continue
if w in seen:
continue
seen.add(w)
ranked.append(w)
if len(ranked) >= max(1, limit):
break
return ranked
def _upsert_auto_skill_snippet(snippets: list, tool_name: str, topics: list[str]) -> tuple[list, bool]:
"""Upsert für auto-generierte Snippets; erweitert Topics und Hit-Counter."""
if not tool_name:
return snippets, False
sid = f"auto:{tool_name}"
changed = False
for s in snippets:
if s.get("id") == sid:
existing_topics = [str(t).lower() for t in (s.get("topics") or []) if str(t).strip()]
merged = list(existing_topics)
for t in topics:
tl = str(t).lower().strip()
if tl and tl not in merged:
merged.append(tl)
changed = True
s["topics"] = merged
s["source"] = "auto"
s["hits"] = int(s.get("hits", 0)) + 1
s["last_updated"] = int(time.time())
changed = True
return snippets, changed
new_entry = {
"id": sid,
"source": "auto",
"hits": 1,
"last_updated": int(time.time()),
"topics": [str(t).lower().strip() for t in topics if str(t).strip()],
"snippet": (
f"If this intent appears, prefer tool `{tool_name}` first. "
"If tool output is insufficient, run a minimal follow-up tool call and then return a concise final answer."
),
}
snippets.append(new_entry)
return snippets, True
def _learn_skill_snippets_from_tool_calls(user_text: str, tool_calls: list[dict]) -> None:
"""Self-learning (Variante 1): keyword-basiertes Upsert in skill_snippets.json."""
if not ENABLE_AUTO_SKILL_LEARNING:
return
if not tool_calls:
return
topics = _extract_learning_topics(user_text, limit=AUTO_SKILL_TOPICS_PER_CALL)
if not topics:
return
tool_names = []
for c in tool_calls:
name = str((c or {}).get("name", "")).strip()
if name and name not in tool_names:
tool_names.append(name)
if not tool_names:
return
try:
snippets = _load_skill_snippets()
changed_any = False
for name in tool_names:
snippets, changed = _upsert_auto_skill_snippet(snippets, name, topics)
changed_any = changed_any or changed
if changed_any:
_save_skill_snippets(snippets)
log.info(f"skill snippet self-learning: updated tools={tool_names} topics={topics}")
except Exception as e:
log.warning(f"skill snippet self-learning failed: {e}")
def _is_mail_delete_exec_call(call: dict) -> bool:
"""Erkennt exec-Calls, die Himalaya-Mails löschen/verschieben."""
if not isinstance(call, dict) or call.get("name") != "exec":
return False
args = call.get("arguments") or {}
cmd = str(args.get("command", "")).lower()
# Sicherheitsrelevant: alle delete/move Varianten (inkl. Trash/Cabinet/andere Ordner)
return ("message delete" in cmd) or ("message move" in cmd)
def _enforce_write_before_mail_delete(tool_calls: list[dict]) -> tuple[list[dict], bool]:
"""
Safety-Guard für Batch-Tool-Calls:
Mail-Delete/Move (in beliebige Ordner, inkl. Trash/Cabinet) darf in derselben Batch
nur passieren, wenn vorher ein write/edit Call enthalten ist.
Returns: (filtered_calls, blocked_any)
"""
if not tool_calls:
return [], False
out = []
blocked_any = False
has_write_before = False
for c in tool_calls:
name = (c or {}).get("name")
if name in ("write", "edit"):
has_write_before = True
out.append(c)
continue
if _is_mail_delete_exec_call(c) and not has_write_before:
blocked_any = True
log.warning("blocked unsafe mail delete/move call without prior write/edit in same batch")
continue
out.append(c)
return out, blocked_any
def _is_human_readable_target(messages: list) -> bool:
"""
Heuristik: Nur bei menschlichen Zielkanälen JSON->Human-Text-Fallback aktivieren.
False für klar maschinelle Runs (z.B. cron).
True für typische Human-Channels (whatsapp/telegram/signal/discord/slack/webchat...).
"""
user_text = "\n".join(
_extract_text_content(m.get("content"))
for m in messages
if m.get("role") == "user"
).lower()
# Explizit maschineller Trigger
if "[cron:" in user_text:
return False
# Chat-Metadaten aus OpenClaw-User-Envelope (auch ohne system channel marker)
user_human_markers = [
"conversation info (untrusted metadata)",
'"is_group_chat": true',
'"is_group_chat": false',
'"conversation_label":',
'"sender": "+',
]
if any(marker in user_text for marker in user_human_markers):
return True
system_text = "\n".join(
_extract_text_content(m.get("content"))
for m in messages
if m.get("role") == "system"
).lower()
human_channel_markers = [
"channel=whatsapp", '"channel": "whatsapp"',
"channel=telegram", '"channel": "telegram"',
"channel=signal", '"channel": "signal"',
"channel=imessage", '"channel": "imessage"',
"channel=discord", '"channel": "discord"',
"channel=slack", '"channel": "slack"',
"channel=googlechat", '"channel": "googlechat"',
"channel=irc", '"channel": "irc"',
"channel=webchat", '"channel": "webchat"',
'"chat_type": "group"', '"chat_type": "direct"',
]
if any(marker in system_text for marker in human_channel_markers):
return True
# OpenClaw-Session ohne explizite Channel-Marker -> für Nutzer standardmäßig als human behandeln
if "you are a personal assistant running inside openclaw." in system_text:
return True
# Sonst eher API-/Maschinenverkehr
return False
# --- Config ---
PORT = int(os.environ.get("ACADEMICAI_PROXY_PORT", 11435))
API_KEY = os.environ.get("ACADEMICAI_PROXY_API_KEY", "academicai-proxy")
DEBUG_DUMPS = os.environ.get("ACADEMICAI_DEBUG_DUMPS", "false").lower() in ("1", "true", "yes", "on")
# Proxy-Defaults (nur wenn Client keinen Wert setzt)
DEFAULT_CHAT_TEMPERATURE = float(os.environ.get("ACADEMICAI_DEFAULT_CHAT_TEMPERATURE", "0.6"))
DEFAULT_TOOL_TEMPERATURE = float(os.environ.get("ACADEMICAI_DEFAULT_TOOL_TEMPERATURE", "0.1"))
DEFAULT_CHAT_VERBOSITY = os.environ.get("ACADEMICAI_DEFAULT_CHAT_VERBOSITY", "medium")
DEFAULT_TOOL_VERBOSITY = os.environ.get("ACADEMICAI_DEFAULT_TOOL_VERBOSITY", "low")
DEFAULT_TOOL_REASONING_EFFORT = os.environ.get("ACADEMICAI_DEFAULT_TOOL_REASONING_EFFORT", "low")
# Optionaler zweiter Pass: strukturiertes Ergebnis -> natürlichsprachliche Endantwort
ENABLE_HUMANIZATION_PASS = os.environ.get("ACADEMICAI_ENABLE_HUMANIZATION_PASS", "false").lower() in ("1", "true", "yes", "on")
HUMANIZATION_MODEL = os.environ.get("ACADEMICAI_HUMANIZATION_MODEL", "").strip()
HUMANIZATION_TEMPERATURE = float(os.environ.get("ACADEMICAI_HUMANIZATION_TEMPERATURE", "0.2"))
# Optional: skill snippet retrieval/injection to improve tool-call reliability
ENABLE_SKILL_SNIPPETS = os.environ.get("ACADEMICAI_ENABLE_SKILL_SNIPPETS", "false").lower() in ("1", "true", "yes", "on")
# NOTE: skill snippets are installation-specific runtime data (esp. with auto-learning enabled)
# Default location is under ./data/ (relative to the working directory).
SKILL_SNIPPETS_FILE = os.environ.get(
"ACADEMICAI_SKILL_SNIPPETS_FILE",
str(Path("data") / "skill_snippets.json"),
)
SKILL_SNIPPETS_MAX = int(os.environ.get("ACADEMICAI_SKILL_SNIPPETS_MAX", "1"))
# Optional: self-learning updates for skill_snippets.json (keyword-basiert, ohne Vektor-Index)
ENABLE_AUTO_SKILL_LEARNING = os.environ.get("ACADEMICAI_ENABLE_AUTO_SKILL_LEARNING", "false").lower() in ("1", "true", "yes", "on")
AUTO_SKILL_TOPICS_PER_CALL = int(os.environ.get("ACADEMICAI_AUTO_SKILL_TOPICS_PER_CALL", "6"))
AUTO_SKILL_MIN_TOPIC_LEN = int(os.environ.get("ACADEMICAI_AUTO_SKILL_MIN_TOPIC_LEN", "4"))
# Optional: Cost API cache (doku-konform via /api/v1/cost)
# Standardmäßig deaktiviert; aktiviert nur mit ACADEMICAI_ENABLE_COST_MONITORING=true
ENABLE_COST_MONITORING = os.environ.get("ACADEMICAI_ENABLE_COST_MONITORING", "false").lower() in ("1", "true", "yes", "on")
COST_CACHE_FILE = os.environ.get(
"ACADEMICAI_COST_CACHE_FILE",
str(Path("data") / "cost_cache.json"),
)
COST_CACHE_TTL_SECONDS = max(60, int(os.environ.get("ACADEMICAI_COST_CACHE_TTL_SECONDS", "600")))
COST_REFRESH_TIMEOUT_SECONDS = max(1.0, float(os.environ.get("ACADEMICAI_COST_REFRESH_TIMEOUT_SECONDS", "8")))
_cost_lock = threading.Lock()
_cost_refresh_in_flight = False
def _now_utc_iso() -> str:
return datetime.now(timezone.utc).isoformat()
def _parse_iso_ts(raw: str) -> Optional[datetime]:
if not raw:
return None
try:
value = raw.replace("Z", "+00:00")
return datetime.fromisoformat(value)
except Exception:
return None
def _safe_float(value) -> Optional[float]:
try:
return float(value)
except Exception:
return None
def _extract_cost_summary(payload: dict) -> dict:
root = payload if isinstance(payload, dict) else {}
data = root.get("data") if isinstance(root.get("data"), dict) else root
total_cost = _safe_float(data.get("totalCost"))
total_clients = data.get("totalClients")
costs = data.get("costs") if isinstance(data.get("costs"), list) else []
try:
total_clients = int(total_clients) if total_clients is not None else None
except Exception:
total_clients = None
return {
"total_cost": total_cost,
"total_clients": total_clients,
"cost_entries": len(costs),
}
def _read_cost_cache() -> dict:
p = Path(COST_CACHE_FILE)
if not p.exists():
return {}
try:
data = json.loads(p.read_text(encoding="utf-8"))
return data if isinstance(data, dict) else {}
except Exception:
return {}
def _write_cost_cache(cache: dict) -> None:
p = Path(COST_CACHE_FILE)
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text(json.dumps(cache, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
def _is_cost_cache_stale(cache: dict) -> bool:
ts = _parse_iso_ts(str(cache.get("updated_at", "")))
if ts is None:
return True
return (datetime.now(timezone.utc) - ts).total_seconds() > COST_CACHE_TTL_SECONDS
def _build_cost_headers(cache: dict) -> dict:
if not ENABLE_COST_MONITORING:
return {}
if not cache:
return {}
headers = {
"X-AcademicAI-Cost-Stale": "true" if _is_cost_cache_stale(cache) else "false",
}
updated_at = str(cache.get("updated_at", "")).strip()
if updated_at:
headers["X-AcademicAI-Cost-Updated-At"] = updated_at
total_cost = _safe_float(cache.get("total_cost"))
if total_cost is not None:
headers["X-AcademicAI-Total-Cost"] = f"{total_cost:.6f}".rstrip("0").rstrip(".")
total_clients = cache.get("total_clients")
if isinstance(total_clients, int):
headers["X-AcademicAI-Total-Clients"] = str(total_clients)
cost_entries = cache.get("cost_entries")
if isinstance(cost_entries, int):
headers["X-AcademicAI-Cost-Entries"] = str(cost_entries)
return headers
def _fetch_cost_snapshot() -> dict:
if not ENABLE_COST_MONITORING:
return {}
base_url = get_base_url().rstrip("/")
cost_url = f"{base_url}/api/v1/cost"
headers = dict(get_headers() or {})
headers.setdefault("Accept", "application/json")
with httpx.Client(timeout=COST_REFRESH_TIMEOUT_SECONDS, follow_redirects=True) as client:
resp = client.get(cost_url, headers=headers)
resp.raise_for_status()
payload = resp.json()
summary = _extract_cost_summary(payload)
return {
"updated_at": _now_utc_iso(),
"source": "live",
"raw": payload,
**summary,
}
def _refresh_cost_cache_sync() -> dict:
if not ENABLE_COST_MONITORING:
return _read_cost_cache()
with _cost_lock:
fresh = _fetch_cost_snapshot()
if fresh:
_write_cost_cache(fresh)
return fresh
async def _refresh_cost_cache_background() -> None:
global _cost_refresh_in_flight
try:
await run_in_threadpool(_refresh_cost_cache_sync)
except Exception as e:
log.warning(f"cost refresh failed: {e}")
finally:
_cost_refresh_in_flight = False
def _get_cost_cache_with_lazy_refresh() -> dict:
global _cost_refresh_in_flight
cache = _read_cost_cache()
if not ENABLE_COST_MONITORING:
return cache
if _is_cost_cache_stale(cache) and not _cost_refresh_in_flight:
try:
loop = asyncio.get_running_loop()
_cost_refresh_in_flight = True
loop.create_task(_refresh_cost_cache_background())
except RuntimeError:
# Kein laufender Loop (z.B. in unit tests) -> synchron vermeiden
pass
return cache
def _build_humanization_messages(original_user_query: str, structured_content: str) -> list:
"""Prompt für den optionalen zweiten LLM-Pass (Humanisierung)."""
system_msg = {
"role": "system",
"content": (
"You rewrite structured tool output into a natural final answer for a human chat. "
"Return only the final answer text for the user. "
"Do NOT include JSON, code blocks, field names, metadata, or debug info."
),
}
user_msg = {
"role": "user",
"content": (
f"Original user question:\n{original_user_query.strip() or '-'}\n\n"
f"Structured/tool-derived result:\n{structured_content.strip()}\n\n"
"Task: Write a concise, natural-language final reply for the user."
),
}
return [system_msg, user_msg]
async def _run_humanization_pass(model: str, original_user_query: str, structured_content: str) -> Optional[str]:
"""Führt optionalen zweiten LLM-Pass aus und liefert finalen Text zurück."""
try:
human_model = HUMANIZATION_MODEL or model
resp = await run_in_threadpool(
academicai.completion,
model=human_model,
messages=_build_humanization_messages(original_user_query, structured_content),
temperature=HUMANIZATION_TEMPERATURE,
)
text = (resp.choices[0].message.content or "").strip()
return text or None
except Exception as e:
log.warning(f"humanization pass failed, fallback to first-pass content: {e}")
return None
# --- Setup ---
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
log = logging.getLogger("academicai-proxy")
app = FastAPI(
title="AcademicAI Proxy",
description="OpenAI-kompatibler Proxy für AcademicAI",
version="1.0.0",
)
security = HTTPBearer(auto_error=False)
def verify_key(credentials: HTTPAuthorizationCredentials = Depends(security)):
if credentials is None or credentials.credentials != API_KEY:
raise HTTPException(status_code=401, detail="Unauthorized")
return credentials.credentials
# --- Health ---
@app.get("/health")
def health():
return {"status": "ok", "service": "academicai-proxy"}
@app.get("/internal/cost-status")
def cost_status(key: str = Depends(verify_key)):
cache = _get_cost_cache_with_lazy_refresh()
return {
"enabled": ENABLE_COST_MONITORING,
"total_cost": _safe_float(cache.get("total_cost")),
"total_clients": cache.get("total_clients"),
"cost_entries": cache.get("cost_entries"),
"updated_at": cache.get("updated_at"),
"is_stale": _is_cost_cache_stale(cache) if cache else True,
"source": cache.get("source", "cache" if cache else "none"),
}
# --- Models ---
@app.get("/v1/models")
async def list_models(key: str = Depends(verify_key)):
try:
return await run_in_threadpool(academicai.get_models)
except Exception as e:
log.error(f"get_models failed: {e}")
raise HTTPException(status_code=502, detail=str(e))
# --- Chat Completions ---
@app.post("/v1/chat/completions")
async def chat_completions(request: Request, key: str = Depends(verify_key)):
body = await request.json()
# Vollständiges Request-Dump für Debugging (optional via Env)
if DEBUG_DUMPS:
import json as _json
_dump_path = os.path.join(os.path.dirname(__file__), "last_request.json")
try:
with open(_dump_path, "w", encoding="utf-8") as _f:
_json.dump(body, _f, ensure_ascii=False, indent=2)
except Exception:
pass
model = body.get("model")
messages = list(body.get("messages") or [])
# Top-level "system" Parameter (z.B. von Anthropic-style Clients) → in messages einfügen
top_level_system = body.get("system")
if top_level_system and not any(m.get("role") == "system" for m in messages):
messages.insert(0, {"role": "system", "content": top_level_system})
original_user_query = _last_user_text(messages)
# Tools extrahieren — werden via Prompt-Injection emuliert
tools = body.get("tools") or body.get("functions") or []
has_tools = bool(tools)
if ("tool_choice" in body) and not has_tools:
log.warning("tool_choice provided without tools; ignoring tool emulation for this request")
log.info(f"incoming: model={model} stream={body.get('stream')} roles={[m.get('role') for m in messages]} tools={len(tools)} has_tools={has_tools}")
# Optional: passende Skill-Snippets injizieren (z.B. mailbox/email -> Himalaya wrapper)
if has_tools:
messages = _inject_skill_snippet_context(messages, user_text=original_user_query)
# Bei Follow-up nach Tool-Result finale Antwort stärker priorisieren
messages = _apply_post_tool_guard(messages, has_tools=has_tools)
# Tool-Definitionen in System-Prompt injizieren
if tools:
messages = inject_tools_into_messages(messages, tools)
if not model or not messages:
raise HTTPException(status_code=422, detail="model und messages sind Pflichtfelder")
want_stream = bool(body.get("stream"))
human_target_hint = _is_human_readable_target(messages)
cost_cache = _get_cost_cache_with_lazy_refresh()
response_headers = _build_cost_headers(cost_cache)
# Optionale Parameter weiterreichen — nur bekannte, AcademicAI-sichere Felder
# tools / tool_choice / functions werden via Prompt-Injection emuliert (nicht nativ weitergegeben)
optional = {}
for field in [
"temperature", "max_tokens", "max_completion_tokens",
"frequency_penalty", "presence_penalty",
"reasoning_effort", "verbosity", "seed", "stop",
]:
if field in body:
optional[field] = body[field]
# Sinnvolle Proxy-Defaults (nur falls Client nichts gesetzt hat)
if has_tools:
optional.setdefault("temperature", DEFAULT_TOOL_TEMPERATURE)
# GPT-5-Modelle profitieren bei Emulation von knapper, deterministischerem Stil
if model and "gpt-5" in model:
optional.setdefault("verbosity", DEFAULT_TOOL_VERBOSITY)
optional.setdefault("reasoning_effort", DEFAULT_TOOL_REASONING_EFFORT)
elif human_target_hint:
optional.setdefault("temperature", DEFAULT_CHAT_TEMPERATURE)
if model and "gpt-5" in model:
optional.setdefault("verbosity", DEFAULT_CHAT_VERBOSITY)
# response_format: bei Tool-Emulation JSON-Mode erzwingen,
# sonst Wert aus Request durchreichen (ausser json_schema)
if tools:
optional["response_format"] = {"type": "json_object"}
elif "response_format" in body:
rf = body.get("response_format")
if isinstance(rf, dict):
if rf.get("type") != "json_schema":
optional["response_format"] = rf
else:
log.warning("ignoring non-dict response_format from client")
# tailoredAiId via extra_body
if "extra_body" in body and "tailoredAiId" in body["extra_body"]:
optional["extra_body"] = {"tailoredAiId": body["extra_body"]["tailoredAiId"]}
try:
response = await run_in_threadpool(academicai.completion, model=model, messages=messages, **optional)
except Exception as e:
log.error(f"completion failed: model={model} error={e}")
raise HTTPException(status_code=502, detail=str(e))
completion_id = response.id
created_ts = response.created
resp_model = response.model
choice = response.choices[0]
content = choice.message.content or ""
finish_reason = choice.finish_reason or "stop"
usage = {
"prompt_tokens": response.usage.prompt_tokens,
"completion_tokens": response.usage.completion_tokens,
"total_tokens": response.usage.total_tokens,
}
# JSON-Mode Response verarbeiten (nur wenn Tools im Request waren)
tool_calls_data = []
blocked_unsafe_delete = False
human_target = human_target_hint
if has_tools:
tool_calls_data = parse_tool_calls(content)
if tool_calls_data:
tool_calls_data, blocked_unsafe_delete = _enforce_write_before_mail_delete(tool_calls_data)
names = [c.get("name", "?") for c in tool_calls_data]
log.info(f"tool_call(s) detected: count={len(tool_calls_data)} names={names}")
_learn_skill_snippets_from_tool_calls(original_user_query, tool_calls_data)
else:
# Kein Tool-Call — entweder {"action":"respond",...} oder Fallback
extracted = extract_respond_content(content)
if extracted is not None:
log.info(f"json_mode respond: content_len={len(extracted)}")
content = extracted
# GPT-5 liefert teils action=respond mit JSON-String in content.
# Auf Human-Targets trotzdem in natürlich lesbaren Text umformen.
if human_target:
humanized_from_content = format_arbitrary_json_for_humans(content)
if humanized_from_content is not None:
log.warning("json_mode respond: JSON-string content -> human text (human target)")
content = humanized_from_content
else:
# Letzter Fallback nur für human-readable Targets.
# Für maschinelle Flows (z.B. cron) bleibt raw content erhalten.
if human_target:
human_text = format_arbitrary_json_for_humans(content)
if human_text is not None:
log.warning(f"json_mode: arbitrary JSON -> human text (human target): {content[:80]}")
content = human_text
else:
# Fallback-Fallback: falls Rendern scheitert, wenigstens lesbar
codeblock = format_arbitrary_json_as_codeblock(content)
if codeblock is not None:
log.warning(f"json_mode: arbitrary JSON -> code block fallback (human target): {content[:80]}")
content = codeblock
else:
log.warning(f"json_mode parse failed, using raw content: {content[:120]}")
else:
log.warning("json_mode: arbitrary JSON on non-human target, keeping raw content")
# Klarer User-Text wenn ein unsicherer Delete-Call geblockt wurde
if has_tools and blocked_unsafe_delete and not tool_calls_data:
content = (
"Blocked unsafe mail action: message delete/move requires a prior write/edit "
"in the same tool-call batch."
)
finish_reason = "stop"
# Optionaler zweiter Pass: natürliche Endantwort für Human-Channels
if (
ENABLE_HUMANIZATION_PASS
and human_target
and has_tools
and not tool_calls_data
and (content or "").strip()
):
humanized = await _run_humanization_pass(model=resp_model, original_user_query=original_user_query, structured_content=content)
if humanized:
log.info(f"humanization pass applied: len_before={len(content)} len_after={len(humanized)}")
content = humanized
# Wenn Streaming gewünscht: Antwort als SSE emulieren
if want_stream:
def sse_generator():
if tool_calls_data:
# Tool-Call-Chunks im OpenAI-Streaming-Format
for chunk in build_tool_calls_sse_chunks(completion_id, created_ts, resp_model, tool_calls_data):
yield f"data: {json.dumps(chunk)}\n\n"
else:
# Normaler Text-Response als SSE
# Chunk 1: role delta
yield f"data: {json.dumps({'id': completion_id, 'object': 'chat.completion.chunk', 'created': created_ts, 'model': resp_model, 'choices': [{'index': 0, 'delta': {'role': 'assistant', 'content': ''}, 'finish_reason': None}]})}\n\n"
# Chunk 2: Content
yield f"data: {json.dumps({'id': completion_id, 'object': 'chat.completion.chunk', 'created': created_ts, 'model': resp_model, 'choices': [{'index': 0, 'delta': {'content': content}, 'finish_reason': None}]})}\n\n"
# Chunk 3: finish
yield f"data: {json.dumps({'id': completion_id, 'object': 'chat.completion.chunk', 'created': created_ts, 'model': resp_model, 'choices': [{'index': 0, 'delta': {}, 'finish_reason': finish_reason}], 'usage': usage})}\n\n"
yield "data: [DONE]\n\n"
return StreamingResponse(sse_generator(), media_type="text/event-stream", headers=response_headers)
# Kein Streaming: normaler JSON-Response
if tool_calls_data:
return JSONResponse(
content=build_tool_calls_response(completion_id, created_ts, resp_model, tool_calls_data, usage),
headers=response_headers,
)
return JSONResponse(
content={
"id": completion_id,
"object": "chat.completion",
"created": created_ts,
"model": resp_model,
"choices": [
{
"index": 0,
"message": {"role": "assistant", "content": content},
"finish_reason": finish_reason,
}
],
"usage": usage,
},
headers=response_headers,
)
# --- Start ---
if __name__ == "__main__":
log.info(f"AcademicAI Proxy startet auf Port {PORT}")
log.info("API-Key configured: yes")
uvicorn.run(app, host="127.0.0.1", port=PORT, log_level="info")