Kevin Hu
commited on
Commit
·
481246c
1
Parent(s):
76468f0
Fixs for translation agent (#3557)
Browse files### What problem does this PR solve?
#3556
### Type of change
- [x] Bug Fix (non-breaking change which fixes an issue)
- [x] New Feature (non-breaking change which adds functionality)
- agent/component/base.py +7 -3
- agent/component/template.py +85 -0
- api/apps/canvas_app.py +4 -1
- api/apps/user_app.py +1 -0
agent/component/base.py
CHANGED
@@ -385,10 +385,14 @@ class ComponentBase(ABC):
|
|
385 |
"""
|
386 |
return """{{
|
387 |
"component_name": "{}",
|
388 |
-
"params": {}
|
|
|
|
|
389 |
}}""".format(self.component_name,
|
390 |
-
self._param
|
391 |
-
)
|
|
|
|
|
392 |
|
393 |
def __init__(self, canvas, id, param: ComponentParamBase):
|
394 |
self._canvas = canvas
|
|
|
385 |
"""
|
386 |
return """{{
|
387 |
"component_name": "{}",
|
388 |
+
"params": {},
|
389 |
+
"output": {},
|
390 |
+
"inputs": {}
|
391 |
}}""".format(self.component_name,
|
392 |
+
self._param,
|
393 |
+
json.dumps(json.loads(str(self._param))["output"], ensure_ascii=False),
|
394 |
+
json.dumps(json.loads(str(self._param))["inputs"], ensure_ascii=False)
|
395 |
+
)
|
396 |
|
397 |
def __init__(self, canvas, id, param: ComponentParamBase):
|
398 |
self._canvas = canvas
|
agent/component/template.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#
|
2 |
+
# Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
#
|
16 |
+
import re
|
17 |
+
from agent.component.base import ComponentBase, ComponentParamBase
|
18 |
+
|
19 |
+
|
20 |
+
class TemplateParam(ComponentParamBase):
|
21 |
+
"""
|
22 |
+
Define the Generate component parameters.
|
23 |
+
"""
|
24 |
+
|
25 |
+
def __init__(self):
|
26 |
+
super().__init__()
|
27 |
+
self.content = ""
|
28 |
+
self.parameters = []
|
29 |
+
|
30 |
+
def check(self):
|
31 |
+
self.check_empty(self.content, "[Template] Content")
|
32 |
+
return True
|
33 |
+
|
34 |
+
|
35 |
+
class Template(ComponentBase):
|
36 |
+
component_name = "Template"
|
37 |
+
|
38 |
+
def get_dependent_components(self):
|
39 |
+
cpnts = set([para["component_id"].split("@")[0] for para in self._param.parameters \
|
40 |
+
if para.get("component_id") \
|
41 |
+
and para["component_id"].lower().find("answer") < 0 \
|
42 |
+
and para["component_id"].lower().find("begin") < 0])
|
43 |
+
return list(cpnts)
|
44 |
+
|
45 |
+
def _run(self, history, **kwargs):
|
46 |
+
content = self._param.content
|
47 |
+
|
48 |
+
self._param.inputs = []
|
49 |
+
for para in self._param.parameters:
|
50 |
+
if not para.get("component_id"): continue
|
51 |
+
component_id = para["component_id"].split("@")[0]
|
52 |
+
if para["component_id"].lower().find("@") >= 0:
|
53 |
+
cpn_id, key = para["component_id"].split("@")
|
54 |
+
for p in self._canvas.get_component(cpn_id)["obj"]._param.query:
|
55 |
+
if p["key"] == key:
|
56 |
+
kwargs[para["key"]] = p.get("value", "")
|
57 |
+
self._param.inputs.append(
|
58 |
+
{"component_id": para["component_id"], "content": kwargs[para["key"]]})
|
59 |
+
break
|
60 |
+
else:
|
61 |
+
assert False, f"Can't find parameter '{key}' for {cpn_id}"
|
62 |
+
continue
|
63 |
+
|
64 |
+
cpn = self._canvas.get_component(component_id)["obj"]
|
65 |
+
if cpn.component_name.lower() == "answer":
|
66 |
+
hist = self._canvas.get_history(1)
|
67 |
+
if hist:
|
68 |
+
hist = hist[0]["content"]
|
69 |
+
else:
|
70 |
+
hist = ""
|
71 |
+
kwargs[para["key"]] = hist
|
72 |
+
continue
|
73 |
+
|
74 |
+
_, out = cpn.output(allow_partial=False)
|
75 |
+
if "content" not in out.columns:
|
76 |
+
kwargs[para["key"]] = ""
|
77 |
+
else:
|
78 |
+
kwargs[para["key"]] = " - "+"\n - ".join([o if isinstance(o, str) else str(o) for o in out["content"]])
|
79 |
+
self._param.inputs.append({"component_id": para["component_id"], "content": kwargs[para["key"]]})
|
80 |
+
|
81 |
+
for n, v in kwargs.items():
|
82 |
+
content = re.sub(r"\{%s\}" % re.escape(n), re.escape(str(v)), content)
|
83 |
+
|
84 |
+
return Template.be_output(content)
|
85 |
+
|
api/apps/canvas_app.py
CHANGED
@@ -120,7 +120,10 @@ def run():
|
|
120 |
try:
|
121 |
for ans in canvas.run(stream=True):
|
122 |
if ans.get("running_status"):
|
123 |
-
yield "data:" + json.dumps({"code": 0, "message": "",
|
|
|
|
|
|
|
124 |
continue
|
125 |
for k in ans.keys():
|
126 |
final_ans[k] = ans[k]
|
|
|
120 |
try:
|
121 |
for ans in canvas.run(stream=True):
|
122 |
if ans.get("running_status"):
|
123 |
+
yield "data:" + json.dumps({"code": 0, "message": "",
|
124 |
+
"data": {"answer": ans["content"],
|
125 |
+
"running_status": True}},
|
126 |
+
ensure_ascii=False) + "\n\n"
|
127 |
continue
|
128 |
for k in ans.keys():
|
129 |
final_ans[k] = ans[k]
|
api/apps/user_app.py
CHANGED
@@ -518,6 +518,7 @@ def user_register(user_id, user):
|
|
518 |
"model_type": llm.model_type,
|
519 |
"api_key": settings.API_KEY,
|
520 |
"api_base": settings.LLM_BASE_URL,
|
|
|
521 |
}
|
522 |
)
|
523 |
|
|
|
518 |
"model_type": llm.model_type,
|
519 |
"api_key": settings.API_KEY,
|
520 |
"api_base": settings.LLM_BASE_URL,
|
521 |
+
"max_tokens": llm.max_tokens
|
522 |
}
|
523 |
)
|
524 |
|