-
Notifications
You must be signed in to change notification settings - Fork 1
/
chatglm.py
185 lines (169 loc) · 5.54 KB
/
chatglm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import asyncio
import json
import logging
import time
import os
import uuid
import chatglm_cpp
import llama_cpp
from sse_starlette.sse import EventSourceResponse
from pprint import pprint
from fastapi import HTTPException, status
from llama_cpp.server.types import ChatCompletionRequestMessage
from llama_cpp.llama_types import (
ChatCompletionResponseChoice,
ChatCompletionMessageToolCall,
ChatCompletionStreamResponseChoice,
CreateChatCompletionStreamResponse,
CompletionUsage,
ChatCompletionStreamResponseDelta,
CreateChatCompletionResponse,
ChatCompletionResponseMessage,
ChatCompletionRequestAssistantMessage,
ChatCompletionMessageToolCallFunction,
ChatCompletionStreamResponseDeltaEmpty,
)
def _buid_msg(body: ChatCompletionRequestMessage):
messages = []
if body.tools:
system_content = (
"Answer the following questions as best as you can. You have access to the following tools:\n"
+ json.dumps(body.tools, indent=4)
)
messages.insert(
0, chatglm_cpp.ChatMessage(role="system", content=system_content)
)
for msg in body.messages:
role = msg["role"]
if role not in ["user", "assistant", "system", "observation"]:
role = "observation"
content = msg["content"]
if isinstance(content, str):
messages.append(chatglm_cpp.ChatMessage(role=role, content=content))
else:
for text in content:
messages.append(chatglm_cpp.ChatMessage(role=role, content=text["text"] ))
return messages
def stream_chat(
chatglm_pipeline: chatglm_cpp.Pipeline,
body: ChatCompletionRequestMessage,
max_context_length: int,
num_threads: int,
):
max_tokens = 1024
if body.max_tokens:
max_tokens = body.max_tokens
for chunk in chatglm_pipeline.chat(
messages=_buid_msg(body),
max_length=max_tokens,
max_context_length=max_context_length,
do_sample=body.temperature > 0,
top_p=body.top_p,
temperature=body.temperature,
num_threads=num_threads,
stream=True,
):
choices = [
ChatCompletionStreamResponseChoice(
index=1,
delta=ChatCompletionStreamResponseDelta(
content=chunk.content, role=chunk.role
),
finish_reason=None,
logprobs=None,
)
]
chunk = llama_cpp.ChatCompletionChunk(
id="chatcmpl-" + uuid.uuid4().hex,
model=body.model,
object="chat.completion.chunk",
created=int(time.time()),
choices=choices,
)
yield chunk
def create_chat_completion(
chatglm_pipeline: chatglm_cpp.Pipeline,
body: ChatCompletionRequestMessage,
max_context_length: int,
num_threads: int,
) -> CreateChatCompletionResponse:
def to_json_arguments(arguments):
def tool_call(**kwargs):
return kwargs
try:
return json.dumps(eval(arguments, dict(tool_call=tool_call)))
except Exception:
return arguments
if not body.messages:
raise HTTPException(status.HTTP_400_BAD_REQUEST, "empty messages")
max_tokens = 2048
if body.max_tokens:
max_tokens = body.max_tokens
messages = _buid_msg(body)
output = chatglm_pipeline.chat(
messages=messages,
max_length=max_tokens,
max_context_length=max_context_length,
do_sample=body.temperature > 0,
top_p=body.top_p,
temperature=body.temperature,
# num_threads=num_threads,
)
print("raw output: ", output)
prompt_tokens = len(
chatglm_pipeline.tokenizer.apply_chat_template(messages, max_context_length)
)
completion_tokens = len(
chatglm_pipeline.tokenizer.encode(output.content, max_tokens)
)
finish_reason = "stop"
tool_calls = None
if output.tool_calls:
tool_calls = [
ChatCompletionMessageToolCall(
id="tool_call_" + uuid.uuid4().hex,
type=tool_call.type,
function=ChatCompletionMessageToolCallFunction(
name=tool_call.function.name,
arguments=to_json_arguments(tool_call.function.arguments),
),
)
for tool_call in output.tool_calls
]
finish_reason = "tool_calls"
if tool_calls is None:
choices = [
ChatCompletionResponseChoice(
index=0,
message=ChatCompletionResponseMessage(
role="assistant", content=output.content
),
finish_reason=finish_reason,
logprobs=None,
)
]
else:
choices = [
ChatCompletionResponseChoice(
index=0,
message=ChatCompletionRequestAssistantMessage(
role="assistant", content=output.content, tool_calls=tool_calls
),
finish_reason=finish_reason,
logprobs=None,
)
]
response = CreateChatCompletionResponse(
id="chatcmpl",
object="chat.completion",
created=int(time.time()),
model="chatglm",
choices=choices,
usage=CompletionUsage(
prompt_tokens=prompt_tokens,
completion_tokens=completion_tokens,
total_tokens=prompt_tokens + completion_tokens,
),
)
print(response)
return response