File size: 1,809 Bytes
718ea39 54101a1 718ea39 54101a1 718ea39 71bf9a9 718ea39 71bf9a9 718ea39 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
from smolagents import Tool
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
import torch
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_name= "deepseek-ai/deepseek-math-7b-base"):
print(f"Loading math model: {model_name}")
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
print("loaded tokenizer")
self.model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16)
print("loaded auto model")
self.model.generation_config = GenerationConfig.from_pretrained(model_name)
print("loaded coonfig")
self.model.generation_config.pad_token_id = self.model.generation_config.eos_token_id
print("loaded pad token")
def forward(self, problem: str) -> str:
print(f"[MathModelTool] Question: {problem}")
inputs = self.tokenizer(problem, return_tensors="pt")
outputs =self.model.generate(**inputs, max_new_tokens=100)
result = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
return result
class WikipediaTool(Tool):
name = "wikip_tool"
description = "Searches Wikipedia and provides summary about the queried topic."
inputs = {
"query": {
"type": "string",
"description": "Topic of wikipedia search",
}
}
output_type = "string"
def __init__(self):
import wikipedia
def forward(self, query: str) -> str:
return wikipedia.summary(query, sentences=3)
|