fix some errors for auto-completion

This commit is contained in:
shaobo 2024-07-16 18:55:27 +08:00
parent 7c3cbf6b33
commit 5fe6c27ade
2 changed files with 7 additions and 6 deletions

View File

@ -43,24 +43,23 @@ class CodegeexChatModel:
"do_sample": True if request.temperature else request.temperature,
}
length = 0
for i, outputs in enumerate(self.model.stream_generate(**inputs, **gen_configs)):
for outputs in self.model.stream_generate(**inputs, **gen_configs):
response = self.tokenizer.decode(outputs.tolist()[0][len(inputs["input_ids"][0]):-1])
if not response or response[-1] == "<EFBFBD>":
continue
resp = ChatCompletionStreamResponse()
resp.choices[0].index = i
resp.choices[0].delta.content = response[length:]
event = Event(id=resp.id, data=resp.json(), event='message')
event = Event(data=resp.json(), event='message')
yield event.dump()
length = len(response)
resp = ChatCompletionStreamResponse()
resp.choices[0].finish_reason = 'stop'
event = Event(id=resp.id, data=resp.json(), event='message')
event = Event(data=resp.json(), event='message')
yield event.dump()
except Exception as e:
resp = ChatCompletionStreamResponse()
resp.choices[0].finish_reason = 'stop'
event = Event(id=resp.id, data=f"请求报错,错误原因:{e}", event='message')
event = Event(data=f"请求报错,错误原因:{e}", event='message')
yield event.dump()
def chat(self, request: ChatCompletionRequest):

View File

@ -10,6 +10,8 @@ from typing import Literal
import shortuuid
from pydantic import BaseModel
completion_id = f"chatcmpl-{shortuuid.random()}"
class ChatMessage(BaseModel):
role: str
@ -39,7 +41,7 @@ class ChatCompletionResponseStreamChoice(BaseModel):
class ChatCompletionStreamResponse(BaseModel):
id: str = f"chatcmpl-{shortuuid.random()}"
id: str = completion_id
object: str = "chat.completion.chunk"
created: int = int(time.time())
model: str = "codegeex4"