fbdeme commited on
Commit
61e7787
ยท
verified ยท
1 Parent(s): 7d7a443

Preserve original agent runtime with verified inbound Llama transport

Browse files
Files changed (5) hide show
  1. Dockerfile +1 -1
  2. app.py +2 -1
  3. gateway-public-key.json +5 -0
  4. public_agent.py +57 -19
  5. requirements.txt +1 -2
Dockerfile CHANGED
@@ -6,7 +6,7 @@ WORKDIR /app
6
  COPY --chown=user:user requirements.txt .
7
  RUN pip install --no-cache-dir -r requirements.txt
8
 
9
- COPY --chown=user:user *.py public_datasets.json .
10
  USER user
11
  ENV HOME=/home/user \
12
  HF_HOME=/home/user/.cache/huggingface \
 
6
  COPY --chown=user:user requirements.txt .
7
  RUN pip install --no-cache-dir -r requirements.txt
8
 
9
+ COPY --chown=user:user *.py *.json .
10
  USER user
11
  ENV HOME=/home/user \
12
  HF_HOME=/home/user/.cache/huggingface \
app.py CHANGED
@@ -29,8 +29,9 @@ ALLOWED_ORIGINS = [
29
 
30
  app = FastAPI(title="ONTIC TabICL Demo API", version="2.0.0")
31
  app.include_router(upload_router)
32
- from public_agent import router as agent_router
33
  app.include_router(agent_router)
 
34
  app.add_middleware(
35
  CORSMiddleware,
36
  allow_origins=ALLOWED_ORIGINS,
 
29
 
30
  app = FastAPI(title="ONTIC TabICL Demo API", version="2.0.0")
31
  app.include_router(upload_router)
32
+ from public_agent import router as agent_router, local_router
33
  app.include_router(agent_router)
34
+ app.include_router(local_router)
35
  app.add_middleware(
36
  CORSMiddleware,
37
  allow_origins=ALLOWED_ORIGINS,
gateway-public-key.json ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ {
2
+ "kty": "OKP",
3
+ "crv": "Ed25519",
4
+ "x": "E1ty597n0p3yz7fQuf6saSgpBScZUl__sf53YTGpzw0"
5
+ }
public_agent.py CHANGED
@@ -6,9 +6,6 @@ from __future__ import annotations
6
  import asyncio
7
  import hashlib
8
  import base64
9
- import httpx
10
- import logging
11
- import requests
12
  import json
13
  import os
14
  import queue
@@ -24,7 +21,8 @@ import pandas as pd
24
  import rdflib
25
  from agents import Agent, OpenAIChatCompletionsModel, RunContextWrapper, function_tool
26
  from fastapi import APIRouter, Depends, HTTPException, Request
27
- from fastapi.responses import StreamingResponse
 
28
  from openai import AsyncOpenAI, OpenAI
29
  from pydantic import BaseModel, ConfigDict, Field
30
  from sklearn.metrics import roc_auc_score
@@ -44,13 +42,13 @@ RESULT_LOCK = threading.Lock()
44
  RESULT_DB = os.environ.get('ONTIC_PUBLIC_RESULTS_DB', '/tmp/ontic-public-results.sqlite')
45
 
46
 
47
- GATEWAY = 'https://ontic-page.cho-suwan96.workers.dev'
48
 
49
 
50
  def credentials(token):
51
  if not token:
52
  raise RuntimeError('Signed deployment grant is missing')
53
- return GATEWAY+'/internal/agent-llm/v1', token
54
 
55
 
56
  def tables(task):
@@ -152,7 +150,7 @@ def propose_features(ctx:RunContextWrapper,task:str,n:int=4,requirements:str='',
152
  """Use the LLM to synthesize feature programs, then validate against the ontology. task: churn|stockout|defect, n:1-8. summary: Korean status."""
153
  def run():
154
  if not 1<=n<=8:raise ValueError('n must be 1-8')
155
- result=llm_features(task,n,ctx.context.gateway_token,requirements);ctx.context.proposals[task]=result;return result
156
  return observe(ctx,'propose_features',run)
157
 
158
 
@@ -237,30 +235,31 @@ class PublicSession(SessionRuntime):
237
  steps=STEPS
238
  own_tools=set(STEPS)
239
  def __init__(self):
240
- super().__init__();self.gateway_token=None;self.proposals={};self.features={};self.runs={};self.touched=time.monotonic()
241
  def _agent(self,mcp_servers=()):
242
- base,key=credentials(self.gateway_token)
243
  return Agent(name='ONTIC',instructions=SYSTEM_PROMPT+'\n'+DEPLOYMENT_NOTES,tools=TOOLS,model=OpenAIChatCompletionsModel(model=MODEL,openai_client=AsyncOpenAI(base_url=base,api_key=key,timeout=120)))
244
 
245
  SESSIONS={}
246
  SESSIONS_LOCK=threading.Lock()
247
 
248
 
 
 
 
249
  async def authorize(request:Request):
250
- # Reuse the existing deployment HMAC key without exporting or copying it.
251
- # Only the Worker holds the key. This server receives short-lived grants and
252
- # asks the Worker to verify that the grant matches this exact request body.
253
  token=request.headers.get('authorization','').removeprefix('Bearer ')
254
  if not token or len(token)>2048:raise HTTPException(401,'Unauthorized')
255
  content=await request.body() if request.method=='POST' else request.url.path.encode()
256
  digest=base64.urlsafe_b64encode(hashlib.sha256(content).digest()).decode().rstrip('=')
257
  try:
258
- response=await asyncio.to_thread(requests.post,GATEWAY+'/internal/agent-verify',headers={'authorization':'Bearer '+token},json={'digest':digest},timeout=15)
259
- valid=response.status_code==200 and response.json().get('valid') is True
260
- if not valid:logging.getLogger('uvicorn.error').warning('agent grant rejected: verifier status=%s',response.status_code)
261
- except Exception as exc:
262
- logging.getLogger('uvicorn.error').warning('agent grant verification transport: %s: %s',type(exc).__name__,str(exc)[:500])
263
- valid=False
264
  if not valid:raise HTTPException(401,'Unauthorized')
265
  request.state.gateway_token=token
266
 
@@ -287,7 +286,6 @@ def turn(inp:ChatInput,request:Request):
287
  if s.busy():raise HTTPException(409,'์ด์ „ ์‘๋‹ต์ด ์ง„ํ–‰ ์ค‘์ž…๋‹ˆ๋‹ค.')
288
  if sum(x.busy() for x in SESSIONS.values())>=2:raise HTTPException(429,'๋‹ค๋ฅธ ๋ถ„์„์ด ์ง„ํ–‰ ์ค‘์ž…๋‹ˆ๋‹ค. ์ž ์‹œ ํ›„ ๋‹ค์‹œ ์‹œ๋„ํ•ด ์ฃผ์„ธ์š”.')
289
  s.touched=time.monotonic()
290
- s.gateway_token=request.state.gateway_token
291
  try:
292
  if inp.approve is not None:
293
  if inp.message is not None:raise HTTPException(422,'Choose message or approval')
@@ -315,3 +313,43 @@ def report(run_id:str):
315
  row=con.execute('SELECT result FROM reports WHERE id=? AND created>?',(run_id,time.time()-86400)).fetchone()
316
  if not row:raise HTTPException(404,'Report expired')
317
  return json.loads(row[0])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  import asyncio
7
  import hashlib
8
  import base64
 
 
 
9
  import json
10
  import os
11
  import queue
 
21
  import rdflib
22
  from agents import Agent, OpenAIChatCompletionsModel, RunContextWrapper, function_tool
23
  from fastapi import APIRouter, Depends, HTTPException, Request
24
+ from fastapi.responses import StreamingResponse, JSONResponse
25
+ from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey
26
  from openai import AsyncOpenAI, OpenAI
27
  from pydantic import BaseModel, ConfigDict, Field
28
  from sklearn.metrics import roc_auc_score
 
42
  RESULT_DB = os.environ.get('ONTIC_PUBLIC_RESULTS_DB', '/tmp/ontic-public-results.sqlite')
43
 
44
 
45
+ PUBLIC_KEY_PATH = Path(__file__).with_name('gateway-public-key.json')
46
 
47
 
48
  def credentials(token):
49
  if not token:
50
  raise RuntimeError('Signed deployment grant is missing')
51
+ return 'http://127.0.0.1:7860/local-llm/v1', token
52
 
53
 
54
  def tables(task):
 
150
  """Use the LLM to synthesize feature programs, then validate against the ontology. task: churn|stockout|defect, n:1-8. summary: Korean status."""
151
  def run():
152
  if not 1<=n<=8:raise ValueError('n must be 1-8')
153
+ result=llm_features(task,n,ctx.context.id,requirements);ctx.context.proposals[task]=result;return result
154
  return observe(ctx,'propose_features',run)
155
 
156
 
 
235
  steps=STEPS
236
  own_tools=set(STEPS)
237
  def __init__(self):
238
+ super().__init__();self.proposals={};self.features={};self.runs={};self.model_requests={};self.touched=time.monotonic()
239
  def _agent(self,mcp_servers=()):
240
+ base,key=credentials(self.id)
241
  return Agent(name='ONTIC',instructions=SYSTEM_PROMPT+'\n'+DEPLOYMENT_NOTES,tools=TOOLS,model=OpenAIChatCompletionsModel(model=MODEL,openai_client=AsyncOpenAI(base_url=base,api_key=key,timeout=120)))
242
 
243
  SESSIONS={}
244
  SESSIONS_LOCK=threading.Lock()
245
 
246
 
247
+ def decode64(value):
248
+ return base64.urlsafe_b64decode(value+'='*(-len(value)%4))
249
+
250
  async def authorize(request:Request):
251
+ # Public material only: the existing secret and derived private key stay in the Worker.
 
 
252
  token=request.headers.get('authorization','').removeprefix('Bearer ')
253
  if not token or len(token)>2048:raise HTTPException(401,'Unauthorized')
254
  content=await request.body() if request.method=='POST' else request.url.path.encode()
255
  digest=base64.urlsafe_b64encode(hashlib.sha256(content).digest()).decode().rstrip('=')
256
  try:
257
+ value,signature=token.split('.')
258
+ public=json.loads(PUBLIC_KEY_PATH.read_text())
259
+ Ed25519PublicKey.from_public_bytes(decode64(public['x'])).verify(decode64(signature),value.encode())
260
+ claims=json.loads(decode64(value))
261
+ valid=claims['kind']=='agent' and claims['digest']==digest and claims['expires']>time.time()*1000
262
+ except Exception:valid=False
263
  if not valid:raise HTTPException(401,'Unauthorized')
264
  request.state.gateway_token=token
265
 
 
286
  if s.busy():raise HTTPException(409,'์ด์ „ ์‘๋‹ต์ด ์ง„ํ–‰ ์ค‘์ž…๋‹ˆ๋‹ค.')
287
  if sum(x.busy() for x in SESSIONS.values())>=2:raise HTTPException(429,'๋‹ค๋ฅธ ๋ถ„์„์ด ์ง„ํ–‰ ์ค‘์ž…๋‹ˆ๋‹ค. ์ž ์‹œ ํ›„ ๋‹ค์‹œ ์‹œ๋„ํ•ด ์ฃผ์„ธ์š”.')
288
  s.touched=time.monotonic()
 
289
  try:
290
  if inp.approve is not None:
291
  if inp.message is not None:raise HTTPException(422,'Choose message or approval')
 
313
  row=con.execute('SELECT result FROM reports WHERE id=? AND created>?',(run_id,time.time()-86400)).fetchone()
314
  if not row:raise HTTPException(404,'Report expired')
315
  return json.loads(row[0])
316
+
317
+
318
+ class ModelResult(BaseModel):
319
+ model_config=ConfigDict(extra='forbid')
320
+ sid:str=Field(pattern=r'^[0-9a-f]{32}$')
321
+ request_id:str=Field(pattern=r'^[0-9a-f]{32}$')
322
+ result:dict
323
+
324
+ @router.post('/model-result')
325
+ def model_result(inp:ModelResult):
326
+ session=SESSIONS.get(inp.sid)
327
+ pending=session.model_requests.get(inp.request_id) if session else None
328
+ if not pending:raise HTTPException(404,'Unknown model request')
329
+ pending['result']=inp.result;pending['ready'].set()
330
+ return {'ok':True}
331
+
332
+ local_router=APIRouter()
333
+
334
+ @local_router.post('/local-llm/v1/chat/completions')
335
+ async def local_completion(request:Request):
336
+ if not request.client or request.client.host not in ('127.0.0.1','::1'):raise HTTPException(403,'Loopback only')
337
+ sid=request.headers.get('authorization','').removeprefix('Bearer ')
338
+ session=SESSIONS.get(sid)
339
+ if not session:raise HTTPException(401,'Unknown local agent session')
340
+ data=await request.json()
341
+ rid=secrets.token_hex(16);pending={'ready':threading.Event(),'result':None}
342
+ session.model_requests[rid]=pending
343
+ session._emit(type='model_request',data={'sid':sid,'request_id':rid,'input':data})
344
+ try:
345
+ ready=await asyncio.to_thread(pending['ready'].wait,120)
346
+ if not ready:raise HTTPException(504,'Llama response timed out')
347
+ result=pending['result']
348
+ if 'error' in result:return JSONResponse(result,status_code=502)
349
+ if not data.get('stream'):return result
350
+ choice=result['choices'][0];message=choice['message'];delta=dict(message)
351
+ if message.get('tool_calls'):delta['tool_calls']=[dict(t,index=i) for i,t in enumerate(message['tool_calls'])]
352
+ def chunk(d,finish):return dict(result,object='chat.completion.chunk',choices=[{'index':0,'delta':d,'finish_reason':finish}])
353
+ stream='data: '+json.dumps(chunk(delta,None),ensure_ascii=False)+'\n\ndata: '+json.dumps(chunk({},choice['finish_reason']),ensure_ascii=False)+'\n\ndata: [DONE]\n\n'
354
+ return StreamingResponse(iter([stream]),media_type='text/event-stream')
355
+ finally:session.model_requests.pop(rid,None)
requirements.txt CHANGED
@@ -10,5 +10,4 @@ duckdb>=1.5.5,<2
10
  pandas>=2.2,<3
11
  pyyaml>=6,<7
12
  rdflib>=7,<8
13
- httpx>=0.28,<1
14
- requests>=2.32,<3
 
10
  pandas>=2.2,<3
11
  pyyaml>=6,<7
12
  rdflib>=7,<8
13
+ cryptography>=42,<49