Skip to content

Commit d63eecd

Browse files
committed
fix async ptaas
Change-Id: If57b470952a1db94d67dc36430262f2ee720d209
1 parent 855a8a4 commit d63eecd

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

cozeloop/internal/httpclient/client.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,47 @@ def post_stream(
153153
logger.error(f"Http client stream request failed, path: {path}, err: {e}.")
154154
raise consts.NetworkError from e
155155

156+
async def arequest(
157+
self,
158+
path: str,
159+
method: str,
160+
response_model: Type[T],
161+
*,
162+
params: Optional[Dict[str, str]] = None,
163+
form: Optional[Dict[str, str]] = None,
164+
json: Optional[Union[BaseModel, Dict]] = None,
165+
files: Optional[Dict[str, FileType]] = None,
166+
headers: Optional[Dict[str, str]] = None,
167+
timeout: Optional[int] = None,
168+
) -> T:
169+
url = self._build_url(path)
170+
_headers = self._set_headers(headers)
171+
172+
_timeout = timeout if timeout is not None else self.timeout
173+
174+
if isinstance(json, BaseModel):
175+
if pydantic.VERSION.startswith('1'):
176+
json = json.dict(by_alias=True)
177+
else:
178+
json = json.model_dump(by_alias=True)
179+
180+
try:
181+
response = await self.http_client.arequest(
182+
method,
183+
url,
184+
params=params,
185+
data=form,
186+
json=json,
187+
files=files,
188+
headers=_headers,
189+
timeout=_timeout
190+
)
191+
except httpx.HTTPError as e:
192+
logger.error(f"Http client request failed, path: {path}, err: {e}.")
193+
raise consts.NetworkError from e
194+
195+
return parse_response(url, response, response_model)
196+
156197
async def apost_stream(
157198
self,
158199
path: str,
@@ -170,7 +211,7 @@ async def apost_stream(
170211

171212
try:
172213
# 返回stream_context,让StreamReader管理上下文
173-
stream_context = self.http_client.stream(
214+
stream_context = self.http_client.astream(
174215
"POST",
175216
url,
176217
json=json,

cozeloop/internal/prompt/openapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ def execute_streaming(self, request: ExecuteRequest, timeout: Optional[int] = No
259259

260260
async def aexecute(self, request: ExecuteRequest, timeout: Optional[int] = None) -> ExecuteData:
261261
"""异步执行Prompt请求"""
262-
response = self.http_client.request(
262+
response = await self.http_client.arequest(
263263
EXECUTE_PROMPT_PATH,
264264
"POST",
265265
ExecuteResponse,

0 commit comments

Comments
 (0)