Update README.md with tool-use support for huggingface and vLLM

#9
Files changed (1) hide show
  1. README.md +96 -0
README.md CHANGED
@@ -78,6 +78,102 @@ completion = client.chat.completions.create(
78
  )
79
  ```
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  ---
82
 
83
  ## **Evaluation Summary**
 
78
  )
79
  ```
80
 
81
+ ## With Tool-use
82
+ To enable tool-use, load, tokenize, and generate as the examples above, but additionally define a set of tools and pass them to the chat template.
83
+
84
+ **Using transformers**
85
+ ```python
86
+ from transformers import AutoModelForCausalLM, AutoTokenizer
87
+
88
+ model = AutoModelForCausalLM.from_pretrained("llm360/k2-v2-instruct", device_map="auto")
89
+ tokenizer = AutoTokenizer.from_pretrained("llm360/k2-v2-instruct")
90
+
91
+ prompt = "Explain why the derivative of sin(x) is cos(x)."
92
+ messages = [
93
+ {"role": "system", "content": "You are K2, a helpful assistant created by Mohamed bin Zayed University of Artificial Intelligence (MBZUAI) Institute of Foundation Models (IFM)."},
94
+ {"role": "user", "content": prompt}
95
+ ]
96
+
97
+ # Define list of tools
98
+ tools = [
99
+ {
100
+ "type": "function",
101
+ "name": "get_horoscope",
102
+ "description": "Get today's horoscope for an astrological sign.",
103
+ "parameters": {
104
+ "type": "object",
105
+ "properties": {
106
+ "sign": {
107
+ "type": "string",
108
+ "description": "An astrological sign like Taurus or Aquarius",
109
+ },
110
+ },
111
+ "required": ["sign"],
112
+ },
113
+ },
114
+ ]
115
+
116
+ text = tokenizer.apply_chat_template(
117
+ messages,
118
+ tokenize=False,
119
+ add_generation_prompt=True
120
+ reasoning_effort="high" # Or "medium"/"low"
121
+ tools=tools
122
+ )
123
+
124
+ inputs = tokenizer(text, return_tensors="pt").to(model.device)
125
+ outputs = model.generate(**inputs, max_new_tokens=200)
126
+ print(tokenizer.decode(outputs[0], skip_special_tokens=True))
127
+ ```
128
+
129
+ **Using vLLM**
130
+ Serve the model with tool-use enabled and parser set.
131
+
132
+ ```
133
+ vllm serve LLM360/K2-V2-Instruct --enable-auto-tool-choice --tool-call-parser hermes --tensor-parallel-size 8 --port 8000
134
+ ```
135
+
136
+ ```
137
+ from openai import OpenAI
138
+
139
+ client = OpenAI(
140
+ base_url="http://localhost:8000/v1",
141
+ api_key="key"
142
+ )
143
+
144
+ # Define list of tools
145
+ tools = [
146
+ {
147
+ "type": "function",
148
+ "name": "get_horoscope",
149
+ "description": "Get today's horoscope for an astrological sign.",
150
+ "parameters": {
151
+ "type": "object",
152
+ "properties": {
153
+ "sign": {
154
+ "type": "string",
155
+ "description": "An astrological sign like Taurus or Aquarius",
156
+ },
157
+ },
158
+ "required": ["sign"],
159
+ },
160
+ },
161
+ ]
162
+
163
+ completion = client.chat.completions.create(
164
+ model="LLM360/K2-V2-Instruct",
165
+ messages = [
166
+ {"role": "system", "content": "You are K2, a helpful assistant created by Mohamed bin Zayed University of Artificial Intelligence (MBZUAI) Institute of Foundation Models (IFM)."},
167
+ {"role": "user", "content": "Explain why the derivative of sin(x) is cos(x)."}
168
+ ],
169
+ extra_body={
170
+ "chat_template_kwargs": {"reasoning_effort": "high"},
171
+ },
172
+ tools=tools,
173
+ )
174
+ ```
175
+
176
+
177
  ---
178
 
179
  ## **Evaluation Summary**