-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor!: return every message in full_round; richer returns in func…
…tion call methods
- Loading branch information
Showing
9 changed files
with
129 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from . import engines, exceptions, utils | ||
from .ai_function import AIFunction, AIParam, ai_function | ||
from .internal import ExceptionHandleResult, FunctionCallResult | ||
from .kani import Kani | ||
from .models import ChatMessage, ChatRole, FunctionCall | ||
from .utils.cli import chat_in_terminal, chat_in_terminal_async |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from .models import ChatMessage | ||
|
||
|
||
class FunctionCallResult: | ||
"""A model requested a function call, and the kani runtime resolved it.""" | ||
|
||
def __init__(self, is_model_turn: bool, message: ChatMessage): | ||
""" | ||
:param is_model_turn: True if the model should immediately react; False if the user speaks next. | ||
:param message: The message containing the result of the function call, to add to the chat history. | ||
""" | ||
self.is_model_turn = is_model_turn | ||
self.message = message | ||
|
||
|
||
class ExceptionHandleResult: | ||
"""A function call raised an exception, and the kani runtime has prompted the model with exception information.""" | ||
|
||
def __init__(self, should_retry: bool, message: ChatMessage): | ||
""" | ||
:param should_retry: Whether the model should be allowed to retry the call that caused this exception. | ||
:param message: The message containing details about the exception and/or instructions to retry, to add to the | ||
chat history. | ||
""" | ||
self.should_retry = should_retry | ||
self.message = message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
""" | ||
A couple convenience formatters to customize :meth:`.Kani.full_round_str`. | ||
You can pass any of these functions in with, e.g., ``Kani.full_round_str(..., message_formatter=all_message_contents)``. | ||
""" | ||
from kani.models import ChatMessage, ChatRole | ||
|
||
|
||
def all_message_contents(msg: ChatMessage): | ||
"""Return the content of any message.""" | ||
return msg.content | ||
|
||
|
||
def assistant_message_contents(msg: ChatMessage): | ||
"""Return the content of any assistant message; otherwise don't return anything.""" | ||
if msg.role == ChatRole.ASSISTANT: | ||
return msg.content | ||
|
||
|
||
def assistant_message_contents_thinking(msg: ChatMessage): | ||
"""Return the content of any assistant message, and "Thinking..." on function calls.""" | ||
if msg.role == ChatRole.ASSISTANT: | ||
content = msg.content | ||
if msg.function_call and content: | ||
return f"{content}\n Thinking ({msg.function_call.name})..." | ||
elif msg.function_call: | ||
return f"Thinking ({msg.function_call.name})..." | ||
return content |