88import requests
99import json
1010from typing import List , Dict , Optional , Generator
11- from src .utils .logger import info , warning , error
11+ from src .utils .logger import info , warning , error , debug , LogCategory
1212
1313class SiliconFlowClient :
1414 """Client for SiliconFlow API"""
@@ -36,6 +36,7 @@ def __init__(self, api_key: str, model: str = None):
3636 'Authorization' : f'Bearer { api_key } ' ,
3737 'Content-Type' : 'application/json'
3838 }
39+ info (f"SiliconFlow client initialized with model: { self .model } " , category = LogCategory .API )
3940
4041 def chat_completion (
4142 self ,
@@ -58,7 +59,7 @@ def chat_completion(
5859 """
5960 # Validate temperature
6061 if not 0 <= temperature <= 2 :
61- warning (f"Temperature { temperature } out of range [0, 2], clamping" )
62+ warning (f"Temperature { temperature } out of range [0, 2], clamping" , category = LogCategory . API )
6263 temperature = max (0 , min (2 , temperature ))
6364
6465 url = f"{ self .BASE_URL } /chat/completions"
@@ -70,15 +71,23 @@ def chat_completion(
7071 'stream' : stream
7172 }
7273
74+ debug (f"Sending API request to { url } with { len (messages )} messages" , category = LogCategory .API )
75+
7376 try :
7477 if stream :
7578 return self ._stream_chat_completion (url , data )
7679 else :
77- response = requests .post (url , headers = self .headers , json = data , timeout = 60 )
80+ # Use shorter timeout to prevent UI freeze
81+ response = requests .post (url , headers = self .headers , json = data , timeout = 30 )
7882 response .raise_for_status ()
79- return response .json ()
83+ result = response .json ()
84+ info (f"API request successful, response size: { len (str (result ))} chars" , category = LogCategory .API )
85+ return result
86+ except requests .exceptions .Timeout as e :
87+ error (f"SiliconFlow API timeout after 30s: { str (e )} " , category = LogCategory .API )
88+ raise
8089 except requests .exceptions .RequestException as e :
81- error (f"SiliconFlow API error: { str (e )} " )
90+ error (f"SiliconFlow API error: { str (e )} " , category = LogCategory . API )
8291 raise
8392
8493 def _stream_chat_completion (self , url : str , data : Dict ) -> Generator :
@@ -92,13 +101,14 @@ def _stream_chat_completion(self, url: str, data: Dict) -> Generator:
92101 Yields:
93102 Chunks of response text
94103 """
104+ debug ("Starting streaming API request" , category = LogCategory .API )
95105 try :
96106 response = requests .post (
97107 url ,
98108 headers = self .headers ,
99109 json = data ,
100110 stream = True ,
101- timeout = 60
111+ timeout = 30
102112 )
103113 response .raise_for_status ()
104114
@@ -108,6 +118,7 @@ def _stream_chat_completion(self, url: str, data: Dict) -> Generator:
108118 if line .startswith ('data: ' ):
109119 data_str = line [6 :]
110120 if data_str .strip () == '[DONE]' :
121+ info ("Streaming completed" , category = LogCategory .API )
111122 break
112123 try :
113124 chunk = json .loads (data_str )
@@ -117,8 +128,11 @@ def _stream_chat_completion(self, url: str, data: Dict) -> Generator:
117128 yield delta ['content' ]
118129 except json .JSONDecodeError :
119130 continue
131+ except requests .exceptions .Timeout as e :
132+ error (f"SiliconFlow streaming timeout after 30s: { str (e )} " , category = LogCategory .API )
133+ raise
120134 except requests .exceptions .RequestException as e :
121- error (f"SiliconFlow streaming error: { str (e )} " )
135+ error (f"SiliconFlow streaming error: { str (e )} " , category = LogCategory . API )
122136 raise
123137
124138 def text_completion (
@@ -177,16 +191,23 @@ def get_embedding(self, text: str) -> List[float]:
177191 'input' : text
178192 }
179193
194+ debug (f"Requesting embedding for text length: { len (text )} " , category = LogCategory .API )
195+
180196 try :
181- response = requests .post (url , headers = self .headers , json = data , timeout = 30 )
197+ response = requests .post (url , headers = self .headers , json = data , timeout = 20 )
182198 response .raise_for_status ()
183199 result = response .json ()
184200
185201 if 'data' in result and len (result ['data' ]) > 0 :
186- return result ['data' ][0 ]['embedding' ]
202+ embedding = result ['data' ][0 ]['embedding' ]
203+ info (f"Embedding received, dimension: { len (embedding )} " , category = LogCategory .API )
204+ return embedding
187205 return []
206+ except requests .exceptions .Timeout as e :
207+ error (f"SiliconFlow embedding timeout after 20s: { str (e )} " , category = LogCategory .API )
208+ raise
188209 except requests .exceptions .RequestException as e :
189- error (f"SiliconFlow embedding error: { str (e )} " )
210+ error (f"SiliconFlow embedding error: { str (e )} " , category = LogCategory . API )
190211 raise
191212
192213 def test_connection (self ) -> bool :
@@ -196,10 +217,12 @@ def test_connection(self) -> bool:
196217 Returns:
197218 True if connection successful, False otherwise
198219 """
220+ debug ("Testing API connection" , category = LogCategory .API )
199221 try :
200222 messages = [{'role' : 'user' , 'content' : 'Hello' }]
201223 self .chat_completion (messages , max_tokens = 10 )
224+ info ("API connection test successful" , category = LogCategory .API )
202225 return True
203226 except Exception as e :
204- error (f"Connection test failed: { str (e )} " )
227+ error (f"Connection test failed: { str (e )} " , category = LogCategory . API )
205228 return False
0 commit comments