-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenai_api.py
341 lines (269 loc) · 10.3 KB
/
openai_api.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import numpy as np
import asyncio
import logging
import time
from typing import List, Literal, Optional, Union
import chatglm_cpp
import uvicorn
from fastapi import FastAPI, HTTPException, status
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field, computed_field
from pydantic_settings import BaseSettings
from sse_starlette.sse import EventSourceResponse
from transformers import AutoTokenizer, AutoModel
import torch
logging.basicConfig(level=logging.INFO,
format=r"%(asctime)s - %(module)s - %(levelname)s - %(message)s")
class Settings(BaseSettings):
model: str = ".\models\chatglm3\chatglm3-ggml-q4_0.bin"
embeddingModel: str = ".\models\shibing624\\text2vec-base-chinese"
num_threads: int = 0
settings = Settings()
embeddingTokenizer = AutoTokenizer.from_pretrained(settings.embeddingModel)
embeddingModel = AutoModel.from_pretrained(settings.embeddingModel)
pipeline = chatglm_cpp.Pipeline(settings.model)
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"]
)
lock = asyncio.Lock()
def average_embeddings(embeddings):
"""
计算嵌入向量列表的平均值。
:param embeddings: 嵌入向量的列表。
:return: 平均嵌入向量。
"""
# 将嵌入向量列表转换为 NumPy 数组
embeddings_array = np.array(embeddings)
# 计算平均值
avg_embedding = np.mean(embeddings_array, axis=0)
return avg_embedding.tolist()
def split_into_parts(text, max_length):
"""
将文本分割成多个部分,每部分长度不超过 max_length。
:param text: 要分割的原始文本。
:param max_length: 每部分文本的最大长度。
:return: 分割后的文本列表。
"""
# 确保文本是字符串
if not isinstance(text, str):
raise ValueError("Text must be a string.")
# 初始化变量
parts = []
current_part = ""
# 按空格分割文本,简单处理
for word in text.split():
if len(current_part) + len(word) + 1 > max_length:
parts.append(current_part)
current_part = word
else:
current_part += " " + word
# 添加最后一部分
if current_part:
parts.append(current_part)
return parts
class ModelCard(BaseModel):
id: str
object: Literal["model"] = "model"
owned_by: str = "owner"
permission: List = []
class ModelList(BaseModel):
object: Literal["list"] = "list"
data: List[ModelCard] = []
model_config = {
"json_schema_extra": {
"examples": [
{
"object": "list",
"data": [{"id": "gpt-3.5-turbo", "object": "model", "owned_by": "owner", "permission": []}],
}
]
}
}
class ChatMessage(BaseModel):
role: Literal["system", "user", "assistant"]
content: str
class DeltaMessage(BaseModel):
role: Optional[Literal["system", "user", "assistant"]] = None
content: Optional[str] = None
class ChatCompletionRequest(BaseModel):
model: str = "default-model"
messages: List[ChatMessage]
temperature: float = Field(default=0.95, ge=0.0, le=2.0)
top_p: float = Field(default=0.7, ge=0.0, le=1.0)
stream: bool = False
max_tokens: int = Field(default=2048, ge=0)
model_config = {
"json_schema_extra": {"examples": [{"model": "default-model", "messages": [{"role": "user", "content": "你好"}]}]}
}
class ChatCompletionResponseChoice(BaseModel):
index: int = 0
message: ChatMessage
finish_reason: Literal["stop", "length"] = "stop"
class ChatCompletionResponseStreamChoice(BaseModel):
index: int = 0
delta: DeltaMessage
finish_reason: Optional[Literal["stop", "length"]] = None
class ChatCompletionUsage(BaseModel):
prompt_tokens: int
completion_tokens: int
@computed_field
@property
def total_tokens(self) -> int:
return self.prompt_tokens + self.completion_tokens
class ChatCompletionResponse(BaseModel):
id: str = "chatcmpl"
model: str = "default-model"
object: Literal["chat.completion", "chat.completion.chunk"]
created: int = Field(default_factory=lambda: int(time.time()))
choices: Union[List[ChatCompletionResponseChoice],
List[ChatCompletionResponseStreamChoice]]
usage: Optional[ChatCompletionUsage] = None
model_config = {
"json_schema_extra": {
"examples": [
{
"id": "chatcmpl",
"model": "default-model",
"object": "chat.completion",
"created": 1691166146,
"choices": [
{
"index": 0,
"message": {"role": "assistant", "content": "你好👋!我是人工智能助手 ChatGLM2-6B,很高兴见到你,欢迎问我任何问题。"},
"finish_reason": "stop",
}
],
"usage": {"prompt_tokens": 17, "completion_tokens": 29, "total_tokens": 46},
}
]
}
}
class EmbeddingCreateParams(BaseModel):
input: Union[str, List[str], List[int], List[List[int]]]
model: str
encoding_format: Optional[str] = "float"
user: Optional[str] = None
class Embedding(BaseModel):
embedding: List[float]
index: int
object: str = "embedding"
class CreateEmbeddingResponse(BaseModel):
data: List[Embedding]
model: str
object: str = "list"
usage: dict
def stream_chat(messages, body):
yield ChatCompletionResponse(
object="chat.completion.chunk",
choices=[ChatCompletionResponseStreamChoice(
delta=DeltaMessage(role="assistant"))],
)
for chunk in pipeline.chat(
messages=messages,
max_length=body.max_tokens,
do_sample=body.temperature > 0,
top_p=body.top_p,
temperature=body.temperature,
num_threads=settings.num_threads,
stream=True,
):
yield ChatCompletionResponse(
object="chat.completion.chunk",
choices=[ChatCompletionResponseStreamChoice(
delta=DeltaMessage(content=chunk.content))],
)
yield ChatCompletionResponse(
object="chat.completion.chunk",
choices=[ChatCompletionResponseStreamChoice(
delta=DeltaMessage(), finish_reason="stop")],
)
async def stream_chat_event_publisher(history, body):
output = ""
try:
async with lock:
for chunk in stream_chat(history, body):
# yield control back to event loop for cancellation check
await asyncio.sleep(0)
output += chunk.choices[0].delta.content or ""
yield chunk.model_dump_json(exclude_unset=True)
logging.info(f'prompt: "{history[-1]}", stream response: "{output}"')
except asyncio.CancelledError as e:
logging.info(
f'prompt: "{history[-1]}", stream response (partial): "{output}"')
raise e
@app.post("/v1/chat/completions")
async def create_chat_completion(body: ChatCompletionRequest) -> ChatCompletionResponse:
if not body.messages:
raise HTTPException(status.HTTP_400_BAD_REQUEST, "empty messages")
logging.info(
f'prompt: "{body.messages[-1].content}"')
messages = [chatglm_cpp.ChatMessage(
role=msg.role, content=msg.content) for msg in body.messages]
if body.stream:
generator = stream_chat_event_publisher(messages, body)
return EventSourceResponse(generator)
max_context_length = 512
output = pipeline.chat(
messages=messages,
max_length=body.max_tokens,
max_context_length=max_context_length,
do_sample=body.temperature > 0,
top_p=body.top_p,
temperature=body.temperature,
)
logging.info(
f'sync response: "{output.content}"')
# 如果返回的结果长度小于1024,可能会导致进程结束,参考
# https://github.com/li-plus/chatglm.cpp/issues/244
if body.max_tokens < 1024:
body.max_tokens = 1024
prompt_tokens = len(pipeline.tokenizer.encode_messages(
messages, max_context_length))
completion_tokens = len(pipeline.tokenizer.encode(
output.content, body.max_tokens))
logging.info("Returning from function")
return ChatCompletionResponse(
object="chat.completion",
choices=[ChatCompletionResponseChoice(
message=ChatMessage(role="assistant", content=output.content))],
usage=ChatCompletionUsage(
prompt_tokens=prompt_tokens, completion_tokens=completion_tokens),
)
@app.post("/v1/embeddings")
def create_embeddings(params: EmbeddingCreateParams) -> CreateEmbeddingResponse:
inputs = params.input if isinstance(params.input, list) else [params.input]
embeddings = []
total_tokens = 0
for index, input_text in enumerate(inputs):
if isinstance(input_text, (str, list)):
# 分割文本
# 减去2是为了 [CLS] 和 [SEP] token
parts = split_into_parts(input_text, max_length=512-2)
embeddings_for_this_input = []
for part in parts:
inputs = embeddingTokenizer(
part, return_tensors="pt", padding='max_length', truncation=True, max_length=512)
total_tokens += inputs.input_ids.size(1)
with torch.no_grad():
outputs = embeddingModel(**inputs)
embedding = outputs.last_hidden_state.mean(
dim=1).squeeze().tolist()
embeddings_for_this_input.append(embedding)
# 对单个输入的所有部分进行平均或其他形式的合并
final_embedding = average_embeddings(embeddings_for_this_input)
embeddings.append(
Embedding(embedding=final_embedding, index=index))
return CreateEmbeddingResponse(
data=embeddings,
model=params.model,
usage={"prompt_tokens": total_tokens, "total_tokens": total_tokens}
)
@app.get("/v1/models")
async def list_models() -> ModelList:
return ModelList(data=[ModelCard(id="gpt-3.5-turbo"), ModelCard(id="text-embedding-ada-002")])
uvicorn.run(app, host='0.0.0.0', port=8000, workers=1)