chat_utils.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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_knowledge_search_tools() -> list:
  30. return [{
  31. "type": "knowledge_search",
  32. "knowledge_resource_id": "kb-916f7f4baa369d97", # 替换为实际知识库ID
  33. "limit": 10, # 最多返回10条搜索结果
  34. }]
  35. # 豆包助手工具
  36. def get_doubao_tools() -> list:
  37. return [{
  38. "type": "doubao_app",
  39. "feature": {
  40. # 联网搜索功能
  41. "ai_search": {
  42. "type": "disabled",
  43. "role_description": config.ROLE_DESCRIPTION
  44. },
  45. # 边想边搜功能
  46. "reasoning_search":{
  47. "type": "enabled",
  48. "role_description": config.ROLE_DESCRIPTION
  49. }
  50. },
  51. "user_location": {
  52. "type": "approximate",
  53. "country": "中国",
  54. "region": "浙江",
  55. "city": "杭州"
  56. }
  57. }]