chat_service.py 5.8 KB

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