Skip to content

Commit e7df809

Browse files
authored
Merge branch 'main' into feat_ptaas_trace
2 parents c8cfbdb + d29c69a commit e7df809

24 files changed

Lines changed: 410 additions & 312 deletions

cozeloop/_client.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,9 @@ def execute_prompt(
295295
timeout: Optional[int] = None
296296
) -> Union[ExecuteResult, StreamReader[ExecuteResult]]:
297297
"""
298-
执行Prompt请求
298+
Execute Prompt request
299299
300-
:param timeout: 请求超时时间(秒),可选,默认为600秒(10分钟)
300+
:param timeout: Request timeout (seconds), optional, default is 600 seconds (10 minutes)
301301
"""
302302
if self._closed:
303303
raise ClientClosedError()
@@ -323,9 +323,9 @@ async def aexecute_prompt(
323323
timeout: Optional[int] = None
324324
) -> Union[ExecuteResult, StreamReader[ExecuteResult]]:
325325
"""
326-
异步执行Prompt请求
326+
Asynchronously execute Prompt request
327327
328-
:param timeout: 请求超时时间(秒),可选,默认为600秒(10分钟)
328+
:param timeout: Request timeout (seconds), optional, default is 600 seconds (10 minutes)
329329
"""
330330
if self._closed:
331331
raise ClientClosedError()
@@ -449,9 +449,9 @@ def execute_prompt(
449449
timeout: Optional[int] = None
450450
) -> Union[ExecuteResult, StreamReader[ExecuteResult]]:
451451
"""
452-
执行Prompt请求
452+
Execute Prompt request
453453
454-
:param timeout: 请求超时时间(秒),可选,默认为600秒(10分钟)
454+
:param timeout: Request timeout (seconds), optional, default is 600 seconds (10 minutes)
455455
"""
456456
return get_default_client().execute_prompt(
457457
prompt_key,
@@ -475,9 +475,9 @@ async def aexecute_prompt(
475475
timeout: Optional[int] = None
476476
) -> Union[ExecuteResult, StreamReader[ExecuteResult]]:
477477
"""
478-
异步执行Prompt请求
478+
Asynchronously execute Prompt request
479479
480-
:param timeout: 请求超时时间(秒),可选,默认为600秒(10分钟)
480+
:param timeout: Request timeout (seconds), optional, default is 600 seconds (10 minutes)
481481
"""
482482
return await get_default_client().aexecute_prompt(
483483
prompt_key,

cozeloop/entities/prompt.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class Prompt(BaseModel):
127127

128128

129129
class ExecuteParam(BaseModel):
130-
"""Execute参数"""
130+
"""Execute parameters"""
131131
prompt_key: str
132132
version: str = ""
133133
label: str = ""
@@ -136,13 +136,13 @@ class ExecuteParam(BaseModel):
136136

137137

138138
class TokenUsage(BaseModel):
139-
"""Token使用统计"""
139+
"""Token usage statistics"""
140140
input_tokens: int = 0
141141
output_tokens: int = 0
142142

143143

144144
class ExecuteResult(BaseModel):
145-
"""Execute结果"""
145+
"""Execute result"""
146146
message: Optional[Message] = None
147147
finish_reason: Optional[str] = None
148148
usage: Optional[TokenUsage] = None

cozeloop/entities/stream.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,29 @@
88

99

1010
class StreamReader(ABC, Generic[T]):
11-
"""流式读取器接口"""
11+
"""Stream reader interface"""
1212

1313
@abstractmethod
1414
def __iter__(self) -> Iterator[T]:
15-
"""支持同步迭代 - for循环直接读取"""
15+
"""Support synchronous iteration - for loop direct reading"""
1616
pass
1717

1818
@abstractmethod
1919
def __next__(self) -> T:
20-
"""支持next()函数调用"""
20+
"""Support next() function call"""
2121
pass
2222

2323
@abstractmethod
2424
def __aiter__(self) -> AsyncIterator[T]:
25-
"""支持异步迭代 - async for循环直接读取"""
25+
"""Support asynchronous iteration - async for loop direct reading"""
2626
pass
2727

2828
@abstractmethod
2929
async def __anext__(self) -> T:
30-
"""支持async next()调用"""
30+
"""Support async next() call"""
3131
pass
3232

3333
@abstractmethod
3434
def close(self):
35-
"""关闭流"""
35+
"""Close stream"""
3636
pass

cozeloop/internal/consts/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
DEFAULT_PROMPT_CACHE_REFRESH_INTERVAL = 60
1616
DEFAULT_TIMEOUT = 3
1717
DEFAULT_UPLOAD_TIMEOUT = 30
18-
DEFAULT_PROMPT_EXECUTE_TIMEOUT = 600 # 10分钟,专用于execute_prompt和aexecute_prompt方法
18+
DEFAULT_PROMPT_EXECUTE_TIMEOUT = 600 # 10 minutes, dedicated for execute_prompt and aexecute_prompt methods
1919

2020
LOG_ID_HEADER = "x-tt-logid"
2121
AUTHORIZE_HEADER = "Authorization"

cozeloop/internal/httpclient/client.py

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def post_stream(
139139
json: Union[BaseModel, Dict] = None,
140140
timeout: Optional[int] = None,
141141
):
142-
"""发起流式POST请求,返回stream_context"""
142+
"""Initiate streaming POST request, return stream_context"""
143143
url = self._build_url(path)
144144
headers = self._set_headers({"Content-Type": "application/json"})
145145

@@ -149,7 +149,7 @@ def post_stream(
149149
_timeout = timeout if timeout is not None else self.timeout
150150

151151
try:
152-
# 返回stream_context,让StreamReader管理上下文
152+
# Return stream_context, let StreamReader manage context
153153
stream_context = self.http_client.stream(
154154
"POST",
155155
url,
@@ -162,13 +162,54 @@ def post_stream(
162162
logger.error(f"Http client stream request failed, path: {path}, err: {e}.")
163163
raise consts.NetworkError from e
164164

165+
async def arequest(
166+
self,
167+
path: str,
168+
method: str,
169+
response_model: Type[T],
170+
*,
171+
params: Optional[Dict[str, str]] = None,
172+
form: Optional[Dict[str, str]] = None,
173+
json: Optional[Union[BaseModel, Dict]] = None,
174+
files: Optional[Dict[str, FileType]] = None,
175+
headers: Optional[Dict[str, str]] = None,
176+
timeout: Optional[int] = None,
177+
) -> T:
178+
url = self._build_url(path)
179+
_headers = self._set_headers(headers)
180+
181+
_timeout = timeout if timeout is not None else self.timeout
182+
183+
if isinstance(json, BaseModel):
184+
if pydantic.VERSION.startswith('1'):
185+
json = json.dict(by_alias=True)
186+
else:
187+
json = json.model_dump(by_alias=True)
188+
189+
try:
190+
response = await self.http_client.arequest(
191+
method,
192+
url,
193+
params=params,
194+
data=form,
195+
json=json,
196+
files=files,
197+
headers=_headers,
198+
timeout=_timeout
199+
)
200+
except httpx.HTTPError as e:
201+
logger.error(f"Http client request failed, path: {path}, err: {e}.")
202+
raise consts.NetworkError from e
203+
204+
return parse_response(url, response, response_model)
205+
165206
async def apost_stream(
166207
self,
167208
path: str,
168209
json: Union[BaseModel, Dict] = None,
169210
timeout: Optional[int] = None,
170211
):
171-
"""发起异步流式POST请求,返回stream_context"""
212+
"""Initiate asynchronous streaming POST request, return stream_context"""
172213
url = self._build_url(path)
173214
headers = self._set_headers({"Content-Type": "application/json"})
174215

@@ -178,8 +219,8 @@ async def apost_stream(
178219
_timeout = timeout if timeout is not None else self.timeout
179220

180221
try:
181-
# 返回stream_context,让StreamReader管理上下文
182-
stream_context = self.http_client.stream(
222+
# Return stream_context, let StreamReader manage context
223+
stream_context = self.http_client.astream(
183224
"POST",
184225
url,
185226
json=json,

cozeloop/internal/httpclient/http_client.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
# SPDX-License-Identifier: MIT
33

44
import logging
5-
from typing import Dict, Type, TypeVar
5+
import typing
6+
from typing import Dict, Type, TypeVar, Any, Generator
67

78
import httpx
89
import pydantic
10+
from httpx import URL, Response
911
from pydantic import ValidationError
1012

1113
from cozeloop.internal import consts
@@ -16,9 +18,24 @@
1618
T = TypeVar('T', bound=BaseResponse)
1719

1820

19-
class HTTPClient(httpx.Client):
21+
class HTTPClient:
2022
def __init__(self):
21-
super().__init__()
23+
self.sync_client = httpx.Client()
24+
self.async_client = httpx.AsyncClient()
25+
26+
def request(self, method: str, url: URL | str, **kwargs: Any) -> Response:
27+
return self.sync_client.request(method, url, **kwargs)
28+
29+
def stream(self, method: str, url: URL | str, **kwargs: Any):
30+
"""Return synchronous stream context manager"""
31+
return self.sync_client.stream(method, url, **kwargs)
32+
33+
async def arequest(self, method: str, url: URL | str, **kwargs: Any) -> Response:
34+
return await self.async_client.request(method, url, **kwargs)
35+
36+
def astream(self, method: str, url: URL | str, **kwargs: Any):
37+
"""Return asynchronous stream context manager"""
38+
return self.async_client.stream(method, url, **kwargs)
2239

2340

2441
def _check_oauth_error(body: Dict, http_code: int, log_id: str) -> None:
@@ -63,4 +80,4 @@ def parse_response(url: str, response: httpx.Response, response_model: Type[T])
6380
logger.error(f"Failed to parse response. Path: {url}, http code: {http_code}, log id: {log_id}, error: {e}.")
6481
raise consts.InternalError from e
6582
logger.debug(f"Call remote service success. Path: {url}, response: {res}, log id: {log_id}")
66-
return res
83+
return res

0 commit comments

Comments
 (0)