-
Notifications
You must be signed in to change notification settings - Fork 1
Added chat and chat/health handler registration, dispatch, and testing #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,7 +1,17 @@ | ||
| from ..shared.mued_api_v0_1_0 import ChatCapabilities | ||
| from ..shared.mued_api_v0_1_0 import ChatHealthResponse | ||
| from ..shared.mued_api_v0_1_0 import ChatRequest | ||
| from ..shared.mued_api_v0_1_0 import ChatResponse | ||
| from ..shared.mued_api_v0_1_0 import Message | ||
| from .params import ChatParams | ||
| from .result import ChatResult | ||
|
|
||
| __all__ = ["ChatRequest", "ChatResponse", "Message", "ChatParams", "ChatResult"] | ||
| __all__ = [ | ||
| "ChatRequest", | ||
| "ChatResponse", | ||
| "ChatCapabilities", | ||
| "ChatHealthResponse", | ||
| "Message", | ||
| "ChatParams", | ||
| "ChatResult", | ||
| ] |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,4 +1,4 @@ | ||
| from typing import Literal | ||
|
|
||
|
|
||
| Command = Literal["eval", "preview"] | ||
| Command = Literal["eval", "preview", "chat", "chat/health"] |
This file contains hidden or 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,30 @@ | ||
| from lf_toolkit.chat import ChatHealthResponse | ||
| from lf_toolkit.chat import ChatRequest | ||
| from lf_toolkit.chat import ChatResponse | ||
| from lf_toolkit.io.base_server import BaseServer | ||
|
|
||
|
|
||
| class ConcreteServer(BaseServer): | ||
| async def run(self): | ||
| pass | ||
|
|
||
|
|
||
| class TestBaseServerChatRegistration: | ||
|
|
||
| def test_chat_registers_under_chat_name(self): | ||
| server = ConcreteServer() | ||
|
|
||
| @server.chat | ||
| def chat_fn(request: ChatRequest) -> ChatResponse: | ||
| raise NotImplementedError | ||
|
|
||
| assert server._handler._handlers["chat"] is chat_fn | ||
|
|
||
| def test_chat_health_registers_under_chat_slash_health_name(self): | ||
| server = ConcreteServer() | ||
|
|
||
| @server.chat_health | ||
| def chat_health_fn() -> ChatHealthResponse: | ||
| raise NotImplementedError | ||
|
|
||
| assert server._handler._handlers["chat/health"] is chat_health_fn |
This file contains hidden or 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,130 @@ | ||
| import pytest | ||
|
|
||
| from lf_toolkit.chat import ChatCapabilities | ||
| from lf_toolkit.chat import ChatHealthResponse | ||
| from lf_toolkit.chat import ChatRequest | ||
| from lf_toolkit.chat import ChatResponse | ||
| from lf_toolkit.chat import Message | ||
| from lf_toolkit.io.file_server import FileHandler | ||
| from lf_toolkit.shared.mued_api_v0_1_0 import DataPolicySupport | ||
| from lf_toolkit.shared.mued_api_v0_1_0 import HealthStatus | ||
| from lf_toolkit.shared.mued_api_v0_1_0 import Role | ||
|
|
||
| pytest_plugins = ('pytest_asyncio',) | ||
|
|
||
|
|
||
| class TestHandleChat: | ||
|
|
||
| @pytest.fixture | ||
| def handler(self): | ||
| return FileHandler() | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_calls_registered_handler_with_chat_request(self, handler): | ||
| received = {} | ||
|
|
||
| def chat_fn(request: ChatRequest) -> ChatResponse: | ||
| received["request"] = request | ||
| return ChatResponse(output=Message(role=Role.ASSISTANT, content="hi there")) | ||
|
|
||
| handler.register("chat", chat_fn) | ||
|
|
||
| result = await handler.handle_chat({ | ||
| "params": {"messages": [{"role": "USER", "content": "hello"}]} | ||
| }) | ||
|
|
||
| assert isinstance(received["request"], ChatRequest) | ||
| assert received["request"].messages[0].content == "hello" | ||
| assert result == {"output": {"role": "ASSISTANT", "content": "hi there"}} | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_passes_through_non_chat_response_result(self, handler): | ||
| handler.register("chat", lambda request: {"output": {"role": "ASSISTANT", "content": "raw"}}) | ||
|
|
||
| result = await handler.handle_chat({ | ||
| "params": {"messages": [{"role": "USER", "content": "hello"}]} | ||
| }) | ||
|
|
||
| assert result == {"output": {"role": "ASSISTANT", "content": "raw"}} | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_raises_when_no_handler_registered(self, handler): | ||
| with pytest.raises(ValueError, match="No user handler for 'chat'"): | ||
| await handler.handle_chat({ | ||
| "params": {"messages": [{"role": "USER", "content": "hello"}]} | ||
| }) | ||
|
|
||
|
|
||
| class TestHandleChatHealth: | ||
|
|
||
| @pytest.fixture | ||
| def handler(self): | ||
| return FileHandler() | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_calls_registered_handler_with_no_arguments(self, handler): | ||
| def chat_health_fn() -> ChatHealthResponse: | ||
| return ChatHealthResponse( | ||
| status=HealthStatus.OK, | ||
| capabilities=ChatCapabilities( | ||
| supportsChat=True, | ||
| supportsDataPolicy=DataPolicySupport.NOT_SUPPORTED, | ||
| ), | ||
| ) | ||
|
|
||
| handler.register("chat/health", chat_health_fn) | ||
|
|
||
| result = await handler.handle_chat_health({"params": {}}) | ||
|
|
||
| assert result == { | ||
| "status": "OK", | ||
| "capabilities": {"supportsChat": True, "supportsDataPolicy": "NOT_SUPPORTED"}, | ||
| } | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_passes_through_non_chat_health_response_result(self, handler): | ||
| handler.register("chat/health", lambda: {"status": "OK"}) | ||
|
|
||
| result = await handler.handle_chat_health({"params": {}}) | ||
|
|
||
| assert result == {"status": "OK"} | ||
|
|
||
|
|
||
| class TestHandleDispatch: | ||
| """Covers the name -> method lookup in Handler.handle, including the | ||
| 'chat/health' slash normalisation.""" | ||
|
|
||
| @pytest.fixture | ||
| def handler(self): | ||
| return FileHandler() | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_dispatches_chat_to_handle_chat(self, handler): | ||
| handler.register("chat", lambda request: ChatResponse( | ||
| output=Message(role=Role.ASSISTANT, content="ok") | ||
| )) | ||
|
|
||
| result = await handler.handle("chat", { | ||
| "params": {"messages": [{"role": "USER", "content": "hi"}]} | ||
| }) | ||
|
|
||
| assert result == {"output": {"role": "ASSISTANT", "content": "ok"}} | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_dispatches_chat_slash_health_to_handle_chat_health(self, handler): | ||
| handler.register("chat/health", lambda: ChatHealthResponse( | ||
| status=HealthStatus.OK, | ||
| capabilities=ChatCapabilities( | ||
| supportsChat=True, | ||
| supportsDataPolicy=DataPolicySupport.NOT_SUPPORTED, | ||
| ), | ||
| )) | ||
|
|
||
| result = await handler.handle("chat/health", {"params": {}}) | ||
|
|
||
| assert result["status"] == "OK" | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_unknown_command_raises(self, handler): | ||
| with pytest.raises(ValueError, match="No handler for 'unknown'"): | ||
| await handler.handle("unknown", {"params": {}}) |
This file contains hidden or 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,71 @@ | ||
| import ujson | ||
| import pytest | ||
|
|
||
| from lf_toolkit.chat import ChatCapabilities | ||
| from lf_toolkit.chat import ChatHealthResponse | ||
| from lf_toolkit.chat import ChatRequest | ||
| from lf_toolkit.chat import ChatResponse | ||
| from lf_toolkit.chat import Message | ||
| from lf_toolkit.io.rpc_handler import JsonRpcHandler | ||
| from lf_toolkit.shared.mued_api_v0_1_0 import DataPolicySupport | ||
| from lf_toolkit.shared.mued_api_v0_1_0 import HealthStatus | ||
| from lf_toolkit.shared.mued_api_v0_1_0 import Role | ||
|
|
||
| pytest_plugins = ('pytest_asyncio',) | ||
|
|
||
|
|
||
| class TestJsonRpcHandlerChat: | ||
|
|
||
| @pytest.fixture | ||
| def handler(self): | ||
| return JsonRpcHandler() | ||
|
|
||
| def test_registers_chat_methods(self, handler): | ||
| assert "chat" in handler._methods | ||
| assert "chat/health" in handler._methods | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_dispatch_chat_round_trip(self, handler): | ||
| def chat_fn(request: ChatRequest) -> ChatResponse: | ||
| last = request.messages[-1] | ||
| return ChatResponse(output=Message(role=Role.ASSISTANT, content=f"echo: {last.content}")) | ||
|
|
||
| handler.register("chat", chat_fn) | ||
|
|
||
| # go-ethereum's rpc.Client sends a single positional params array, | ||
| # not a params object -- this is the actual wire shape shimmy produces. | ||
| req = ujson.dumps({ | ||
| "jsonrpc": "2.0", | ||
| "method": "chat", | ||
| "params": [{"messages": [{"role": "USER", "content": "hi"}]}], | ||
| "id": 1, | ||
| }) | ||
|
|
||
| response = ujson.loads(await handler.dispatch(req)) | ||
|
|
||
| assert response["result"] == {"output": {"role": "ASSISTANT", "content": "echo: hi"}} | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_dispatch_chat_health_round_trip(self, handler): | ||
| def chat_health_fn() -> ChatHealthResponse: | ||
| return ChatHealthResponse( | ||
| status=HealthStatus.OK, | ||
| capabilities=ChatCapabilities( | ||
| supportsChat=True, | ||
| supportsDataPolicy=DataPolicySupport.NOT_SUPPORTED, | ||
| ), | ||
| ) | ||
|
|
||
| handler.register("chat/health", chat_health_fn) | ||
|
|
||
| req = ujson.dumps({ | ||
| "jsonrpc": "2.0", | ||
| "method": "chat/health", | ||
| "params": [{}], | ||
| "id": 2, | ||
| }) | ||
|
|
||
| response = ujson.loads(await handler.dispatch(req)) | ||
|
|
||
| assert response["result"]["status"] == "OK" | ||
| assert response["result"]["capabilities"]["supportsChat"] is True |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so this was for legacy evaluate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's also for muEd, shimmy works as a protocol facade, it converts muEd into what the evaluation function expects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so then would it be worth using the mued naming for consistency?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Switching the term here would most likely break the legacy implementation. Currently, the shimmy facade takes muEd and basically converts to legacy, as this was a simpler implementation and no additional muEd functionality is currently required.