chat_service.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. import httpx
  10. from pqai_agent import configs
  11. from pqai_agent.logging import logger
  12. import cozepy
  13. from cozepy import Coze, TokenAuth, Message, ChatStatus, MessageType, JWTOAuthApp, JWTAuth
  14. import time
  15. from openai import OpenAI, AsyncOpenAI, http_client
  16. COZE_API_TOKEN = os.getenv("COZE_API_TOKEN")
  17. COZE_CN_BASE_URL = 'https://api.coze.cn'
  18. VOLCENGINE_API_TOKEN = '5e275c38-44fd-415f-abcf-4b59f6377f72'
  19. VOLCENGINE_BASE_URL = "https://ark.cn-beijing.volces.com/api/v3"
  20. VOLCENGINE_MODEL_DEEPSEEK_V3 = "deepseek-v3-250324"
  21. VOLCENGINE_MODEL_DOUBAO_PRO_1_5_32K = 'doubao-1-5-pro-32k-250115'
  22. VOLCENGINE_MODEL_DOUBAO_PRO_32K = 'doubao-pro-32k-241215'
  23. VOLCENGINE_MODEL_DOUBAO_1_5_VISION_PRO = 'doubao-1-5-vision-pro-32k-250115'
  24. DEEPSEEK_API_TOKEN = 'sk-67daad8f424f4854bda7f1fed7ef220b'
  25. DEEPSEEK_BASE_URL = 'https://api.deepseek.com/'
  26. DEEPSEEK_CHAT_MODEL = 'deepseek-chat'
  27. VOLCENGINE_BOT_BASE_URL = "https://ark.cn-beijing.volces.com/api/v3/bots"
  28. VOLCENGINE_BOT_DEEPSEEK_V3_SEARCH = "bot-20250427173459-9h2xp"
  29. OPENAI_API_TOKEN = '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'
  30. OPENAI_BASE_URL = 'https://api.openai.com/v1'
  31. OPENAI_MODEL_GPT_4o = 'gpt-4o'
  32. OPENAI_MODEL_GPT_4o_mini = 'gpt-4o-mini'
  33. OPENROUTER_API_TOKEN = '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'
  34. OPENROUTER_BASE_URL = 'https://openrouter.ai/api/v1/'
  35. OPENROUTER_MODEL_CLAUDE_3_7_SONNET = 'anthropic/claude-3.7-sonnet'
  36. OPENROUTER_MODEL_GEMINI_2_5_PRO = 'google/gemini-2.5-pro'
  37. ALIYUN_API_TOKEN = 'sk-47381479425f4485af7673d3d2fd92b6'
  38. ALIYUN_BASE_URL = 'https://dashscope.aliyuncs.com/compatible-mode/v1'
  39. class ChatServiceType(Enum):
  40. OPENAI_COMPATIBLE = auto()
  41. COZE_CHAT = auto()
  42. class ModelPrice:
  43. EXCHANGE_RATE_TO_CNY = {
  44. "USD": 7.2, # Example conversion rate, adjust as needed
  45. }
  46. def __init__(self, input_price: float, output_price: float, currency: str = 'CNY'):
  47. """
  48. :param input_price: input price for per million tokens
  49. :param output_price: output price for per million tokens
  50. """
  51. self.input_price = input_price
  52. self.output_price = output_price
  53. self.currency = currency
  54. def get_total_cost(self, input_tokens: int, output_tokens: int, convert_to_cny: bool = True) -> float:
  55. """
  56. Calculate the total cost based on input and output tokens.
  57. :param input_tokens: Number of input tokens
  58. :param output_tokens: Number of output tokens
  59. :param convert_to_cny: Whether to convert the cost to CNY (default is True)
  60. :return: Total cost in the specified currency
  61. """
  62. total_cost = (self.input_price * input_tokens / 1_000_000) + (self.output_price * output_tokens / 1_000_000)
  63. if convert_to_cny and self.currency != 'CNY':
  64. conversion_rate = self.EXCHANGE_RATE_TO_CNY.get(self.currency, 1.0)
  65. total_cost *= conversion_rate
  66. return total_cost
  67. def __repr__(self):
  68. return f"ModelPrice(input_price={self.input_price}, output_price={self.output_price}, currency={self.currency})"
  69. class OpenAICompatible:
  70. volcengine_models = [
  71. VOLCENGINE_MODEL_DOUBAO_PRO_32K,
  72. VOLCENGINE_MODEL_DOUBAO_PRO_1_5_32K,
  73. VOLCENGINE_MODEL_DOUBAO_1_5_VISION_PRO,
  74. VOLCENGINE_MODEL_DEEPSEEK_V3
  75. ]
  76. deepseek_models = [
  77. DEEPSEEK_CHAT_MODEL,
  78. ]
  79. openai_models = [
  80. OPENAI_MODEL_GPT_4o_mini,
  81. OPENAI_MODEL_GPT_4o
  82. ]
  83. openrouter_models = [
  84. OPENROUTER_MODEL_CLAUDE_3_7_SONNET,
  85. OPENROUTER_MODEL_GEMINI_2_5_PRO
  86. ]
  87. model_prices = {
  88. VOLCENGINE_MODEL_DEEPSEEK_V3: ModelPrice(input_price=2, output_price=8),
  89. VOLCENGINE_MODEL_DOUBAO_PRO_32K: ModelPrice(input_price=0.8, output_price=2),
  90. VOLCENGINE_MODEL_DOUBAO_PRO_1_5_32K: ModelPrice(input_price=0.8, output_price=2),
  91. VOLCENGINE_MODEL_DOUBAO_1_5_VISION_PRO: ModelPrice(input_price=3, output_price=9),
  92. DEEPSEEK_CHAT_MODEL: ModelPrice(input_price=2, output_price=8),
  93. OPENAI_MODEL_GPT_4o: ModelPrice(input_price=2.5, output_price=10, currency='USD'),
  94. OPENAI_MODEL_GPT_4o_mini: ModelPrice(input_price=0.15, output_price=0.6, currency='USD'),
  95. OPENROUTER_MODEL_CLAUDE_3_7_SONNET: ModelPrice(input_price=3, output_price=15, currency='USD'),
  96. OPENROUTER_MODEL_GEMINI_2_5_PRO: ModelPrice(input_price=1.25, output_price=10, currency='USD'),
  97. }
  98. @staticmethod
  99. def create_client(model_name, **kwargs) -> OpenAI:
  100. if model_name in OpenAICompatible.volcengine_models:
  101. llm_client = OpenAI(api_key=VOLCENGINE_API_TOKEN, base_url=VOLCENGINE_BASE_URL, **kwargs)
  102. elif model_name in OpenAICompatible.deepseek_models:
  103. llm_client = OpenAI(api_key=DEEPSEEK_API_TOKEN, base_url=DEEPSEEK_BASE_URL, **kwargs)
  104. elif model_name in OpenAICompatible.openai_models:
  105. socks_conf = configs.get().get('system', {}).get('outside_proxy', {}).get('socks5', {})
  106. if socks_conf:
  107. http_client = httpx.Client(
  108. timeout=httpx.Timeout(600, connect=5.0),
  109. proxy=f"socks5://{socks_conf['hostname']}:{socks_conf['port']}"
  110. )
  111. kwargs['http_client'] = http_client
  112. llm_client = OpenAI(api_key=OPENAI_API_TOKEN, base_url=OPENAI_BASE_URL, **kwargs)
  113. elif model_name in OpenAICompatible.openrouter_models:
  114. llm_client = OpenAI(api_key=OPENROUTER_API_TOKEN, base_url=OPENROUTER_BASE_URL, **kwargs)
  115. else:
  116. raise Exception("Unsupported model: %s" % model_name)
  117. return llm_client
  118. @staticmethod
  119. def get_price(model_name: str) -> ModelPrice:
  120. """
  121. Get the price for a given model.
  122. :param model_name: Name of the model
  123. :return: ModelPrice object containing input and output prices
  124. """
  125. if model_name not in OpenAICompatible.model_prices:
  126. raise ValueError(f"Model {model_name} not found in price list.")
  127. return OpenAICompatible.model_prices[model_name]
  128. @staticmethod
  129. def calculate_cost(model_name: str, input_tokens: int, output_tokens: int, convert_to_cny: bool = True) -> float:
  130. """
  131. Calculate the cost for a given model based on input and output tokens.
  132. :param model_name: Name of the model
  133. :param input_tokens: Number of input tokens
  134. :param output_tokens: Number of output tokens
  135. :param convert_to_cny: Whether to convert the cost to CNY (default is True)
  136. :return: Total cost in the model's currency
  137. """
  138. if model_name not in OpenAICompatible.model_prices:
  139. raise ValueError(f"Model {model_name} not found in price list.")
  140. price = OpenAICompatible.model_prices[model_name]
  141. return price.get_total_cost(input_tokens, output_tokens, convert_to_cny)
  142. class CrossAccountJWTOAuthApp(JWTOAuthApp):
  143. def __init__(self, account_id: str, client_id: str, private_key: str, public_key_id: str, base_url):
  144. self.account_id = account_id
  145. super().__init__(client_id, private_key, public_key_id, base_url)
  146. def get_access_token(
  147. self, ttl: int = 900, scope: Optional[cozepy.Scope] = None, session_name: Optional[str] = None
  148. ) -> cozepy.OAuthToken:
  149. jwt_token = self._gen_jwt(self._public_key_id, self._private_key, 3600, session_name)
  150. url = f"{self._base_url}/api/permission/oauth2/account/{self.account_id}/token"
  151. headers = {"Authorization": f"Bearer {jwt_token}"}
  152. body = {
  153. "duration_seconds": ttl,
  154. "grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer",
  155. "scope": scope.model_dump() if scope else None,
  156. }
  157. return self._requester.request("post", url, False, cozepy.OAuthToken, headers=headers, body=body)
  158. class CozeChat:
  159. def __init__(self, base_url: str, auth_token: Optional[str] = None, auth_app: Optional[JWTOAuthApp] = None):
  160. if not auth_token and not auth_app:
  161. raise ValueError("Either auth_token or auth_app must be provided.")
  162. self.thread = None
  163. self.thread_running = False
  164. self.last_token_fresh = 0
  165. if auth_token:
  166. self.coze = Coze(auth=TokenAuth(auth_token), base_url=base_url)
  167. else:
  168. self.auth_app = auth_app
  169. oauth_token = auth_app.get_access_token(ttl=12*3600)
  170. self.last_token_fresh = time.time()
  171. self.coze = Coze(auth=JWTAuth(oauth_app=auth_app), base_url=base_url)
  172. self.setup_token_refresh()
  173. def create(self, bot_id: str, user_id: str, messages: List, custom_variables: Dict):
  174. response = self.coze.chat.create_and_poll(
  175. bot_id=bot_id, user_id=user_id, additional_messages=messages,
  176. custom_variables=custom_variables)
  177. logger.debug("Coze response size: {}".format(len(response.messages)))
  178. if response.chat.status != ChatStatus.COMPLETED:
  179. logger.error("Coze chat not completed: {}".format(response.chat.status))
  180. return None
  181. final_response = None
  182. for message in response.messages:
  183. if message.type == MessageType.ANSWER:
  184. final_response = message.content
  185. return final_response
  186. def setup_token_refresh(self):
  187. self.thread = threading.Thread(target=self.refresh_token_loop)
  188. self.thread.start()
  189. self.thread_running = True
  190. def refresh_token_loop(self):
  191. while self.thread_running:
  192. if time.time() - self.last_token_fresh < 11*3600:
  193. time.sleep(1)
  194. continue
  195. if self.auth_app:
  196. self.auth_app.get_access_token(ttl=12*3600)
  197. self.last_token_fresh = time.time()
  198. def __del__(self):
  199. self.thread_running = False
  200. @staticmethod
  201. def get_oauth_app(client_id, private_key_path, public_key_id, base_url=None, account_id=None) -> JWTOAuthApp:
  202. if not base_url:
  203. base_url = COZE_CN_BASE_URL
  204. with open(private_key_path, "r") as f:
  205. private_key = f.read()
  206. if not account_id:
  207. jwt_oauth_app = JWTOAuthApp(
  208. client_id=str(client_id),
  209. private_key=private_key,
  210. public_key_id=public_key_id,
  211. base_url=base_url,
  212. )
  213. else:
  214. jwt_oauth_app = CrossAccountJWTOAuthApp(
  215. account_id=account_id,
  216. client_id=str(client_id),
  217. private_key=private_key,
  218. public_key_id=public_key_id,
  219. base_url=base_url,
  220. )
  221. return jwt_oauth_app
  222. if __name__ == '__main__':
  223. # Init the Coze client through the access_token.
  224. coze = Coze(auth=TokenAuth(token=COZE_API_TOKEN), base_url=COZE_CN_BASE_URL)
  225. # Create a bot instance in Coze, copy the last number from the web link as the bot's ID.
  226. bot_id = "7491250992952999973"
  227. # The user id identifies the identity of a user. Developers can use a custom business ID
  228. # or a random string.
  229. user_id = "dev_user"
  230. chat = coze.chat.create_and_poll(
  231. bot_id=bot_id,
  232. user_id=user_id,
  233. additional_messages=[Message.build_user_question_text("钱塘江边 樱花开得不错,推荐一个视频吧")],
  234. custom_variables={
  235. 'agent_name': '芳华',
  236. 'agent_age': '25',
  237. 'agent_region': '北京',
  238. 'name': '李明',
  239. 'preferred_nickname': '李叔',
  240. 'age': '70',
  241. 'last_interaction_interval': '12',
  242. 'current_time_period': '上午',
  243. 'if_first_interaction': 'False',
  244. 'if_active_greeting': 'False'
  245. }
  246. )
  247. for message in chat.messages:
  248. print(message, flush=True)
  249. if chat.chat.status == ChatStatus.COMPLETED:
  250. print("token usage:", chat.chat.usage.token_count)