22from nonebot .log import logger
33
44from ..config import config
5+ from ..compat import model_dump
56
67# from ..function_call import registry
78from ..exception import RequestException
1112class API :
1213 _headers = {
1314 "Accept" : "application/json" ,
14- "Authorization" : f"Bearer { config .api_key } " ,
1515 }
1616
1717 @classmethod
1818 async def chat (cls , message : list [dict [str , str ]], model : str = "deepseek-chat" ) -> ChatCompletions :
1919 """普通对话"""
2020 model_config = config .get_model_config (model )
21+
22+ api_key = model_config .api_key or config .api_key
23+ prompt = model_dump (model_config , exclude_none = True ).get ("prompt" , config .prompt )
24+
2125 json = {
22- "messages" : [{"content" : config .prompt , "role" : "system" }] + message
23- if config .prompt and model == "deepseek-chat"
24- else message ,
26+ "messages" : [{"content" : prompt , "role" : "system" }] + message if prompt else message ,
2527 "model" : model ,
2628 ** model_config .to_dict (),
2729 }
@@ -31,7 +33,7 @@ async def chat(cls, message: list[dict[str, str]], model: str = "deepseek-chat")
3133 async with httpx .AsyncClient () as client :
3234 response = await client .post (
3335 f"{ model_config .base_url } /chat/completions" ,
34- headers = {** cls ._headers , "Content-Type" : "application/json" },
36+ headers = {** cls ._headers , "Authorization" : f"Bearer { api_key } " , " Content-Type" : "application/json" },
3537 json = json ,
3638 timeout = 50 ,
3739 )
@@ -40,12 +42,15 @@ async def chat(cls, message: list[dict[str, str]], model: str = "deepseek-chat")
4042 return ChatCompletions (** response .json ())
4143
4244 @classmethod
43- async def query_balance (cls ) -> Balance :
44- """查询账号余额"""
45+ async def query_balance (cls , model_name : str ) -> Balance :
46+ model_config = config .get_model_config (model_name )
47+ api_key = model_config .api_key or config .api_key
48+
4549 async with httpx .AsyncClient () as client :
4650 response = await client .get (
47- f"{ config . get_model_url ( 'deepseek-chat' ) } /user/balance" ,
48- headers = cls ._headers ,
51+ f"{ model_config . base_url } /user/balance" ,
52+ headers = { ** cls ._headers , "Authorization" : f"Bearer { api_key } " } ,
4953 )
50-
54+ if response .status_code == 404 :
55+ raise RequestException ("本地模型不支持查询余额,请更换默认模型" )
5156 return Balance (** response .json ())
0 commit comments