MySQL MVCC底层原理与银行转账事务设计实战
2026/10/21 6:39:38
SmallThinker-3B-Preview是基于Qwen2.5-3b-Instruct模型微调而来的轻量级AI模型。这个模型特别适合在资源有限的环境中使用,同时也能作为更大模型的辅助工具。
模型主要特点:
首先需要在本地或服务器上安装Ollama服务:
# Linux/macOS安装命令 curl -fsSL https://ollama.com/install.sh | sh # Windows安装 # 下载安装包:https://ollama.com/download安装完成后,拉取SmallThinker-3B-Preview模型:
ollama pull smallthinker:3bpip install requestsimport requests def simple_query(prompt): url = "http://localhost:11434/api/generate" data = { "model": "smallthinker:3b", "prompt": prompt, "stream": False } response = requests.post(url, json=data) return response.json() # 示例调用 result = simple_query("解释一下量子计算的基本原理") print(result["response"])import json from typing import List def batch_cot_inference(prompts: List[str], max_tokens=8192): url = "http://localhost:11434/api/generate" results = [] for prompt in prompts: data = { "model": "smallthinker:3b", "prompt": prompt, "options": { "num_ctx": max_tokens # 设置最大上下文长度 }, "stream": False } try: response = requests.post(url, json=data) response.raise_for_status() result = response.json() results.append({ "prompt": prompt, "response": result["response"], "context": result.get("context", []) }) except Exception as e: print(f"处理提示'{prompt[:30]}...'时出错: {str(e)}") results.append({ "prompt": prompt, "error": str(e) }) return results# 准备一组需要COT推理的问题 cot_prompts = [ "请逐步解释光合作用的过程", "详细说明如何从零开始训练一个神经网络", "分析气候变化对全球经济的影响,分步骤说明", "描述开发一个移动应用的完整流程" ] # 执行批量推理 results = batch_cot_inference(cot_prompts) # 保存结果 with open("cot_results.json", "w", encoding="utf-8") as f: json.dump(results, f, ensure_ascii=False, indent=2)对于大规模数据处理,可以使用生成器来节省内存:
def stream_cot_inference(prompt_file, output_file): with open(prompt_file, "r", encoding="utf-8") as infile, \ open(output_file, "w", encoding="utf-8") as outfile: for line in infile: prompt = line.strip() if not prompt: continue result = simple_query(prompt) outfile.write(json.dumps({ "prompt": prompt, "response": result["response"] }, ensure_ascii=False) + "\n")num_ctx参数def generate_teaching_materials(topic, difficulty="high school"): prompt = f"""作为{diificulty}教师,请为"{topic}"主题创建教学材料: 1. 核心概念解释 2. 3个实际应用例子 3. 常见学生疑问解答 4. 5道练习题及答案""" return simple_query(prompt)["response"]def generate_business_analysis(company, industry): prompt = f"""为{company}公司准备{industry}行业分析报告,包含: 1. 市场趋势分析 2. 竞争对手比较 3. SWOT分析 4. 发展建议""" results = batch_cot_inference([prompt]) return results[0]["response"]通过本文介绍的方法,您可以轻松使用Python调用Ollama API实现SmallThinker-3B-Preview模型的批量COT推理。这种轻量级模型特别适合:
最佳实践建议:
num_ctx参数获取更多AI镜像
想探索更多AI镜像和应用场景?访问 CSDN星图镜像广场,提供丰富的预置镜像,覆盖大模型推理、图像生成、视频生成、模型微调等多个领域,支持一键部署。