INVESTIGA-ES / eval /run_model.py
Januka2009's picture
Upload 104 files
87ab2b9 verified
Raw History Blame Contribute Delete
3.18 kB
"""Opt-in OpenAI-compatible HTTP runner. Does not execute during normal validation.
Secrets are read from an environment variable and never logged or saved.
"""
import argparse,json,os,sys,urllib.request
from datetime import datetime,timezone
from pathlib import Path
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'scripts'))
from common import *
from agent_sandbox import dispatch
def generate(endpoint,model,messages,tools=None):
body={'model':model,'messages':messages,'temperature':0}
if tools:body['tools']=tools
headers={'Content-Type':'application/json'}
token=os.environ.get('INVESTIGA_API_KEY')
if token:headers['Authorization']='Bearer '+token
req=urllib.request.Request(endpoint.rstrip('/')+'/chat/completions',data=json.dumps(body).encode(),headers=headers)
with urllib.request.urlopen(req,timeout=120) as response:return json.load(response)['choices'][0]['message']
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('--endpoint',required=True);p.add_argument('--model',required=True);p.add_argument('--config',choices=['benchmark','rag','agentic'],required=True);p.add_argument('--output',type=Path,required=True);a=p.parse_args()
assert not a.output.exists(),'Refuse to overwrite model predictions'
rows=[r for c,_,r in all_data() if c==a.config]
with a.output.open('x') as f:
for r in rows:
if a.config=='agentic':prompt=r['objective']+'\n'+r['initial_context']
elif a.config=='rag':prompt=r['query']+'\nDocumentos simulados:\n'+json.dumps(r['documents'],ensure_ascii=False)
else:prompt=r['prompt']
messages=[{'role':'user','content':prompt}];tools=None;calls=[]
if a.config=='agentic':tools=[{'type':'function','function':{'name':t['name'],'description':t['description'],'parameters':json.loads(t['input_schema_json'])}} for t in r['available_tools']]
message={}
for iteration in range(12):
message=generate(a.endpoint,a.model,messages,tools);messages.append(message)
if not message.get('tool_calls'):break
for t in message['tool_calls']:
arguments=json.loads(t['function']['arguments']);name=t['function']['name']
try:result=dispatch(r,name,arguments)
except Exception:result={'error':'tool or arguments not allowed in this closed task'}
calls.append({'tool':name,'arguments':arguments,'result':result})
messages.append({'role':'tool','tool_call_id':t['id'],'content':json.dumps(result,ensure_ascii=False)})
output={'id':r['id'],'calls':calls,'artifact':message.get('content') or ''} if a.config=='agentic' else {'id':r['id'],'prediction':message.get('content') or ''}
f.write(json.dumps(output,ensure_ascii=False)+'\n');f.flush()
dump(str(a.output)+'.run.json',{'model':a.model,'config':a.config,'temperature':0,'max_agent_rounds':12,'executed_at':datetime.now(timezone.utc).isoformat(),'references_sent':False,'note':'No model weights or endpoint credentials recorded; endpoint response capabilities may vary.'})