async def sync_model_to_llm(ns={}): import aiohttp # 从数据库读取domain和Bearer token db = DBPools() async with db.sqlorContext('kboss') as sor: domain_li = await sor.R('params', {'pname': 'cntoai_domain'}) user_key = await sor.R('params', {'pname': 'cntoai_already_sync_user_key'}) if not domain_li or not user_key: return { 'status': False, 'msg': '未找到params domain或Bearer token' } domain = domain_li[0]['pvalue'] bearer_token = user_key[0]['pvalue'] url = f"{domain}/llmage/list_llms" header = { 'Authorization': f'Bearer {bearer_token}', 'Content-Type': 'application/json', } try: async with aiohttp.ClientSession() as session: async with session.get(url, headers=header) as response: result = await response.json() if not result: return { 'status': False, 'msg': '没有找到模型列表' } # 插入数据库 db = DBPools() async with db.sqlorContext('kboss') as sor: new_llms_count = 0 new_llms_list = [] for category_list in result: for item in category_list.get('llms', []): # 查找数据库中是否已经存在,不存在就插入 exist_llm = await sor.R('llm', {'model': item.get('model')}) if exist_llm: continue new_llms = { 'id': item.get('id'), 'name': item.get('name'), 'model': item.get('model'), 'description': item.get('description'), 'llmcatelogid': item.get('catelog_id') or item.get('catelogid'), 'iconid': item.get('iconid'), 'upappid': item.get('upappid'), 'apiname': item.get('apiname'), 'providerid': item.get('providerid'), 'ownerid': item.get('ownerid') or '0', 'enabled_date': item.get('enabled_date'), 'expired_date': item.get('expired_date'), 'query_apiname': item.get('query_apiname') or '', 'query_period': item.get('query_period'), 'ppid': item.get('ppid'), } new_llms_count += 1 new_llms_list.append(new_llms.get('model')) await sor.C('llm', new_llms) return { 'status': True, 'msg': f"sync_llm_list同步模型成功,共插入{new_llms_count}个模型,模型列表: {new_llms_list}" } except Exception as e: return { 'status': False, 'msg': f"sync_llm_list同步模型失败, {domain}, {bearer_token}: {e}" } ret = await sync_model_to_llm(params_kw) return ret