Phi-4-mini-reasoning代码实例:Python调用API实现多轮数学对话

张开发
2026/4/21 14:31:33 15 分钟阅读
Phi-4-mini-reasoning代码实例:Python调用API实现多轮数学对话
Phi-4-mini-reasoning代码实例Python调用API实现多轮数学对话1. 模型简介Phi-4-mini-reasoning是一款3.8B参数的轻量级开源模型专为数学推理、逻辑推导和多步解题等强逻辑任务设计。这个模型由微软Azure AI Foundry开发主打小参数、强推理、长上下文、低延迟的特点。作为一款专注于推理能力的模型Phi-4-mini-reasoning在数学问题解答和代码理解方面表现突出。虽然参数规模不大但通过专注于高质量推理数据的训练它在逻辑任务上的表现可以媲美更大的模型。2. 环境准备2.1 安装必要库在开始之前我们需要安装几个Python库。打开终端或命令行运行以下命令pip install requests python-dotenv2.2 获取API访问信息假设你已经按照前面的指南部署好了Phi-4-mini-reasoning服务服务运行在本地7860端口。我们需要创建一个.env文件来存储API地址# .env文件内容 API_URLhttp://localhost:7860/api/v1/generate3. 基础API调用3.1 简单单次请求让我们先写一个最简单的Python函数来调用APIimport requests import json from dotenv import load_dotenv import os load_dotenv() # 加载环境变量 def ask_phi4(question): headers {Content-Type: application/json} data { inputs: question, parameters: { max_new_tokens: 512, temperature: 0.3, top_p: 0.85, repetition_penalty: 1.2 } } response requests.post(os.getenv(API_URL), headersheaders, datajson.dumps(data)) if response.status_code 200: return response.json()[generated_text] else: return fError: {response.status_code} - {response.text} # 测试调用 print(ask_phi4(What is 15% of 200?))3.2 解析响应结果API返回的响应是一个JSON对象我们可以提取其中的关键信息def parse_response(response): try: result response.json() return { answer: result[generated_text], time_taken: result.get(time_taken, N/A), tokens_generated: result.get(tokens_generated, N/A) } except Exception as e: return {error: str(e)}4. 实现多轮对话4.1 维护对话历史要实现多轮对话我们需要维护一个对话历史列表class MathChatbot: def __init__(self): self.conversation_history [] def add_to_history(self, role, content): self.conversation_history.append({role: role, content: content}) def get_full_prompt(self): return \n.join([f{msg[role]}: {msg[content]} for msg in self.conversation_history]) def ask(self, question): self.add_to_history(User, question) full_prompt self.get_full_prompt() response ask_phi4(full_prompt) if not response.startswith(Error): self.add_to_history(Assistant, response) return response4.2 完整对话示例让我们看一个完整的数学问题对话示例bot MathChatbot() # 第一轮问题 question1 If a train travels 300 miles in 5 hours, what is its average speed? answer1 bot.ask(question1) print(fQ: {question1}\nA: {answer1}\n) # 基于答案的后续问题 question2 How long would it take to travel 420 miles at that speed? answer2 bot.ask(question2) print(fQ: {question2}\nA: {answer2}\n) # 更复杂的问题 question3 If the speed increases by 20%, how much time would be saved on a 500 mile trip? answer3 bot.ask(question3) print(fQ: {question3}\nA: {answer3})5. 高级功能实现5.1 带步骤的数学解答Phi-4-mini-reasoning特别擅长分步解答数学问题。我们可以修改提示词来获取更详细的解答def ask_with_steps(question): prompt fPlease solve the following math problem step by step: {question} Provide your answer in the following format: 1. [First step explanation] 2. [Second step explanation] ... Final Answer: [The final answer] return ask_phi4(prompt) # 测试分步解答 problem A rectangular garden has a length of 12 meters and width of 8 meters. If a path of uniform width is built around the garden, increasing the total area to 180 square meters, what is the width of the path? print(ask_with_steps(problem))5.2 代码生成与解释除了数学问题Phi-4-mini-reasoning还能生成和解释代码code_question Write a Python function to calculate the area of a circle given its radius. Include type hints and a docstring. Then explain how the function works. print(ask_phi4(code_question))6. 参数调优建议根据不同的使用场景你可能需要调整生成参数场景temperaturetop_pmax_new_tokens说明数学计算0.1-0.30.7-0.9256-512低随机性确保准确性创意解题0.5-0.70.9-1.0512-1024更高创造性代码生成0.3-0.50.8-0.95512-1024平衡准确与创意def ask_with_params(question, temp0.3, top_p0.85, max_tokens512): headers {Content-Type: application/json} data { inputs: question, parameters: { max_new_tokens: max_tokens, temperature: temp, top_p: top_p, repetition_penalty: 1.2 } } response requests.post(os.getenv(API_URL), headersheaders, datajson.dumps(data)) return response.json()[generated_text]7. 总结通过本文的代码示例我们学习了如何使用Python调用Phi-4-mini-reasoning的API接口实现基础的单次问答功能构建多轮对话系统维护对话历史获取分步的数学问题解答生成和解释代码根据不同的使用场景调整生成参数Phi-4-mini-reasoning虽然参数规模不大但在数学推理和逻辑任务上表现优异。它的轻量级特性使得部署和运行成本较低特别适合需要频繁进行逻辑推理和数学计算的应用程序。在实际应用中你可以进一步扩展这个基础框架比如添加错误处理、实现异步调用、或者集成到Web应用中。模型的长上下文支持(128K tokens)让它特别适合处理复杂的多步推理问题。获取更多AI镜像想探索更多AI镜像和应用场景访问 CSDN星图镜像广场提供丰富的预置镜像覆盖大模型推理、图像生成、视频生成、模型微调等多个领域支持一键部署。

更多文章