chat_service.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # vim:fenc=utf-8
  4. #
  5. import os
  6. import threading
  7. from typing import List, Dict, Optional
  8. from enum import Enum, auto
  9. from logging_service import logger
  10. import cozepy
  11. from cozepy import Coze, TokenAuth, Message, ChatStatus, MessageType, JWTOAuthApp, JWTAuth
  12. import time
  13. COZE_API_TOKEN = os.getenv("COZE_API_TOKEN")
  14. COZE_CN_BASE_URL = 'https://api.coze.cn'
  15. VOLCENGINE_API_TOKEN = '5e275c38-44fd-415f-abcf-4b59f6377f72'
  16. VOLCENGINE_BASE_URL = "https://ark.cn-beijing.volces.com/api/v3"
  17. VOLCENGINE_MODEL_DEEPSEEK_V3 = "deepseek-v3-250324"
  18. VOLCENGINE_MODEL_DOUBAO_PRO_1_5 = 'ep-20250307150409-4blz9'
  19. VOLCENGINE_MODEL_DOUBAO_PRO_32K = 'ep-20250414202859-6nkz5'
  20. VOLCENGINE_MODEL_DOUBAO_1_5_VISION_PRO = 'ep-20250421193334-nz5wd'
  21. DEEPSEEK_API_TOKEN = 'sk-67daad8f424f4854bda7f1fed7ef220b'
  22. DEEPSEEK_BASE_URL = 'https://api.deepseek.com/'
  23. DEEPSEEK_CHAT_MODEL = 'deepseek-chat'
  24. class ChatServiceType(Enum):
  25. OPENAI_COMPATIBLE = auto()
  26. COZE_CHAT = auto()
  27. class CrossAccountJWTOAuthApp(JWTOAuthApp):
  28. def __init__(self, account_id: str, client_id: str, private_key: str, public_key_id: str, base_url):
  29. self.account_id = account_id
  30. super().__init__(client_id, private_key, public_key_id, base_url)
  31. def get_access_token(
  32. self, ttl: int = 900, scope: Optional[cozepy.Scope] = None, session_name: Optional[str] = None
  33. ) -> cozepy.OAuthToken:
  34. jwt_token = self._gen_jwt(self._public_key_id, self._private_key, 3600, session_name)
  35. url = f"{self._base_url}/api/permission/oauth2/account/{self.account_id}/token"
  36. headers = {"Authorization": f"Bearer {jwt_token}"}
  37. body = {
  38. "duration_seconds": ttl,
  39. "grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer",
  40. "scope": scope.model_dump() if scope else None,
  41. }
  42. return self._requester.request("post", url, False, cozepy.OAuthToken, headers=headers, body=body)
  43. class CozeChat:
  44. def __init__(self, base_url: str, auth_token: Optional[str] = None, auth_app: Optional[JWTOAuthApp] = None):
  45. if not auth_token and not auth_app:
  46. raise ValueError("Either auth_token or auth_app must be provided.")
  47. self.thread = None
  48. self.thread_running = False
  49. self.last_token_fresh = 0
  50. if auth_token:
  51. self.coze = Coze(auth=TokenAuth(auth_token), base_url=base_url)
  52. else:
  53. self.auth_app = auth_app
  54. oauth_token = auth_app.get_access_token(ttl=12*3600)
  55. self.last_token_fresh = time.time()
  56. self.coze = Coze(auth=JWTAuth(oauth_app=auth_app), base_url=base_url)
  57. self.setup_token_refresh()
  58. def create(self, bot_id: str, user_id: str, messages: List, custom_variables: Dict):
  59. response = self.coze.chat.create_and_poll(
  60. bot_id=bot_id, user_id=user_id, additional_messages=messages,
  61. custom_variables=custom_variables)
  62. logger.debug("Coze response size: {}".format(len(response.messages)))
  63. if response.chat.status != ChatStatus.COMPLETED:
  64. logger.error("Coze chat not completed: {}".format(response.chat.status))
  65. return None
  66. final_response = None
  67. for message in response.messages:
  68. if message.type == MessageType.ANSWER:
  69. final_response = message.content
  70. return final_response
  71. def setup_token_refresh(self):
  72. self.thread = threading.Thread(target=self.refresh_token_loop)
  73. self.thread.start()
  74. self.thread_running = True
  75. def refresh_token_loop(self):
  76. while self.thread_running:
  77. if time.time() - self.last_token_fresh < 11*3600:
  78. time.sleep(1)
  79. continue
  80. if self.auth_app:
  81. self.auth_app.get_access_token(ttl=12*3600)
  82. self.last_token_fresh = time.time()
  83. def __del__(self):
  84. self.thread_running = False
  85. @staticmethod
  86. def get_oauth_app(client_id, private_key_path, public_key_id, base_url=None, account_id=None) -> JWTOAuthApp:
  87. if not base_url:
  88. base_url = COZE_CN_BASE_URL
  89. with open(private_key_path, "r") as f:
  90. private_key = f.read()
  91. if not account_id:
  92. jwt_oauth_app = JWTOAuthApp(
  93. client_id=str(client_id),
  94. private_key=private_key,
  95. public_key_id=public_key_id,
  96. base_url=base_url,
  97. )
  98. else:
  99. jwt_oauth_app = CrossAccountJWTOAuthApp(
  100. account_id=account_id,
  101. client_id=str(client_id),
  102. private_key=private_key,
  103. public_key_id=public_key_id,
  104. base_url=base_url,
  105. )
  106. return jwt_oauth_app
  107. if __name__ == '__main__':
  108. # Init the Coze client through the access_token.
  109. coze = Coze(auth=TokenAuth(token=COZE_API_TOKEN), base_url=COZE_CN_BASE_URL)
  110. # Create a bot instance in Coze, copy the last number from the web link as the bot's ID.
  111. bot_id = "7491250992952999973"
  112. # The user id identifies the identity of a user. Developers can use a custom business ID
  113. # or a random string.
  114. user_id = "dev_user"
  115. chat = coze.chat.create_and_poll(
  116. bot_id=bot_id,
  117. user_id=user_id,
  118. additional_messages=[Message.build_user_question_text("钱塘江边 樱花开得不错,推荐一个视频吧")],
  119. custom_variables={
  120. 'agent_name': '芳华',
  121. 'agent_age': '25',
  122. 'agent_region': '北京',
  123. 'name': '李明',
  124. 'preferred_nickname': '李叔',
  125. 'age': '70',
  126. 'last_interaction_interval': '12',
  127. 'current_time_period': '上午',
  128. 'if_first_interaction': 'False',
  129. 'if_active_greeting': 'False'
  130. }
  131. )
  132. for message in chat.messages:
  133. print(message, flush=True)
  134. if chat.chat.status == ChatStatus.COMPLETED:
  135. print("token usage:", chat.chat.usage.token_count)