From be92f8c317a10bc9e911d68eac060a6f73ccde76 Mon Sep 17 00:00:00 2001 From: C9luster <138663536+C9luster@users.noreply.github.com> Date: Thu, 2 Jan 2025 17:29:52 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=84=E8=8C=83SDK=E6=97=A5=E5=BF=97=20(#705?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * å­规范SDK日志 * update --------- Co-authored-by: yinjiaqi --- go/appbuilder/util.go | 22 +++++++----- .../base/utils/http/HttpClient.java | 16 +++++++-- python/core/_client.py | 11 +++++- python/core/component.py | 2 +- .../doc_format_converter/component.py | 1 - .../core/components/doc_splitter/component.py | 1 - .../components/extract_table/component.py | 2 -- python/core/components/llms/base.py | 13 ++----- .../llms/hallucination_detection/component.py | 12 ------- .../llms/oral_query_generation/component.py | 13 ------- .../ppt_generation_from_file/component.py | 36 ------------------- .../component.py | 36 ------------------- .../ppt_generation_from_paper/component.py | 36 ------------------- .../llms/hallucination_detection/component.py | 12 ------- .../llms/oral_query_generation/component.py | 13 ------- .../appbuilder_client/appbuilder_client.py | 1 - python/core/console/dataset/dataset.py | 1 - .../tests/test_assistant_e2e_stream_cancel.py | 1 + 18 files changed, 41 insertions(+), 188 deletions(-) diff --git a/go/appbuilder/util.go b/go/appbuilder/util.go index 26d6f2c22..67e1b6271 100644 --- a/go/appbuilder/util.go +++ b/go/appbuilder/util.go @@ -19,6 +19,7 @@ import ( "errors" "fmt" "io" + "log" "net/http" ) @@ -29,16 +30,19 @@ type SSEEvent struct { } func checkHTTPResponse(rsp *http.Response) (string, error) { - requestID := rsp.Header.Get("X-Appbuilder-Request-Id") - if rsp.StatusCode == http.StatusOK { - return requestID, nil - } + requestID := rsp.Header.Get("X-Appbuilder-Request-Id") + if rsp.StatusCode == http.StatusOK { + log.Printf("Successful HTTP response. RequestID: %s", requestID) + return requestID, nil + } - data, err := io.ReadAll(rsp.Body) - if err != nil { - return requestID, err - } - return requestID, fmt.Errorf("http status code is %d, content is %s", rsp.StatusCode, string(data)) + data, err := io.ReadAll(rsp.Body) + if err != nil { + log.Printf("Failed to read response body. RequestID: %s, Error: %v", requestID, err) + return requestID, err + } + log.Printf("HTTP response with unexpected status code. RequestID: %s, StatusCode: %d, Content: %s", requestID, rsp.StatusCode, string(data)) + return requestID, fmt.Errorf("http status code is %d, content is %s", rsp.StatusCode, string(data)) } func NewSSEReader(bufSize int, reader *bufio.Reader) *sseReader { diff --git a/java/src/main/java/com/baidubce/appbuilder/base/utils/http/HttpClient.java b/java/src/main/java/com/baidubce/appbuilder/base/utils/http/HttpClient.java index 507473f48..ede5fdf6d 100644 --- a/java/src/main/java/com/baidubce/appbuilder/base/utils/http/HttpClient.java +++ b/java/src/main/java/com/baidubce/appbuilder/base/utils/http/HttpClient.java @@ -210,12 +210,24 @@ public HttpResponse execute(ClassicHttpRequest request, Type bodyType) HttpResponse response = new HttpResponse().setCode(resp.getCode()).setMessage(resp.getReasonPhrase()) .setRequestId(requestId).setHeaders(headers).setStringBody(stringBody); - if (resp.getCode() == 200 && bodyType != null) { - response.setBody(JsonUtils.deserialize(stringBody, bodyType)); + if (resp.getCode() == 200) { + LOGGER.log(Level.FINE, "Successful response with code 200 for request ID: {0}", new Object[]{requestId}); + if (bodyType != null) { + response.setBody(JsonUtils.deserialize(stringBody, bodyType)); + } + } else { + LOGGER.log(Level.SEVERE, "Error response with code {0} for request ID: {1}, message: {2}", new Object[]{resp.getCode(), requestId, resp.getReasonPhrase()}); } return response; }); if (httpResponse.getCode() != 200) { + String errorMessage = String.format( + "Error after processing response with code %d for request ID: %s, message: %s", + httpResponse.getCode(), + httpResponse.getRequestId(), + httpResponse.getMessage() + ); + LOGGER.log(Level.SEVERE, errorMessage); throw new AppBuilderServerException(httpResponse.getRequestId(), httpResponse.getCode(), httpResponse.getMessage(), httpResponse.getStringBody()); } diff --git a/python/core/_client.py b/python/core/_client.py index 2f9b87dc6..c493f5710 100644 --- a/python/core/_client.py +++ b/python/core/_client.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. - """Base client for interact with backend server""" import os @@ -23,6 +22,7 @@ from requests.adapters import HTTPAdapter, Retry from aiohttp import ClientResponse +from appbuilder.utils.logger_util import logger from appbuilder import get_default_header from appbuilder.core._exception import * @@ -114,21 +114,30 @@ def check_response_header(response: requests.Response): """ status_code = response.status_code if status_code == requests.codes.ok: + logger.debug("request_id={} , http status code is {} , response text is {}".format( + __class__.response_request_id(response), status_code, response.text + )) return message = "request_id={} , http status code is {}, body is {}".format( __class__.response_request_id(response), status_code, response.text ) if status_code == requests.codes.bad_request: + logger.error(message) raise BadRequestException(message) elif status_code == requests.codes.forbidden: + logger.error(message) raise ForbiddenException(message) elif status_code == requests.codes.not_found: + logger.error(message) raise NotFoundException(message) elif status_code == requests.codes.precondition_required: + logger.error(message) raise PreconditionFailedException(message) elif status_code == requests.codes.internal_server_error: + logger.error(message) raise InternalServerErrorException(message) else: + logger.error(message) raise BaseRPCException(message) def service_url(self, sub_path: str, prefix: str = None): diff --git a/python/core/component.py b/python/core/component.py index b62d92621..2468f5d0d 100644 --- a/python/core/component.py +++ b/python/core/component.py @@ -242,7 +242,7 @@ def set_secret_key_and_gateway(self, secret_key: Optional[str] = None, gateway: except AttributeError: # 目前async仅在AppBuilderClient中使用,所以没有async属性的组件都可以设置为False self.is_async = False - + if self.is_async: self._http_client = AsyncHTTPClient(self.secret_key, self.gateway) else: diff --git a/python/core/components/doc_format_converter/component.py b/python/core/components/doc_format_converter/component.py index 337b3ea0a..fc16670bb 100644 --- a/python/core/components/doc_format_converter/component.py +++ b/python/core/components/doc_format_converter/component.py @@ -27,7 +27,6 @@ from appbuilder.core._exception import AppBuilderServerException, InvalidRequestArgumentError from appbuilder.core.component import Component, Message -from appbuilder.utils.logger_util import logger from appbuilder.core._client import HTTPClient from appbuilder.core.components.doc_format_converter.model import DocFormatConverterInMessage, \ DocFormatConverterOutMessage, \ diff --git a/python/core/components/doc_splitter/component.py b/python/core/components/doc_splitter/component.py index 1f3b38427..19586cc2b 100644 --- a/python/core/components/doc_splitter/component.py +++ b/python/core/components/doc_splitter/component.py @@ -23,7 +23,6 @@ from appbuilder.core._exception import AppBuilderServerException from appbuilder.core.components.doc_parser.base import ParseResult from appbuilder.core.component import Component, Message, ComponentArguments -from appbuilder.utils.logger_util import logger from appbuilder.core.components.doc_parser.base import DocSegment from appbuilder.utils.trace.tracer_wrapper import components_run_trace, components_run_stream_trace diff --git a/python/core/components/extract_table/component.py b/python/core/components/extract_table/component.py index 30dad487e..f6fd97685 100644 --- a/python/core/components/extract_table/component.py +++ b/python/core/components/extract_table/component.py @@ -21,7 +21,6 @@ import json from appbuilder.core.component import Component, Message, ComponentArguments -from appbuilder.utils.logger_util import logger from appbuilder.utils.trace.tracer_wrapper import components_run_trace, components_run_stream_trace @@ -125,7 +124,6 @@ def run(self, message: Message, table_max_size: int = 800, doc_node_num_before_t "field_before_table_cnt": doc_node_num_before_table } url = self.http_client.service_url(sub_path="", prefix=self.base_url) - # logger.info("request url: {}, headers: {}".format(url, headers)) headers = self.http_client.auth_header() headers["Content-Type"] = "application/json" resp = self.http_client.session.post(url=url, data=json.dumps(params), headers=headers) diff --git a/python/core/components/llms/base.py b/python/core/components/llms/base.py index 437c96c6c..346a47743 100644 --- a/python/core/components/llms/base.py +++ b/python/core/components/llms/base.py @@ -430,21 +430,12 @@ def completion( stream = True if request.response_mode == "streaming" else False url = self.http_client.service_url(completion_url, self.base_url) - logger.debug( - "request url: {}, method: {}, json: {}, headers: {}".format(url, - "POST", - request.params, - headers)) response = self.http_client.session.post(url, json=request.params, headers=headers, timeout=timeout, stream=stream) - - logger.debug( - "request url: {}, method: {}, json: {}, headers: {}, response: {}".format(url, "POST", - request.params, - headers, - response)) + return self.gene_response(response, stream) + @staticmethod def check_service_error(data: dict): r"""check service internal error. diff --git a/python/core/components/llms/hallucination_detection/component.py b/python/core/components/llms/hallucination_detection/component.py index fcc47de1c..36266aa73 100644 --- a/python/core/components/llms/hallucination_detection/component.py +++ b/python/core/components/llms/hallucination_detection/component.py @@ -18,7 +18,6 @@ from appbuilder.core.components.llms.base import CompletionBaseComponent, ModelArgsConfig from appbuilder.core.message import Message from appbuilder.core._exception import AppBuilderServerException -from appbuilder.utils.logger_util import logger from appbuilder.utils.trace.tracer_wrapper import components_run_trace, components_run_stream_trace from .base import HallucinationDetectionArgs @@ -140,19 +139,8 @@ def completion(self, version, base_url, request, timeout: float = None, stream = True if request.response_mode == "streaming" else False url = self.http_client.service_url("/app/hallucination_detection", self.base_url) - logger.debug( - "request url: {}, method: {}, json: {}, headers: {}".format(url, - "POST", - request.params, - headers)) response = self.http_client.session.post(url, json=request.params, headers=headers, timeout=timeout, stream=stream) - - logger.debug( - "request url: {}, method: {}, json: {}, headers: {}, response: {}".format(url, "POST", - request.params, - headers, - response)) return self.gene_response(response, stream) @components_run_trace diff --git a/python/core/components/llms/oral_query_generation/component.py b/python/core/components/llms/oral_query_generation/component.py index 7d14af1a1..aff5cd62a 100644 --- a/python/core/components/llms/oral_query_generation/component.py +++ b/python/core/components/llms/oral_query_generation/component.py @@ -19,7 +19,6 @@ from appbuilder.core.components.llms.base import CompletionBaseComponent, ModelArgsConfig from appbuilder.core.message import Message from appbuilder.core._exception import AppBuilderServerException -from appbuilder.utils.logger_util import logger from appbuilder.utils.trace.tracer_wrapper import components_run_trace, components_run_stream_trace from .base import OralQueryGenerationArgs @@ -139,20 +138,8 @@ def completion(self, version, base_url, request, timeout: float = None, stream = True if request.response_mode == "streaming" else False url = self.http_client.service_url("/app/query_generation", self.base_url) - - logger.debug( - "request url: {}, method: {}, json: {}, headers: {}".format(url, - "POST", - request.params, - headers)) response = self.http_client.session.post(url, json=request.params, headers=headers, timeout=timeout, stream=stream) - - logger.debug( - "request url: {}, method: {}, json: {}, headers: {}, response: {}".format(url, "POST", - request.params, - headers, - response)) return self.gene_response(response, stream) @components_run_trace diff --git a/python/core/components/ppt_generation_from_file/component.py b/python/core/components/ppt_generation_from_file/component.py index f0cd9fe47..3a3f48c6a 100644 --- a/python/core/components/ppt_generation_from_file/component.py +++ b/python/core/components/ppt_generation_from_file/component.py @@ -115,24 +115,10 @@ def ppt_generation(self, url = self.http_client.service_url(self.ppt_generation_url, self.uniform_prefix) headers = self.http_client.auth_header() headers['Content-Type'] = 'application/json' - - logger.debug('[ppt_generation] request url: {}, method: {}, json: {}, headers: {}'.format( - url, - 'POST', - post_data, - headers - )) response = self.http_client.session.post(url, json=post_data, headers=headers, timeout=timeout) - logger.debug('[ppt_generation] request url: {}, method: {}, json: {}, headers: {}, response: {}'.format( - url, - 'POST', - post_data, - headers, - response - )) self.http_client.check_response_header(response) resp_data = response.json() if resp_data.get('code', None) != 200 or resp_data.get('msg', None) != 'success': @@ -166,22 +152,11 @@ def get_ppt_generation_status(self, headers = self.http_client.auth_header() headers['Content-Type'] = 'application/json' - logger.debug('[get_ppt_generation_status] request url: {}, method: {}, headers: {}'.format( - url, - 'GET', - headers - )) status = -1 for _ in range(request_times): response = self.http_client.session.get(url, headers=headers, timeout=timeout) - logger.debug('[get_ppt_generation_status] request url: {}, method: {}, headers: {}, response: {}'.format( - url, - 'GET', - headers, - response - )) try: self.http_client.check_response_header(response) except: @@ -239,20 +214,9 @@ def get_ppt_download_link(self, url = self.http_client.service_url(self.get_ppt_download_link_url, self.uniform_prefix) + f'?id={job_id}' headers = self.http_client.auth_header() headers['Content-Type'] = 'application/json' - logger.debug('[get_ppt_download_link] request url: {}, method: {}, headers: {}'.format( - url, - 'GET', - headers - )) response = self.http_client.session.get(url, headers=headers, timeout=timeout) - logger.debug('[get_ppt_download_link] request url: {}, method: {}, headers: {}, response: {}'.format( - url, - 'GET', - headers, - response - )) self.http_client.check_response_header(response) resp_data = response.json() if resp_data.get('code', None) != 200 or resp_data.get('msg', None) != 'success': diff --git a/python/core/components/ppt_generation_from_instruction/component.py b/python/core/components/ppt_generation_from_instruction/component.py index bd3e276a8..d481b303f 100644 --- a/python/core/components/ppt_generation_from_instruction/component.py +++ b/python/core/components/ppt_generation_from_instruction/component.py @@ -119,24 +119,10 @@ def ppt_generation(self, url = self.http_client.service_url(self.ppt_generation_url, self.uniform_prefix) headers = self.http_client.auth_header() headers['Content-Type'] = 'application/json' - - logger.debug('[ppt_generation] request url: {}, method: {}, json: {}, headers: {}'.format( - url, - 'POST', - post_data, - headers - )) response = self.http_client.session.post(url, json=post_data, headers=headers, timeout=timeout) - logger.debug('[ppt_generation] request url: {}, method: {}, json: {}, headers: {}, response: {}'.format( - url, - 'POST', - post_data, - headers, - response - )) self.http_client.check_response_header(response) resp_data = response.json() if resp_data.get('code', None) != 200 or resp_data.get('msg', None) != 'success': @@ -174,22 +160,11 @@ def get_ppt_generation_status(self, headers = self.http_client.auth_header() headers['Content-Type'] = 'application/json' - logger.debug('[get_ppt_generation_status] request url: {}, method: {}, headers: {}'.format( - url, - 'GET', - headers - )) status = -1 for _ in range(request_times): response = self.http_client.session.get(url, headers=headers, timeout=timeout) - logger.debug('[get_ppt_generation_status] request url: {}, method: {}, headers: {}, response: {}'.format( - url, - 'GET', - headers, - response - )) try: self.http_client.check_response_header(response) except: @@ -246,20 +221,9 @@ def get_ppt_download_link(self, url = self.http_client.service_url(self.get_ppt_download_link_url, self.uniform_prefix) + f'?id={job_id}' headers = self.http_client.auth_header() headers['Content-Type'] = 'application/json' - logger.debug('[get_ppt_download_link] request url: {}, method: {}, headers: {}'.format( - url, - 'GET', - headers - )) response = self.http_client.session.get(url, headers=headers, timeout=timeout) - logger.debug('[get_ppt_download_link] request url: {}, method: {}, headers: {}, response: {}'.format( - url, - 'GET', - headers, - response - )) self.http_client.check_response_header(response) resp_data = response.json() if resp_data.get('code', None) != 200 or resp_data.get('msg', None) != 'success': diff --git a/python/core/components/ppt_generation_from_paper/component.py b/python/core/components/ppt_generation_from_paper/component.py index fc65a4cbe..381ad2d0d 100644 --- a/python/core/components/ppt_generation_from_paper/component.py +++ b/python/core/components/ppt_generation_from_paper/component.py @@ -115,24 +115,10 @@ def ppt_generation(self, url = self.http_client.service_url(self.ppt_generation_url, self.uniform_prefix) headers = self.http_client.auth_header() headers['Content-Type'] = 'application/json' - - logger.debug('[ppt_generation] request url: {}, method: {}, json: {}, headers: {}'.format( - url, - 'POST', - post_data, - headers - )) response = self.http_client.session.post(url, json=post_data, headers=headers, timeout=timeout) - logger.debug('[ppt_generation] request url: {}, method: {}, json: {}, headers: {}, response: {}'.format( - url, - 'POST', - post_data, - headers, - response - )) self.http_client.check_response_header(response) resp_data = response.json() if resp_data.get('code', None) != 200 or resp_data.get('msg', None) != 'success': @@ -169,22 +155,11 @@ def get_ppt_generation_status(self, headers = self.http_client.auth_header() headers['Content-Type'] = 'application/json' - logger.debug('[get_ppt_generation_status] request url: {}, method: {}, headers: {}'.format( - url, - 'GET', - headers - )) status = -1 for _ in range(request_times): response = self.http_client.session.get(url, headers=headers, timeout=timeout) - logger.debug('[get_ppt_generation_status] request url: {}, method: {}, headers: {}, response: {}'.format( - url, - 'GET', - headers, - response - )) try: self.http_client.check_response_header(response) except: @@ -242,20 +217,9 @@ def get_ppt_download_link(self, url = self.http_client.service_url(self.get_ppt_download_link_url, self.uniform_prefix) + f'?id={job_id}' headers = self.http_client.auth_header() headers['Content-Type'] = 'application/json' - logger.debug('[get_ppt_download_link] request url: {}, method: {}, headers: {}'.format( - url, - 'GET', - headers - )) response = self.http_client.session.get(url, headers=headers, timeout=timeout) - logger.debug('[get_ppt_download_link] request url: {}, method: {}, headers: {}, response: {}'.format( - url, - 'GET', - headers, - response - )) self.http_client.check_response_header(response) resp_data = response.json() if resp_data.get('code', None) != 200 or resp_data.get('msg', None) != 'success': diff --git a/python/core/components/v2/llms/hallucination_detection/component.py b/python/core/components/v2/llms/hallucination_detection/component.py index cc2e663cc..a4f153db4 100644 --- a/python/core/components/v2/llms/hallucination_detection/component.py +++ b/python/core/components/v2/llms/hallucination_detection/component.py @@ -18,7 +18,6 @@ from appbuilder.core.components.llms.base import CompletionBaseComponent, ModelArgsConfig from appbuilder.core.message import Message from appbuilder.core._exception import AppBuilderServerException -from appbuilder.utils.logger_util import logger from appbuilder.utils.trace.tracer_wrapper import components_run_trace, components_run_stream_trace from appbuilder.core.components.llms.hallucination_detection.base import HallucinationDetectionArgs @@ -140,19 +139,8 @@ def completion(self, version, base_url, request, timeout: float = None, stream = True if request.response_mode == "streaming" else False url = self.http_client.service_url("/app/hallucination_detection", self.base_url) - logger.debug( - "request url: {}, method: {}, json: {}, headers: {}".format(url, - "POST", - request.params, - headers)) response = self.http_client.session.post(url, json=request.params, headers=headers, timeout=timeout, stream=stream) - - logger.debug( - "request url: {}, method: {}, json: {}, headers: {}, response: {}".format(url, "POST", - request.params, - headers, - response)) return self.gene_response(response, stream) @components_run_trace diff --git a/python/core/components/v2/llms/oral_query_generation/component.py b/python/core/components/v2/llms/oral_query_generation/component.py index 3fff92e34..c5c1105b9 100644 --- a/python/core/components/v2/llms/oral_query_generation/component.py +++ b/python/core/components/v2/llms/oral_query_generation/component.py @@ -19,7 +19,6 @@ from appbuilder.core.components.llms.base import CompletionBaseComponent, ModelArgsConfig from appbuilder.core.message import Message from appbuilder.core._exception import AppBuilderServerException -from appbuilder.utils.logger_util import logger from appbuilder.utils.trace.tracer_wrapper import components_run_trace, components_run_stream_trace from appbuilder.core.components.llms.oral_query_generation.base import OralQueryGenerationArgs @@ -140,20 +139,8 @@ def completion(self, version, base_url, request, timeout: float = None, stream = True if request.response_mode == "streaming" else False url = self.http_client.service_url("/app/query_generation", self.base_url) - - logger.debug( - "request url: {}, method: {}, json: {}, headers: {}".format(url, - "POST", - request.params, - headers)) response = self.http_client.session.post(url, json=request.params, headers=headers, timeout=timeout, stream=stream) - - logger.debug( - "request url: {}, method: {}, json: {}, headers: {}, response: {}".format(url, "POST", - request.params, - headers, - response)) return self.gene_response(response, stream) @components_run_trace diff --git a/python/core/console/appbuilder_client/appbuilder_client.py b/python/core/console/appbuilder_client/appbuilder_client.py index 5d8779a4a..f104af0d4 100644 --- a/python/core/console/appbuilder_client/appbuilder_client.py +++ b/python/core/console/appbuilder_client/appbuilder_client.py @@ -25,7 +25,6 @@ from appbuilder.utils.sse_util import SSEClient from appbuilder.core._client import HTTPClient from appbuilder.utils.func_utils import deprecated -from appbuilder.utils.logger_util import logger from appbuilder.utils.trace.tracer_wrapper import client_run_trace, client_tool_trace diff --git a/python/core/console/dataset/dataset.py b/python/core/console/dataset/dataset.py index 4abec0486..40a504d1a 100644 --- a/python/core/console/dataset/dataset.py +++ b/python/core/console/dataset/dataset.py @@ -5,7 +5,6 @@ import json import os from appbuilder.utils.func_utils import deprecated -from appbuilder.utils.logger_util import logger from appbuilder.utils.trace.tracer_wrapper import client_tool_trace diff --git a/python/tests/test_assistant_e2e_stream_cancel.py b/python/tests/test_assistant_e2e_stream_cancel.py index a1c0737fb..e9feb290c 100644 --- a/python/tests/test_assistant_e2e_stream_cancel.py +++ b/python/tests/test_assistant_e2e_stream_cancel.py @@ -31,6 +31,7 @@ def get_cur_whether(location:str, unit:str): } } +@unittest.skip("暂时跳过") class TestCancel(unittest.TestCase): def setUp(self): os.environ["APPBUILDER_TOKEN"] = os.environ["APPBUILDER_TOKEN_V2"]