-
Notifications
You must be signed in to change notification settings - Fork 47
feat: ROS2 High level generic API #360
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
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
497308b
feat: impl minimal BaseMessage
maciejmajek a6d337c
feat: implement ros_connector
maciejmajek 035b472
feat: implement http_connector
maciejmajek 0775994
test: ros_connector
maciejmajek 4db7e1b
test: split test_connector_cleanup into two tests
maciejmajek 8f82fcf
feat: allow custom qos profile
maciejmajek 269fb1c
feat: add destroy_publisher method
maciejmajek 4f76da4
refactor(test_ros_connector): use topic names based on test name, mov…
maciejmajek d7f8c60
refactor(test_transport): use function name based topics, move ROS 2 …
maciejmajek 503ac64
refactor: HRI and RRI connectors
maciejmajek 4c238e5
fix: string messages
maciejmajek 17dde2b
chore: move ros2 apis to communication/ros folder
maciejmajek 66c1494
feat: implement ROS2TopicAPI
maciejmajek 581aef3
feat: implement ROS2ServiceAPI
maciejmajek 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
This file was deleted.
Oops, something went wrong.
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,110 @@ | ||
# Copyright (C) 2024 Robotec.AI | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from abc import ABC, abstractmethod | ||
from typing import Annotated, List, Literal, Optional, Sequence | ||
|
||
from langchain_core.messages import AIMessage | ||
from langchain_core.messages import BaseMessage as LangchainBaseMessage | ||
from langchain_core.messages import HumanMessage | ||
|
||
from rai.messages import AiMultimodalMessage, HumanMultimodalMessage | ||
from rai.messages.multimodal import MultimodalMessage as RAIMultimodalMessage | ||
|
||
|
||
class HRIMessage: | ||
type: Literal["ai", "human"] | ||
text: str | ||
images: Optional[Annotated[List[str], "base64 encoded png images"]] | ||
audios: Optional[Annotated[List[str], "base64 encoded wav audio"]] | ||
|
||
def __repr__(self): | ||
return f"HRIMessage(type={self.type}, text={self.text}, images={self.images}, audios={self.audios})" | ||
|
||
def __init__( | ||
self, | ||
type: Literal["ai", "human"], | ||
text: str, | ||
images: Optional[List[str]] = None, | ||
audios: Optional[List[str]] = None, | ||
): | ||
self.type = type | ||
self.text = text | ||
self.images = images | ||
self.audios = audios | ||
|
||
def to_langchain(self) -> LangchainBaseMessage: | ||
match self.type: | ||
case "human": | ||
if self.images is None and self.audios is None: | ||
return HumanMessage(content=self.text) | ||
return HumanMultimodalMessage( | ||
content=self.text, images=self.images, audios=self.audios | ||
) | ||
case "ai": | ||
if self.images is None and self.audios is None: | ||
return AIMessage(content=self.text) | ||
return AiMultimodalMessage( | ||
content=self.text, images=self.images, audios=self.audios | ||
) | ||
case _: | ||
raise ValueError( | ||
f"Invalid message type: {self.type} for {self.__class__.__name__}" | ||
) | ||
|
||
@classmethod | ||
def from_langchain( | ||
cls, | ||
message: LangchainBaseMessage | RAIMultimodalMessage, | ||
) -> "HRIMessage": | ||
if isinstance(message, RAIMultimodalMessage): | ||
text = str(message.content["text"]) | ||
images = message.images | ||
audios = message.audios | ||
else: | ||
text = str(message.content) | ||
images = None | ||
audios = None | ||
if message.type not in ["ai", "human"]: | ||
raise ValueError(f"Invalid message type: {message.type} for {cls.__name__}") | ||
return cls( | ||
type=message.type, # type: ignore | ||
text=text, | ||
images=images, | ||
audios=audios, | ||
) | ||
|
||
|
||
class HRIConnector(ABC): | ||
""" | ||
Base class for Human-Robot Interaction (HRI) connectors. | ||
Used for sending and receiving messages between human and robot from various sources. | ||
""" | ||
|
||
configured_targets: Sequence[str] | ||
configured_sources: Sequence[str] | ||
|
||
def build_message( | ||
self, | ||
message: LangchainBaseMessage | RAIMultimodalMessage, | ||
) -> HRIMessage: | ||
return HRIMessage.from_langchain(message) | ||
|
||
@abstractmethod | ||
def send_message(self, message: LangchainBaseMessage | RAIMultimodalMessage): | ||
pass | ||
|
||
@abstractmethod | ||
def receive_message(self) -> LangchainBaseMessage | RAIMultimodalMessage: | ||
pass |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.