2023-12-30 10:53:33 +00:00
|
|
|
from fastapi import FastAPI, Depends
|
|
|
|
from fastapi.routing import APIRoute
|
2024-06-24 18:17:18 +00:00
|
|
|
from fastapi.responses import StreamingResponse
|
2023-11-19 00:47:12 +00:00
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
2024-05-26 07:37:09 +00:00
|
|
|
from starlette.middleware.sessions import SessionMiddleware
|
2024-06-18 13:03:31 +00:00
|
|
|
from sqlalchemy.orm import Session
|
2024-05-26 08:15:48 +00:00
|
|
|
from apps.webui.routers import (
|
2024-01-08 07:43:32 +00:00
|
|
|
auths,
|
|
|
|
users,
|
|
|
|
chats,
|
|
|
|
documents,
|
2024-06-11 03:39:55 +00:00
|
|
|
tools,
|
2024-05-24 07:26:00 +00:00
|
|
|
models,
|
2024-01-08 07:43:32 +00:00
|
|
|
prompts,
|
|
|
|
configs,
|
2024-05-19 15:00:07 +00:00
|
|
|
memories,
|
2024-01-08 07:43:32 +00:00
|
|
|
utils,
|
2024-06-18 18:36:55 +00:00
|
|
|
files,
|
2024-06-20 07:49:11 +00:00
|
|
|
functions,
|
2024-01-08 07:43:32 +00:00
|
|
|
)
|
2024-06-20 11:38:59 +00:00
|
|
|
from apps.webui.models.functions import Functions
|
2024-07-04 20:41:18 +00:00
|
|
|
from apps.webui.models.models import Models
|
2024-06-20 11:38:59 +00:00
|
|
|
from apps.webui.utils import load_function_module_by_id
|
2024-07-04 20:41:18 +00:00
|
|
|
|
2024-06-24 18:17:18 +00:00
|
|
|
from utils.misc import stream_message_template
|
2024-07-04 20:41:18 +00:00
|
|
|
from utils.task import prompt_template
|
|
|
|
|
2024-06-20 11:38:59 +00:00
|
|
|
|
2024-02-14 09:17:43 +00:00
|
|
|
from config import (
|
2024-05-26 07:49:30 +00:00
|
|
|
WEBUI_BUILD_HASH,
|
2024-06-04 04:17:43 +00:00
|
|
|
SHOW_ADMIN_DETAILS,
|
|
|
|
ADMIN_EMAIL,
|
2024-02-14 09:17:43 +00:00
|
|
|
WEBUI_AUTH,
|
|
|
|
DEFAULT_MODELS,
|
|
|
|
DEFAULT_PROMPT_SUGGESTIONS,
|
|
|
|
DEFAULT_USER_ROLE,
|
|
|
|
ENABLE_SIGNUP,
|
|
|
|
USER_PERMISSIONS,
|
2024-03-21 01:35:02 +00:00
|
|
|
WEBHOOK_URL,
|
2024-03-26 21:30:53 +00:00
|
|
|
WEBUI_AUTH_TRUSTED_EMAIL_HEADER,
|
2024-06-15 20:04:11 +00:00
|
|
|
WEBUI_AUTH_TRUSTED_NAME_HEADER,
|
2024-05-10 05:36:10 +00:00
|
|
|
JWT_EXPIRES_IN,
|
2024-05-26 19:18:43 +00:00
|
|
|
WEBUI_BANNERS,
|
2024-05-26 16:10:25 +00:00
|
|
|
ENABLE_COMMUNITY_SHARING,
|
2024-06-11 03:39:55 +00:00
|
|
|
AppConfig,
|
2024-06-28 13:31:40 +00:00
|
|
|
OAUTH_USERNAME_CLAIM,
|
2024-07-01 07:36:21 +00:00
|
|
|
OAUTH_PICTURE_CLAIM,
|
2024-02-14 09:17:43 +00:00
|
|
|
)
|
2023-11-19 00:47:12 +00:00
|
|
|
|
2024-07-11 22:20:56 +00:00
|
|
|
from apps.socket.main import get_event_call, get_event_emitter
|
|
|
|
|
2024-06-24 18:17:18 +00:00
|
|
|
import inspect
|
|
|
|
import uuid
|
|
|
|
import time
|
|
|
|
import json
|
|
|
|
|
2024-07-11 20:43:44 +00:00
|
|
|
from typing import Iterator, Generator, Optional
|
2024-06-24 18:17:18 +00:00
|
|
|
from pydantic import BaseModel
|
|
|
|
|
2023-11-19 00:47:12 +00:00
|
|
|
app = FastAPI()
|
|
|
|
|
|
|
|
origins = ["*"]
|
|
|
|
|
2024-05-10 07:03:24 +00:00
|
|
|
app.state.config = AppConfig()
|
2024-02-20 04:44:00 +00:00
|
|
|
|
2024-05-10 07:03:24 +00:00
|
|
|
app.state.config.ENABLE_SIGNUP = ENABLE_SIGNUP
|
|
|
|
app.state.config.JWT_EXPIRES_IN = JWT_EXPIRES_IN
|
2024-06-11 05:38:48 +00:00
|
|
|
app.state.AUTH_TRUSTED_EMAIL_HEADER = WEBUI_AUTH_TRUSTED_EMAIL_HEADER
|
2024-06-15 20:04:11 +00:00
|
|
|
app.state.AUTH_TRUSTED_NAME_HEADER = WEBUI_AUTH_TRUSTED_NAME_HEADER
|
2024-05-10 07:03:24 +00:00
|
|
|
|
2024-06-04 04:17:43 +00:00
|
|
|
|
|
|
|
app.state.config.SHOW_ADMIN_DETAILS = SHOW_ADMIN_DETAILS
|
|
|
|
app.state.config.ADMIN_EMAIL = ADMIN_EMAIL
|
|
|
|
|
|
|
|
|
2024-05-10 07:03:24 +00:00
|
|
|
app.state.config.DEFAULT_MODELS = DEFAULT_MODELS
|
|
|
|
app.state.config.DEFAULT_PROMPT_SUGGESTIONS = DEFAULT_PROMPT_SUGGESTIONS
|
|
|
|
app.state.config.DEFAULT_USER_ROLE = DEFAULT_USER_ROLE
|
|
|
|
app.state.config.USER_PERMISSIONS = USER_PERMISSIONS
|
|
|
|
app.state.config.WEBHOOK_URL = WEBHOOK_URL
|
2024-05-26 19:18:43 +00:00
|
|
|
app.state.config.BANNERS = WEBUI_BANNERS
|
2024-05-25 01:26:36 +00:00
|
|
|
|
2024-05-26 16:10:25 +00:00
|
|
|
app.state.config.ENABLE_COMMUNITY_SHARING = ENABLE_COMMUNITY_SHARING
|
2024-05-25 01:26:36 +00:00
|
|
|
|
2024-06-28 13:31:40 +00:00
|
|
|
app.state.config.OAUTH_USERNAME_CLAIM = OAUTH_USERNAME_CLAIM
|
|
|
|
app.state.config.OAUTH_PICTURE_CLAIM = OAUTH_PICTURE_CLAIM
|
|
|
|
|
2024-05-25 01:26:36 +00:00
|
|
|
app.state.MODELS = {}
|
2024-06-11 05:38:48 +00:00
|
|
|
app.state.TOOLS = {}
|
2024-06-20 07:37:02 +00:00
|
|
|
app.state.FUNCTIONS = {}
|
2024-05-19 15:00:07 +00:00
|
|
|
|
2023-11-19 00:47:12 +00:00
|
|
|
app.add_middleware(
|
|
|
|
CORSMiddleware,
|
|
|
|
allow_origins=origins,
|
|
|
|
allow_credentials=True,
|
|
|
|
allow_methods=["*"],
|
|
|
|
allow_headers=["*"],
|
|
|
|
)
|
|
|
|
|
2024-06-20 07:49:11 +00:00
|
|
|
|
|
|
|
app.include_router(configs.router, prefix="/configs", tags=["configs"])
|
2023-11-19 00:47:12 +00:00
|
|
|
app.include_router(auths.router, prefix="/auths", tags=["auths"])
|
2024-01-01 08:55:50 +00:00
|
|
|
app.include_router(users.router, prefix="/users", tags=["users"])
|
|
|
|
app.include_router(chats.router, prefix="/chats", tags=["chats"])
|
2024-05-19 15:00:07 +00:00
|
|
|
|
2024-01-08 07:43:32 +00:00
|
|
|
app.include_router(documents.router, prefix="/documents", tags=["documents"])
|
2024-05-24 07:26:00 +00:00
|
|
|
app.include_router(models.router, prefix="/models", tags=["models"])
|
2024-01-03 05:00:50 +00:00
|
|
|
app.include_router(prompts.router, prefix="/prompts", tags=["prompts"])
|
2024-06-20 07:49:11 +00:00
|
|
|
|
2024-05-19 15:00:07 +00:00
|
|
|
app.include_router(memories.router, prefix="/memories", tags=["memories"])
|
2024-06-20 07:49:11 +00:00
|
|
|
app.include_router(files.router, prefix="/files", tags=["files"])
|
|
|
|
app.include_router(tools.router, prefix="/tools", tags=["tools"])
|
|
|
|
app.include_router(functions.router, prefix="/functions", tags=["functions"])
|
2024-05-19 15:00:07 +00:00
|
|
|
|
2023-12-27 06:10:22 +00:00
|
|
|
app.include_router(utils.router, prefix="/utils", tags=["utils"])
|
2023-11-19 00:47:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.get("/")
|
|
|
|
async def get_status():
|
2024-01-03 00:48:10 +00:00
|
|
|
return {
|
|
|
|
"status": True,
|
|
|
|
"auth": WEBUI_AUTH,
|
2024-05-10 07:03:24 +00:00
|
|
|
"default_models": app.state.config.DEFAULT_MODELS,
|
|
|
|
"default_prompt_suggestions": app.state.config.DEFAULT_PROMPT_SUGGESTIONS,
|
2024-01-03 00:48:10 +00:00
|
|
|
}
|
2024-06-20 11:38:59 +00:00
|
|
|
|
|
|
|
|
2024-06-21 12:58:57 +00:00
|
|
|
async def get_pipe_models():
|
|
|
|
pipes = Functions.get_functions_by_type("pipe", active_only=True)
|
2024-06-20 11:38:59 +00:00
|
|
|
pipe_models = []
|
|
|
|
|
|
|
|
for pipe in pipes:
|
|
|
|
# Check if function is already loaded
|
|
|
|
if pipe.id not in app.state.FUNCTIONS:
|
2024-06-24 03:31:40 +00:00
|
|
|
function_module, function_type, frontmatter = load_function_module_by_id(
|
|
|
|
pipe.id
|
|
|
|
)
|
2024-06-20 11:38:59 +00:00
|
|
|
app.state.FUNCTIONS[pipe.id] = function_module
|
|
|
|
else:
|
|
|
|
function_module = app.state.FUNCTIONS[pipe.id]
|
|
|
|
|
2024-06-24 17:37:57 +00:00
|
|
|
if hasattr(function_module, "valves") and hasattr(function_module, "Valves"):
|
|
|
|
valves = Functions.get_function_valves_by_id(pipe.id)
|
|
|
|
function_module.valves = function_module.Valves(
|
|
|
|
**(valves if valves else {})
|
|
|
|
)
|
|
|
|
|
2024-06-20 11:38:59 +00:00
|
|
|
# Check if function is a manifold
|
|
|
|
if hasattr(function_module, "type"):
|
|
|
|
if function_module.type == "manifold":
|
|
|
|
manifold_pipes = []
|
|
|
|
|
|
|
|
# Check if pipes is a function or a list
|
2024-06-20 11:47:40 +00:00
|
|
|
if callable(function_module.pipes):
|
|
|
|
manifold_pipes = function_module.pipes()
|
2024-06-20 11:38:59 +00:00
|
|
|
else:
|
2024-06-20 11:47:40 +00:00
|
|
|
manifold_pipes = function_module.pipes
|
2024-06-20 11:38:59 +00:00
|
|
|
|
|
|
|
for p in manifold_pipes:
|
|
|
|
manifold_pipe_id = f'{pipe.id}.{p["id"]}'
|
|
|
|
manifold_pipe_name = p["name"]
|
|
|
|
|
2024-06-20 11:47:40 +00:00
|
|
|
if hasattr(function_module, "name"):
|
2024-06-22 08:39:53 +00:00
|
|
|
manifold_pipe_name = (
|
|
|
|
f"{function_module.name}{manifold_pipe_name}"
|
|
|
|
)
|
2024-06-20 11:38:59 +00:00
|
|
|
|
2024-07-11 23:24:59 +00:00
|
|
|
pipe_flag = {"type": pipe.type}
|
|
|
|
if hasattr(function_module, "ChatValves"):
|
|
|
|
pipe_flag["valves_spec"] = function_module.ChatValves.schema()
|
|
|
|
|
2024-06-20 11:38:59 +00:00
|
|
|
pipe_models.append(
|
|
|
|
{
|
|
|
|
"id": manifold_pipe_id,
|
|
|
|
"name": manifold_pipe_name,
|
|
|
|
"object": "model",
|
|
|
|
"created": pipe.created_at,
|
|
|
|
"owned_by": "openai",
|
2024-07-11 23:24:59 +00:00
|
|
|
"pipe": pipe_flag,
|
2024-06-20 11:38:59 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
else:
|
2024-07-11 23:24:59 +00:00
|
|
|
pipe_flag = {"type": "pipe"}
|
|
|
|
if hasattr(function_module, "ChatValves"):
|
|
|
|
pipe_flag["valves_spec"] = function_module.ChatValves.schema()
|
|
|
|
|
2024-06-20 11:38:59 +00:00
|
|
|
pipe_models.append(
|
|
|
|
{
|
|
|
|
"id": pipe.id,
|
|
|
|
"name": pipe.name,
|
|
|
|
"object": "model",
|
|
|
|
"created": pipe.created_at,
|
|
|
|
"owned_by": "openai",
|
2024-07-11 23:24:59 +00:00
|
|
|
"pipe": pipe_flag,
|
2024-06-20 11:38:59 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return pipe_models
|
2024-06-24 18:17:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def generate_function_chat_completion(form_data, user):
|
2024-07-04 20:41:18 +00:00
|
|
|
model_id = form_data.get("model")
|
|
|
|
model_info = Models.get_model_by_id(model_id)
|
|
|
|
|
2024-07-11 20:43:44 +00:00
|
|
|
metadata = None
|
|
|
|
if "metadata" in form_data:
|
|
|
|
metadata = form_data["metadata"]
|
|
|
|
del form_data["metadata"]
|
|
|
|
|
2024-07-11 22:20:56 +00:00
|
|
|
__event_emitter__ = None
|
|
|
|
__event_call__ = None
|
|
|
|
__task__ = None
|
|
|
|
|
2024-07-11 20:43:44 +00:00
|
|
|
if metadata:
|
2024-07-11 22:20:56 +00:00
|
|
|
if (
|
|
|
|
metadata.get("session_id")
|
|
|
|
and metadata.get("chat_id")
|
|
|
|
and metadata.get("message_id")
|
|
|
|
):
|
|
|
|
__event_emitter__ = await get_event_emitter(metadata)
|
|
|
|
__event_call__ = await get_event_call(metadata)
|
|
|
|
|
|
|
|
if metadata.get("task"):
|
|
|
|
__task__ = metadata.get("task")
|
2024-07-11 20:43:44 +00:00
|
|
|
|
2024-07-04 20:41:18 +00:00
|
|
|
if model_info:
|
|
|
|
if model_info.base_model_id:
|
|
|
|
form_data["model"] = model_info.base_model_id
|
|
|
|
|
|
|
|
model_info.params = model_info.params.model_dump()
|
|
|
|
|
|
|
|
if model_info.params:
|
|
|
|
if model_info.params.get("temperature", None) is not None:
|
|
|
|
form_data["temperature"] = float(model_info.params.get("temperature"))
|
|
|
|
|
|
|
|
if model_info.params.get("top_p", None):
|
|
|
|
form_data["top_p"] = int(model_info.params.get("top_p", None))
|
|
|
|
|
|
|
|
if model_info.params.get("max_tokens", None):
|
|
|
|
form_data["max_tokens"] = int(model_info.params.get("max_tokens", None))
|
|
|
|
|
|
|
|
if model_info.params.get("frequency_penalty", None):
|
|
|
|
form_data["frequency_penalty"] = int(
|
|
|
|
model_info.params.get("frequency_penalty", None)
|
|
|
|
)
|
|
|
|
|
|
|
|
if model_info.params.get("seed", None):
|
|
|
|
form_data["seed"] = model_info.params.get("seed", None)
|
|
|
|
|
|
|
|
if model_info.params.get("stop", None):
|
|
|
|
form_data["stop"] = (
|
|
|
|
[
|
|
|
|
bytes(stop, "utf-8").decode("unicode_escape")
|
|
|
|
for stop in model_info.params["stop"]
|
|
|
|
]
|
|
|
|
if model_info.params.get("stop", None)
|
|
|
|
else None
|
|
|
|
)
|
|
|
|
|
|
|
|
system = model_info.params.get("system", None)
|
|
|
|
if system:
|
|
|
|
system = prompt_template(
|
|
|
|
system,
|
|
|
|
**(
|
|
|
|
{
|
|
|
|
"user_name": user.name,
|
|
|
|
"user_location": (
|
|
|
|
user.info.get("location") if user.info else None
|
|
|
|
),
|
|
|
|
}
|
|
|
|
if user
|
|
|
|
else {}
|
|
|
|
),
|
|
|
|
)
|
|
|
|
# Check if the payload already has a system message
|
|
|
|
# If not, add a system message to the payload
|
|
|
|
if form_data.get("messages"):
|
|
|
|
for message in form_data["messages"]:
|
|
|
|
if message.get("role") == "system":
|
|
|
|
message["content"] = system + message["content"]
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
form_data["messages"].insert(
|
|
|
|
0,
|
|
|
|
{
|
|
|
|
"role": "system",
|
|
|
|
"content": system,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
else:
|
|
|
|
pass
|
|
|
|
|
2024-06-24 18:17:18 +00:00
|
|
|
async def job():
|
|
|
|
pipe_id = form_data["model"]
|
|
|
|
if "." in pipe_id:
|
|
|
|
pipe_id, sub_pipe_id = pipe_id.split(".", 1)
|
|
|
|
print(pipe_id)
|
|
|
|
|
|
|
|
# Check if function is already loaded
|
|
|
|
if pipe_id not in app.state.FUNCTIONS:
|
|
|
|
function_module, function_type, frontmatter = load_function_module_by_id(
|
|
|
|
pipe_id
|
|
|
|
)
|
|
|
|
app.state.FUNCTIONS[pipe_id] = function_module
|
|
|
|
else:
|
|
|
|
function_module = app.state.FUNCTIONS[pipe_id]
|
|
|
|
|
|
|
|
if hasattr(function_module, "valves") and hasattr(function_module, "Valves"):
|
|
|
|
|
|
|
|
valves = Functions.get_function_valves_by_id(pipe_id)
|
|
|
|
function_module.valves = function_module.Valves(
|
|
|
|
**(valves if valves else {})
|
|
|
|
)
|
|
|
|
|
|
|
|
pipe = function_module.pipe
|
|
|
|
|
|
|
|
# Get the signature of the function
|
|
|
|
sig = inspect.signature(pipe)
|
|
|
|
params = {"body": form_data}
|
|
|
|
|
|
|
|
if "__user__" in sig.parameters:
|
|
|
|
__user__ = {
|
|
|
|
"id": user.id,
|
|
|
|
"email": user.email,
|
|
|
|
"name": user.name,
|
|
|
|
"role": user.role,
|
|
|
|
}
|
|
|
|
|
|
|
|
try:
|
|
|
|
if hasattr(function_module, "UserValves"):
|
|
|
|
__user__["valves"] = function_module.UserValves(
|
|
|
|
**Functions.get_user_valves_by_id_and_user_id(pipe_id, user.id)
|
|
|
|
)
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
|
|
|
|
params = {**params, "__user__": __user__}
|
|
|
|
|
2024-07-11 22:20:56 +00:00
|
|
|
if "__event_emitter__" in sig.parameters:
|
|
|
|
params = {**params, "__event_emitter__": __event_emitter__}
|
|
|
|
|
|
|
|
if "__event_call__" in sig.parameters:
|
|
|
|
params = {**params, "__event_call__": __event_call__}
|
|
|
|
|
|
|
|
if "__task__" in sig.parameters:
|
|
|
|
params = {**params, "__task__": __task__}
|
|
|
|
|
2024-06-24 18:17:18 +00:00
|
|
|
if form_data["stream"]:
|
|
|
|
|
|
|
|
async def stream_content():
|
|
|
|
try:
|
|
|
|
if inspect.iscoroutinefunction(pipe):
|
|
|
|
res = await pipe(**params)
|
|
|
|
else:
|
|
|
|
res = pipe(**params)
|
2024-06-24 19:56:41 +00:00
|
|
|
|
|
|
|
# Directly return if the response is a StreamingResponse
|
|
|
|
if isinstance(res, StreamingResponse):
|
|
|
|
async for data in res.body_iterator:
|
|
|
|
yield data
|
|
|
|
return
|
|
|
|
if isinstance(res, dict):
|
|
|
|
yield f"data: {json.dumps(res)}\n\n"
|
|
|
|
return
|
|
|
|
|
2024-06-24 18:17:18 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(f"Error: {e}")
|
|
|
|
yield f"data: {json.dumps({'error': {'detail':str(e)}})}\n\n"
|
|
|
|
return
|
|
|
|
|
|
|
|
if isinstance(res, str):
|
|
|
|
message = stream_message_template(form_data["model"], res)
|
|
|
|
yield f"data: {json.dumps(message)}\n\n"
|
|
|
|
|
|
|
|
if isinstance(res, Iterator):
|
|
|
|
for line in res:
|
|
|
|
if isinstance(line, BaseModel):
|
|
|
|
line = line.model_dump_json()
|
|
|
|
line = f"data: {line}"
|
2024-07-01 05:28:43 +00:00
|
|
|
if isinstance(line, dict):
|
|
|
|
line = f"data: {json.dumps(line)}"
|
|
|
|
|
2024-06-24 18:17:18 +00:00
|
|
|
try:
|
|
|
|
line = line.decode("utf-8")
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if line.startswith("data:"):
|
|
|
|
yield f"{line}\n\n"
|
|
|
|
else:
|
|
|
|
line = stream_message_template(form_data["model"], line)
|
|
|
|
yield f"data: {json.dumps(line)}\n\n"
|
|
|
|
|
|
|
|
if isinstance(res, str) or isinstance(res, Generator):
|
|
|
|
finish_message = {
|
|
|
|
"id": f"{form_data['model']}-{str(uuid.uuid4())}",
|
|
|
|
"object": "chat.completion.chunk",
|
|
|
|
"created": int(time.time()),
|
|
|
|
"model": form_data["model"],
|
|
|
|
"choices": [
|
|
|
|
{
|
|
|
|
"index": 0,
|
|
|
|
"delta": {},
|
|
|
|
"logprobs": None,
|
|
|
|
"finish_reason": "stop",
|
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
yield f"data: {json.dumps(finish_message)}\n\n"
|
|
|
|
yield f"data: [DONE]"
|
|
|
|
|
|
|
|
return StreamingResponse(stream_content(), media_type="text/event-stream")
|
|
|
|
else:
|
|
|
|
|
|
|
|
try:
|
|
|
|
if inspect.iscoroutinefunction(pipe):
|
|
|
|
res = await pipe(**params)
|
|
|
|
else:
|
|
|
|
res = pipe(**params)
|
2024-06-24 19:56:41 +00:00
|
|
|
|
|
|
|
if isinstance(res, StreamingResponse):
|
|
|
|
return res
|
2024-06-24 18:17:18 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(f"Error: {e}")
|
|
|
|
return {"error": {"detail": str(e)}}
|
|
|
|
|
|
|
|
if isinstance(res, dict):
|
|
|
|
return res
|
|
|
|
elif isinstance(res, BaseModel):
|
|
|
|
return res.model_dump()
|
|
|
|
else:
|
|
|
|
message = ""
|
|
|
|
if isinstance(res, str):
|
|
|
|
message = res
|
|
|
|
if isinstance(res, Generator):
|
|
|
|
for stream in res:
|
|
|
|
message = f"{message}{stream}"
|
|
|
|
|
|
|
|
return {
|
|
|
|
"id": f"{form_data['model']}-{str(uuid.uuid4())}",
|
|
|
|
"object": "chat.completion",
|
|
|
|
"created": int(time.time()),
|
|
|
|
"model": form_data["model"],
|
|
|
|
"choices": [
|
|
|
|
{
|
|
|
|
"index": 0,
|
|
|
|
"message": {
|
|
|
|
"role": "assistant",
|
|
|
|
"content": message,
|
|
|
|
},
|
|
|
|
"logprobs": None,
|
|
|
|
"finish_reason": "stop",
|
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
return await job()
|