-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
109 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
- ✅ 讯飞星火大模型/Spark | ||
- ✅ 腾讯混元大模型/Hunyuan | ||
- ✅ DeepSeek | ||
- ✅ 智谱/ChatGLM | ||
|
||
### Install | ||
```shell | ||
|
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,75 @@ | ||
from chatchat.base import Base | ||
import httpx | ||
|
||
class Completion(Base): | ||
def __init__(self, model='glm-4-flash', proxy=None, timeout=None): | ||
super().__init__() | ||
|
||
plat = 'zhipu' | ||
self.verify_secret_data(plat, ('api_key',)) | ||
self.jdata = self.secret_data[plat] | ||
|
||
self.model_type = set([ | ||
'glm-4-0520', | ||
'glm-4', | ||
'glm-4-air', | ||
'glm-4-airx', | ||
'glm-4-flash', | ||
'glm-4v', | ||
'glm-3-turbo', | ||
]) | ||
|
||
if model not in self.model_type: | ||
raise RuntimeError(f'supported chat type: {list(self.model_type)}') | ||
self.model = model | ||
|
||
self.url = 'https://open.bigmodel.cn/api/paas/v4/chat/completions' | ||
self.client = httpx.Client(proxy=proxy, timeout=timeout) | ||
self.headers = { | ||
'Content-Type': 'application/json', | ||
'Accept': 'application/json', | ||
'Authorization': f'Bearer {self.jdata["api_key"]}', | ||
} | ||
|
||
def create(self, message, max_tokens=1024, temperature=0.95, top_p=0.7, stream=False): | ||
jmsg = { | ||
'model': self.model, | ||
'messages': [{ | ||
"role": "user", | ||
"content": message, | ||
}], | ||
'max_tokens': max_tokens, | ||
'temperature': temperature, | ||
'top_p': top_p, | ||
'stream': stream, | ||
} | ||
r = self.client.post(self.url, headers=self.headers, json=jmsg) | ||
return r.json() | ||
|
||
class Chat(Completion): | ||
def __init__(self, model='glm-4-flash', history=[], proxy=None, timeout=None): | ||
super().__init__(model=model, proxy=proxy, timeout=timeout) | ||
self.history = history | ||
|
||
def chat(self, message, max_tokens=1024, temperature=0.95, top_p=0.7, stream=False): | ||
self.history.append({ | ||
'role': 'user', | ||
'content': message, | ||
}) | ||
|
||
jmsg = { | ||
'model': self.model, | ||
'messages': self.history, | ||
'max_tokens': max_tokens, | ||
'temperature': temperature, | ||
'top_p': top_p, | ||
'stream': stream, | ||
} | ||
r = self.client.post(self.url, headers=self.headers, json=jmsg) | ||
r = r.json() | ||
|
||
if 'choices' in r: | ||
assistant_output = r['choices'][0]['message'] | ||
self.history.append(assistant_output) | ||
|
||
return r |
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,8 @@ | ||
from chatchat.zhipu import Chat | ||
|
||
chat = Chat(model='glm-3-turbo') | ||
while True: | ||
user = input('user: ') | ||
r = chat.chat(user) | ||
message = r['choices'][0]['message'] | ||
print(f"{message['role']}: {message['content']}") |
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,19 @@ | ||
from chatchat.zhipu import Completion | ||
|
||
completion = Completion(model='glm-3-turbo') | ||
r = completion.create('Hi', max_tokens=64) | ||
# { | ||
# 'choices': [ | ||
# { | ||
# 'finish_reason': 'stop', 'index': 0, 'message': | ||
# { | ||
# 'content': "Hello 👋! I'm ChatGLM(智谱清言).", | ||
# 'role': 'assistant' | ||
# } | ||
# } | ||
# ], | ||
# 'created': 1111, 'id': '2222', 'model': 'glm-3-turbo', 'request_id': '8730203825971097305', 'usage': { | ||
# 'completion_tokens': 39, 'prompt_tokens': 6, 'total_tokens': 45 | ||
# } | ||
# } | ||
print(r) |
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