mirror of
https://github.com/open-webui/pipelines
synced 2025-05-12 16:40:45 +00:00
feat: support stream=false
This commit is contained in:
parent
3fee0347a1
commit
32928c754e
32
main.py
32
main.py
@ -65,6 +65,8 @@ async def get_models():
|
|||||||
async def generate_openai_chat_completion(form_data: OpenAIChatCompletionForm):
|
async def generate_openai_chat_completion(form_data: OpenAIChatCompletionForm):
|
||||||
user_message = get_last_user_message(form_data.messages)
|
user_message = get_last_user_message(form_data.messages)
|
||||||
|
|
||||||
|
if form_data.stream:
|
||||||
|
|
||||||
def stream_content():
|
def stream_content():
|
||||||
res = get_response(user_message, messages=form_data.messages)
|
res = get_response(user_message, messages=form_data.messages)
|
||||||
|
|
||||||
@ -91,6 +93,36 @@ async def generate_openai_chat_completion(form_data: OpenAIChatCompletionForm):
|
|||||||
yield f"data: [DONE]"
|
yield f"data: [DONE]"
|
||||||
|
|
||||||
return StreamingResponse(stream_content(), media_type="text/event-stream")
|
return StreamingResponse(stream_content(), media_type="text/event-stream")
|
||||||
|
else:
|
||||||
|
|
||||||
|
res = get_response(user_message, messages=form_data.messages)
|
||||||
|
|
||||||
|
message = ""
|
||||||
|
|
||||||
|
if isinstance(res, str):
|
||||||
|
message = stream_message_template(res)
|
||||||
|
|
||||||
|
elif isinstance(res, Generator):
|
||||||
|
for stream in res:
|
||||||
|
message = f"{message}{stream}"
|
||||||
|
|
||||||
|
return {
|
||||||
|
"id": f"rag-{str(uuid.uuid4())}",
|
||||||
|
"object": "chat.completion",
|
||||||
|
"created": int(time.time()),
|
||||||
|
"model": MODEL_ID,
|
||||||
|
"choices": [
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"message": {
|
||||||
|
"role": "assistant",
|
||||||
|
"content": message,
|
||||||
|
},
|
||||||
|
"logprobs": None,
|
||||||
|
"finish_reason": "stop",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
|
@ -10,6 +10,7 @@ class OpenAIChatMessage(BaseModel):
|
|||||||
|
|
||||||
|
|
||||||
class OpenAIChatCompletionForm(BaseModel):
|
class OpenAIChatCompletionForm(BaseModel):
|
||||||
|
stream: bool = True
|
||||||
model: str
|
model: str
|
||||||
messages: List[OpenAIChatMessage]
|
messages: List[OpenAIChatMessage]
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user