2222.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import requests
  2. import logging
  3. import json
  4. import requests
  5. from fastapi import FastAPI, HTTPException, Request
  6. from pydantic import BaseModel
  7. import aiohttp
  8. import logging
  9. from fastapi.responses import JSONResponse
  10. import json
  11. from typing import Optional, List, Dict
  12. import asyncio
  13. # 配置日志
  14. logging.basicConfig(level=logging.INFO)
  15. logger = logging.getLogger(__name__)
  16. def call_llm(question: str, userId: str) -> Optional[Dict]:
  17. url = "http://192.168.100.100:7861/chat/kb_chat"
  18. payload = {
  19. "query": question,
  20. "mode": "local_kb",
  21. "kb_name": "lqbz",
  22. "top_k": 3,
  23. "score_threshold": 2,
  24. "history": [
  25. {"content": '你好', "role": "user"},
  26. {"content": "", "role": "assistant"}
  27. ],
  28. "stream": True, # 设置为True以启用流式传输
  29. "model": "qwen2.5:1.5b",
  30. "temperature": 0.7,
  31. "max_tokens": 0,
  32. "prompt_name": "default",
  33. "return_direct": False
  34. }
  35. combined_text = ""
  36. all_docs = []
  37. think_blocks = [] # 存储所有的think块内容
  38. current_think_block = "" # 当前的think块内容
  39. in_think_block = False
  40. try:
  41. with requests.post(url, json=payload, stream=True) as response:
  42. if response.status_code == 200:
  43. for line in response.iter_lines():
  44. if line: # 过滤掉keep-alive新行
  45. decoded_line = line.decode('utf-8')
  46. if decoded_line.startswith('data: '):
  47. json_str = decoded_line[len('data: '):]
  48. try:
  49. json_obj = json.loads(json_str)
  50. # 提取choices中的内容并合并
  51. for choice in json_obj.get("choices", []):
  52. content = choice.get("delta", {}).get("content", "")
  53. if content:
  54. while "<think>" in content or "</think>" in content:
  55. start_index = content.find("<think>")
  56. end_index = content.find("</think>")
  57. if start_index != -1 and end_index != -1:
  58. # 如果找到了完整的<think>...</think>块
  59. if not in_think_block:
  60. in_think_block = True
  61. think_part = content[start_index + len("<think>"):end_index]
  62. current_think_block += think_part
  63. think_blocks.append(current_think_block.strip())
  64. current_think_block = ""
  65. content = content[end_index + len("</think>"):]
  66. in_think_block = False
  67. elif start_index != -1:
  68. # 只有<think>开始标签
  69. if not in_think_block:
  70. in_think_block = True
  71. content = content[start_index + len("<think>"):]
  72. elif end_index != -1:
  73. # 只有</think>结束标签
  74. current_think_block += content[:end_index]
  75. think_blocks.append(current_think_block.strip())
  76. current_think_block = ""
  77. content = content[end_index + len("</think>"):]
  78. in_think_block = False
  79. break # 结束当前循环,继续处理剩余内容
  80. else:
  81. break # 没有找到任何标签,跳出循环
  82. if in_think_block:
  83. current_think_block += content
  84. else:
  85. combined_text += content
  86. # 如果在循环结束时仍有未完成的think块,则添加
  87. if current_think_block and not in_think_block:
  88. think_blocks.append(current_think_block.strip())
  89. # 收集docs中的内容
  90. docs = json_obj.get("docs", [])
  91. all_docs.extend(docs)
  92. except json.JSONDecodeError as e:
  93. logger.error(f"无法解析的块: {json_str[:50]}...")
  94. logger.error(f"错误信息: {e}")
  95. # 返回结果,包括保留换行符的answer和think块列表
  96. return {"answer": combined_text, "docs": all_docs, "think": think_blocks}
  97. else:
  98. logger.error(f"Error calling service: {response.status_code} - {response.text}")
  99. return None
  100. except requests.RequestException as e:
  101. logger.error(f"Unexpected error while calling service: {e}")
  102. return None
  103. # 示例:如何调用这个函数,并显示保留换行的答案
  104. if __name__ == "__main__":
  105. question = "你好"
  106. userId = "用户ID"
  107. result = call_llm(question, userId)
  108. print("**********")
  109. print(result['think'])
  110. print("**********")
  111. if result is not None:
  112. print("成功获取结果:")
  113. print("Answer:\n{}".format(result["answer"])) # 直接打印,保留换行符
  114. if result["docs"]:
  115. print("Docs:\n{}".format(result["docs"]))
  116. if result["think"]:
  117. print("Think Blocks Content:")
  118. for idx, block in enumerate(result["think"], 1):
  119. print(f"Block {idx}:\n{block}")
  120. else:
  121. print("未能成功获取结果")