Skip to content

Commit

Permalink
规范SDK日志 (#705)
Browse files Browse the repository at this point in the history
* å­规范SDK日志

* update

---------

Co-authored-by: yinjiaqi <[email protected]>
  • Loading branch information
C9luster and yinjiaqi authored Jan 2, 2025
1 parent 0c9957d commit be92f8c
Show file tree
Hide file tree
Showing 18 changed files with 41 additions and 188 deletions.
22 changes: 13 additions & 9 deletions go/appbuilder/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"fmt"
"io"
"log"
"net/http"
)

Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,24 @@ public <T> HttpResponse<T> execute(ClassicHttpRequest request, Type bodyType)
HttpResponse<T> response =
new HttpResponse<T>().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());
}
Expand Down
11 changes: 10 additions & 1 deletion python/core/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 *
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion python/core/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion python/core/components/doc_format_converter/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down
1 change: 0 additions & 1 deletion python/core/components/doc_splitter/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions python/core/components/extract_table/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
Expand Down
13 changes: 2 additions & 11 deletions python/core/components/llms/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 0 additions & 12 deletions python/core/components/llms/hallucination_detection/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
13 changes: 0 additions & 13 deletions python/core/components/llms/oral_query_generation/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
36 changes: 0 additions & 36 deletions python/core/components/ppt_generation_from_file/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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':
Expand Down
Loading

0 comments on commit be92f8c

Please sign in to comment.