chat_service.py 5.8 KB

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