Uploaded model
- Developed by: aliRafik
- License: apache-2.0
- Finetuned from model : unsloth/gpt-oss-20b-unsloth-bnb-4bit
This gpt_oss model was trained 2x faster with Unsloth and Huggingface's TRL library.
GPT-OSS-20B-Agentic-SwissKnife-Tool-Calling
AliRafik/GPT-OSS-20B-Agentic-SwissKnife-Tool-calling-full-model is a Lora Finetunned -parameter causal language model fine-tuned for tool-augmented reasoning. This model can intelligently detect when to use external tools and execute them in a structured way to provide precise answers. It has been trained on a diverse set of tasks including mathematical computation, Python code execution, and legal document analysis.
🚀 Features
This Great model can perform a wide variety of tool-augmented tasks, including but not limited to:
Mathematical and computational tools
execute_python_code: Safely run Python code.calculate_mean,calculate_median,calculate_sum,calculate_average: Compute statistics over numerical data.add_numbers,multiply_numbers: Perform basic arithmetic.solve_linear_equation,solve_quadratic_equation,solve_cubic_equation: Solve linear, quadratic, and cubic equations.
Legal and document analysis tools
analyze_contract: Extract key elements from contracts.extract_legal_entities: Identify entities in legal texts.detect_legal_risks: Detect potential risks in contracts or legal documents.compare_legal_documents: Compare two legal documents for similarity.summarize_legal_document,legal_case_summary: Provide summaries of legal documents or cases.check_compliance,check_regulation,get_related_legal_rules,search_legal_precedents: Assist with regulatory and compliance checks.
Data processing tools
sort_list: Sort numerical data in ascending or descending order.calculate_variance: Compute variance of numbers.
The model can automatically detect which tool is needed for a given query and generate structured tool calls to facilitate execution.
📦 Model Usage
can use this model with Hugging Face Transformers:
from unsloth import FastLanguageModel
from peft import PeftModel
# Load the base model first
base_model, tokenizer = FastLanguageModel.from_pretrained(
"unsloth/gpt-oss-20b",
load_in_4bit=True,
full_finetuning=False,
)
model = PeftModel.from_pretrained(
base_model,
"aliRafik/GPT-OSS-20B-Agentic-SwissKnife-Tool-calling"
)
prompt = "Calculate the mean and variance of [3, 5, 7, 9]."
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=200)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
⚠️ Note: The model outputs structured tool calls for tasks. You need to parse these tool calls and execute them programmatically to get the final results. as the following code example of parsing the tools
from transformers import AutoModelForCausalLM, AutoTokenizer
import json
# Load model and tokenizer
from unsloth import FastLanguageModel
from peft import PeftModel
# Load the base model first
base_model, tokenizer = FastLanguageModel.from_pretrained(
"unsloth/gpt-oss-20b",
load_in_4bit=True,
full_finetuning=False,
)
model = PeftModel.from_pretrained(
base_model,
"aliRafik/GPT-OSS-20B-Agentic-SwissKnife-Tool-calling"
)
# Define tools
def execute_tool(tool_name, args):
if tool_name == "add_numbers":
return args["a"] + args["b"]
elif tool_name == "multiply_numbers":
return args["a"] * args["b"]
elif tool_name == "calculate_sum":
return sum(args["numbers"])
elif tool_name == "calculate_mean":
numbers = args["numbers"]
return sum(numbers)/len(numbers)
elif tool_name == "calculate_median":
numbers = sorted(args["numbers"])
n = len(numbers)
mid = n // 2
if n % 2 == 0:
return (numbers[mid-1] + numbers[mid]) / 2
return numbers[mid]
else:
return f"Tool {tool_name} not implemented."
# Example messages
messages = [
{"role": "system", "content": """
You are an AI assistant with access to a set of tools. Follow the numbered workflow with <think> reasoning and <final_answer> for conclusions.
"""},
{"role": "user", "content": "Calculate the sum of [3,5,7,9] using a tool"}
]
# Apply chat template
inputs = tokenizer.apply_chat_template(
messages,
add_system_prompt=False,
add_generation_prompt=True,
return_tensors="pt",
return_dict=True,
).to(model.device)
# Generate initial response
outputs = model.generate(**inputs, max_new_tokens=512)
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
print("=== MODEL OUTPUT ===")
print(generated_text)
# Parse tool call
def extract_tool_call(text):
try:
start = text.index('{"tool_calls":')
end = text.index('}', start) + 1
tool_call_json = text[start:end]
data = json.loads(tool_call_json)
return data["tool_calls"][0]
except ValueError:
return None
tool_call = extract_tool_call(generated_text)
# Execute tool and feed back
if tool_call:
tool_name = tool_call["function"]["name"]
args = json.loads(tool_call["function"]["arguments"])
tool_result = execute_tool(tool_name, args)
tool_message = {"role": "tool", "content": json.dumps({"tool_call_id": tool_call["id"], "output": str(tool_result)})}
messages.append(tool_message)
# Regenerate final answer
inputs = tokenizer.apply_chat_template(
messages,
add_system_prompt=False,
add_generation_prompt=True,
return_tensors="pt",
return_dict=True
).to(model.device)
outputs = model.generate(**inputs, max_new_tokens=1024)
final_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
print("\n=== FINAL ANSWER ===")
print(final_text)
else:
print("\nNo tool call detected. Final output:")
print(generated_text)
This loop allows the model to detect tool calls, execute the tools, and return the final answer automatically.
#example of the Great Output.
user: Calculate the sum of [3,5,7,9] using a toolassistantanalysisThe user asks: "Calculate the sum of [3,5,7,9] using a tool".
We have tool 4: calculate_sum. So choose tool calculate_sum, with arguments numbers=[3,5,7,9]. Provide tool call id unique.
Then we expect tool result message with tool_call_id equal to that id. So step 1: understand query: sum of numbers.
Step 2: decide tool use: tool 4. Step 3: select and argue tool: tool 4, calculate_sum, because we need sum.
Step 4: execute tool: tool call id, e.g., "call_1".
Provide tool call: {"tool_calls": [{"id": "call_1", "type": "function", "function": {"name": "calculate_sum", "arguments": "{"numbers": [3, 5, 7, 9]}"}]}}
We must format tool call arguments as a JSON string. So generate tool call with id "call_1".
Then the system will provide tool result with {"tool_call_id": "call_1", "output": ...}.
We then process that output. The expected output is the sum: 3+5+7+9 = 24. So tool output should be "24" (string).
So we will receive tool result: {"tool_call_id": "call_1", "output": "24"}.
Then we process tool results: we have a list of tool results with one entry: {"tool_call_id": "call_1", "output": "24"}.
We then formulate final response: ": 24" (with colon as per example). And conclude with <final_answer>.
So assistant message should have role "assistant", content with the reasoning steps, and tool_calls with the tool call.
Then the system will provide tool result. Then we will provide final answer.
Thus, output assistant message now.assistantfinal{"tool_calls": [{"id": "call_1", "type": "function", "function": {"name": "calculate_sum", "arguments": "{"numbers": [3, 5, 7, 9]}"}]}}<think>Selected tool calculate_sum (tool 4) to compute the sum of the list [3, 5, 7, 9].
Arguments: {"numbers": [3, 5, 7, 9]}</think><final_answer>: 24</final_answer>
💾 Training Data
The model has been trained on curated datasets for tool-calling tasks, combining:
- Python code execution tasks
- Mathematical computation tasks
- Legal and document analysis tasks
You can access the training data here: []
📃 Example Tool Calls
{"tool": "calculate_mean", "args": {"numbers": [3,5,7,9]}}
{"tool": "execute_python_code", "args": {"code": "sum([1,2,3,4])"}}
{"tool": "analyze_contract", "args": {"text": "This contract states..." }}
The model generates these JSON-structured calls, which should then be executed in application loop.
⚡️ Capabilities
- Tool-aware reasoning with dynamic execution.
- Multi-domain: legal analysis, computational tasks, and Python execution.
- Generates structured outputs ready for programmatic execution.
📜 License
[Specify license here, e.g., MIT, Apache-2.0]