Skip to content

Commit

Permalink
修改API响应格式 (Sourcery refactored) (#1066)
Browse files Browse the repository at this point in the history
* Update api.py

* 'Refactored by Sourcery'

---------

Co-authored-by: Matt Gideon <117586514+Haibersut@users.noreply.github.com>
Co-authored-by: Sourcery AI <>
  • Loading branch information
sourcery-ai[bot] and Haibersut committed Jul 17, 2023
1 parent 2be1837 commit 8ec1fa8
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions adapter/chatgpt/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,13 @@ async def request(self, session_id: str = None, messages: list = None) -> str:
async with aiohttp.ClientSession() as session:
with async_timeout.timeout(self.bot.timeout):
async with session.post(f'{api_endpoint}/chat/completions', headers=headers,
data=json.dumps(data), proxy=proxy) as resp:
data=json.dumps(data), proxy=proxy) as resp:
if resp.status != 200:
response_text = await resp.text()
raise Exception(
f"{resp.status} {resp.reason} {response_text}",
)
content = await self._process_response(resp, session_id)

return content
return await self._process_response(resp, session_id)

async def request_with_stream(self, session_id: str = None, messages: list = None) -> AsyncGenerator[str, None]:
proxy, api_endpoint, headers, data = self._prepare_request(session_id, messages, stream=True)
Expand Down Expand Up @@ -249,7 +247,7 @@ async def request_with_stream(self, session_id: str = None, messages: list = Non
event_text = delta['content']
completion_text += event_text
self.latest_role = response_role
yield completion_text
yield event_text
self.bot.add_to_conversation(completion_text, response_role, session_id)

async def compressed_session(self, session_id: str):
Expand Down Expand Up @@ -290,17 +288,18 @@ async def ask(self, prompt: str) -> AsyncGenerator[str, None]:
self.bot.add_to_conversation(prompt, "user", session_id=self.session_id)
start_time = time.time()

full_response = ''

if config.openai.gpt_params.stream:
async for completion_text in self.request_with_stream(session_id=self.session_id):
yield completion_text
async for resp in self.request_with_stream(session_id=self.session_id):
full_response += resp
yield full_response

token_count = self.bot.count_tokens(self.session_id, self.bot.engine)
logger.debug(f"[ChatGPT-API:{self.bot.engine}] 响应:{completion_text}")
logger.debug(f"[ChatGPT-API:{self.bot.engine}] 响应:{full_response}")
logger.debug(f"[ChatGPT-API:{self.bot.engine}] 使用 token 数:{token_count}")
else:
completion_text = await self.request(session_id=self.session_id)
yield completion_text

yield await self.request(session_id=self.session_id)
event_time = time.time() - start_time
if event_time is not None:
logger.debug(f"[ChatGPT-API:{self.bot.engine}] 接收到全部消息花费了{event_time:.2f}秒")
Expand Down

0 comments on commit 8ec1fa8

Please sign in to comment.