|
7 | 7 | import inspect |
8 | 8 | from typing import Any, Callable, Literal, Union, get_args, get_origin |
9 | 9 |
|
10 | | -__version__ = "0.2.3" |
| 10 | +__version__ = "0.2.4" |
11 | 11 |
|
12 | 12 | import requests |
13 | 13 | from packaging import version |
@@ -395,14 +395,17 @@ def handle_tool_call(tool_call:dict) -> tuple[str, str, dict, str] : |
395 | 395 | def functions_to_tools(funcs: list[Callable]) -> list[dict[str, Any]]: |
396 | 396 | return [utils.function_to_openai_tool(f) for f in funcs] |
397 | 397 |
|
398 | | -def handle_thinking(token, is_thinking): |
399 | | - if "<think>" in token or is_thinking : |
400 | | - is_thinking=True |
| 398 | +def handle_thinking(TOKEN, is_thinking): |
| 399 | + token, CoT = TOKEN, None |
401 | 400 |
|
402 | | - if "</think>" in token : |
403 | | - is_thinking=False |
| 401 | + if "<think>" in TOKEN or is_thinking : |
| 402 | + token, CoT = None, TOKEN |
| 403 | + is_thinking = True |
| 404 | + |
| 405 | + if "</think>" in TOKEN : |
| 406 | + is_thinking = False |
404 | 407 |
|
405 | | - return is_thinking |
| 408 | + return is_thinking, token, CoT |
406 | 409 |
|
407 | 410 | def remove_thinks(message:str): |
408 | 411 | assert type(message) == str |
@@ -500,9 +503,6 @@ def manage_assistant_response(self, response): |
500 | 503 | def manage_messages_in_reply(self): |
501 | 504 | """ |
502 | 505 | Function to manage message history, executed at each step (after agent response or tool call) |
503 | | -
|
504 | | -
|
505 | | - ``` |
506 | 506 | """ |
507 | 507 | pass |
508 | 508 |
|
@@ -547,25 +547,21 @@ def __call__(self, prompt): |
547 | 547 | response = "" |
548 | 548 | reasoning = "" |
549 | 549 | for token, tool_calls, run in handle_streaming(self.create_stream()) : |
550 | | - if token : |
551 | | - |
552 | | - if "<think>" in token or is_thinking : |
553 | | - is_thinking=True |
554 | | - |
555 | | - if "</think>" in token : |
556 | | - is_thinking=False |
557 | | - |
558 | | - if self.meta["is_thinking_enabled"] : |
559 | | - reasoning += token |
560 | | - else : |
561 | | - response += token |
562 | | - |
563 | | - if self.meta["yield_thinking"]: |
564 | | - yield self.manage_token_yield(token, is_thinking) |
565 | | - |
| 550 | + is_thinking, token, CoT = handle_thinking(token, is_thinking) |
| 551 | + |
| 552 | + if is_thinking: |
| 553 | + if self.meta["is_thinking_enabled"]: |
| 554 | + reasoning += CoT |
566 | 555 | else : |
567 | | - yield self.manage_token_yield(token, is_thinking) |
568 | | - response += token |
| 556 | + response += CoT |
| 557 | + |
| 558 | + if self.meta["yield_thinking"]: |
| 559 | + yield self.manage_token_yield(token, is_thinking=True) |
| 560 | + |
| 561 | + else : |
| 562 | + yield self.manage_token_yield(token, is_thinking=False) |
| 563 | + if token : response += token |
| 564 | + |
569 | 565 |
|
570 | 566 | if run: |
571 | 567 |
|
|
0 commit comments