-
Notifications
You must be signed in to change notification settings - Fork 878
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: yihong0618 <[email protected]>
- Loading branch information
1 parent
2258b88
commit f25d458
Showing
8 changed files
with
147 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
"""ChatGLM bot""" | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
from http import HTTPStatus | ||
import dashscope | ||
from dashscope import Generation | ||
from dashscope.api_entities.dashscope_response import Role | ||
from rich import print | ||
|
||
from xiaogpt.bot.base_bot import BaseBot, ChatHistoryMixin | ||
|
||
|
||
class QwenBot(ChatHistoryMixin, BaseBot): | ||
name = "Qian Wen" | ||
|
||
def __init__(self, qwen_key: str) -> None: | ||
self.history = [ | ||
{"role": Role.SYSTEM, "content": "You are a helpful assistant."} | ||
] | ||
dashscope.api_key = qwen_key | ||
|
||
@classmethod | ||
def from_config(cls, config): | ||
return cls(qwen_key=config.qwen_key) | ||
|
||
async def ask(self, query, **options): | ||
# from https://help.aliyun.com/zh/dashscope/developer-reference/api-details | ||
self.history.append({"role": Role.USER, "content": query}) | ||
|
||
response = Generation.call( | ||
Generation.Models.qwen_turbo, | ||
messages=self.history, | ||
result_format="message", # set the result to be "message" format. | ||
) | ||
if response.status_code == HTTPStatus.OK: | ||
# append result to messages. | ||
content = response.output.choices[0]["message"]["content"] | ||
self.history.append( | ||
{ | ||
"role": response.output.choices[0]["message"]["role"], | ||
"content": content, | ||
} | ||
) | ||
# keep last five | ||
first_history = self.history.pop(0) | ||
self.history = [first_history] + self.history[-5:] | ||
print(content) | ||
return content | ||
else: | ||
print( | ||
"Request id: %s, Status code: %s, error code: %s, error message: %s" | ||
% ( | ||
response.request_id, | ||
response.status_code, | ||
response.code, | ||
response.message, | ||
) | ||
) | ||
return "没有返回" | ||
|
||
async def ask_stream(self, query: str, **options: Any): | ||
self.history.append({"role": Role.USER, "content": query}) | ||
responses = Generation.call( | ||
Generation.Models.qwen_turbo, | ||
messages=self.history, | ||
result_format="message", # set the result to be "message" format. | ||
stream=True, | ||
incremental_output=True, # get streaming output incrementally | ||
) | ||
full_content = "" # with incrementally we need to merge output. | ||
role = None | ||
for response in responses: | ||
if response.status_code == HTTPStatus.OK: | ||
content = response.output.choices[0]["message"]["content"] | ||
full_content += content | ||
if not role: | ||
role = response.output.choices[0]["message"]["role"] | ||
print(content, end="") | ||
yield content | ||
else: | ||
print( | ||
"Request id: %s, Status code: %s, error code: %s, error message: %s" | ||
% ( | ||
response.request_id, | ||
response.status_code, | ||
response.code, | ||
response.message, | ||
) | ||
) | ||
self.history.append({"role": role, "content": full_content}) | ||
first_history = self.history.pop(0) | ||
self.history = [first_history] + self.history[-5:] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters