from openai import OpenAIfrom pydantic import BaseModelMODEL_NAME = "Qwen/Qwen2.5-7B-Instruct"API_KEY = "您的 APIKEY", # 从https://cloud.siliconflow.cn/account/ak获取client = OpenAI(api_key=API_KEY, base_url="https://api.siliconflow.cn/v1")class Step(BaseModel): explanation: str output: strclass MathReasoning(BaseModel): steps: list[Step] final_answer: strjson_schema = MathReasoning.model_json_schema()completion = client.chat.completions.create( model=MODEL_NAME, messages=[ {"role": "system", "content": "You are a helpful math tutor. Guide the user through the solution step by step."}, {"role": "user", "content": "how can I solve 8x + 7 = -23"} ], response_format={ "type": "json_schema", "json_schema": { "name": "math_response", "schema": json_schema } })math_reasoning = completion.choices[0].message.contentprint(math_reasoning)
模型将输出:
Copy
{ "steps": [ { "explanation": "To solve the equation for x, we need to isolate x on one side of the equation.", "output": "8x + 7 = -23" }, { "explanation": "First, we need to subtract 7 from both sides to cancel out the 7 on the left side.", "output": "8x + 7 - 7 = -23 - 7" }, { "explanation": "Simplify the equation.", "output": "8x = -30" }, { "explanation": "Next, we need to divide both sides by 8 to isolate x.", "output": "8x / 8 = -30 / 8" }, { "explanation": "Simplify to find the value of x.", "output": "x = -30 / 8" }, { "explanation": "We can simplify the fraction further by dividing the numerator and the denominator by their greatest common divisor, which is 2.", "output": "x = -15 / 4" } ], "final_answer": "The solution to the equation 8x + 7 = -23 is x = -15/4."}
Assistant
Responses are generated using AI and may contain mistakes.