소스 검색

Update chat_service: add deepseek api and support graceful shutdown

StrayWarrior 2 주 전
부모
커밋
b92262cd29
1개의 변경된 파일19개의 추가작업 그리고 4개의 파일을 삭제
  1. 19 4
      chat_service.py

+ 19 - 4
chat_service.py

@@ -18,6 +18,9 @@ VOLCENGINE_API_TOKEN = '5e275c38-44fd-415f-abcf-4b59f6377f72'
 VOLCENGINE_BASE_URL = "https://ark.cn-beijing.volces.com/api/v3"
 VOLCENGINE_MODEL_DEEPSEEK_V3 = "ep-20250213194558-rrmr2"
 VOLCENGINE_MODEL_DOUBAO_PRO_1_5 = 'ep-20250307150409-4blz9'
+DEEPSEEK_API_TOKEN = 'sk-67daad8f424f4854bda7f1fed7ef220b'
+DEEPSEEK_BASE_URL = 'https://api.deepseek.com/'
+DEEPSEEK_CHAT_MODEL = 'deepseek-chat'
 
 class ChatServiceType(Enum):
     OPENAI_COMPATIBLE = auto
@@ -45,11 +48,15 @@ class CozeChat:
     def __init__(self, base_url: str, auth_token: Optional[str] = None, auth_app: Optional[JWTOAuthApp] = None):
         if not auth_token and not auth_app:
             raise ValueError("Either auth_token or auth_app must be provided.")
+        self.thread = None
+        self.thread_running = False
+        self.last_token_fresh = 0
         if auth_token:
             self.coze = Coze(auth=TokenAuth(auth_token), base_url=base_url)
         else:
             self.auth_app = auth_app
             oauth_token = auth_app.get_access_token(ttl=12*3600)
+            self.last_token_fresh = time.time()
             self.coze = Coze(auth=JWTAuth(oauth_app=auth_app), base_url=base_url)
             self.setup_token_refresh()
 
@@ -68,14 +75,22 @@ class CozeChat:
         return final_response
 
     def setup_token_refresh(self):
-        thread = threading.Thread(target=self.refresh_token_loop)
-        thread.start()
+        self.thread = threading.Thread(target=self.refresh_token_loop)
+        self.thread.start()
+        self.thread_running = True
 
     def refresh_token_loop(self):
-        while True:
-            time.sleep(11*3600)
+        while self.thread_running:
+            if time.time() - self.last_token_fresh < 11*3600:
+                time.sleep(1)
+                continue
             if self.auth_app:
                 self.auth_app.get_access_token(ttl=12*3600)
+                self.last_token_fresh = time.time()
+
+    def __del__(self):
+        self.thread_running = False
+
 
     @staticmethod
     def get_oauth_app(client_id, private_key_path, public_key_id, base_url=None, account_id=None) -> JWTOAuthApp: