Files
quality_analyse/scripts/nao_trial.py

98 lines
4.4 KiB
Python

"""Isolated nao trial using the upstream packaged servers and existing model profile."""
import argparse,json,os,secrets,signal,subprocess,sys
from pathlib import Path
from dbgpt_trial import model_settings
ROOT=Path(__file__).resolve().parents[1]
TRIAL=ROOT/'.runtime/nao-trial'
PROJECT=TRIAL/'project'
SITE=TRIAL/'venv/lib/python3.11/site-packages/nao_core'
BIN=SITE/'bin'
def environment():
base,key,model=model_settings()
state=TRIAL/'private.json'
if not state.exists():
state.write_text(json.dumps({'auth_secret':secrets.token_urlsafe(40),'password':secrets.token_urlsafe(24),'email':'quality-trial@localhost.invalid'}));state.chmod(0o600)
private=json.loads(state.read_text())
e=os.environ.copy()
e.update(NAO_MODEL_BASE=base,NAO_MODEL_KEY=key,NAO_MODEL_NAME=model,NAO_DEFAULT_PROJECT_PATH=str(PROJECT),
BETTER_AUTH_URL='http://127.0.0.1:5674',BETTER_AUTH_SECRET=private['auth_secret'],
DB_URI='sqlite:'+str(TRIAL/'metadata.sqlite'),FASTAPI_PORT='5675',PORT='5675',MODE='prod',
NAO_CORE_VERSION='0.3.26',POSTHOG_DISABLED='true',DO_NOT_TRACK='1')
return e
def prepare():
import sqlite3,duckdb
PROJECT.mkdir(parents=True,exist_ok=True)
src=sqlite3.connect('file:'+str(ROOT/'.runtime/dbgpt-trial/data/quality_trial.sqlite')+'?mode=ro',uri=True)
target=PROJECT/'quality_trial.duckdb'
if not target.exists():
db=duckdb.connect(str(target))
for (table,) in src.execute("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'"):
cols=src.execute('PRAGMA table_info("'+table+'")').fetchall()
kinds={'TEXT':'VARCHAR','INTEGER':'BIGINT','REAL':'DOUBLE','BLOB':'BLOB','NUMERIC':'DOUBLE'}
db.execute('CREATE TABLE "'+table+'" ('+','.join('"'+c[1]+'" '+kinds.get(c[2].upper(),'VARCHAR') for c in cols)+')')
rows=src.execute('SELECT * FROM "'+table+'"').fetchall()
if rows:db.executemany('INSERT INTO "'+table+'" VALUES ('+','.join('?' for _ in cols)+')',rows)
db.close()
rule=(ROOT/'.runtime/dbgpt-trial/home/skills/quality-center/SKILL.md').read_text()
rule=rule.split('---',2)[-1].replace('sqlite_quality_trial','quality_trial')
(PROJECT/'RULES.md').write_text('# 质量中心本地试验\n\n请默认使用中文。数据库为DuckDB质量副本,使用相应SQL语法。\n'+rule)
cfg='''project_name: quality-trial
databases:
- name: quality_trial
type: duckdb
path: ./quality_trial.duckdb
templates: [columns, preview]
llm:
annotation_model: openaiCompatible:mimo-v2.6-flash
providers:
- provider: openaiCompatible
api_key: "{{ env('NAO_MODEL_KEY') }}"
base_url: "{{ env('NAO_MODEL_BASE') }}"
models:
- id: mimo-v2.6-flash
default: true
'''
(PROJECT/'nao_config.yaml').write_text(cfg)
print('Prepared isolated DuckDB copy and business context')
def run_cli(args):
return subprocess.run([str(TRIAL/'venv/bin/nao'),*args],cwd=PROJECT,env=environment()).returncode
def start():
state=TRIAL/'pids.json'
if state.exists():
alive=[]
for pid in json.loads(state.read_text()).values():
try:os.kill(pid,0);alive.append(pid)
except ProcessLookupError:pass
if len(alive)==2:print('nao trial is already running');return
if alive:raise RuntimeError('A trial server is still running; stop it before starting again')
env=environment();pids={}
for name,args,cwd in [('fastapi',[str(TRIAL/'venv/bin/python'),str(BIN/'fastapi/main.py')],PROJECT),('server',[str(BIN/'nao-chat-server'),'serve','--port','5674','--host','127.0.0.1'],BIN)]:
fd=os.open(TRIAL/(name+'.log'),os.O_CREAT|os.O_APPEND|os.O_WRONLY,0o600)
with os.fdopen(fd,'ab') as log:
p=subprocess.Popen(args,cwd=cwd,env=env,stdin=subprocess.DEVNULL,stdout=log,stderr=log,start_new_session=True)
pids[name]=p.pid
(TRIAL/'pids.json').write_text(json.dumps(pids))
print('nao trial started: http://127.0.0.1:5674; model='+model_settings()[2])
def stop():
p=TRIAL/'pids.json'
if not p.exists():return
for name,pid in json.loads(p.read_text()).items():
r=subprocess.run(['ps','-p',str(pid),'-o','args='],capture_output=True,text=True,errors='replace')
if r.returncode:continue
if str(TRIAL) not in r.stdout or os.getpgid(pid)!=pid:raise RuntimeError('Process identity mismatch')
os.killpg(pid,signal.SIGTERM)
p.unlink();print('nao trial stopped')
if __name__=='__main__':
a=argparse.ArgumentParser();a.add_argument('action',choices=['prepare','sync','start','stop']);v=a.parse_args()
if v.action=='prepare':prepare()
elif v.action=='sync':sys.exit(run_cli(['sync','--provider','databases']))
elif v.action=='start':start()
else:stop()