Hermes Agent commited on
Commit
4e5e577
·
1 Parent(s): 18e0d8b

Add append-only chat.log + /logs endpoint

Browse files
Files changed (1) hide show
  1. app.py +37 -1
app.py CHANGED
@@ -16,6 +16,7 @@ if STATIC_DIR.exists():
16
  DATA_DIR = Path("/data")
17
  LLAMA_DIR = Path("/home/user/llama.cpp")
18
  MODELS_DIR = DATA_DIR / "models"
 
19
  LLAMA_PORT = 8080
20
  LLAMA_URL = f"http://127.0.0.1:{LLAMA_PORT}"
21
 
@@ -155,6 +156,16 @@ async def chat_completions(request: Request):
155
  if not server_running():
156
  raise HTTPException(status_code=503, detail="llama-server not running. POST /start first.")
157
  body = await request.json()
 
 
 
 
 
 
 
 
 
 
158
  # inject default max_tokens if missing
159
  if "max_tokens" not in body and "max_completion_tokens" not in body:
160
  body["max_tokens"] = 512
@@ -165,10 +176,35 @@ async def chat_completions(request: Request):
165
  async with httpx.AsyncClient(timeout=300.0) as client:
166
  try:
167
  response = await client.post(f"{LLAMA_URL}/v1/chat/completions", json=body)
168
- return Response(content=response.content, status_code=response.status_code, headers=dict(response.headers))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
  except httpx.ConnectError:
170
  raise HTTPException(status_code=503, detail="llama-server not reachable")
171
 
 
 
 
 
 
 
 
 
 
 
 
172
  @app.get("/v1/models")
173
  async def list_models():
174
  models = []
 
16
  DATA_DIR = Path("/data")
17
  LLAMA_DIR = Path("/home/user/llama.cpp")
18
  MODELS_DIR = DATA_DIR / "models"
19
+ CHAT_LOG = DATA_DIR / "chat.log"
20
  LLAMA_PORT = 8080
21
  LLAMA_URL = f"http://127.0.0.1:{LLAMA_PORT}"
22
 
 
156
  if not server_running():
157
  raise HTTPException(status_code=503, detail="llama-server not running. POST /start first.")
158
  body = await request.json()
159
+ # Log user messages
160
+ try:
161
+ with open(CHAT_LOG, "a", encoding="utf-8") as lf:
162
+ ts = time.strftime("%Y-%m-%d %H:%M:%S")
163
+ for msg in body.get("messages", []):
164
+ if msg.get("role") == "user":
165
+ lf.write(f"[{ts}] USER: {msg.get('content','')}\n")
166
+ except Exception:
167
+ pass
168
+
169
  # inject default max_tokens if missing
170
  if "max_tokens" not in body and "max_completion_tokens" not in body:
171
  body["max_tokens"] = 512
 
176
  async with httpx.AsyncClient(timeout=300.0) as client:
177
  try:
178
  response = await client.post(f"{LLAMA_URL}/v1/chat/completions", json=body)
179
+ content = response.content
180
+ # Log assistant response
181
+ try:
182
+ with open(CHAT_LOG, "a", encoding="utf-8") as lf:
183
+ ts = time.strftime("%Y-%m-%d %H:%M:%S")
184
+ resp_json = json.loads(content)
185
+ for choice in resp_json.get("choices", []):
186
+ msg = choice.get("message", {})
187
+ if msg.get("content"):
188
+ lf.write(f"[{ts}] ASSISTANT: {msg['content']}\n")
189
+ elif msg.get("tool_calls"):
190
+ lf.write(f"[{ts}] ASSISTANT-TOOL: {json.dumps(msg['tool_calls'], ensure_ascii=False)}\n")
191
+ except Exception:
192
+ pass
193
+ return Response(content=content, status_code=response.status_code, headers=dict(response.headers))
194
  except httpx.ConnectError:
195
  raise HTTPException(status_code=503, detail="llama-server not reachable")
196
 
197
+ @app.get("/logs")
198
+ def get_logs(lines: int = 100):
199
+ try:
200
+ if not CHAT_LOG.exists():
201
+ return {"lines": []}
202
+ with open(CHAT_LOG, "r", encoding="utf-8") as f:
203
+ all_lines = f.readlines()
204
+ return {"lines": [line.rstrip("\n") for line in all_lines[-lines:]]}
205
+ except Exception as e:
206
+ raise HTTPException(status_code=500, detail=str(e))
207
+
208
  @app.get("/v1/models")
209
  async def list_models():
210
  models = []