chat_utils.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from typing import List, Optional, Dict
  2. from ..schemas.chat import ChatMessage
  3. from ..core.ark_client import config
  4. def convert_messages_for_api(messages: List[ChatMessage]) -> List[Dict[str, str]]:
  5. return [{"role": msg.role, "content": msg.content} for msg in messages]
  6. def get_latest_user_message(messages: List[ChatMessage]) -> Optional[ChatMessage]:
  7. for message in reversed(messages):
  8. if message.role == "user":
  9. return message
  10. return None
  11. def get_previous_response_id(username: str, chat_history: dict) -> Optional[str]:
  12. if username in chat_history:
  13. for message in reversed(chat_history[username]):
  14. if message.role == "assistant" and message.response_id:
  15. return message.response_id
  16. return None
  17. # 联网搜索工具
  18. def get_web_search_tools() -> list:
  19. return [{
  20. "type": "web_search",
  21. "max_keyword": 20,
  22. "limit": 20,
  23. "sources": ["douyin", "moji", "toutiao"],# 附加搜索来源(抖音百科、墨迹天气、头条图文等平台)
  24. "user_location": { # 用户地理位置(用于优化搜索结果)
  25. "type": "approximate", # 大致位置
  26. "country": "中国",
  27. "region": "浙江",
  28. "city": "杭州"
  29. }
  30. }]
  31. # 豆包助手工具
  32. def get_doubao_tools() -> list:
  33. return [{
  34. "type": "doubao_app",
  35. "feature": {
  36. # 联网搜索功能
  37. "ai_search": {
  38. "type": "disabled",
  39. "role_description": config.ROLE_DESCRIPTION
  40. },
  41. # 边想边搜功能
  42. "reasoning_search":{
  43. "type": "enabled",
  44. "role_description": config.ROLE_DESCRIPTION
  45. }
  46. },
  47. "user_location": {
  48. "type": "approximate",
  49. "country": "中国",
  50. "region": "浙江",
  51. "city": "杭州"
  52. }
  53. }]