-- ============================================================
-- 豆包大模型接入: 火山方舟 ARK API
-- 
-- 模型:
--   1. doubao-2.1-pro     — 豆包大模型2.1 Pro (文本对话)
--   2. doubao-seedream-5.0 — 豆包图像创作模型5.0 (文生图)
--   3. doubao-speech-2.0   — 豆包语音识别模型2.0 (语音识别)
--
-- Provider: 豆包 (Cy-zrc4DgLIFuA0KMQzlw)
-- Upapp: huoshanfangzhou (火山方舟, baseurl: https://ark.cn-beijing.volces.com/api/v3)
-- ============================================================

-- ===================== Part 1: llm =====================
INSERT INTO llm (id,name,model,description,iconid,upappid,providerid,ownerid,enabled_date,expired_date,min_balance,status) VALUES
('llm_doubao_21_pro','doubao-2.1-pro','doubao-seed-2-1-pro-260628','豆包大模型2.1 Pro — 260628版本，256K上下文，文本对话',NULL,'huoshanfangzhou','Cy-zrc4DgLIFuA0KMQzlw','0','2025-01-01','2099-12-31',0.00,'published'),
('llm_doubao_seedream_5','doubao-seedream-5.0','doubao-seedream-5-0-260128','豆包图像创作模型5.0 — 260128版本，文生图',NULL,'huoshanfangzhou','Cy-zrc4DgLIFuA0KMQzlw','0','2025-01-01','2099-12-31',0.00,'published'),
('llm_doubao_speech_2','doubao-speech-2.0','doubao-speech-2.0','豆包语音识别模型2.0 — 语音转文字',NULL,'huoshanfangzhou','Cy-zrc4DgLIFuA0KMQzlw','0','2025-01-01','2099-12-31',0.00,'published')
ON DUPLICATE KEY UPDATE model=VALUES(model), description=VALUES(description);

-- ===================== Part 2: llm_api_map =====================
INSERT INTO llm_api_map (id,llmid,llmcatelogid,apiname,query_apiname,query_period,ppid,isdefaultcatelog) VALUES
-- 豆包2.1 Pro → 文本对话 (t2t)
('map_doubao_21_pro','llm_doubao_21_pro','t2t','doubao-chat',NULL,NULL,'pp_doubao_21_pro','1'),
-- 豆包图像5.0 → 文生图 (t2i)
('map_doubao_seedream_5','llm_doubao_seedream_5','t2i','doubao-image',NULL,NULL,'pp_doubao_seedream_5','1'),
-- 豆包语音2.0 → 语音识别 (asr)
('map_doubao_speech_2','llm_doubao_speech_2','asr','doubao-asr',NULL,NULL,'pp_doubao_speech_2','1')
ON DUPLICATE KEY UPDATE apiname=VALUES(apiname), ppid=VALUES(ppid);

-- ===================== Part 3: uapi =====================
-- 3a. 文本对话 (OpenAI-compatible chat) — sync
INSERT INTO uapi (id,name,title,description,need_auth,stream,path,httpmethod,chunk_match,headers,params,data,response,ioid,callbackurl,upappid) VALUES
('uapi_doubao_chat','doubao-chat','豆包大模型对话','豆包2.1 Pro文本对话','0','sync','/chat/completions','POST',NULL,'{"Content-Type":"application/json"}',NULL,
'{%if stream==True%}"stream_options":{"include_usage":true},"stream":true,{%endif%}
"messages":[
{%if sys_prompt%}{"role":"system","content":{{json.dumps(sys_prompt,ensure_ascii=False)}}},{%endif%}
{"role":"user","content":[{%if image_file%}{"type":"image_url","image_url":"{{b64media2url(request,image_file)}}"},{%endif%}{%if video_file%}{"type":"video_url","video_url":"{{b64media2url(request,video_file)}}"},{%endif%}{%if audio_file%}{"type":"audio_url","audio_url":"{{b64media2url(request,audio_file)}}"},{%endif%}{"type":"text","text":{{json.dumps(prompt,ensure_ascii=False)}}}]}
],
"model":"{{model}}"',
'{"id":"{{id}}","object":"chat.completion","created":{{created}},"model":"{{model}}","choices":{{json.dumps(choices,ensure_ascii=False)}},"usage":{{json.dumps(usage,ensure_ascii=False)}}}',
NULL,NULL,'huoshanfangzhou')
ON DUPLICATE KEY UPDATE data=VALUES(data), response=VALUES(response);

-- 3b. 图像生成 (async, ARK contents/generations) — async
INSERT INTO uapi (id,name,title,description,need_auth,stream,path,httpmethod,chunk_match,headers,params,data,response,ioid,callbackurl,upappid) VALUES
('uapi_doubao_image','doubao-image','豆包图像创作','豆包Seedream5.0图像生成','0','async','/contents/generations/tasks','POST',NULL,'{"Content-Type":"application/json"}',NULL,
'{"model":"{{model}}","content":[{"type":"text","text":{{json.dumps(prompt,ensure_ascii=False)}}}],"size":"{{size or \"1024x1024\"}}"}',
'{"taskid":"{{id}}","taskstatus":"PENDING","status":"PENDING"}',
NULL,NULL,'huoshanfangzhou')
ON DUPLICATE KEY UPDATE data=VALUES(data), response=VALUES(response);

-- 3c. 语音识别 (sync, ARK audio/transcriptions) — sync
INSERT INTO uapi (id,name,title,description,need_auth,stream,path,httpmethod,chunk_match,headers,params,data,response,ioid,callbackurl,upappid) VALUES
('uapi_doubao_asr','doubao-asr','豆包语音识别','豆包Speech2.0语音转文字','0','sync','/audio/transcriptions','POST',NULL,'{"Content-Type":"multipart/form-data"}',NULL,
'{"model":"{{model}}"{%if audio_file%},"file":"{{b64media2url(request,audio_file)}}"{%endif%}{%if language%},"language":"{{language}}"{%endif%}}',
'{"text":"{{text}}"{%if segments%},"segments":{{json.dumps(segments,ensure_ascii=False)}}{%endif%}{%if usage%},"usage":{{json.dumps(usage,ensure_ascii=False)}}{%endif%}}',
NULL,NULL,'huoshanfangzhou')
ON DUPLICATE KEY UPDATE data=VALUES(data), response=VALUES(response);


-- ===================== Part 3b: uapiio (输入输出字段定义) =====================
INSERT INTO uapiio (id,name,description,input_fields) VALUES
('doubao_chat_io','豆包大模型对话','文本对话输入输出','{"prompt":{"type":"string","required":true,"description":"用户输入文本"},"sys_prompt":{"type":"string","required":false,"description":"系统提示词"},"image_file":{"type":"string","required":false,"description":"图像URL(多模态)"},"max_tokens":{"type":"integer","required":false,"default":1024,"description":"最大输出token数"}}'),
('doubao_image_io','豆包图像创作','文生图输入输出','{"prompt":{"type":"string","required":true,"description":"图像描述文本"},"size":{"type":"string","required":false,"default":"1024x1024","description":"图像尺寸"},"n":{"type":"integer","required":false,"default":1,"description":"生成数量"}}'),
('doubao_asr_io','豆包语音识别','语音转文字输入输出','{"audio_file":{"type":"string","required":true,"description":"音频文件URL"},"language":{"type":"string","required":false,"default":"zh","description":"语言代码"}}')
ON DUPLICATE KEY UPDATE description=VALUES(description),input_fields=VALUES(input_fields);

-- Link uapi to uapiio
UPDATE uapi SET ioid='doubao_chat_io' WHERE id='uapi_doubao_chat';
UPDATE uapi SET ioid='doubao_image_io' WHERE id='uapi_doubao_image';
UPDATE uapi SET ioid='doubao_asr_io' WHERE id='uapi_doubao_asr';

-- ===================== Part 4: pricing_program =====================
INSERT INTO pricing_program (id,name,ownerid,providerid,pricing_belong,description) VALUES
('pp_doubao_21_pro','豆包大模型2.1 Pro',NULL,'Cy-zrc4DgLIFuA0KMQzlw',NULL,'文本对话·按token计费'),
('pp_doubao_seedream_5','豆包图像创作5.0',NULL,'Cy-zrc4DgLIFuA0KMQzlw',NULL,'文生图·按张计费'),
('pp_doubao_speech_2','豆包语音识别2.0',NULL,'Cy-zrc4DgLIFuA0KMQzlw',NULL,'语音识别·按秒计费')
ON DUPLICATE KEY UPDATE name=VALUES(name);

-- ===================== Part 5: pricing_program_timing =====================
INSERT INTO pricing_program_timing (id,ppid,name,pricing_data,enabled_date,expired_date) VALUES
-- 文本对话: 按1K tokens计费
('pt_doubao_21_pro','pp_doubao_21_pro','豆包2.1 Pro定价',
'unit_values:\n  completion_tokens: 1000\n  prompt_tokens: 1000\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n- price_factors: completion_tokens\n  unit_prices: 0.001\n  unit: 1K tokens\n- price_factors: prompt_tokens\n  unit_prices: 0.0005\n  unit: 1K tokens',
'2025-01-01','9999-12-31'),

-- 图像生成: 按张计费
('pt_doubao_seedream_5','pp_doubao_seedream_5','豆包Seedream5.0定价',
'unit_values:\n  image_count: 1\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n- price_factors: image_count\n  unit_prices: 0.02\n  unit: image_count',
'2025-01-01','9999-12-31'),

-- 语音识别: 按秒计费
('pt_doubao_speech_2','pp_doubao_speech_2','豆包Speech2.0定价',
'unit_values:\n  audio_seconds: 1\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n- price_factors: audio_seconds\n  unit_prices: 0.004\n  unit: audio_seconds',
'2025-01-01','9999-12-31')
ON DUPLICATE KEY UPDATE ppid=VALUES(ppid), pricing_data=VALUES(pricing_data);
