Update README.md
Browse files
README.md
CHANGED
|
@@ -178,7 +178,90 @@ curl http://localhost:30000/v1/chat/completions \
|
|
| 178 |
|
| 179 |
## Function call
|
| 180 |
|
| 181 |
-
### 1.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
Соберите dev версию, коммит>=[21bb323](https://github.com/vllm-project/vllm/tree/21bb323542bad9d7a7206d949f33734caf48c40c))
|
| 184 |
|
|
@@ -228,7 +311,7 @@ curl http://localhost:8000/v1/chat/completions \
|
|
| 228 |
}'
|
| 229 |
```
|
| 230 |
|
| 231 |
-
###
|
| 232 |
|
| 233 |
Соберите dev версию на данной ветке - https://github.com/sgl-project/sglang/pull/14765.
|
| 234 |
|
|
|
|
| 178 |
|
| 179 |
## Function call
|
| 180 |
|
| 181 |
+
### 1. `transformers`
|
| 182 |
+
|
| 183 |
+
<details><summary>Click for a dropdown</summary>
|
| 184 |
+
|
| 185 |
+
```python
|
| 186 |
+
import torch
|
| 187 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
|
| 188 |
+
import json
|
| 189 |
+
import re
|
| 190 |
+
REGEX_FUNCTION_CALL_V3 = re.compile(r"function call<\|role_sep\|>\n(.*)$", re.DOTALL)
|
| 191 |
+
REGEX_CONTENT_PATTERN = re.compile(r"^(.*?)<\|message_sep\|>", re.DOTALL)
|
| 192 |
+
def parse_function_and_content(completion_str: str):
|
| 193 |
+
"""
|
| 194 |
+
Using the regexes the user provided, attempt to extract function call and content.
|
| 195 |
+
Returns (function_call_str_or_None, content_str_or_None)
|
| 196 |
+
"""
|
| 197 |
+
|
| 198 |
+
function_call = None
|
| 199 |
+
content = None
|
| 200 |
+
|
| 201 |
+
m_func = REGEX_FUNCTION_CALL_V3.search(completion_str)
|
| 202 |
+
if m_func:
|
| 203 |
+
try:
|
| 204 |
+
function_call = json.loads(m_func.group(1))
|
| 205 |
+
if isinstance(function_call, dict) and "name" in function_call and "arguments" in function_call:
|
| 206 |
+
if not isinstance(function_call["arguments"], dict):
|
| 207 |
+
function_call = None
|
| 208 |
+
else:
|
| 209 |
+
function_call = None
|
| 210 |
+
except json.JSONDecodeError:
|
| 211 |
+
function_call = None
|
| 212 |
+
|
| 213 |
+
# will return raw string in failed attempt of function calling
|
| 214 |
+
return function_call, completion_str
|
| 215 |
+
|
| 216 |
+
m_content = REGEX_CONTENT_PATTERN.search(completion_str)
|
| 217 |
+
if m_content:
|
| 218 |
+
content = m_content.group(1)
|
| 219 |
+
else:
|
| 220 |
+
# as a fallback, everything before the first message_sep marker if present
|
| 221 |
+
if "<|message_sep|>" in completion_str:
|
| 222 |
+
content = completion_str.split("<|message_sep|>")[0]
|
| 223 |
+
else:
|
| 224 |
+
content = completion_str
|
| 225 |
+
|
| 226 |
+
return function_call, content
|
| 227 |
+
|
| 228 |
+
model_name = "ai-sage/GigaChat3-10B-A1.8B"
|
| 229 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 230 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, device_map="auto")
|
| 231 |
+
model.generation_config = GenerationConfig.from_pretrained(model_name)
|
| 232 |
+
tools = [
|
| 233 |
+
{
|
| 234 |
+
"type": "function",
|
| 235 |
+
"function": {
|
| 236 |
+
"name": "get_weather",
|
| 237 |
+
"description": "Получить информацию о текущей погоде в указанном городе.",
|
| 238 |
+
"parameters": {
|
| 239 |
+
"type": "object",
|
| 240 |
+
"properties": {
|
| 241 |
+
"city": {
|
| 242 |
+
"type": "string",
|
| 243 |
+
"description": "Название города (например, Москва, Казань)."
|
| 244 |
+
}
|
| 245 |
+
},
|
| 246 |
+
"required": ["city"]
|
| 247 |
+
}
|
| 248 |
+
}
|
| 249 |
+
}
|
| 250 |
+
]
|
| 251 |
+
messages = [
|
| 252 |
+
{"role": "user", "content": "Какая сейчас погода в Москве?"}
|
| 253 |
+
]
|
| 254 |
+
input_tensor = tokenizer.apply_chat_template(messages, tools=tools, add_generation_prompt=True, return_tensors="pt")
|
| 255 |
+
outputs = model.generate(input_tensor.to(model.device), max_new_tokens=1000)
|
| 256 |
+
|
| 257 |
+
result = parse_function_and_content(tokenizer.decode(outputs[0][input_tensor.shape[1]:], skip_special_tokens=False))[0]
|
| 258 |
+
print(result)
|
| 259 |
+
```
|
| 260 |
+
|
| 261 |
+
</details>
|
| 262 |
+
|
| 263 |
+
|
| 264 |
+
### 2. `vLLM`
|
| 265 |
|
| 266 |
Соберите dev версию, коммит>=[21bb323](https://github.com/vllm-project/vllm/tree/21bb323542bad9d7a7206d949f33734caf48c40c))
|
| 267 |
|
|
|
|
| 311 |
}'
|
| 312 |
```
|
| 313 |
|
| 314 |
+
### 3. `SGLang`
|
| 315 |
|
| 316 |
Соберите dev версию на данной ветке - https://github.com/sgl-project/sglang/pull/14765.
|
| 317 |
|