kboss/b/cntoai/sync_model_to_llm.dspy
2026-07-21 17:15:53 +08:00

165 lines
7.3 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

async def sync_model_to_llm(ns={}):
import aiohttp
async def get_sync_orgid(sor):
if ns.get('orgid'):
return ns.get('orgid')
user_li = await sor.R('users', {'id': await get_user()})
return user_li[0].get('orgid') if user_li else ''
# 从数据库读取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']
# sync_orgid = await get_sync_orgid(sor)
sync_orgid = "mIWUHBeeDM8mwAFPIQ8pS" # 固定写
if not sync_orgid:
return {
'status': False,
'msg': '未找到当前用户所属机构'
}
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
sync_products_count = 0
new_llms_list = []
for category_list in result:
for item in category_list.get('llms', []):
provider_orgcode = item.get('providerid')
provider_name = item.get('provider_name') or item.get('providerid') or '模型供应商'
if not provider_orgcode:
continue
provider_ns = {
'orgid': sync_orgid,
'orgcode': provider_orgcode,
'orgname': provider_name,
'name': provider_name,
'description': provider_name,
'appid': item.get('upappid') or '',
'providerkey': provider_orgcode,
'state': '1',
'start_date': '2026-07-01',
'end_date': '9999-12-31',
'settle_flowid': '0',
'salemode': '0',
'settle_mode': '3',
'settle_datep': 'M00',
'contactor': '连战',
'contactor_phone': '13800138000',
'emailaddress': 'lianzhan@cntoai.com',
'address': '北京市海淀区',
'org_type': '4'
}
provider_res = await path_call('../provider/providerAdd.dspy', provider_ns)
if not provider_res.get('status'):
return {
'status': False,
'msg': '供应商同步失败, %s, %s' % (provider_name, provider_res.get('msg'))
}
provider_data = provider_res.get('data') or {}
provider_orgid = provider_data.get('provider_orgid') or provider_data.get('orgid')
protocolid = provider_data.get('protocolid')
if not provider_orgid or not protocolid:
return {
'status': False,
'msg': '供应商同步失败, 未返回provider_orgid或protocolid, %s' % provider_name
}
product_ns = {
'providerid': provider_orgid,
'providerpid': item.get('id'),
'protocolid': protocolid,
'name': item.get('name'),
'product_code': item.get('model'),
'reseller_orgid': sync_orgid,
'label': item.get('catelogname') or category_list.get('catelogname'),
'classify': 'A',
'ptype': '21',
'unit': 'hour',
'discount': '1',
'product_area': '北京'
}
product_res = await path_call('../product/productAdd.dspy', product_ns)
if not product_res.get('status'):
return {
'status': False,
'msg': '产品同步失败, %s, %s' % (item.get('name'), product_res.get('msg'))
}
sync_products_count += 1
# 查找数据库中是否已经存在,不存在就插入
exist_llm = await sor.R('llm', {'model': item.get('model')})
if exist_llm:
# 更新
exits_id = exist_llm[0].get('id')
await sor.U('llm', {'id': exits_id}, item)
continue
new_llms = {
'id': item.get('id'),
'name': item.get('name'),
'model': item.get('model'),
'description': item.get('description'),
'iconid': item.get('iconid'),
'upappid': item.get('upappid'),
'providerid': item.get('providerid'),
'ownerid': item.get('ownerid'),
'enabled_date': item.get('enabled_date'),
'expired_date': item.get('expired_date'),
'min_balance': item.get('min_balance'),
'status': item.get('status'),
'catelogname': item.get('catelogname'),
'catelog_id': item.get('catelog_id'),
'apiname': item.get('apiname'),
'query_apiname': item.get('query_apiname'),
'query_period': item.get('query_period'),
'ppid': item.get('ppid'),
'provider_name': item.get('provider_name'),
'pricing_display': item.get('pricing_display'),
'provider_iconurl': item.get('provider_iconurl')
}
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同步模型成功共同步{sync_products_count}个产品,共插入{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