File size: 861 Bytes
258a8d6
54101a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from smolagents import Tool
import hashlib
import json
from transformers import AutoTokenizer, AutoModelForCausalLM
import os


class ModelMathTool(Tool):
    name = "math_model"
    description = "Answers advanced math questions using a pretrained math model."

    inputs = {
        "problem": {
            "type": "string",
            "description": "Math problem to solve.",
        }
    }

    output_type = "string"

    def __init__(self, model_id="Qwen/Qwen2.5-Math-7B"):
        print(f"Loading math model: {model_id}")
        self.tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
        self.model = HfApiModel(model_id=model_id, max_tokens=512)

    def forward(self, problem: str) -> str:
        print(f"[MathModelTool] Question: {problem}")
        response = self.model.__call__(problem)
        return response