111.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import requests
  2. from fastapi import FastAPI, HTTPException, Request
  3. from pydantic import BaseModel
  4. import aiohttp
  5. import logging
  6. from fastapi.responses import JSONResponse
  7. import json
  8. from typing import Optional, List, Dict
  9. import asyncio
  10. # 配置日志
  11. logging.basicConfig(level=logging.INFO)
  12. logger = logging.getLogger(__name__)
  13. def call_llm(question: str, userId: str) -> Optional[Dict]:
  14. url = "http://192.168.100.100:7861/chat/kb_chat"
  15. payload = {
  16. "query": question,
  17. "mode": "local_kb",
  18. "kb_name": "lqbz",
  19. "top_k": 3,
  20. "score_threshold": 2,
  21. "history": [
  22. {"content": '你好', "role": "user"},
  23. {"content": "", "role": "assistant"}
  24. ],
  25. "stream": True, # 设置为True以启用流式传输
  26. "model": "deepseek-r1:1.5b",
  27. "temperature": 0.7,
  28. "max_tokens": 0,
  29. "prompt_name": "default",
  30. "return_direct": False
  31. }
  32. combined_text = ""
  33. all_docs = []
  34. try:
  35. with requests.post(url, json=payload, stream=True) as response:
  36. if response.status_code == 200:
  37. for line in response.iter_lines():
  38. if line: # 过滤掉keep-alive新行
  39. decoded_line = line.decode('utf-8')
  40. if decoded_line.startswith('data: '):
  41. json_str = decoded_line[len('data: '):]
  42. try:
  43. json_obj = json.loads(json_str)
  44. # 提取choices中的内容并合并
  45. for choice in json_obj.get("choices", []):
  46. content = choice.get("delta", {}).get("content", "")
  47. if content:
  48. combined_text += content # 直接追加,保留换行符
  49. # 收集docs中的内容
  50. docs = json_obj.get("docs", [])
  51. all_docs.extend(docs)
  52. except json.JSONDecodeError as e:
  53. logger.error(f"无法解析的块: {json_str[:50]}...")
  54. logger.error(f"错误信息: {e}")
  55. # print(combined_text)
  56. print("************")
  57. think = combined_text.split("</think>")[0]
  58. think = think.replace("<think>", "")
  59. print("think", think)
  60. answer = combined_text.split("</think>")[1]
  61. print("answer:", answer)
  62. print("************")
  63. # 返回结果,包括保留换行符的answer
  64. return {"answer": combined_text, "docs": all_docs}
  65. else:
  66. logger.error(f"Error calling service: {response.status_code} - {response.text}")
  67. return None
  68. except requests.RequestException as e:
  69. logger.error(f"Unexpected error while calling service: {e}")
  70. return None
  71. # 示例:如何调用这个函数,并显示保留换行的答案
  72. if __name__ == "__main__":
  73. question = "你好"
  74. userId = "用户ID"
  75. result = call_llm(question, userId)
  76. if result is not None:
  77. print("成功获取结果:")
  78. print("Answer:\n{}".format(result)) # 直接打印,保留换行符
  79. if result["docs"]:
  80. print("Docs:\n{}".format(result["docs"]))
  81. else:
  82. print("未能成功获取结果")