Views
No views yet
1# Clone the repository
2git clone https://huggingface.co/Manojb/qwen3-4b-toolcall-gguf-llamacpp-codex
3cd qwen3-4b-toolcall-llamacpp-codex
4
5# Run the installation script
6./install.shpip install -r requirements.txt1# Download the model file
2huggingface-cli download Manojb/qwen3-4b-toolcall-gguf-llamacpp-codex Qwen3-4B-Function-Calling-Pro.gguf1# For CPU-only (default)
2pip install llama-cpp-python
3
4# For CUDA support (if you have NVIDIA GPU)
5CMAKE_ARGS="-DLLAMA_CUBLAS=on" pip install llama-cpp-python
6
7# For OpenBLAS support
8CMAKE_ARGS="-DLLAMA_BLAS=on -DLLAMA_BLAS_VENDOR=OpenBLAS" pip install llama-cpp-python1# Interactive mode (default)
2./run_model.sh
3# or
4source ./run_model.sh
5
6# Start Codex server
7./run_model.sh server
8# or
9source ./run_model.sh server
10
11# Show help
12./run_model.sh help
13# or
14source ./run_model.sh help1from llama_cpp import Llama
2
3# Load the model
4llm = Llama(
5 model_path="Qwen3-4B-Function-Calling-Pro.gguf",
6 n_ctx=2048,
7 n_threads=8,
8 temperature=0.7
9)
10
11# Simple chat
12response = llm("What's the weather like in London?", max_tokens=200)
13print(response['choices'][0]['text'])python3 quick_start.py1import json
2import re
3from llama_cpp import Llama
4
5def extract_tool_calls(text):
6 """Extract tool calls from model response"""
7 tool_calls = []
8 json_pattern = r'\[.*?\]'
9 matches = re.findall(json_pattern, text)
10
11 for match in matches:
12 try:
13 parsed = json.loads(match)
14 if isinstance(parsed, list):
15 for item in parsed:
16 if isinstance(item, dict) and 'name' in item:
17 tool_calls.append(item)
18 except json.JSONDecodeError:
19 continue
20 return tool_calls
21
22# Initialize model
23llm = Llama(
24 model_path="Qwen3-4B-Function-Calling-Pro.gguf",
25 n_ctx=2048,
26 temperature=0.7
27)
28
29# Chat with tool calling
30prompt = "Get the weather for New York"
31formatted_prompt = f"<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n"
32
33response = llm(formatted_prompt, max_tokens=200, stop=["<|im_end|>", "<|im_start|>"])
34response_text = response['choices'][0]['text']
35
36# Extract tool calls
37tool_calls = extract_tool_calls(response_text)
38print(f"Tool calls: {tool_calls}")1# The model will generate:
2# [{"name": "get_weather", "arguments": {"q": "London"}}]1# The model will generate:
2# [{"name": "search_stays", "arguments": {"check_in": "2023-04-01", "check_out": "2023-04-08", "city": "Paris"}}]1# The model will generate:
2# [{"name": "flights_search", "arguments": {"q": "New York to Tokyo"}}]1# The model will generate:
2# [{"name": "search_news", "arguments": {"q": "AI", "gl": "us"}}]pip install llama-cpp-python[server]1python -m llama_cpp.server \
2 --model Qwen3-4B-Function-Calling-Pro.gguf \
3 --host 0.0.0.0 \
4 --port 8000 \
5 --n_ctx 2048 \
6 --n_threads 8 \
7 --temperature 0.7http://localhost:8000Qwen3-4B-Function-Calling-Pro1# codex_integration.py
2import requests
3import json
4
5class CodexClient:
6 def __init__(self, base_url="http://localhost:8000"):
7 self.base_url = base_url
8 self.session = requests.Session()
9
10 def chat_completion(self, messages, tools=None, temperature=0.7):
11 """Send chat completion request to Codex"""
12 payload = {
13 "model": "Qwen3-4B-Function-Calling-Pro",
14 "messages": messages,
15 "temperature": temperature,
16 "max_tokens": 512,
17 "stop": ["<|im_end|>", "<|im_start|>"]
18 }
19
20 if tools:
21 payload["tools"] = tools
22
23 response = self.session.post(
24 f"{self.base_url}/v1/chat/completions",
25 json=payload,
26 headers={"Content-Type": "application/json"}
27 )
28
29 return response.json()
30
31 def extract_tool_calls(self, response):
32 """Extract tool calls from Codex response"""
33 tool_calls = []
34 if "choices" in response and len(response["choices"]) > 0:
35 message = response["choices"][0]["message"]
36 if "tool_calls" in message:
37 tool_calls = message["tool_calls"]
38 return tool_calls
39
40# Usage with Codex
41codex = CodexClient()
42
43# Define tools for Codex
44tools = [
45 {
46 "type": "function",
47 "function": {
48 "name": "get_weather",
49 "description": "Get current weather for a location",
50 "parameters": {
51 "type": "object",
52 "properties": {
53 "location": {
54 "type": "string",
55 "description": "City name"
56 }
57 },
58 "required": ["location"]
59 }
60 }
61 }
62]
63
64# Send request
65messages = [{"role": "user", "content": "What's the weather in London?"}]
66response = codex.chat_completion(messages, tools=tools)
67tool_calls = codex.extract_tool_calls(response)
68
69print(f"Response: {response}")
70print(f"Tool calls: {tool_calls}")Dockerfile for easy deployment:1FROM python:3.11-slim
2
3WORKDIR /app
4
5# Install dependencies
6COPY requirements.txt .
7RUN pip install -r requirements.txt
8
9# Install llama-cpp-python with server support
10RUN pip install llama-cpp-python[server]
11
12# Copy model and scripts
13COPY . .
14
15# Expose port
16EXPOSE 8000
17
18# Start server
19CMD ["python", "-m", "llama_cpp.server", \
20 "--model", "Qwen3-4B-Function-Calling-Pro.gguf", \
21 "--host", "0.0.0.0", \
22 "--port", "8000", \
23 "--n_ctx", "2048"]1docker build -t qwen3-codex-server .
2docker run -p 8000:8000 qwen3-codex-server1class Qwen3ToolCalling:
2 def __init__(self, model_path):
3 self.llm = Llama(
4 model_path=model_path,
5 n_ctx=2048,
6 n_threads=8,
7 temperature=0.7,
8 verbose=False
9 )
10
11 def chat(self, message, system_message=None):
12 # Build prompt with proper formatting
13 prompt_parts = []
14 if system_message:
15 prompt_parts.append(f"<|im_start|>system\n{system_message}<|im_end|>")
16 prompt_parts.append(f"<|im_start|>user\n{message}<|im_end|>")
17 prompt_parts.append("<|im_start|>assistant\n")
18
19 formatted_prompt = "\n".join(prompt_parts)
20
21 # Generate response
22 response = self.llm(
23 formatted_prompt,
24 max_tokens=512,
25 stop=["<|im_end|>", "<|im_start|>"],
26 temperature=0.7
27 )
28
29 response_text = response['choices'][0]['text']
30 tool_calls = self.extract_tool_calls(response_text)
31
32 return {
33 'response': response_text,
34 'tool_calls': tool_calls
35 }| Component | Minimum | Recommended |
|---|---|---|
| RAM | 6GB | 8GB+ |
| Storage | 5GB | 10GB+ |
| CPU | 4 cores | 8+ cores |
| GPU | Optional | NVIDIA RTX 3060+ |
<tool_call> - Start of tool call</tool_call> - End of tool call<tool_response> - Start of tool response</tool_response> - End of tool response<|im_start|>system
{system_message}<|im_end|>
<|im_start|>user
{user_message}<|im_end|>
<|im_start|>assistant
{assistant_response}<|im_end|>qwen3-4b-toolcall-llamacpp/
├── Qwen3-4B-Function-Calling-Pro.gguf # Main model file
├── qwen3_toolcalling_example.py # Complete example
├── quick_start.py # Quick start demo
├── codex_integration.py # Codex integration example
├── run_model.sh # Run script for llama-cpp
├── install.sh # Installation script
├── requirements.txt # Python dependencies
├── README.md # This file
├── config.json # Model configuration
├── tokenizer_config.json # Tokenizer configuration
├── special_tokens_map.json # Special tokens mapping
├── added_tokens.json # Added tokens
├── chat_template.jinja # Chat template
├── Dockerfile # Docker configuration
├── docker-compose.yml # Docker Compose setup
└── .gitignore # Git ignore file1@model{Manojb/Qwen3-4b-toolcall-gguf-llamacpp-codex,
2 title={Qwen3-4B-toolcalling-gguf-codex: Local Function Calling},
3 author={Manojb},
4 year={2025},
5 url={https://huggingface.co/Manojb/Qwen3-4b-toolcall-gguf-llamacpp-codex}
6}