| 396 | } |
| 397 | |
| 398 | void GPT_OSS::update_state(int token_id) { |
| 399 | std::string token_str = tokenizer->decode({ token_id }); |
| 400 | accumulated_text += token_str; |
| 401 | |
| 402 | switch (current_state) { |
| 403 | case STATE_EXPECT_TO_FUNCTIONS: { |
| 404 | if (accumulated_text.find("to=functions.") != std::string::npos) { |
| 405 | current_state = STATE_EXPECT_FUNCTION_NAME; |
| 406 | accumulated_text.clear(); |
| 407 | header_print("TOOLCALLING", "Completed 'to=functions.', expecting function name"); |
| 408 | } |
| 409 | break; |
| 410 | } |
| 411 | case STATE_EXPECT_FUNCTION_NAME: { |
| 412 | //bool found = false; |
| 413 | for (const auto& tool : tools) { |
| 414 | std::string name = tool["function"]["name"]; |
| 415 | if (accumulated_text == name) { |
| 416 | current_state = STATE_EXPECT_CONSTRAIN; |
| 417 | accumulated_text.clear(); |
| 418 | //found = true; |
| 419 | header_print("TOOLCALLING", "Matched function: " + name); |
| 420 | break; |
| 421 | } |
| 422 | } |
| 423 | break; |
| 424 | } |
| 425 | case STATE_EXPECT_CONSTRAIN: { |
| 426 | if (token_str.find("json") != std::string::npos) { |
| 427 | current_state = STATE_EXPECT_MESSAGE; |
| 428 | accumulated_text.clear(); |
| 429 | header_print("TOOLCALLING", "Starting MESSAGE generation"); |
| 430 | } |
| 431 | break; |
| 432 | } |
| 433 | case STATE_EXPECT_MESSAGE: { |
| 434 | if (token_str.find("<|message|>") != std::string::npos) { |
| 435 | current_state = STATE_COMPLETE; |
| 436 | accumulated_text.clear(); |
| 437 | } |
| 438 | break; |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | void GPT_OSS::mask_logits(buffer<bf16>& logits, const std::vector<int>& allowed_tokens) { |
| 444 | const float NEG_INF = -std::numeric_limits<float>::infinity(); |