Skip to content

Commit

Permalink
fix: #436 (#440)
Browse files Browse the repository at this point in the history
* fix: #436

Signed-off-by: yihong0618 <[email protected]>

* fix: lint

Signed-off-by: yihong0618 <[email protected]>

---------

Signed-off-by: yihong0618 <[email protected]>
  • Loading branch information
yihong0618 authored Jan 28, 2024
1 parent 5a02fdc commit 9a74fd2
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 39 deletions.
28 changes: 9 additions & 19 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dependencies = [
"openai>=1",
"aiohttp",
"rich",
"zhipuai",
"zhipuai==2.0.1",
"bardapi",
"edge-tts>=6.1.3",
"EdgeGPT==0.1.26",
Expand Down
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ certifi==2023.7.22
charset-normalizer==3.3.2
colorama==0.4.6
dashscope==1.10.0
dataclasses==0.6
dataclasses-json==0.6.3
deep-translator==1.11.4
distro==1.9.0
Expand Down Expand Up @@ -88,4 +87,4 @@ urllib3==2.1.0
wcwidth==0.2.13
websockets==12.0
yarl==1.9.4
zhipuai==1.0.7
zhipuai==2.0.1
1 change: 1 addition & 0 deletions xiaogpt/bot/bard_bot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""ChatGLM bot"""

from __future__ import annotations

from typing import Any
Expand Down
1 change: 1 addition & 0 deletions xiaogpt/bot/gemini_bot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Google Gemini bot"""

from __future__ import annotations

from typing import Any
Expand Down
39 changes: 28 additions & 11 deletions xiaogpt/bot/glm_bot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""ChatGLM bot"""

from __future__ import annotations

from typing import Any
Expand All @@ -13,34 +14,50 @@ class GLMBot(ChatHistoryMixin, BaseBot):
default_options = {"model": "chatglm_turbo"}

def __init__(self, glm_key: str) -> None:
import zhipuai
from zhipuai import ZhipuAI

self.model = "glm-4" # Change glm model here

self.history = []
zhipuai.api_key = glm_key
self.client = ZhipuAI(api_key=glm_key)

@classmethod
def from_config(cls, config):
return cls(glm_key=config.glm_key)

def ask(self, query, **options):
import zhipuai

ms = self.get_messages()
kwargs = {**self.default_options, **options}
kwargs["prompt"] = ms
kwargs["model"] = self.model
ms.append({"role": "user", "content": f"{query}"})
kwargs["messages"] = ms
try:
r = zhipuai.model_api.sse_invoke(**kwargs)
r = self.client.chat.completions.create(**kwargs)
except Exception as e:
print(str(e))
return
message = ""
for i in r.events():
message += str(i.data)
message = r.choices[0].message.content

self.add_message(query, message)
print(message)
return message

def ask_stream(self, query: str, **options: Any):
raise Exception("GLM do not support stream")
async def ask_stream(self, query: str, **options: Any):
ms = self.get_messages()
kwargs = {**self.default_options, **options}
kwargs["model"] = self.model
ms.append({"role": "user", "content": f"{query}"})
kwargs["messages"] = ms
kwargs["stream"] = True
try:
r = self.client.chat.completions.create(**kwargs)
except Exception as e:
print(str(e))
return
full_content = ""
for chunk in r:
content = chunk.choices[0].delta.content
full_content += content
print(content, end="")
yield content
self.add_message(query, full_content)
7 changes: 3 additions & 4 deletions xiaogpt/bot/qwen_bot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""ChatGLM bot"""
"""Qwen bot"""

from __future__ import annotations

from http import HTTPStatus
Expand All @@ -16,9 +17,7 @@ def __init__(self, qwen_key: str) -> None:
import dashscope
from dashscope.api_entities.dashscope_response import Role

self.history = [
{"role": Role.SYSTEM, "content": "You are a helpful assistant."}
]
self.history = []
dashscope.api_key = qwen_key

@classmethod
Expand Down
4 changes: 2 additions & 2 deletions xiaogpt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ def main():
)

options = parser.parse_args()
if options.bot in ["glm", "bard"] and options.stream:
raise Exception("For now ChatGLM do not support stream")
if options.bot in ["bard"] and options.stream:
raise Exception("For now Bard do not support stream")
config = Config.from_options(options)

miboy = MiGPT(config)
Expand Down

0 comments on commit 9a74fd2

Please sign in to comment.