chat_service.py 12 KB

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