Skip to content
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

支持输入dict创建json输出 #724

Closed
wants to merge 3 commits into from
Closed
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
16 changes: 3 additions & 13 deletions python/core/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from appbuilder.core.utils import ttl_lru_cache
from appbuilder.core._client import HTTPClient, AsyncHTTPClient
from appbuilder.core.message import Message
from urllib.parse import urlparse, unquote

class ComponentArguments(BaseModel):
"""
Expand Down Expand Up @@ -560,6 +559,8 @@ def create_output(cls, type, text, role="tool", name="", visible_scope="all", ra
key_list = ["detail", "steps"]
elif type == "function_call":
key_list = ["thought", "name", "arguments"]
elif type == "json":
key_list = ["data"]
else:
raise ValueError("Unknown type: {}".format(type))
assert all(key in text for key in key_list), "all keys:{} must be included in the text field".format(key_list)
Expand All @@ -580,15 +581,4 @@ def create_output(cls, type, text, role="tool", name="", visible_scope="all", ra
"metrics": metrics
}]
}
return ComponentOutput(**result)

@staticmethod
def get_filename_from_url(url):
"""从给定URL中提取文件名"""
parsed_url = urlparse(url)
# 提取路径部分
path = parsed_url.path
# 从路径中获取文件名
filename = path.split('/')[-1]
# 解码URL编码的文件名
return unquote(filename)
return ComponentOutput(**result)
3 changes: 2 additions & 1 deletion python/core/components/v2/text_to_image/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from appbuilder.core.component import Component
from appbuilder.core.message import Message
from appbuilder.core._client import HTTPClient
from appbuilder.core.utils import get_filename_from_url
from appbuilder.core._exception import AppBuilderServerException, RiskInputException
from appbuilder.core.components.text_to_image.model import Text2ImageSubmitRequest, Text2ImageQueryRequest, \
Text2ImageQueryResponse, Text2ImageSubmitResponse, Text2ImageOutMessage
Expand Down Expand Up @@ -197,7 +198,7 @@ def tool_eval(
yield self.create_output(
type='image',
text={
'filename': self.get_filename_from_url(img_urls[url_number]),
'filename': get_filename_from_url(img_urls[url_number]),
'url': img_urls[url_number],
},
raw_data=raw_data,
Expand Down
14 changes: 2 additions & 12 deletions python/core/components/v2/tree_mind/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from appbuilder.core.message import Message
from appbuilder.core._client import HTTPClient
from appbuilder.core._exception import *
from appbuilder.core.utils import get_filename_from_url
from appbuilder.utils.trace.tracer_wrapper import components_run_trace, components_run_stream_trace
from appbuilder.core.components.v2.tree_mind.model import TreeMindRequest, TreeMindResponse

Expand Down Expand Up @@ -84,17 +85,6 @@ def _post(self, query, **kwargs):
img_link = treemind_response.info.downloadInfo.fileInfo.pic
return img_link, jump_link

@staticmethod
def get_filename_from_url(url):
"""从给定URL中提取文件名"""
parsed_url = urlparse(url)
# 提取路径部分
path = parsed_url.path
# 从路径中获取文件名
filename = path.split('/')[-1]
# 解码URL编码的文件名
return unquote(filename)

@components_run_stream_trace
def tool_eval(
self,
Expand Down Expand Up @@ -127,7 +117,7 @@ def tool_eval(
img_link_result = self.create_output(
type="image",
text={
"filename": self.get_filename_from_url(img_link),
"filename": get_filename_from_url(img_link),
"url": img_link
},
visible_scope='all',
Expand Down
11 changes: 10 additions & 1 deletion python/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import time
import itertools
from typing import List
from urllib.parse import urlparse
from urllib.parse import urlparse, unquote
from appbuilder.core._client import HTTPClient
from appbuilder.core._exception import TypeNotSupportedException, ModelNotSupportedException
from appbuilder.utils.model_util import GetModelListRequest, Models, RemoteModelCollector
Expand Down Expand Up @@ -57,6 +57,15 @@ def get_model_list(secret_key: str = "", api_type_filter: List[str] = [], is_ava
models.append(model.name)
return models

def get_filename_from_url(url):
"""从给定URL中提取文件名"""
parsed_url = urlparse(url)
# 提取路径部分
path = parsed_url.path
# 从路径中获取文件名
filename = path.split('/')[-1]
# 解码URL编码的文件名
return unquote(filename)

def convert_cloudhub_url(client: HTTPClient, qianfan_url: str) -> str:
"""将千帆url转换为AppBuilder url"""
Expand Down
2 changes: 2 additions & 0 deletions python/tests/test_base_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def test_valid_output_with_dict(self):
output9 = self.component.create_output(type="plan", text={"detail": "hello", "steps":[{"name": "1", "arguments": {"query": "a", "chat_history": "world"}}]})
output10 = self.component.create_output(type="function_call", text={"thought": "hello", "name": "AppBuilder", "arguments": {"query": "a", "chat_history": "world"}})
output11 = self.component.create_output(type="references", text={"type": "engine", "doc_id": "1", "content": "hello, world", "title": "Have a nice day", "source": "bing", "extra": {"key": "value"}})
output12 = self.component.create_output(type="json", text={"key": "value"})
self.assertIsInstance(output1, ComponentOutput)
self.assertIsInstance(output2, ComponentOutput)
self.assertIsInstance(output3, ComponentOutput)
Expand All @@ -44,6 +45,7 @@ def test_valid_output_with_dict(self):
self.assertIsInstance(output9, ComponentOutput)
self.assertIsInstance(output10, ComponentOutput)
self.assertIsInstance(output11, ComponentOutput)
self.assertIsInstance(output12, ComponentOutput)
self.assertEqual(output11.content[0].text.extra["key"], "value")

def test_valid_output_type_with_same_key(self):
Expand Down
Loading