mirror of
https://github.com/JasonYANG170/CodeGeeX4.git
synced 2024-11-23 12:16:33 +00:00
fix some errors for auto-completion
This commit is contained in:
parent
7c3cbf6b33
commit
5fe6c27ade
|
@ -43,24 +43,23 @@ class CodegeexChatModel:
|
||||||
"do_sample": True if request.temperature else request.temperature,
|
"do_sample": True if request.temperature else request.temperature,
|
||||||
}
|
}
|
||||||
length = 0
|
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])
|
response = self.tokenizer.decode(outputs.tolist()[0][len(inputs["input_ids"][0]):-1])
|
||||||
if not response or response[-1] == "<EFBFBD>":
|
if not response or response[-1] == "<EFBFBD>":
|
||||||
continue
|
continue
|
||||||
resp = ChatCompletionStreamResponse()
|
resp = ChatCompletionStreamResponse()
|
||||||
resp.choices[0].index = i
|
|
||||||
resp.choices[0].delta.content = response[length:]
|
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()
|
yield event.dump()
|
||||||
length = len(response)
|
length = len(response)
|
||||||
resp = ChatCompletionStreamResponse()
|
resp = ChatCompletionStreamResponse()
|
||||||
resp.choices[0].finish_reason = 'stop'
|
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()
|
yield event.dump()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
resp = ChatCompletionStreamResponse()
|
resp = ChatCompletionStreamResponse()
|
||||||
resp.choices[0].finish_reason = 'stop'
|
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()
|
yield event.dump()
|
||||||
|
|
||||||
def chat(self, request: ChatCompletionRequest):
|
def chat(self, request: ChatCompletionRequest):
|
||||||
|
|
|
@ -10,6 +10,8 @@ from typing import Literal
|
||||||
import shortuuid
|
import shortuuid
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
completion_id = f"chatcmpl-{shortuuid.random()}"
|
||||||
|
|
||||||
|
|
||||||
class ChatMessage(BaseModel):
|
class ChatMessage(BaseModel):
|
||||||
role: str
|
role: str
|
||||||
|
@ -39,7 +41,7 @@ class ChatCompletionResponseStreamChoice(BaseModel):
|
||||||
|
|
||||||
|
|
||||||
class ChatCompletionStreamResponse(BaseModel):
|
class ChatCompletionStreamResponse(BaseModel):
|
||||||
id: str = f"chatcmpl-{shortuuid.random()}"
|
id: str = completion_id
|
||||||
object: str = "chat.completion.chunk"
|
object: str = "chat.completion.chunk"
|
||||||
created: int = int(time.time())
|
created: int = int(time.time())
|
||||||
model: str = "codegeex4"
|
model: str = "codegeex4"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user