{"id":3916,"date":"2025-07-26T16:59:52","date_gmt":"2025-07-26T08:59:52","guid":{"rendered":"https:\/\/fushuling.com\/?p=3916"},"modified":"2025-07-26T16:59:54","modified_gmt":"2025-07-26T08:59:54","slug":"%e6%96%b0%e7%94%9flab-6-%e5%a4%a7%e6%a8%a1%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/fushuling.com\/index.php\/2025\/07\/26\/%e6%96%b0%e7%94%9flab-6-%e5%a4%a7%e6%a8%a1%e5%9e%8b\/","title":{"rendered":"\u65b0\u751fLab-6 \u5927\u6a21\u578b"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Task #0: \u7528\u811a\u672c\u8bf7\u6c42\u5546\u7528\u5927\u6a21\u578b<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u76ee\u6807<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b66\u4f1a\u4f7f\u7528python\u811a\u672c\u8bf7\u6c42\u5546\u7528\u5927\u6a21\u578b<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u9a8c\u5185\u5bb9<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6ce8\u518c\u5e76\u9009\u62e9\u7845\u57fa\u6d41\u52a8\u4e0a\u7684\u4efb\u610f\u5927\u8bed\u8a00\u6a21\u578b\u4f5c\u4e3a\u4f60\u7684\u5b9e\u9a8c\u5bf9\u8c61 \uff0c\u83b7\u53d6\u514d\u8d39\u989d\u5ea6<\/li>\n\n\n\n<li>\u9605\u8bfb\u6587\u6863\uff0c\u64b0\u5199Python\u811a\u672c\uff0c\u5b9e\u73b0\u5bf9\u5927\u6a21\u578b\u7684\u81ea\u52a8\u5316\u8bf7\u6c42\u5305\u88c5\u6210\u4e00\u4e2a\u540d\u4e3achat\u7684\u51fd\u6570\uff0c\u4fbf\u4e8e\u540e\u9762\u4f7f\u7528<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u9a8c\u6d41\u7a0b<\/h3>\n\n\n\n<p>\u5148\u53bb<a href=\"https:\/\/cloud.siliconflow.cn\/\">https:\/\/cloud.siliconflow.cn<\/a>\u6ce8\u518c\u4e00\u4e2a\u53f7\uff0c\u62ff\u5230api\uff0c\u8fd9\u91cc\u5c31\u9009\u4e0d\u8981\u94b1\u7684\u6a21\u578b\u4e86\uff0c\u7136\u540e\u5199\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u4f20\u5165\u7684\u53c2\u6570\u662f\u60f3\u95ee\u7684\u95ee\u9898\uff0c\u8fd4\u56de\u662fllm\u7684\u56de\u7b54\uff0c\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from langchain_openai import ChatOpenAI<br>\u200b<br>API_KEY = \"sk-xxx\"<br>BASE_URL = \"https:\/\/api.siliconflow.cn\/v1\/\"<br>MODEL_NAME = \"deepseek-ai\/DeepSeek-R1-0528-Qwen3-8B\"<br>\u200b<br># --- \u5168\u5c40 LLM \u5b9e\u4f8b ---<br>llm = ChatOpenAI(<br> \u00a0  api_key=API_KEY,<br> \u00a0  base_url=BASE_URL,<br> \u00a0  model=MODEL_NAME,<br> \u00a0  temperature=0.7 <br>)<br>\u200b<br>\u200b<br>def ask(question: str) -> str:<br> \u00a0  \"\"\"<br> \u00a0  \u5411\u5927\u8bed\u8a00\u6a21\u578b\u63d0\u95ee\u5e76\u83b7\u53d6\u7eaf\u6587\u672c\u56de\u7b54\u3002<br>\u200b<br> \u00a0  \u53c2\u6570:<br> \u00a0  - question (str): \u4f60\u60f3\u8981\u95ee\u7684\u95ee\u9898\u3002<br>\u200b<br> \u00a0  \u8fd4\u56de:<br> \u00a0  - str: \u5927\u8bed\u8a00\u6a21\u578b\u8fd4\u56de\u7684\u56de\u7b54\u5185\u5bb9\u3002<br> \u00a0  \"\"\"<br> \u00a0  try:<br> \u00a0 \u00a0 \u00a0  # \u4f7f\u7528 llm.invoke() \u65b9\u6cd5\u53d1\u9001\u8bf7\u6c42<br> \u00a0 \u00a0 \u00a0  response_message = llm.invoke(question)<br> \u00a0 \u00a0 \u00a0 \u00a0<br> \u00a0 \u00a0 \u00a0  # \u4ece\u8fd4\u56de\u7684 AIMessage \u5bf9\u8c61\u4e2d\u63d0\u53d6 .content \u5c5e\u6027\uff0c\u5373\u6a21\u578b\u7684\u56de\u7b54\u6587\u672c<br> \u00a0 \u00a0 \u00a0  return response_message.content<br> \u00a0  except Exception as e:<br> \u00a0 \u00a0 \u00a0  print(f\"\u8bf7\u6c42\u6a21\u578b\u65f6\u53d1\u751f\u9519\u8bef: {e}\")<br> \u00a0 \u00a0 \u00a0  return \"\u62b1\u6b49\uff0c\u65e0\u6cd5\u83b7\u53d6\u56de\u7b54\u3002\"<br>\u200b<br>\u200b<br>if __name__ == \"__main__\":<br>\u200b<br> \u00a0  my_question = \"\u6c38\u96cf\u5854\u83f2\u662f\u4ec0\u4e48\uff1f\"<br> \u00a0 \u00a0<br> \u00a0  answer = ask(my_question)<br> \u00a0 \u00a0<br> \u00a0  print(answer)<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/fushuling-1309926051.cos.ap-shanghai.myqcloud.com\/2025\/07\/QQ20250719-172345.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/fushuling-1309926051.cos.ap-shanghai.myqcloud.com\/2025\/07\/QQ20250719-172345.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"QQ20250719-172345\"\/><\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Task #1: \u5927\u6a21\u578b\u80fd\u529b\u8bc4\u6d4b\u548c\u4e0a\u4e0b\u6587\u5b66\u4e60(In-Context Learning)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u76ee\u6807<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7406\u89e3\u5927\u6a21\u578b\u80fd\u529b\u6d4b\u8bc4\u7684\u5168\u6d41\u7a0b<\/li>\n\n\n\n<li>\u7406\u89e30-shot\u548cfew-shot\u7684\u6982\u5ff5<\/li>\n\n\n\n<li>\u5b66\u4f1aIn-Context Learning<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u9a8c\u5185\u5bb9<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b66\u4e60\u5927\u6a21\u578b\u80fd\u529b\u9009\u62e9\u9898Benchmark C-Eval<\/li>\n\n\n\n<li>\u9009\u62e950\u4e2a\u6d4b\u8bd5\u95ee\u9898\uff0c\u5bf90x0\u4e2d\u7684\u5927\u6a21\u578b\u8fdb\u884c\u8bc4\u6d4b\uff0c\u5bf9\u6bd40\u2014shot\u548c5\u2014shot\u7684\u7ed3\u679c<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u9a8c\u6d41\u7a0b<\/h3>\n\n\n\n<p>\u6211\u4eec\u4e0d\u80fd\u7b80\u5355\u5730\u901a\u8fc7\u804a\u5929\u6765\u5224\u65ad\u4e00\u4e2a\u6a21\u578b\u7684\u597d\u574f\u3002\u9700\u8981\u4e00\u5957\u6807\u51c6\u5316\u3001\u53ef\u590d\u73b0\u7684\u6d41\u7a0b\uff0c\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u57fa\u51c6 (Benchmark)<\/strong>\uff1a\u4e00\u4e2a\u6216\u591a\u4e2a\u6807\u51c6\u5316\u7684\u6570\u636e\u96c6\uff0c\u7528\u6765\u6d4b\u8bd5\u6a21\u578b\u7684\u7279\u5b9a\u80fd\u529b\uff08\u5982\u5e38\u8bc6\u63a8\u7406\u3001\u4ee3\u7801\u751f\u6210\u3001\u6570\u5b66\u8ba1\u7b97\u7b49\uff09\u3002<\/li>\n\n\n\n<li><strong>\u6307\u6807 (Metric)<\/strong>\uff1a\u91cf\u5316\u6a21\u578b\u8868\u73b0\u7684\u65b9\u5f0f\uff0c\u5bf9\u4e8e\u9009\u62e9\u9898\uff0c\u6700\u5e38\u7528\u7684\u5c31\u662f\u201c\u51c6\u786e\u7387 (Accuracy)\u201d\u3002<\/li>\n\n\n\n<li><strong>\u65b9\u6cd5 (Method)<\/strong>\uff1a\u5982\u4f55\u5411\u6a21\u578b\u63d0\u95ee\uff0c\u6bd4\u5982\u662f\u76f4\u63a5\u63d0\u95ee\uff080-shot\uff09\u8fd8\u662f\u7ed9\u4e00\u4e9b\u4f8b\u5b50\u518d\u63d0\u95ee\uff08Few-shot\uff09<\/li>\n<\/ul>\n\n\n\n<p>\u4e0a\u4e0b\u6587\u5b66\u4e60\u8fd9\u662f\u73b0\u4ee3\u5927\u6a21\u578b\uff08\u5982GPT\u7cfb\u5217\uff09\u6700\u60ca\u4eba\u7684\u80fd\u529b\u4e4b\u4e00\uff0c\u4e0d\u9700\u8981\u91cd\u65b0\u8bad\u7ec3\u6a21\u578b\uff0c\u53ea\u9700\u5728\u8f93\u5165\u7684\u63d0\u793a(Prompt)\u4e2d\u63d0\u4f9b\u51e0\u4e2a\u793a\u4f8b\uff08\u201cshots\u201d\uff09\uff0c\u6a21\u578b\u5c31\u80fd\u201c\u79d2\u61c2\u201d\u4f60\u7684\u4efb\u52a1\u8981\u6c42\u548c\u8f93\u51fa\u683c\u5f0f\uff0c\u5e76\u7ed9\u51fa\u66f4\u9ad8\u8d28\u91cf\u7684\u56de\u7b54\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>0-shot (\u96f6\u6837\u672c)<\/strong>\uff1a\u4e0d\u7ed9\u4efb\u4f55\u793a\u4f8b\uff0c\u76f4\u63a5\u5411\u6a21\u578b\u63d0\u51fa\u95ee\u9898\u3002\u8fd9\u8003\u9a8c\u7684\u662f\u6a21\u578b\u5728\u9884\u8bad\u7ec3\u9636\u6bb5\u5b66\u5230\u7684\u901a\u7528\u77e5\u8bc6\u548c\u63a8\u7406\u80fd\u529b\u3002\n<ul class=\"wp-block-list\">\n<li>\u4f8b\u5b50\uff1a\u201c\u4e2d\u56fd\u7684\u9996\u90fd\u662f\u54ea\u91cc\uff1fA. \u4e0a\u6d77 B. \u5317\u4eac\u201d -> \u6a21\u578b\u56de\u7b54<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Few-shot (\u5c11\u6837\u672c)<\/strong>\uff1a\u5728\u63d0\u95ee\u524d\uff0c\u5148\u7ed9\u51fa\u51e0\u4e2a\u201c\u95ee\u9898 + \u6b63\u786e\u7b54\u6848\u201d\u7684\u8303\u4f8b\u3002\u8fd9\u80fd\u5e2e\u52a9\u6a21\u578b\u66f4\u597d\u5730\u7406\u89e3\u4efb\u52a1\u7684\u610f\u56fe\u548c\u671f\u671b\u7684\u8f93\u51fa\u683c\u5f0f\u3002\n<ul class=\"wp-block-list\">\n<li>\u4f8b\u5b50\uff1a\u201c\u95ee\u9898\uff1a\u6cd5\u56fd\u7684\u9996\u90fd\u662f\u54ea\u91cc\uff1f\u7b54\u6848\uff1a\u5df4\u9ece\u3002 \u95ee\u9898\uff1a\u65e5\u672c\u7684\u9996\u90fd\u662f\u54ea\u91cc\uff1f\u7b54\u6848\uff1a\u4e1c\u4eac\u3002 \u95ee\u9898\uff1a\u4e2d\u56fd\u7684\u9996\u90fd\u662f\u54ea\u91cc\uff1f\u201d -> \u6a21\u578b\u56de\u7b54<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>\u5b66\u957f\u8bf4\u7684\u90a3\u4e2a\u7f51\u7ad9\u7684\u63a5\u53e3\u6709\u70b9\u6162\uff0c\u6362\u4e86\u4e00\u4e2a\uff0c\u7528\u7684\u6570\u636e\u96c6\u662fboolq_train<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import re<br>from tqdm import tqdm<br>from datasets import load_dataset, load_from_disk<br>from langchain_openai import ChatOpenAI<br>import os<br>\u200b<br>API_KEY = \"sk-xxxx\"<br>BASE_URL = \"https:\/\/api.gptgod.online\/v1\/\"<br>MODEL_NAME = \"gpt-3.5-turbo\"<br>\u200b<br># --- \u8bc4\u6d4b\u6570\u636e\u96c6\u914d\u7f6e ---<br>EVAL_SUBJECT = \"boolq\"<br>CACHE_DIR = \".boolq_cache\"<br>\u200b<br>print(f\"\u6b63\u5728\u521d\u59cb\u5316\u6a21\u578b: {MODEL_NAME}...\")<br>llm = ChatOpenAI(<br> \u00a0  api_key=API_KEY,<br> \u00a0  base_url=BASE_URL,<br> \u00a0  model=MODEL_NAME,<br> \u00a0  temperature=0.1, <br> \u00a0  request_timeout=60,<br>)<br>\u200b<br>def normalize_answer(raw_answer):<br> \u00a0  return \"A\" if raw_answer is True else \"B\"<br>\u200b<br>def ask(question: str) -> str:<br> \u00a0  try:<br> \u00a0 \u00a0 \u00a0  response_message = llm.invoke(question)<br> \u00a0 \u00a0 \u00a0  return response_message.content.strip()<br> \u00a0  except Exception as e:<br> \u00a0 \u00a0 \u00a0  print(f\"\\n[\u9519\u8bef] \u8bf7\u6c42\u6a21\u578b\u65f6\u53d1\u751f\u9519\u8bef: {e}\")<br> \u00a0 \u00a0 \u00a0  return \"ERROR\"<br>\u200b<br>def parse_model_answer(model_output: str) -> str:<br> \u00a0  if \"\u662f\" in model_output or \"\u5bf9\" in model_output or \"A\" in model_output:<br> \u00a0 \u00a0 \u00a0  return \"A\"<br> \u00a0  elif \"\u5426\" in model_output or \"\u4e0d\" in model_output or \"B\" in model_output:<br> \u00a0 \u00a0 \u00a0  return \"B\"<br> \u00a0  else:<br> \u00a0 \u00a0 \u00a0  match = re.search(r\"[A-B]\", model_output.upper())<br> \u00a0 \u00a0 \u00a0  return match.group(0) if match else \"\"<br>\u200b<br>def load_boolq_data(num_questions: int = 50):<br> \u00a0  os.makedirs(CACHE_DIR, exist_ok=True)<br> \u00a0  cache_path = os.path.join(CACHE_DIR, \"boolq_train\")<br>\u200b<br> \u00a0  if os.path.exists(cache_path):<br> \u00a0 \u00a0 \u00a0  print(f\"\u6b63\u5728\u4ece\u672c\u5730\u7f13\u5b58\u52a0\u8f7d\u6570\u636e\u96c6\uff1a{cache_path}\")<br> \u00a0 \u00a0 \u00a0  dataset = load_from_disk(cache_path)<br> \u00a0  else:<br> \u00a0 \u00a0 \u00a0  print(\"\u4ece Hugging Face \u4e0b\u8f7d boolq \u6570\u636e\u96c6...\")<br> \u00a0 \u00a0 \u00a0  dataset = load_dataset(\"boolq\", split=\"train\")<br> \u00a0 \u00a0 \u00a0  dataset.save_to_disk(cache_path)<br>\u200b<br> \u00a0  dataset = dataset.shuffle(seed=42)<br> \u00a0  eval_questions = dataset.select(range(num_questions))<br> \u00a0  shot_examples = dataset.select(range(5))<br> \u00a0  return eval_questions, shot_examples<br>\u200b<br>def run_evaluation(eval_type: str, questions, shots=None):<br> \u00a0  print(f\"\\n--- \u5f00\u59cb {eval_type.upper()} \u8bc4\u6d4b ---\")<br> \u00a0  correct_count = 0<br>\u200b<br> \u00a0  for idx, q in enumerate(tqdm(questions, desc=f\"{eval_type.capitalize()} Progress\")):<br> \u00a0 \u00a0 \u00a0  passage = q[\"passage\"]<br> \u00a0 \u00a0 \u00a0  question = q[\"question\"]<br> \u00a0 \u00a0 \u00a0  correct_letter = normalize_answer(q[\"answer\"])<br>\u200b<br> \u00a0 \u00a0 \u00a0  if eval_type == \"0-shot\":<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  prompt = f\"\"\"\u8bf7\u6839\u636e\u4e0b\u9762\u7684\u6bb5\u843d\u5224\u65ad\u95ee\u9898\u7684\u7b54\u6848\u662f\u201c\u662f\u201d\u8fd8\u662f\u201c\u5426\u201d\u3002\u8bf7\u76f4\u63a5\u56de\u7b54\u5b57\u6bcd\u3002<br>\u6bb5\u843d\uff1a{passage}<br>\u95ee\u9898\uff1a{question}<br>\u9009\u9879\uff1a<br>A. \u662f<br>B. \u5426<br>\u7b54\u6848\uff1a\"\"\"<br> \u00a0 \u00a0 \u00a0  else:<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  shot_prompt = \"\"<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  for shot in shots:<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  shot_passage = shot[\"passage\"]<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  shot_question = shot[\"question\"]<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  shot_answer = \"A\" if shot[\"answer\"] else \"B\"<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  shot_prompt += f\"\u6bb5\u843d\uff1a{shot_passage}\\n\u95ee\u9898\uff1a{shot_question}\\n\u7b54\u6848\uff1a{shot_answer}\\n\\n\"<br>\u200b<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  prompt = f\"\"\"\u4f60\u5c06\u770b\u5230\u4e00\u4e9b\u5224\u65ad\u9898\uff0c\u8bf7\u6839\u636e\u6bb5\u843d\u5185\u5bb9\u76f4\u63a5\u5224\u65ad\u201c\u662f\u201d\u8fd8\u662f\u201c\u5426\u201d\uff0c\u56de\u7b54\u5b57\u6bcd A \u6216 B\u3002<br>A. \u662f<br>B. \u5426<br>\u200b<br>{shot_prompt}\u6bb5\u843d\uff1a{passage}<br>\u95ee\u9898\uff1a{question}<br>\u7b54\u6848\uff1a\"\"\"<br>\u200b<br> \u00a0 \u00a0 \u00a0  model_output = ask(prompt)<br> \u00a0 \u00a0 \u00a0  predicted_letter = parse_model_answer(model_output)<br>\u200b<br> \u00a0 \u00a0 \u00a0  if predicted_letter == correct_letter:<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  correct_count += 1<br>\u200b<br> \u00a0  accuracy = correct_count \/ len(questions)<br> \u00a0  print(f\"\\n--- {eval_type.upper()} \u8bc4\u6d4b\u5b8c\u6210 ---\")<br> \u00a0  print(f\"\u603b\u9898\u6570: {len(questions)}, \u6b63\u786e\u6570: {correct_count}, \u51c6\u786e\u7387: {accuracy:.2%}\")<br> \u00a0  return accuracy<br>\u200b<br>if __name__ == \"__main__\":<br> \u00a0  eval_questions, shot_examples = load_boolq_data(num_questions=50)<br>\u200b<br> \u00a0  zero_shot_accuracy = run_evaluation(\"0-shot\", eval_questions)<br> \u00a0  five_shot_accuracy = run_evaluation(\"5-shot\", eval_questions, shot_examples)<br>\u200b<br> \u00a0  print(\"\\n\\n\" + \"=\"*30)<br> \u00a0  print(\" \u00a0 \u00a0 \u00a0 \u8bc4\u6d4b\u7ed3\u679c\u6c47\u603b\")<br> \u00a0  print(\"=\"*30)<br> \u00a0  print(f\"  \u6a21\u578b \u00a0 \u00a0 \u00a0 : {MODEL_NAME}\")<br> \u00a0  print(f\"  \u6570\u636e\u96c6 \u00a0 \u00a0 : {EVAL_SUBJECT}\")<br> \u00a0  print(f\"  \u95ee\u9898\u6570\u91cf \u00a0 : {len(eval_questions)}\")<br> \u00a0  print(\"-\" * 30)<br> \u00a0  print(f\"  0-Shot \u51c6\u786e\u7387: {zero_shot_accuracy:.2%}\")<br> \u00a0  print(f\"  5-Shot \u51c6\u786e\u7387: {five_shot_accuracy:.2%}\")<br> \u00a0  print(\"=\"*30)<br>\u200b<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/fushuling-1309926051.cos.ap-shanghai.myqcloud.com\/2025\/07\/QQ20250719-181555-2.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/fushuling-1309926051.cos.ap-shanghai.myqcloud.com\/2025\/07\/QQ20250719-181555-2.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"QQ20250719-181555-2\"\/><\/div><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\u6709few-shot\u7684\u8bdd\uff0c\u51c6\u786e\u7387\u4f1a\u9ad8\u4e00\u70b9<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Task #2: \u5b66\u4f1a\u4f7f\u7528\u6587\u672cEmbedding<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u76ee\u6807<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7406\u89e3\u6587\u672cembedding\u6a21\u578b\u7684\u7ed3\u6784<\/li>\n\n\n\n<li>\u90e8\u7f72\u548c\u4f7f\u7528embedding model<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u9a8c\u5185\u5bb9<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b66\u4e60\u57fa\u4e8elanguage model\u7684\u6587\u672c\n<ul class=\"wp-block-list\">\n<li>embedding\u53c2\u8003BERT\u6a21\u578b\u539f\u6587 <a href=\"https:\/\/arxiv.org\/abs\/1810.04805\">https:\/\/arxiv.org\/abs\/1810.04805<\/a><\/li>\n\n\n\n<li>\u53c2\u8003SentBERT <a href=\"https:\/\/arxiv.org\/abs\/1908.10084\">https:\/\/arxiv.org\/abs\/1908.10084<\/a><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u5c1d\u8bd5\u5728\u81ea\u5df1\u7684\u7535\u8111\u4e0a\u6216\u8005modelscope\u5e73\u53f0\u7684\u4e91\u670d\u52a1\u5668\u4e0a\u90e8\u7f72\u4e00\u4e2aembedding\u6a21\u578b\uff08\u65e0\u9700GPU\uff09\n<ul class=\"wp-block-list\">\n<li>GTE\u6587\u672c\u5411\u91cf-\u82f1\u6587-\u901a\u7528\u9886\u57df-small <a href=\"https:\/\/www.modelscope.cn\/models\/iic\/nlp_gte_sentence-embedding_english-small\/summary\">https:\/\/www.modelscope.cn\/models\/iic\/nlp_gte_sentence-embedding_english-small\/summary<\/a><\/li>\n\n\n\n<li>GTE\u6587\u672c\u5411\u91cf-\u4e2d\u6587-\u901a\u7528\u9886\u57df-small <a href=\"https:\/\/www.modelscope.cn\/models\/iic\/nlp_gte_sentence-embedding_chinese-small\/summary\">https:\/\/www.modelscope.cn\/models\/iic\/nlp_gte_sentence-embedding_chinese-small\/summary<\/a><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u601d\u8003\u5e76\u5c1d\u8bd5\uff1aEmbedding\u6a21\u578b\u6709\u4ec0\u4e48\u7528\uff1f\u81ea\u5df1\u8bbe\u8ba1\u4e00\u4e2aCase<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u9a8c\u6d41\u7a0b<\/h3>\n\n\n\n<p>Embedding\u662f\u5c06\u6587\u672c\u8f6c\u6362\u4e3a\u56fa\u5b9a\u7ef4\u5ea6\u5411\u91cf\u7684\u8fc7\u7a0b\uff0c\u4f7f\u5176\u80fd\u88ab\u8ba1\u7b97\u673a\u7406\u89e3\uff0c\u901a\u5e38embedding\u6a21\u578b\u662f\u8bed\u8a00\u6a21\u578b\u7684\u4e00\u90e8\u5206\uff0c\u6bd4\u5982 BERT\u3001GTE\uff0c\u5b83\u4eec\u4f1a\u62bd\u53d6\u6587\u672c\u7684\u8bed\u4e49\u4fe1\u606f\u3002<\/p>\n\n\n\n<p>\u76f4\u63a5\u6284\u7684nlp_gte_sentence-embedding_chinese-small<a href=\"https:\/\/modelscope.cn\/models\/iic\/nlp_gte_sentence-embedding_chinese-small\">\u5b98\u7f51<\/a>\u7684\u4ee3\u7801\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from modelscope.models import Model<br>from modelscope.pipelines import pipeline<br>from modelscope.utils.constant import Tasks<br>\u200b<br>model_id = \"iic\/nlp_gte_sentence-embedding_chinese-small\"<br>pipeline_se = pipeline(Tasks.sentence_embedding,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; model=model_id,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sequence_length=512<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , model_revision='master') # sequence_length \u4ee3\u8868\u6700\u5927\u6587\u672c\u957f\u5ea6\uff0c\u9ed8\u8ba4\u503c\u4e3a128<br>\u200b<br># \u5f53\u8f93\u5165\u5305\u542b\u201csoure_sentence\u201d\u4e0e\u201csentences_to_compare\u201d\u65f6\uff0c\u4f1a\u8f93\u51fasource_sentence\u4e2d\u9996\u4e2a\u53e5\u5b50\u4e0esentences_to_compare\u4e2d\u6bcf\u4e2a\u53e5\u5b50\u7684\u5411\u91cf\u8868\u793a\uff0c\u4ee5\u53casource_sentence\u4e2d\u9996\u4e2a\u53e5\u5b50\u4e0esentences_to_compare\u4e2d\u6bcf\u4e2a\u53e5\u5b50\u7684\u76f8\u4f3c\u5ea6\u3002<br>inputs = {<br> &nbsp; &nbsp; &nbsp;  \"source_sentence\": [\"\u5403\u5b8c\u6d77\u9c9c\u53ef\u4ee5\u559d\u725b\u5976\u5417?\"],<br> &nbsp; &nbsp; &nbsp;  \"sentences_to_compare\": [<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  \"\u4e0d\u53ef\u4ee5\uff0c\u65e9\u6668\u559d\u725b\u5976\u4e0d\u79d1\u5b66\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  \"\u5403\u4e86\u6d77\u9c9c\u540e\u662f\u4e0d\u80fd\u518d\u559d\u725b\u5976\u7684\uff0c\u56e0\u4e3a\u725b\u5976\u4e2d\u542b\u5f97\u6709\u7ef4\u751f\u7d20C\uff0c\u5982\u679c\u6d77\u9c9c\u559d\u725b\u5976\u4e00\u8d77\u670d\u7528\u4f1a\u5bf9\u4eba\u4f53\u9020\u6210\u4e00\u5b9a\u7684\u4f24\u5bb3\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  \"\u5403\u6d77\u9c9c\u662f\u4e0d\u80fd\u540c\u65f6\u559d\u725b\u5976\u5403\u6c34\u679c\uff0c\u8fd9\u4e2a\u81f3\u5c11\u95f4\u96946\u5c0f\u65f6\u4ee5\u4e0a\u624d\u53ef\u4ee5\u3002\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  \"\u5403\u6d77\u9c9c\u662f\u4e0d\u53ef\u4ee5\u5403\u67e0\u6aac\u7684\u56e0\u4e3a\u5176\u4e2d\u7684\u7ef4\u751f\u7d20C\u4f1a\u548c\u6d77\u9c9c\u4e2d\u7684\u77ff\u7269\u8d28\u5f62\u6210\u7837\"<br> &nbsp; &nbsp; &nbsp;  ]<br> &nbsp;  }<br>\u200b<br>result = pipeline_se(input=inputs)<br>print (result)<br>\u200b<br>\u200b<br># \u5f53\u8f93\u5165\u4ec5\u542b\u6709soure_sentence\u65f6\uff0c\u4f1a\u8f93\u51fasource_sentence\u4e2d\u6bcf\u4e2a\u53e5\u5b50\u7684\u5411\u91cf\u8868\u793a\u3002<br>inputs2 = {<br> &nbsp; &nbsp; &nbsp;  \"source_sentence\": [<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  \"\u4e0d\u53ef\u4ee5\uff0c\u65e9\u6668\u559d\u725b\u5976\u4e0d\u79d1\u5b66\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  \"\u5403\u4e86\u6d77\u9c9c\u540e\u662f\u4e0d\u80fd\u518d\u559d\u725b\u5976\u7684\uff0c\u56e0\u4e3a\u725b\u5976\u4e2d\u542b\u5f97\u6709\u7ef4\u751f\u7d20C\uff0c\u5982\u679c\u6d77\u9c9c\u559d\u725b\u5976\u4e00\u8d77\u670d\u7528\u4f1a\u5bf9\u4eba\u4f53\u9020\u6210\u4e00\u5b9a\u7684\u4f24\u5bb3\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  \"\u5403\u6d77\u9c9c\u662f\u4e0d\u80fd\u540c\u65f6\u559d\u725b\u5976\u5403\u6c34\u679c\uff0c\u8fd9\u4e2a\u81f3\u5c11\u95f4\u96946\u5c0f\u65f6\u4ee5\u4e0a\u624d\u53ef\u4ee5\u3002\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  \"\u5403\u6d77\u9c9c\u662f\u4e0d\u53ef\u4ee5\u5403\u67e0\u6aac\u7684\u56e0\u4e3a\u5176\u4e2d\u7684\u7ef4\u751f\u7d20C\u4f1a\u548c\u6d77\u9c9c\u4e2d\u7684\u77ff\u7269\u8d28\u5f62\u6210\u7837\"<br> &nbsp; &nbsp; &nbsp;  ]<br>}<br>result = pipeline_se(input=inputs2)<br>print (result)<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/fushuling-1309926051.cos.ap-shanghai.myqcloud.com\/2025\/07\/QQ20250719-231236-3.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/fushuling-1309926051.cos.ap-shanghai.myqcloud.com\/2025\/07\/QQ20250719-231236-3.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"QQ20250719-231236-3\"\/><\/div><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\u8f93\u51fa\u6bcf\u4e2a\u53e5\u5b50\u7684\u5411\u91cf\u8868\u793a\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u5176\u5b9e\u53ef\u4ee5\u60f3\u5230\u6700\u7b80\u5355\u7684\u8fd0\u7528\uff0c\u5c31\u662f\u7ed9\u5b9a\u4e00\u4e2a\u6e90\u53e5\u5b50\u548c\u4e00\u7ec4\u5019\u9009\u53e5\u5b50\uff0c\u7a0b\u5e8f\u4f1a\u81ea\u52a8\u627e\u51fa\u4e0e\u6e90\u53e5\u5b50\u610f\u601d\u6700\u76f8\u8fd1\u7684\u90a3\u4e2a\u5019\u9009\u53e5\u5b50\uff0c\u5e76\u628a\u5b83\u6253\u5370\u51fa\u6765\uff0c\u4e5f\u5c31\u662f\u5229\u7528\u76f8\u4f3c\u5ea6\u627e\u53e5\u5b50\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import numpy as np<br>from modelscope.pipelines import pipeline<br>from modelscope.utils.constant import Tasks<br>\u200b<br>print(\"\u6b63\u5728\u52a0\u8f7d\u6a21\u578b\uff0c\u8bf7\u7a0d\u5019...\")<br>model_id = \"iic\/nlp_gte_sentence-embedding_chinese-small\"<br>pipeline_se = pipeline(Tasks.sentence_embedding,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; model=model_id,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sequence_length=512,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; model_revision='master')<br>print(\"\u6a21\u578b\u52a0\u8f7d\u5b8c\u6bd5\uff01\\n\")<br>\u200b<br>source_sentence = \"\u4eca\u5929\u5fc3\u60c5\u5f88\u597d\uff0c\u60f3\u51fa\u53bb\u8d70\u8d70\"<br>\u200b<br>sentences_to_compare = [<br> &nbsp;  \"\u8fd9\u9053\u83dc\u7684\u505a\u6cd5\u662f\u4ec0\u4e48\uff1f\",<br> &nbsp;  \"\u5929\u6c14\u4e0d\u9519\uff0c\u9002\u5408\u6563\u6b65\u3002\",<br> &nbsp;  \"\u6df1\u5ea6\u5b66\u4e60\u6539\u53d8\u4e86\u4eba\u5de5\u667a\u80fd\u9886\u57df\u3002\",<br> &nbsp;  \"\u5468\u672b\u4f60\u6709\u4ec0\u4e48\u5b89\u6392\u5417\uff1f\"<br>]<br>\u200b<br>inputs = {<br> &nbsp;  \"source_sentence\": [source_sentence],<br> &nbsp;  \"sentences_to_compare\": sentences_to_compare<br>}<br>\u200b<br>result = pipeline_se(input=inputs)<br>scores = result['scores']<br>\u200b<br># \u4f7f\u7528numpy\u627e\u5230\u5206\u6570\u5217\u8868\u4e2d\u7684\u6700\u5927\u503c\u53ca\u5176\u7d22\u5f15<br>highest_score = np.max(scores)<br>best_match_index = np.argmax(scores)<br>\u200b<br># \u901a\u8fc7\u7d22\u5f15\u627e\u5230\u6700\u5339\u914d\u7684\u53e5\u5b50<br>most_similar_sentence = sentences_to_compare[best_match_index]<br>\u200b<br>print(f\"\u6e90\u53e5\u5b50: '{source_sentence}'\")<br>print(\"-\" * 30)<br>print(\"\u5019\u9009\u53e5\u5b50\u5217\u8868:\")<br>for i, sentence in enumerate(sentences_to_compare):<br> &nbsp;  print(f\"  - '{sentence}' (\u76f8\u4f3c\u5ea6: {scores[i]:.4f})\")<br>print(\"-\" * 30)<br>print(f\"\u5339\u914d\u7ed3\u679c: \u76f8\u4f3c\u5ea6\u6700\u9ad8\u7684\u53e5\u5b50\u662f '{most_similar_sentence}'\")<br>print(f\"\u76f8\u4f3c\u5ea6\u5206\u6570: {highest_score:.4f}\")<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/fushuling-1309926051.cos.ap-shanghai.myqcloud.com\/2025\/07\/QQ20250719-232031-4.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/fushuling-1309926051.cos.ap-shanghai.myqcloud.com\/2025\/07\/QQ20250719-232031-4.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\"\/><\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Task #3: \u77e5\u8bc6\u5e93\u6784\u5efa\u548c\u5411\u91cf\u7d22\u5f15<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u76ee\u6807<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b66\u4e60\u6784\u5efa\u5411\u91cf\u77e5\u8bc6\u5e93<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u9a8c\u5185\u5bb9<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u627e\u4e00\u7bc7\u4f60\u611f\u5174\u8da3\u7684\u6587\u7ae0<\/li>\n\n\n\n<li>\u5b66\u4e60Langchain\u4e2dTextSplitter\u7684\u6982\u5ff5\uff0c\u5c06\u6587\u7ae0\u5206\u6210chunks<\/li>\n\n\n\n<li>\u4f7f\u7528#2\u4e2d\u6784\u5efa\u7684Embedding\u6a21\u578b\uff0c\u5c06chunks\u8f6c\u6362\u4e3aembedding<\/li>\n\n\n\n<li>\u5b66\u4e60FAISS\u5411\u91cf\u6570\u636e\u5e93\u7684\u6982\u5ff5\u548c\u4f7f\u7528\u65b9\u6cd5\u57fa\u4e8e\u8fd9\u7bc7\u6587\u7ae0\u6784\u5efa\u5e76\u4fdd\u5b58\u4e00\u4e2aVectorStore\uff0c\u5e76\u80fd\u591f\u8bfb\u53d6<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u9a8c\u6d41\u7a0b<\/h3>\n\n\n\n<p>\u5f53\u6211\u4eec\u5904\u7406\u4e00\u7bc7\u957f\u6587\u7ae0\u65f6\uff08\u4f8b\u5982\u51e0\u5343\u4e0a\u4e07\u5b57\uff09\uff0c\u4f1a\u9047\u5230\u4e00\u4e2a\u5f88\u5b9e\u9645\u7684\u95ee\u9898\uff1a\u5927\u90e8\u5206\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\uff0c\u5305\u62ec\u6211\u4eec\u5728\u4efb\u52a1 #2 \u4e2d\u4f7f\u7528\u7684 Embedding \u6a21\u578b\uff0c\u90fd\u6709\u4e00\u4e2a<strong>\u8f93\u5165\u957f\u5ea6\u9650\u5236<\/strong>\uff08Context Window\uff09\u3002\u4f8b\u5982\uff0c<code>GTE-small<\/code> \u6a21\u578b\u7684\u6700\u5927\u957f\u5ea6\u662f512\u4e2a\u5b57\u7b26\u3002\u6211\u4eec\u4e0d\u53ef\u80fd\u628a\u6574\u7bc7\u6587\u7ae0\u4e00\u6b21\u6027\u4e22\u7ed9\u6a21\u578b\u6765\u751f\u6210\u4e00\u4e2a\u5355\u4e00\u7684\u5411\u91cf\uff0c\u8fd9\u6837\u505a\u4e0d\u4ec5\u4f1a\u8d85\u51fa\u9650\u5236\uff0c\u800c\u4e14\u751f\u6210\u7684\u5411\u91cf\u4e5f\u65e0\u6cd5\u5f88\u597d\u5730\u4ee3\u8868\u6587\u7ae0\u4e2d\u5177\u4f53\u3001\u7ec6\u8282\u7684\u8bed\u4e49\u3002<code>TextSplitter<\/code>\u7684\u4f5c\u7528\u5c31\u662f\u5c06\u4e00\u7bc7\u957f\u6587\u6863\uff0c\u6309\u7167\u9884\u8bbe\u7684\u89c4\u5219\uff0c\u5207\u5206\u6210\u591a\u4e2a\u66f4\u5c0f\u7684\u3001\u8bed\u4e49\u5b8c\u6574\u7684\u6587\u672c\u5757\uff08Chunks\uff09<\/p>\n\n\n\n<p>\u5728\u6211\u4eec\u5c06\u6587\u7ae0\u5207\u5206\u6210\u6570\u767e\u4e0a\u5343\u4e2a chunks\uff0c\u5e76\u628a\u5b83\u4eec\u90fd\u8f6c\u6362\u6210\u5411\u91cf\u4e4b\u540e\uff0c\u53c8\u6765\u4e86\u4e00\u4e2a\u65b0\u95ee\u9898\uff1a\u5f53\u7528\u6237\u63d0\u95ee\u65f6\uff0c\u6211\u4eec\u5982\u4f55\u5feb\u901f\u5730\u4ece\u8fd9\u6210\u767e\u4e0a\u5343\u4e2a\u5411\u91cf\u4e2d\uff0c\u627e\u5230\u4e0e\u95ee\u9898\u5411\u91cf\u6700\u76f8\u4f3c\u7684\u90a3\u51e0\u4e2a\uff1f\u6700\u6734\u7d20\u7684\u65b9\u6cd5\u662f\uff1a\u7528\u95ee\u9898\u5411\u91cf\u548c\u77e5\u8bc6\u5e93\u91cc\u7684\u6bcf\u4e00\u4e2a\u5411\u91cf\u9010\u4e00\u8ba1\u7b97\u76f8\u4f3c\u5ea6\uff0c\u7136\u540e\u6392\u5e8f\u3002\u5f53\u77e5\u8bc6\u5e93\u5f88\u5c0f\u65f6\u53ef\u884c\uff0c\u4f46\u5f53\u6709\u6210\u5343\u4e0a\u4e07\u4e2a\u5411\u91cf\u65f6\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u4f1a\u53d8\u5f97\u6781\u5176\u7f13\u6162\u3002<\/p>\n\n\n\n<p>FAISS (Facebook AI Similarity Search) \u5c31\u662f\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u800c\u751f\u7684\u3002\u5b83\u662f\u4e00\u4e2a\u4e13\u95e8\u4e3a\u6d77\u91cf\u5411\u91cf\u8fdb\u884c\u9ad8\u6548\u76f8\u4f3c\u5ea6\u641c\u7d22\u548c\u805a\u7c7b\u7684\u5f00\u6e90\u5e93\u3002FAISS \u5e76\u4e0d\u662f\u4e00\u4e2a\u4f20\u7edf\u610f\u4e49\u4e0a\u7684\u6570\u636e\u5e93\uff08\u5982MySQL\uff09\uff0c\u5b83\u66f4\u50cf\u4e00\u4e2a<strong>\u5411\u91cf\u7d22\u5f15<\/strong>\u3002\u5b83\u901a\u8fc7\u5185\u90e8\u7684\u7279\u6b8a\u7b97\u6cd5\uff08\u5982LSH\u3001HNSW\u7b49\uff09\u5bf9\u5411\u91cf\u8fdb\u884c\u9884\u5148\u7ec4\u7ec7\u548c\u5212\u5206\uff0c\u5efa\u7acb\u7d22\u5f15\u3002\u5f53\u8fdb\u884c\u641c\u7d22\u65f6\uff0c\u5b83\u4e0d\u9700\u8981\u8fdb\u884c\u201c\u66b4\u529b\u201d\u7684\u9010\u4e00\u6bd4\u8f83\uff0c\u800c\u662f\u80fd\u901a\u8fc7\u7d22\u5f15\u5feb\u901f\u5b9a\u4f4d\u5230\u6700\u53ef\u80fd\u76f8\u4f3c\u7684\u5411\u91cf\u533a\u57df\uff0c\u4ece\u800c\u5b9e\u73b0\u6beb\u79d2\u7ea7\u7684\u5feb\u901f\u68c0\u7d22\u3002\u5b83\u901f\u5ea6\u6781\u5feb\uff0c\u5e76\u4e14\u53ef\u4ee5\u5b8c\u5168\u5728\u672c\u5730\u8fd0\u884c\uff08<code>faiss-cpu<\/code> \u7248\u672c\uff09\uff0c\u975e\u5e38\u9002\u5408\u6784\u5efa\u4e2a\u4eba\u6216\u4e2d\u5c0f\u578b\u7684\u672c\u5730\u77e5\u8bc6\u5e93\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4eec\u9009\u62e9\u6293\u53d6\u6c38\u96cf\u5854\u83f2\u7684\u767e\u5ea6\u767e\u79d1\uff1a<a href=\"https:\/\/baike.baidu.com\/item\/%E6%B0%B8%E9%9B%8F%E5%A1%94%E8%8F%B2\/61675407\">https:\/\/baike.baidu.com\/item\/%E6%B0%B8%E9%9B%8F%E5%A1%94%E8%8F%B2\/61675407<\/a>\uff0c\u5efa\u7acb\u672c\u5730\u6570\u636e\u5e93<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import os<br>\u200b<br>from langchain_community.document_loaders import TextLoader <br>from langchain.text_splitter import RecursiveCharacterTextSplitter<br>from langchain_community.vectorstores import FAISS<br>from typing import List<br>from langchain.embeddings.base import Embeddings<br>from modelscope.pipelines import pipeline<br>from modelscope.utils.constant import Tasks<br>\u200b<br>\u200b<br>def load_document_from_local_file(file_path):<br> &nbsp;  \"\"\"\u4f7f\u7528TextLoader\u4ece\u672c\u5730\u7684.txt\u6587\u4ef6\u52a0\u8f7d\u6587\u6863\u5185\u5bb9\u3002\"\"\"<br> &nbsp;  print(f\"\u6b63\u5728\u4ece\u672c\u5730\u6587\u4ef6\u52a0\u8f7d\u6587\u6863: {file_path}\")<br> &nbsp; &nbsp;<br> &nbsp;  if not os.path.exists(file_path):<br> &nbsp; &nbsp; &nbsp;  print(f\"\u9519\u8bef: \u6587\u4ef6 '{file_path}' \u4e0d\u5b58\u5728\u3002\")<br> &nbsp; &nbsp; &nbsp;  return None<br> &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp;  # \u4f7f\u7528 TextLoader \u5e76\u6307\u5b9a utf-8 \u7f16\u7801<br> &nbsp;  loader = TextLoader(file_path, encoding='utf-8')<br> &nbsp;  try:<br> &nbsp; &nbsp; &nbsp;  docs = loader.load()<br> &nbsp; &nbsp; &nbsp;  print(\"\u6587\u6863\u52a0\u8f7d\u6210\u529f\uff01\")<br> &nbsp; &nbsp; &nbsp;  return docs<br> &nbsp;  except Exception as e:<br> &nbsp; &nbsp; &nbsp;  print(f\"\u52a0\u8f7d\u6587\u6863\u65f6\u51fa\u9519: {e}\")<br> &nbsp; &nbsp; &nbsp;  return None<br>\u200b<br># \u6587\u672c\u5207\u5206: \u5c06\u52a0\u8f7d\u7684\u6587\u6863\u5207\u5206\u4e3a\u5c0f\u5757 (Chunks) <br>def split_documents_into_chunks(docs):<br> &nbsp;  \"\"\"\u5c06\u52a0\u8f7d\u7684\u6587\u6863\u5217\u8868\u5207\u5206\u4e3a\u66f4\u5c0f\u7684\u6587\u672c\u5757\u3002\"\"\"<br> &nbsp;  if not docs:<br> &nbsp; &nbsp; &nbsp;  print(\"\u6ca1\u6709\u6587\u6863\u53ef\u4ee5\u5207\u5206\u3002\")<br> &nbsp; &nbsp; &nbsp;  return []<br> &nbsp;  print(\"\\n\u6b63\u5728\u5c06\u6587\u6863\u5207\u5206\u4e3a\u6587\u672c\u5757 (Chunks)...\")<br> &nbsp;  text_splitter = RecursiveCharacterTextSplitter(<br> &nbsp; &nbsp; &nbsp;  chunk_size=500,  # \u6bcf\u4e2achunk\u7684\u6700\u5927\u957f\u5ea6\uff08\u5b57\u7b26\u6570\uff09<br> &nbsp; &nbsp; &nbsp;  chunk_overlap=50, # chunk\u4e4b\u95f4\u7684\u91cd\u53e0\u957f\u5ea6<br> &nbsp; &nbsp; &nbsp;  length_function=len,<br> &nbsp; &nbsp; &nbsp;  is_separator_regex=False,<br> &nbsp;  )<br> &nbsp;  chunks = text_splitter.split_documents(docs)<br> &nbsp;  print(f\"\u5207\u5206\u5b8c\u6210\uff0c\u5171\u5f97\u5230 {len(chunks)} \u4e2a\u6587\u672c\u5757\u3002\")<br> &nbsp;  return chunks<br>\u200b<br># \u521d\u59cb\u5316Embedding\u6a21\u578b<br>def initialize_embedding_model():<br> &nbsp;  \"\"\"<br> &nbsp;  \u521d\u59cb\u5316\u7528\u4e8e\u5c06\u6587\u672c\u8f6c\u6362\u4e3a\u5411\u91cf\u7684\u5d4c\u5165\u6a21\u578b\u3002<br> &nbsp;  \u4f7f\u7528ModelScope Pipeline\u5e76\u5305\u88c5\u6210Langchain\u517c\u5bb9\u7684Embeddings\u7c7b\u3002<br> &nbsp;  \"\"\"<br> &nbsp;  print(\"\\n\u6b63\u5728\u521d\u59cb\u5316ModelScope Embedding\u6a21\u578b (GTE-small)...\")<br>\u200b<br> &nbsp;  class ModelScopeEmbeddings(Embeddings):<br> &nbsp; &nbsp; &nbsp;  def __init__(self, model_id):<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  self.pipeline_se = pipeline(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  Tasks.sentence_embedding,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  model=model_id,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  sequence_length=512<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  )<br>\u200b<br> &nbsp; &nbsp; &nbsp;  def embed_documents(self, texts: List[str]) -&gt; List[List[float]]:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  inputs = {\"source_sentence\": texts}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  result = self.pipeline_se(input=inputs)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  return result['text_embedding']<br>\u200b<br> &nbsp; &nbsp; &nbsp;  def embed_query(self, text: str) -&gt; List[float]:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  inputs = {\"source_sentence\": [text]}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  result = self.pipeline_se(input=inputs)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  return result['text_embedding'][0]<br>\u200b<br> &nbsp;  model_id = \"iic\/nlp_gte_sentence-embedding_chinese-small\"<br> &nbsp;  embedding_model = ModelScopeEmbeddings(model_id=model_id)<br> &nbsp; &nbsp;<br> &nbsp;  print(\"ModelScope Embedding\u6a21\u578b\u521d\u59cb\u5316\u6210\u529f\uff01\")<br> &nbsp;  return embedding_model<br>\u200b<br># \u6784\u5efa\u3001\u4fdd\u5b58\u4e0e\u52a0\u8f7dFAISS\u5411\u91cf\u6570\u636e\u5e93<br>def create_and_load_vectorstore(chunks, embedding_model, save_path):<br> &nbsp;  \"\"\"\u6784\u5efa\u3001\u4fdd\u5b58\u5e76\u4ece\u672c\u5730\u91cd\u65b0\u52a0\u8f7dFAISS\u5411\u91cf\u6570\u636e\u5e93\u3002\"\"\"<br> &nbsp;  if not chunks:<br> &nbsp; &nbsp; &nbsp;  print(\"\u6ca1\u6709\u6587\u672c\u5757\u53ef\u4ee5\u521b\u5efa\u7d22\u5f15\u3002\")<br> &nbsp; &nbsp; &nbsp;  return None<br> &nbsp;  print(\"\\n\u6b63\u5728\u5c06\u6587\u672c\u5757\u8f6c\u6362\u4e3a\u5411\u91cf\u5e76\u6784\u5efaFAISS\u7d22\u5f15...\")<br> &nbsp;  vector_store = FAISS.from_documents(chunks, embedding_model)<br> &nbsp;  vector_store.save_local(save_path)<br> &nbsp;  print(f\"FAISS\u7d22\u5f15\u5df2\u6210\u529f\u6784\u5efa\u5e76\u4fdd\u5b58\u5230: '{save_path}'\")<br> &nbsp;  print(f\"\\n\u6b63\u5728\u4ece '{save_path}' \u52a0\u8f7dFAISS\u7d22\u5f15...\")<br> &nbsp;  loaded_vector_store = FAISS.load_local(<br> &nbsp; &nbsp; &nbsp;  save_path, <br> &nbsp; &nbsp; &nbsp;  embedding_model,<br> &nbsp; &nbsp; &nbsp;  allow_dangerous_deserialization=True <br> &nbsp;  )<br> &nbsp;  print(\"FAISS\u7d22\u5f15\u52a0\u8f7d\u6210\u529f\uff01\")<br> &nbsp;  return loaded_vector_store<br>\u200b<br># \u6267\u884c\u8bed\u4e49\u641c\u7d22<br>def search_in_vectorstore(vector_store, query):<br> &nbsp;  \"\"\"\u5728\u5411\u91cf\u6570\u636e\u5e93\u4e2d\u6267\u884c\u8bed\u4e49\u641c\u7d22\u3002\"\"\"<br> &nbsp;  print(\"\\n--------------------------------------------------\")<br> &nbsp;  print(f\"\u5f00\u59cb\u6267\u884c\u8bed\u4e49\u641c\u7d22\uff0c\u67e5\u8be2\u5185\u5bb9: '{query}'\")<br> &nbsp;  results = vector_store.similarity_search_with_score(query, k=3)<br> &nbsp;  if not results:<br> &nbsp; &nbsp; &nbsp;  print(\"\u672a\u627e\u5230\u76f8\u5173\u7ed3\u679c\u3002\")<br> &nbsp; &nbsp; &nbsp;  return<br> &nbsp;  print(\"\\n\u641c\u7d22\u7ed3\u679c\u5982\u4e0b:\")<br> &nbsp;  for i, (doc, score) in enumerate(results):<br> &nbsp; &nbsp; &nbsp;  print(f\"\\n--- [\u7ed3\u679c {i+1} | \u76f8\u4f3c\u5ea6\u5f97\u5206: {score:.4f}] ---\")<br> &nbsp; &nbsp; &nbsp;  print(doc.page_content)<br> &nbsp;  print(\"\\n--------------------------------------------------\")<br>\u200b<br>if __name__ == \"__main__\":<br>\u200b<br> &nbsp;  # \u6307\u5b9a\u8981\u8bfb\u53d6\u7684\u672c\u5730 1.txt \u6587\u4ef6<br> &nbsp;  LOCAL_FILE_PATH = \"1.txt\"<br> &nbsp;  FAISS_INDEX_PATH = \"faiss_yongchu_tafei_index\"<br> &nbsp; &nbsp;<br> &nbsp;  # \u8c03\u7528\u4fee\u6539\u540e\u7684\u51fd\u6570\u4ece\u672c\u5730\u6587\u4ef6\u52a0\u8f7d<br> &nbsp;  documents = load_document_from_local_file(LOCAL_FILE_PATH)<br> &nbsp; &nbsp;<br> &nbsp;  if documents:<br> &nbsp; &nbsp; &nbsp;  text_chunks = split_documents_into_chunks(documents)<br> &nbsp; &nbsp; &nbsp;  embedding_model = initialize_embedding_model()<br> &nbsp; &nbsp; &nbsp;  db = create_and_load_vectorstore(text_chunks, embedding_model, FAISS_INDEX_PATH)<br> &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp;  if db:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  test_query_1 = \"\u6c38\u96cf\u5854\u83f2\u662f\u8c01\uff1f\"<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  search_in_vectorstore(db, test_query_1)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  test_query_2 = \"\u6c38\u96cf\u5854\u83f2\u7684\u5e74\u9f84\uff1f\"<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  search_in_vectorstore(db, test_query_2)<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/fushuling-1309926051.cos.ap-shanghai.myqcloud.com\/2025\/07\/QQ20250720-000702-5.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/fushuling-1309926051.cos.ap-shanghai.myqcloud.com\/2025\/07\/QQ20250720-000702-5.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\"\/><\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Task #4: Retrieval-Augmented Generation\uff08\u68c0\u7d22\u589e\u5f3a\u751f\u6210\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u76ee\u6807<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7406\u89e3RAG\u7684\u539f\u7406<\/li>\n\n\n\n<li>\u624b\u6413RAG<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u9a8c\u5185\u5bb9<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u81ea\u884c\u641c\u96c6\u8d44\u6599\uff0c\u5b66\u4e60RAG\u7684\u673a\u5236<\/li>\n\n\n\n<li>\u57fa\u4e8e#0\u7684\u5b9e\u73b0\u7684\u5927\u6a21\u578b\u63a5\u53e3\uff0c#1\u7684\u4e0a\u4e0b\u6587\u5b66\u4e60\u548c#3\u7684Vectorstore\uff0c\u505a\u4e00\u4e2a\u6839\u636e\u6587\u7ae0\u5185\u5bb9\u7684QA\u5e94\u7528\uff08\u6ce8\u610f\uff1a\u7981\u6b62\u4f7f\u7528Langchain\uff0c\u8bf7\u81ea\u884c\u5b9e\u73b0RAG\u7684\u6d41\u7a0b\uff09<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u9a8c\u6d41\u7a0b<\/h3>\n\n\n\n<p>\u6ca1\u592a\u61c2\u7981\u6b62\u4f7f\u7528Langchain\u4ec0\u4e48\u610f\u601d\uff0c\u5e94\u8be5\u662f\u53ea\u8981\u81ea\u5df1\u5b9e\u73b0RAG\u8fc7\u7a0b\u5c31\u884c\u4e86\u5427\uff0c\u6211\u8fd9\u91cc\u662f\u4f7f\u7528\u4e86Task 3\u7684\u6570\u636e\u5e93\uff0c\u7136\u540e\u81ea\u5df1\u5b9e\u73b0\u4e86\u5411\u91cf\u68c0\u7d22\u548c\u4e0a\u4e0b\u6587\u6dfb\u52a0\uff0c\u8ba9llm\u5728\u6709\u4e0a\u4e0b\u6587\u7684\u57fa\u7840\u4e0a\u56de\u7b54\u95ee\u9898\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import os<br>from typing import List<br>\u200b<br>from langchain_community.vectorstores import FAISS<br>from langchain.embeddings.base import Embeddings<br>from modelscope.pipelines import pipeline<br>from modelscope.utils.constant import Tasks<br>\u200b<br>from langchain_openai import ChatOpenAI<br>from langchain_core.messages import HumanMessage<br>\u200b<br>\u200b<br># \u68c0\u7d22\u5668\u52a0\u8f7d\u5de5\u5177<br>\u200b<br>def initialize_embedding_model():<br> \u00a0  \"\"\"\u521d\u59cb\u5316\u7528\u4e8e\u5c06\u6587\u672c\u8f6c\u6362\u4e3a\u5411\u91cf\u7684\u5d4c\u5165\u6a21\u578b\u3002\"\"\"<br> \u00a0  print(\"\u6b63\u5728\u521d\u59cb\u5316ModelScope Embedding\u6a21\u578b (GTE-small)...\")<br> \u00a0  class ModelScopeEmbeddings(Embeddings):<br> \u00a0 \u00a0 \u00a0  def __init__(self, model_id):<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  self.pipeline_se = pipeline(Tasks.sentence_embedding, model=model_id, sequence_length=512)<br> \u00a0 \u00a0 \u00a0  def embed_documents(self, texts: List[str]) -> List[List[float]]:<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  inputs = {\"source_sentence\": texts}; return self.pipeline_se(input=inputs)['text_embedding']<br> \u00a0 \u00a0 \u00a0  def embed_query(self, text: str) -> List[float]:<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  inputs = {\"source_sentence\": [text]}; return self.pipeline_se(input=inputs)['text_embedding'][0]<br> \u00a0  embedding_model = ModelScopeEmbeddings(model_id=\"iic\/nlp_gte_sentence-embedding_chinese-small\")<br> \u00a0  print(\"Embedding\u6a21\u578b\u521d\u59cb\u5316\u6210\u529f\uff01\")<br> \u00a0  return embedding_model<br>\u200b<br>def load_vectorstore(load_path: str, embedding_model: Embeddings) -> FAISS:<br> \u00a0  \"\"\"\u4ece\u672c\u5730\u78c1\u76d8\u52a0\u8f7dFAISS\u5411\u91cf\u6570\u636e\u5e93\u3002\"\"\"<br> \u00a0  if not os.path.exists(load_path):<br> \u00a0 \u00a0 \u00a0  print(f\"\u9519\u8bef: \u7d22\u5f15\u8def\u5f84 '{load_path}' \u4e0d\u5b58\u5728\u3002\")<br> \u00a0 \u00a0 \u00a0  return None<br> \u00a0  print(f\"\u6b63\u5728\u4ece '{load_path}' \u52a0\u8f7dFAISS\u7d22\u5f15...\")<br> \u00a0  try:<br> \u00a0 \u00a0 \u00a0  vector_store = FAISS.load_local(load_path, embedding_model, allow_dangerous_deserialization=True)<br> \u00a0 \u00a0 \u00a0  print(\"FAISS\u7d22\u5f15\u52a0\u8f7d\u6210\u529f\uff01\")<br> \u00a0 \u00a0 \u00a0  return vector_store<br> \u00a0  except Exception as e:<br> \u00a0 \u00a0 \u00a0  print(f\"\u52a0\u8f7dFAISS\u7d22\u5f15\u65f6\u51fa\u9519: {e}\")<br> \u00a0 \u00a0 \u00a0  return None<br>\u200b<br># \u5927\u8bed\u8a00\u6a21\u578b\u751f\u6210\u5668\u63a5\u53e3<br>\u200b<br>def get_llm_response(prompt: str) -> str:<br> \u00a0  \"\"\"ni<br> \u00a0  \u8c03\u7528\u6307\u5b9a\u7684ChatOpenAI\u63a5\u53e3\u5e76\u8fd4\u56de\u751f\u6210\u7684\u6587\u672c\u3002<br> \u00a0  \"\"\"<br> \u00a0  API_KEY = \"sk-xxxx\"<br> \u00a0  BASE_URL = \"https:\/\/api.gptgod.online\/v1\/\"<br> \u00a0  MODEL_NAME = \"gpt-3.5-turbo\"<br>\u200b<br> \u00a0  print(f\"\u6b63\u5728\u8c03\u7528\u5927\u8bed\u8a00\u6a21\u578b: {MODEL_NAME} at {BASE_URL}...\")<br>\u200b<br> \u00a0  try:<br> \u00a0 \u00a0 \u00a0  # \u521d\u59cb\u5316ChatOpenAI\u5ba2\u6237\u7aef<br> \u00a0 \u00a0 \u00a0  llm = ChatOpenAI(<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  api_key=API_KEY,<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  base_url=BASE_URL,<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  model=MODEL_NAME,<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  temperature=0.1, <br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  request_timeout=60,<br> \u00a0 \u00a0 \u00a0  )<br>\u200b<br> \u00a0 \u00a0 \u00a0  # Langchain\u7684LLM\u901a\u5e38\u63a5\u6536\u4e00\u4e2a\u6d88\u606f\u5217\u8868<br> \u00a0 \u00a0 \u00a0  messages = [HumanMessage(content=prompt)]<br> \u00a0 \u00a0 \u00a0 \u00a0<br> \u00a0 \u00a0 \u00a0  # \u53d1\u8d77\u8c03\u7528<br> \u00a0 \u00a0 \u00a0  response = llm.invoke(messages)<br> \u00a0 \u00a0 \u00a0 \u00a0<br> \u00a0 \u00a0 \u00a0  print(\"\u5927\u6a21\u578b\u8c03\u7528\u6210\u529f\uff01\")<br> \u00a0 \u00a0 \u00a0  # \u4ece\u8fd4\u56de\u7684AIMessage\u5bf9\u8c61\u4e2d\u63d0\u53d6\u6587\u672c\u5185\u5bb9<br> \u00a0 \u00a0 \u00a0  return response.content<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<br> \u00a0  except Exception as e:<br> \u00a0 \u00a0 \u00a0  error_message = f\"\u8c03\u7528LLM\u65f6\u53d1\u751f\u9519\u8bef: {e}\"<br> \u00a0 \u00a0 \u00a0  print(error_message)<br> \u00a0 \u00a0 \u00a0  return error_message<br>\u200b<br># \u624b\u52a8\u5b9e\u73b0\u7684RAG\u6838\u5fc3\u6d41\u7a0b<br>\u200b<br>def answer_question_with_rag(query: str, vector_store: FAISS):<br> \u00a0  \"\"\"<br> \u00a0  \u624b\u52a8\u5b9e\u73b0\u7684RAG\u6d41\u7a0b\uff0c\u7528\u4e8e\u56de\u7b54\u7528\u6237\u95ee\u9898\u3002<br> \u00a0  \"\"\"<br> \u00a0  print(f\"\\n================== \u5f00\u59cbRAG\u6d41\u7a0b ==================\")<br> \u00a0  print(f\"\u7528\u6237\u95ee\u9898: {query}\")<br>\u200b<br> \u00a0  # \u68c0\u7d22 (Retrieve)<br> \u00a0  print(\"\\n[\u6b65\u9aa4 1\/3] \u68c0\u7d22\u76f8\u5173\u6587\u6863...\")<br> \u00a0  retrieved_docs = vector_store.similarity_search(query, k=3)<br> \u00a0  if not retrieved_docs:<br> \u00a0 \u00a0 \u00a0  return \"\u62b1\u6b49\uff0c\u6211\u65e0\u6cd5\u5728\u00b7\u7684\u6587\u6863\u4e2d\u627e\u5230\u76f8\u5173\u4fe1\u606f\u6765\u56de\u7b54\u8fd9\u4e2a\u95ee\u9898\u3002\"<br> \u00a0  context = \"\\n\\n\".join([doc.page_content for doc in retrieved_docs])<br> \u00a0  print(\"\u68c0\u7d22\u5230\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f:\")<br> \u00a0  print(\"--------------------------------------------------\")<br> \u00a0  print(context)<br> \u00a0  print(\"--------------------------------------------------\")<br>\u200b<br> \u00a0  # \u589e\u5f3a (Augment) - \u6784\u5efaPrompt<br> \u00a0  print(\"\\n[\u6b65\u9aa4 2\/3] \u6784\u5efa\u589e\u5f3a\u578bPrompt...\")<br> \u00a0  prompt_template = f\"\"\"<br>\u8bf7\u4e25\u683c\u6839\u636e\u4ee5\u4e0b\u63d0\u4f9b\u7684\u3010\u4e0a\u4e0b\u6587\u4fe1\u606f\u3011\u6765\u56de\u7b54\u3010\u95ee\u9898\u3011\u3002<br>\u5982\u679c\u3010\u4e0a\u4e0b\u6587\u4fe1\u606f\u3011\u4e2d\u6ca1\u6709\u8db3\u591f\u7684\u5185\u5bb9\u6765\u56de\u7b54\u95ee\u9898\uff0c\u8bf7\u76f4\u63a5\u8bf4\uff1a\u201c\u6839\u636e\u63d0\u4f9b\u7684\u8d44\u6599\uff0c\u6211\u65e0\u6cd5\u56de\u7b54\u8fd9\u4e2a\u95ee\u9898\u3002\u201d<br>\u5728\u4f60\u7684\u56de\u7b54\u4e2d\uff0c\u4e0d\u5141\u8bb8\u7f16\u9020\u3001\u5047\u8bbe\u6216\u4f7f\u7528\u3010\u4e0a\u4e0b\u6587\u4fe1\u606f\u3011\u4e4b\u5916\u7684\u4efb\u4f55\u77e5\u8bc6\u3002<br>\u200b<br>\u3010\u4e0a\u4e0b\u6587\u4fe1\u606f\u3011:<br>{context}<br>\u200b<br>\u3010\u95ee\u9898\u3011:<br>{query}<br>\u200b<br>\u3010\u56de\u7b54\u3011:<br>\"\"\"<br> \u00a0  print(\"Prompt\u6784\u5efa\u5b8c\u6210\u3002\")<br>\u200b<br> \u00a0  # \u751f\u6210 (Generate) - \u8c03\u7528\u5927\u6a21\u578b<br> \u00a0  print(\"\\n[\u6b65\u9aa4 3\/3] \u8c03\u7528\u5927\u6a21\u578b\u751f\u6210\u6700\u7ec8\u7b54\u6848...\")<br>\u200b<br> \u00a0  final_answer = get_llm_response(prompt_template)<br> \u00a0 \u00a0<br> \u00a0  print(\"\\n================== RAG\u6d41\u7a0b\u7ed3\u675f ==================\")<br> \u00a0  return final_answer<br>\u200b<br>\u200b<br>\u200b<br>if __name__ == \"__main__\":<br> \u00a0 \u00a0<br> \u00a0  # \u5b9a\u4e49\u77e5\u8bc6\u5e93\u7d22\u5f15\u7684\u8def\u5f84<br> \u00a0  FAISS_INDEX_PATH = \"faiss_yongchu_tafei_index\" <br>\u200b<br> \u00a0  embedding_model = initialize_embedding_model()<br> \u00a0  db = load_vectorstore(FAISS_INDEX_PATH, embedding_model)<br>\u200b<br> \u00a0  if db:<br> \u00a0 \u00a0 \u00a0  test_query_1 = \"\u5854\u83f2\u7684\u7c89\u4e1d\u56e2\u4f53\u53eb\u4ec0\u4e48\u540d\u5b57\uff1f\"<br> \u00a0 \u00a0 \u00a0  answer_1 = answer_question_with_rag(test_query_1, db)<br> \u00a0 \u00a0 \u00a0  print(f\"\\n>>>>>> \u6700\u7ec8\u7b54\u6848 (\u95ee\u98981: {test_query_1}):\\n{answer_1}\\n\")<br>\u200b<br> \u00a0 \u00a0 \u00a0  test_query_2 = \"\u6c38\u96cf\u5854\u83f2\u7684\u76f4\u64ad\u5185\u5bb9\u4e3b\u8981\u6709\u54ea\u4e9b\uff1f\"<br> \u00a0 \u00a0 \u00a0  answer_2 = answer_question_with_rag(test_query_2, db)<br> \u00a0 \u00a0 \u00a0  print(f\"\\n>>>>>> \u6700\u7ec8\u7b54\u6848 (\u95ee\u98982: {test_query_2}):\\n{answer_2}\\n\")<br> \u00a0 \u00a0 \u00a0 \u00a0<br> \u00a0 \u00a0 \u00a0  test_query_3 = \"\u6c38\u96cf\u5854\u83f2\u6700\u559c\u6b22\u4ec0\u4e48\u989c\u8272\uff1f\"<br> \u00a0 \u00a0 \u00a0  answer_3 = answer_question_with_rag(test_query_3, db)<br> \u00a0 \u00a0 \u00a0  print(f\"\\n>>>>>> \u6700\u7d42\u7b54\u6848 (\u95ee\u98983: {test_query_3}):\\n{answer_3}\\n\")<br> \u00a0  else:<br> \u00a0 \u00a0 \u00a0  print(\"\\n\u65e0\u6cd5\u5f00\u59cb\u95ee\u7b54\uff0c\u56e0\u4e3a\u5411\u91cf\u6570\u636e\u5e93\u52a0\u8f7d\u5931\u8d25\u3002\")<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/fushuling-1309926051.cos.ap-shanghai.myqcloud.com\/2025\/07\/QQ20250720-002207-6.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/fushuling-1309926051.cos.ap-shanghai.myqcloud.com\/2025\/07\/QQ20250720-002207-6.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\"\/><\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Task #5: \u5927\u8bed\u8a00\u6a21\u578b\u5de5\u5177\u589e\u5f3a<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u76ee\u6807<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7406\u89e3\u5927\u6a21\u578b\u5de5\u5177\u8c03\u7528\u7684\u539f\u7406<\/li>\n\n\n\n<li>\u5b9e\u73b0\u5927\u6a21\u578b\u5de5\u5177\u8c03\u7528<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u9a8c\u5185\u5bb9<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7528Python\u5b9e\u73b0\u4e00\u4e2a\u8ba1\u7b97\u5668\u51fd\u6570\uff08\u652f\u6301\u52a0\u51cf\u4e58\u9664\uff09<\/li>\n\n\n\n<li>\u57fa\u4e8ePython Flaskapi\u548cUvicorn\u5c06\u4e0a\u9762\u8fd9\u4e2a\u8ba1\u7b97\u5668\u5305\u88c5\u6210Local RESTFUL API\uff08\u6bd4\u5982\u7aef\u53e38641\uff09<\/li>\n\n\n\n<li>\u8bbe\u8ba1\u76f8\u5e94\u7684\u5de5\u5177\u63cf\u8ff0\uff0c\u4fbf\u4e8e\u5927\u6a21\u578b\u7406\u89e3\u5e76\u8c03\u7528\u5de5\u5177<\/li>\n\n\n\n<li>\u5b9e\u73b0\u5de5\u5177\u8c03\u7528\u6d41\u7a0b\uff0c\u80fd\u591f\u6ee1\u8db3\u7528\u6237\u7684\u590d\u6742\u8ba1\u7b97\u8bf7\u6c42\uff08\u4f8b\u5982 \u6211\u60f3\u77e5\u9053129032910921*188231\u201d\uff09<\/li>\n\n\n\n<li>\u8bbe\u8ba1\u4e00\u7ec4\u7528\u4f8b\uff0c\u5bf9\u6bd4\u5de5\u5177\u589e\u5f3a\u524d\u540e\u5927\u6a21\u578b\u7684\u80fd\u529b\u5dee\u5f02<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u9a8c\u6d41\u7a0b<\/h3>\n\n\n\n<p>\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u8ba1\u7b97\u5668\u653e\u57288641\u7aef\u53e3\uff0c\u4f5c\u4e3a\u5de5\u5177\u4ea4\u7ed9llm\u8c03\u7528\uff0c\u8fd9\u91cc\u6211\u4eec\u7684\u95ee\u9898\u5c31\u662f 129032910921 * 188231 \uff0c\u5bf9\u6bd4\u4f7f\u7528\u5de5\u5177\u7684\u7ed3\u679c\u548c\u4e0d\u4f7f\u7528\u5de5\u5177\u8ba9llm\u76f4\u63a5\u56de\u7b54\u7684\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import requests<br>import threading<br>import time<br>import logging<br>from langchain_openai import ChatOpenAI<br>from langchain_core.messages import HumanMessage, ToolMessage<br>from flask import Flask, request, jsonify<br>import uvicorn<br>from asgiref.wsgi import WsgiToAsgi<br>\u200b<br>API_HOST = \"127.0.0.1\"<br>API_PORT = 8641<br>CALCULATOR_API_URL = f\"http:\/\/{API_HOST}:{API_PORT}\/calculate\"<br>\u200b<br>app = Flask(__name__)<br>log = logging.getLogger('werkzeug')<br>log.setLevel(logging.ERROR)<br>\u200b<br>asgi_app = WsgiToAsgi(app)<br>\u200b<br>@app.route('\/calculate', methods=['POST'])<br>def handle_calculate():<br> \u00a0  data = request.json<br> \u00a0  if not data or 'expression' not in data: return jsonify({\"error\": \"Missing 'expression'\"}), 400<br> \u00a0  expression = data['expression']<br> \u00a0  print(f\"\\n[API Server Log] Received calculation request for: '{expression}'\")<br> \u00a0  try:<br> \u00a0 \u00a0 \u00a0  result = eval(expression, {\"__builtins__\": None}, {})<br> \u00a0 \u00a0 \u00a0  response_data = {\"result\": str(result)}<br> \u00a0 \u00a0 \u00a0  print(f\"[API Server Log] Returning result: {result}\")<br> \u00a0  except Exception as e:<br> \u00a0 \u00a0 \u00a0  response_data = {\"result\": f\"Error: {str(e)}\"}<br> \u00a0 \u00a0 \u00a0  print(f\"[API Server Log] Calculation error: {e}\")<br> \u00a0  return jsonify(response_data)<br>\u200b<br>def run_api_server():<br> \u00a0  print(f\"--- \u542f\u52a8 API \u670d\u52a1\u5668\u4e8e http:\/\/{API_HOST}:{API_PORT} (\u540e\u53f0\u7ebf\u7a0b) ---\")<br> \u00a0  uvicorn.run(<br> \u00a0 \u00a0 \u00a0  asgi_app,<br> \u00a0 \u00a0 \u00a0  host=API_HOST,<br> \u00a0 \u00a0 \u00a0  port=API_PORT,<br> \u00a0 \u00a0 \u00a0  log_level=\"warning\",<br> \u00a0 \u00a0 \u00a0  lifespan=\"off\",<br> \u00a0 \u00a0 \u00a0  loop=\"asyncio\",<br> \u00a0 \u00a0 \u00a0  interface=\"asgi3\",<br> \u00a0 \u00a0 \u00a0  timeout_keep_alive=5<br> \u00a0  )<br>\u200b<br>API_KEY = \"sk-xxxx\"<br>BASE_URL = \"https:\/\/o3.fan\/v1\"<br>MODEL_NAME = \"deepseek-ai\/DeepSeek-V3\"<br>\u200b<br>try:<br> \u00a0  print(f\"\u6b63\u5728\u521d\u59cb\u5316\u5ba2\u6237\u7aef: {MODEL_NAME} at {BASE_URL}...\")<br> \u00a0  llm = ChatOpenAI(<br> \u00a0 \u00a0 \u00a0  api_key=API_KEY,<br> \u00a0 \u00a0 \u00a0  base_url=BASE_URL,<br> \u00a0 \u00a0 \u00a0  model=MODEL_NAME,<br> \u00a0 \u00a0 \u00a0  temperature=0.1, <br> \u00a0 \u00a0 \u00a0  request_timeout=60,<br> \u00a0  )<br> \u00a0  print(\"\u5ba2\u6237\u7aef\u521d\u59cb\u5316\u6210\u529f\u3002\")<br>except Exception as e:<br> \u00a0  print(f\"\u65e0\u6cd5\u521d\u59cb\u5316 ChatOpenAI: {e}\")<br> \u00a0  llm = None<br>\u200b<br>tools = [<br> \u00a0  {<br> \u00a0 \u00a0 \u00a0  \"type\": \"function\",<br> \u00a0 \u00a0 \u00a0  \"function\": {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  \"name\": \"calculator\",<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  \"description\": \"\u4e00\u4e2a\u53ef\u4ee5\u6267\u884c\u52a0\u3001\u51cf\u3001\u4e58\u3001\u9664\u7b49\u57fa\u672c\u6570\u5b66\u8fd0\u7b97\u7684\u8ba1\u7b97\u5668\u3002\",<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  \"parameters\": {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  \"type\": \"object\", \"properties\": {\"expression\": {\"type\": \"string\", \"description\": \"\u9700\u8981\u8ba1\u7b97\u7684\u6570\u5b66\u8868\u8fbe\u5f0f\u3002\"}},<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  \"required\": [\"expression\"]<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  }<br> \u00a0 \u00a0 \u00a0  }<br> \u00a0  }<br>]<br>\u200b<br>llm_with_tools_auto = llm.bind_tools(tools) if llm else None<br>llm_with_tools_forced = llm.bind_tools(<br> \u00a0  tools,<br> \u00a0  tool_choice={\"type\": \"function\", \"function\": {\"name\": \"calculator\"}}<br>) if llm else None<br>\u200b<br>def run_conversation(user_prompt: str, force_tool: bool = False):<br> \u00a0  if not llm: <br> \u00a0 \u00a0 \u00a0  print(\"LLM \u5ba2\u6237\u7aef\u672a\u6210\u529f\u521d\u59cb\u5316\uff0c\u65e0\u6cd5\u6267\u884c\u5bf9\u8bdd\u3002\")<br> \u00a0 \u00a0 \u00a0  return<br>\u200b<br> \u00a0  print(\"=\"*50)<br> \u00a0  print(f\"\ud83d\udc64 \u7528\u6237: {user_prompt}\")<br> \u00a0 \u00a0<br> \u00a0  if force_tool:<br> \u00a0 \u00a0 \u00a0  print(\"\u26a1\ufe0f [\u6a21\u5f0f]: \u5f3a\u5236\u8c03\u7528 'calculator' \u5de5\u5177\")<br> \u00a0 \u00a0 \u00a0  active_llm = llm_with_tools_forced<br> \u00a0 \u00a0 \u00a0  messages = [HumanMessage(content=user_prompt)]<br>\u200b<br> \u00a0 \u00a0 \u00a0  try:<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  response_message = active_llm.invoke(messages)<br> \u00a0 \u00a0 \u00a0  except Exception as e:<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  print(f\"\\n[\u9519\u8bef] \u8c03\u7528\u5927\u6a21\u578b\u65f6\u51fa\u9519: {e}\")<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  return<br> \u00a0 \u00a0 \u00a0 \u00a0<br> \u00a0 \u00a0 \u00a0  if response_message.tool_calls:<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  print(\"\u2705 LLM \u51b3\u5b9a\u8c03\u7528\u5de5\u5177...\")<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  messages.append(response_message)<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  for tool_call in response_message.tool_calls:<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  function_name = tool_call[\"name\"]<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  function_args = tool_call[\"args\"]<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  print(f\"\ud83d\udcde \u51c6\u5907\u8c03\u7528 API, \u51fd\u6570: {function_name}, \u53c2\u6570: {function_args}\")<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  expression = function_args.get(\"expression\")<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  try:<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  api_response = requests.post(CALCULATOR_API_URL, json={\"expression\": expression})<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  api_response.raise_for_status()<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  function_response = api_response.json().get('result')<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  except Exception as e:<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  function_response = f\"Error: {e}\"<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  print(f\"\u2699\ufe0f \u5de5\u5177\u8fd4\u56de\u7ed3\u679c: {function_response}\")<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  messages.append(ToolMessage(content=function_response, tool_call_id=tool_call[\"id\"]))<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  final_response = active_llm.invoke(messages)<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  final_answer = final_response.content<br> \u00a0 \u00a0 \u00a0  else:<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  print(\"\u2705 LLM \u672a\u8c03\u7528\u5de5\u5177\uff0c\u76f4\u63a5\u56de\u7b54\u3002\")<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  final_answer = response_message.content<br>\u200b<br> \u00a0  else:<br> \u00a0 \u00a0 \u00a0  print(\"\ud83d\udeab [\u6a21\u5f0f]: \u4e0d\u5141\u8bb8\u4f7f\u7528\u5de5\u5177\uff0c\u4ec5\u8ba9\u6a21\u578b\u81ea\u884c\u56de\u7b54\")<br> \u00a0 \u00a0 \u00a0  try:<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  response_message = llm.invoke([HumanMessage(content=user_prompt)])<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  final_answer = response_message.content<br> \u00a0 \u00a0 \u00a0  except Exception as e:<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  print(f\"\\n[\u9519\u8bef] \u8c03\u7528\u5927\u6a21\u578b\u65f6\u51fa\u9519: {e}\")<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  return<br>\u200b<br> \u00a0  print(f\"\\n\ud83e\udd16 {MODEL_NAME}: {final_answer}\")<br> \u00a0  print(\"=\"*50 + \"\\n\")<br>\u200b<br>\u200b<br>if __name__ == '__main__':<br> \u00a0  if 'server_thread' not in locals() or not server_thread.is_alive():<br> \u00a0 \u00a0 \u00a0  server_thread = threading.Thread(target=run_api_server, daemon=True)<br> \u00a0 \u00a0 \u00a0  server_thread.start()<br> \u00a0 \u00a0 \u00a0  print(\"--- \u4e3b\u7a0b\u5e8f\u542f\u52a8\uff0c\u7b49\u5f85 API \u670d\u52a1\u5668\u5c31\u7eea (2\u79d2)... ---\")<br> \u00a0 \u00a0 \u00a0  time.sleep(2)<br> \u00a0 \u00a0 \u00a0  print(\"--- API \u670d\u52a1\u5668\u5df2\u5c31\u7eea\uff0c\u5f00\u59cb\u6267\u884c\u5bf9\u8bdd\u4efb\u52a1 ---\\n\")<br>\u200b<br> \u00a0  # \u8c03\u7528\u5de5\u5177<br> \u00a0  run_conversation(\"\u6211\u60f3\u77e5\u9053 129032910921 * 188231 \u7b49\u4e8e\u591a\u5c11\uff1f\", force_tool=True)<br> \u00a0  # \u4e0d\u8c03\u7528\u5de5\u5177<br> \u00a0  run_conversation(\"\u6211\u60f3\u77e5\u9053 129032910921 * 188231 \u7b49\u4e8e\u591a\u5c11\uff1f\u4ec5\u7ed9\u51fa\u7b54\u6848\u5373\u53ef\uff0c\u4e0d\u9700\u8981\u5176\u4ed6\u5185\u5bb9\", force_tool=False)<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/fushuling-1309926051.cos.ap-shanghai.myqcloud.com\/2025\/07\/QQ20250720-112653-7.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/fushuling-1309926051.cos.ap-shanghai.myqcloud.com\/2025\/07\/QQ20250720-112653-7.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"QQ20250720-112653-7\"\/><\/div><\/figure>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4eec\u53ef\u4ee5\u5199\u4e2a\u5c0f\u811a\u672c\u6765\u7b97\u7b97\u503c\u7a76\u7adf\u662f\u591a\u5c11\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from decimal import Decimal, getcontext<br>\u200b<br># \u8bbe\u7f6e\u5c0f\u6570\u8ba1\u7b97\u7cbe\u5ea6\uff0c\u9ed8\u8ba4 100 \u4f4d<br>getcontext().prec = 100<br>\u200b<br>\u200b<br>def multiply(a: str, b: str) -&gt; str:<br> &nbsp;  \"\"\"<br> &nbsp;  \u8f93\u5165\u4e24\u4e2a\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u6570\u5b57\uff0c\u53ea\u652f\u6301\u4e58\u6cd5\uff0c\u8fd4\u56de\u5b57\u7b26\u4e32\u7ed3\u679c\u3002<br> &nbsp;  \"\"\"<br> &nbsp;  try:<br> &nbsp; &nbsp; &nbsp;  # \u5224\u65ad\u662f\u5426\u5305\u542b\u5c0f\u6570\u70b9<br> &nbsp; &nbsp; &nbsp;  if \".\" in a or \".\" in b:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  result = Decimal(a) * Decimal(b)<br> &nbsp; &nbsp; &nbsp;  else:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  result = int(a) * int(b)<br> &nbsp; &nbsp; &nbsp;  return str(result)<br> &nbsp;  except Exception as e:<br> &nbsp; &nbsp; &nbsp;  return f\"\u8ba1\u7b97\u51fa\u9519\uff1a{e}\"<br>\u200b<br>\u200b<br>if __name__ == \"__main__\":<br> &nbsp;  print(multiply(\"129032910921\", \"188231\"))<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/fushuling-1309926051.cos.ap-shanghai.myqcloud.com\/2025\/07\/QQ20250720-112936-8.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/fushuling-1309926051.cos.ap-shanghai.myqcloud.com\/2025\/07\/QQ20250720-112936-8.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"QQ20250720-112936-8\"\/><\/div><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u770b\u51fa\u6765\u7ed3\u679c\u5e94\u8be5\u662f24287993855570751\uff0c\u8c03\u7528\u5de5\u5177\u7684\u7ed3\u679c\u662f\u5bf9\u7684\uff0c\u6ca1\u8c03\u7528\u5de5\u5177\u7684\u7ed3\u679c\u662fllm\u4e71\u8bf4\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Task #0: \u7528\u811a\u672c\u8bf7\u6c42\u5546\u7528\u5927\u6a21\u578b \u76ee\u6807 \u5b9e\u9a8c\u5185\u5bb9 \u5b9e\u9a8c\u6d41\u7a0b \u5148\u53bbhttps:\/\/cloud.silico [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-3916","post","type-post","status-publish","format-standard","hentry","category-lab"],"_links":{"self":[{"href":"https:\/\/fushuling.com\/index.php\/wp-json\/wp\/v2\/posts\/3916","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fushuling.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fushuling.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fushuling.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fushuling.com\/index.php\/wp-json\/wp\/v2\/comments?post=3916"}],"version-history":[{"count":1,"href":"https:\/\/fushuling.com\/index.php\/wp-json\/wp\/v2\/posts\/3916\/revisions"}],"predecessor-version":[{"id":3917,"href":"https:\/\/fushuling.com\/index.php\/wp-json\/wp\/v2\/posts\/3916\/revisions\/3917"}],"wp:attachment":[{"href":"https:\/\/fushuling.com\/index.php\/wp-json\/wp\/v2\/media?parent=3916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fushuling.com\/index.php\/wp-json\/wp\/v2\/categories?post=3916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fushuling.com\/index.php\/wp-json\/wp\/v2\/tags?post=3916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}