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

新增Json数据类型 #661

Merged
merged 5 commits into from
Dec 12, 2024
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
8 changes: 7 additions & 1 deletion python/core/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ class FunctionCall(BaseModel, extra='allow'):
name: str = Field(default="", description="工具名")
arguments: dict = Field(default={}, description="参数列表")

class Json(BaseModel, extra='allow'):
data: str = Field(default="", description="json数据")

class Content(BaseModel):
name: str = Field(default="",
Expand All @@ -140,7 +142,7 @@ class Content(BaseModel):
description="耗时、性能、内存等trace及debug所需信息")
type: str = Field(default="text",
description="代表event 类型,包括 text、code、files、urls、oral_text、references、image、chart、audio该字段的取值决定了下面text字段的内容结构")
text: Union[Text, Code, Files, Urls, OralText, References, Image, Chart, Audio, Plan, FunctionCall] = Field(default=Text,
text: Union[Text, Code, Files, Urls, OralText, References, Image, Chart, Audio, Plan, Json, FunctionCall] = Field(default=Text,
description="代表当前 event 元素的内容,每一种 event 对应的 text 结构固定")

@field_validator('text', mode='before')
Expand All @@ -167,6 +169,8 @@ def set_text(cls, v, values, **kwargs):
return Plan(**v)
elif values.data['type'] == 'function_call':
return FunctionCall(**v)
elif values.data['type'] == 'json':
return Json(**v)
else:
raise ValueError(f"Invalid value for 'type': {values['type']}")

Expand Down Expand Up @@ -513,6 +517,8 @@ def create_output(cls, type, text, role="tool", name="", visible_scope="all", ra
text = {"url": text}
elif type == "oral_text":
text = {"info": text}
elif type == "json":
text = {"data": text}
else:
raise ValueError("Only when type=text/code/urls/oral_text, string text is allowed! Please give dict text")
elif isinstance(text, dict):
Expand Down
12 changes: 3 additions & 9 deletions python/tests/test_base_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ def test_valid_output_with_str(self):
out2 = self.component.create_output(type="code", text="import appbuilder")
out3 = self.component.create_output(type="urls", text="http://www.baidu.com")
out4 = self.component.create_output(type="oral_text", text="你是哪个")
out5 = self.component.create_output(type="json", text="{'key':'value'}")
self.assertIsInstance(out1, ComponentOutput)
self.assertIsInstance(out2, ComponentOutput)
self.assertIsInstance(out3, ComponentOutput)
self.assertIsInstance(out4, ComponentOutput)
self.assertIsInstance(out5, ComponentOutput)

def test_valid_output_with_dict(self):
output1 = self.component.create_output(type="text", text={"info": "1"})
Expand Down Expand Up @@ -55,15 +57,7 @@ def test_valid_output_type_with_same_key(self):

def test_invalid_output_type_json(self):
with self.assertRaises(ValueError):
output = self.component.create_output(type="json", text="")
# with self.assertRaises(AssertionError):
# output = self.component.create_output(type="files", text={})
# with self.assertRaises(AssertionError):
# output = self.component.create_output(type="references", text={"info": "text"})
# with self.assertRaises(AssertionError):
# output = self.component.create_output(type="image", text={"url": "https://example.com/img"})
# with self.assertRaises(AssertionError):
# output = self.component.create_output(type="chart", text={"url": "https://example.com/chart"})
output = self.component.create_output(type="test", text="")


if __name__ == '__main__':
Expand Down
Loading