debug_rerank.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. 调试rerank请求差异
  5. """
  6. import requests
  7. import json
  8. url = "http://192.168.91.253:9004/v1/rerank"
  9. prefix = '<|im_start|>system\nJudge whether the Document meets the requirements based on the Query and the Instruct provided. Note that the answer can only be "yes" or "no".<|im_end|>\n<|im_start|>user\n'
  10. suffix = "<|im_end|>\n<|im_start|>assistant\n"
  11. query_template = "{prefix}<Instruct>: {instruction}\n<Query>: {query}\n"
  12. document_template = "<Document>: {doc}{suffix}"
  13. instruction = "Given a web search query, retrieve relevant passages that answer the query"
  14. query = "什么是大语言模型?"
  15. candidates = [
  16. "Qwen 是阿里巴巴推出的大模型系列。",
  17. "今天天气很好,适合出去玩。",
  18. "大语言模型是一种基于Transformer的模型",
  19. "vegetable"
  20. ]
  21. # 格式化
  22. formatted_query = query_template.format(prefix=prefix, instruction=instruction, query=query)
  23. formatted_documents = [document_template.format(doc=doc, suffix=suffix) for doc in candidates]
  24. print("=" * 80)
  25. print("请求数据:")
  26. print("=" * 80)
  27. request_data = {
  28. "query": formatted_query,
  29. "documents": formatted_documents
  30. }
  31. print(json.dumps(request_data, ensure_ascii=False, indent=2))
  32. print("\n" + "=" * 80)
  33. print("发送请求...")
  34. print("=" * 80)
  35. response = requests.post(url, json=request_data).json()
  36. print("\n" + "=" * 80)
  37. print("响应结果:")
  38. print("=" * 80)
  39. print(json.dumps(response, ensure_ascii=False, indent=2))
  40. print("\n" + "=" * 80)
  41. print("关键信息:")
  42. print("=" * 80)
  43. if "results" in response:
  44. for idx, result in enumerate(response["results"]):
  45. print(f"排名{idx+1}: index={result['index']}, score={result['relevance_score']:.6f}")
  46. print(f" 文档预览: {result['document']['text'][:50]}...")