Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

把image和audio的byte字段修改为base64 #758

Merged
merged 2 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions python/core/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class References(BaseModel, extra='allow'):
class Image(BaseModel, extra='allow'):
filename: str = Field(default="", description="图片名称")
url: str = Field(default="", description="图片url")
byte: Optional[bytes] = Field(default=b'', description="图片二进制数据")
base64: Optional[str] = Field(default="", description="图片base64数据")


class Chart(BaseModel, extra='allow'):
Expand All @@ -103,7 +103,7 @@ class Chart(BaseModel, extra='allow'):
class Audio(BaseModel, extra='allow'):
filename: str = Field(default="", description="音频名称")
url: str = Field(default="", description="音频url")
byte: Optional[bytes] = Field(default=b'', description="音频二进制数据")
base64: Optional[str] = Field(default="", description="音频base64数据")


class PlanStep(BaseModel, extra='allow'):
Expand Down
32 changes: 16 additions & 16 deletions python/modelcontextprotocol/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,19 @@ def _convert_image_to_image_content(
) -> ImageContent:
"""convert base64 data, such as image/audio to ImageContent"""
try:
if text.byte:
if text.base64:
logging.info("create ImageContent from Image.byte")
base64_data = base64.b64encode(text.byte).decode("utf-8")
mime_type = self._get_mimetype_from_bytes(text.byte)
base64_data = text.base64
image_byte = io.BytesIO(base64.b64decode(base64_data))
else:
logging.info("create ImageContent from Image.url")
response = requests.get(text.url)
response.raise_for_status()
image = response.content
base64_data = base64.b64encode(image).decode('utf-8')

image_byte = io.BytesIO(image)
mime_type = self._get_mimetype_from_bytes(image_byte)

mime_type = self._get_mimetype_from_bytes(image_byte)

# create ImageContent
return ImageContent(
Expand All @@ -163,21 +163,21 @@ def _convert_audio_to_embedded_resource(
) -> EmbeddedResource:
"""convert audio to EmebeddedResource"""
try:
if text.byte:
if text.base64:
logging.info("convert audio to EmbeddedResource from Audio.byte")
base64_data = base64.b64encode(text.byte).decode("utf-8")
audio_type = self._get_mimetype_from_bytes(text.byte)

base64_data = text.base64
audio_byte = io.BytesIO(base64.b64decode(base64_data))
else:
logging.info("convert audio to EmbeddedResource from Audio.url")
# get data
response = requests.get(text.url)
response.raise_for_status()
# convert to base64
base64_data = base64.b64encode(response.content).decode('utf-8')
# detect audio type
audio_byte = io.BytesIO(response.content)
audio_type = self._get_mimetype_from_bytes(audio_byte)

# detect audio type
audio_type = self._get_mimetype_from_bytes(audio_byte)

# create EmbeddedResource
return EmbeddedResource(
Expand Down Expand Up @@ -268,7 +268,7 @@ def _convert_generator(
return output


def add_component(self, component: Component) -> None:
def convert_component_to_tool(self, component: Component) -> None:
"""
Add an Appbuilder Component and register its tools under the component's URL namespace.

Expand Down Expand Up @@ -309,7 +309,7 @@ def wrapper(*args, **kwargs) -> Any:
# Register with FastMCP using name and description from manifest
self.mcp.tool(name=tool_name, description=tool_decription)(tool_fn)

def add_appbuilder_official_tool(
def add_component(
self,
component_cls: Component,
init_args: dict[str, Any] = {},
Expand All @@ -320,7 +320,7 @@ def add_appbuilder_official_tool(

try:
component = component_cls(**init_args)
self.add_component(component)
self.convert_component_to_tool(component)
logging.info(f"component: {component_name} has been added")

except Exception as e:
Expand All @@ -346,8 +346,8 @@ def run(self, transport: Literal["stdio", "sse"] = "stdio") -> None:
"model": "ERNIE-4.0-8K",
"secret_key": 'bce-v3/ALTAK-RPJR9XSOVFl6mb5GxHbfU/072be74731e368d8bbb628a8941ec50aaeba01cd'
}
server.add_appbuilder_official_tool(Translation, init_args)
server.add_appbuilder_official_tool(Text2Image, init_args)
server.add_component(Translation, init_args)
server.add_component(Text2Image, init_args)

# Add custom tool
@server.tool()
Expand Down
8 changes: 4 additions & 4 deletions python/tests/component_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@
"url": {
"type": "string"
},
"byte": {
"format": "bytes"
"base64": {
"type": "string"
}
},
"required": ["filename", "url"]
Expand Down Expand Up @@ -223,8 +223,8 @@
"url": {
"type": "string"
},
"byte": {
"format": "bytes"
"base64": {
"type": "string"
}
},
"required": ["filename", "url"]
Expand Down