| 327 | } |
| 328 | |
| 329 | int GPT_OSS::_sample_in_tool(buffer<bf16>& logits) { |
| 330 | // to=functions.get_current_weather <|constrain|>json<|message|>{"location":"San Francisco"}<|call|> |
| 331 | std::vector<int> allowed_tokens; |
| 332 | switch (current_state) { |
| 333 | case STATE_EXPECT_TO_FUNCTIONS: { |
| 334 | allowed_tokens = get_to_functions_tokens(); |
| 335 | break; |
| 336 | } |
| 337 | case STATE_EXPECT_FUNCTION_NAME: { |
| 338 | allowed_tokens = get_function_name_tokens(); |
| 339 | break; |
| 340 | } |
| 341 | case STATE_EXPECT_CONSTRAIN: { |
| 342 | auto constrain_tokens = tokenizer->encode(" <|constrain|>"); |
| 343 | auto message_tokens = tokenizer->encode("json"); |
| 344 | allowed_tokens.insert(allowed_tokens.end(), |
| 345 | constrain_tokens.begin(), constrain_tokens.end()); |
| 346 | allowed_tokens.insert(allowed_tokens.end(), |
| 347 | message_tokens.begin(), message_tokens.end()); |
| 348 | break; |
| 349 | } |
| 350 | case STATE_EXPECT_MESSAGE: { |
| 351 | auto tokens = tokenizer->encode("<|message|>"); |
| 352 | allowed_tokens = { tokens[0] }; |
| 353 | break; |
| 354 | } |
| 355 | //case STATE_IN_JSON: { |
| 356 | // header_print("JSONSSS", "SSSS"); |
| 357 | // return this->sampler->sample(logits); |
| 358 | // //allowed_tokens = constrain_by_json_schema(logits); |
| 359 | // //break; |
| 360 | //} |
| 361 | } |
| 362 | mask_logits(logits, allowed_tokens); |
| 363 | int token_id = this->sampler->sample(logits); |
| 364 | update_state(token_id); |
| 365 | return token_id; |
| 366 | //return this->sampler->sample(logits); |
| 367 | } |
| 368 | |
| 369 | std::vector<int> GPT_OSS::get_to_functions_tokens() { |
| 370 | const std::string target = " to=functions."; |