chat_utils.py 1.8 KB

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