tasks.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. from fastapi import APIRouter, Depends, HTTPException, Response, status, Request
  2. from fastapi.responses import JSONResponse, RedirectResponse
  3. from pydantic import BaseModel
  4. from typing import Optional
  5. import logging
  6. import re
  7. from open_webui.utils.chat import generate_chat_completion
  8. from open_webui.utils.task import (
  9. title_generation_template,
  10. query_generation_template,
  11. image_prompt_generation_template,
  12. autocomplete_generation_template,
  13. tags_generation_template,
  14. emoji_generation_template,
  15. moa_response_generation_template,
  16. )
  17. from open_webui.utils.auth import get_admin_user, get_verified_user
  18. from open_webui.constants import TASKS
  19. from open_webui.routers.pipelines import process_pipeline_inlet_filter
  20. from open_webui.utils.task import get_task_model_id
  21. from open_webui.config import (
  22. DEFAULT_TITLE_GENERATION_PROMPT_TEMPLATE,
  23. DEFAULT_TAGS_GENERATION_PROMPT_TEMPLATE,
  24. DEFAULT_IMAGE_PROMPT_GENERATION_PROMPT_TEMPLATE,
  25. DEFAULT_QUERY_GENERATION_PROMPT_TEMPLATE,
  26. DEFAULT_AUTOCOMPLETE_GENERATION_PROMPT_TEMPLATE,
  27. DEFAULT_EMOJI_GENERATION_PROMPT_TEMPLATE,
  28. DEFAULT_MOA_GENERATION_PROMPT_TEMPLATE,
  29. )
  30. from open_webui.env import SRC_LOG_LEVELS
  31. log = logging.getLogger(__name__)
  32. log.setLevel(SRC_LOG_LEVELS["MODELS"])
  33. router = APIRouter()
  34. ##################################
  35. #
  36. # Task Endpoints
  37. #
  38. ##################################
  39. @router.get("/config")
  40. async def get_task_config(request: Request, user=Depends(get_verified_user)):
  41. return {
  42. "TASK_MODEL": request.app.state.config.TASK_MODEL,
  43. "TASK_MODEL_EXTERNAL": request.app.state.config.TASK_MODEL_EXTERNAL,
  44. "TITLE_GENERATION_PROMPT_TEMPLATE": request.app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE,
  45. "IMAGE_PROMPT_GENERATION_PROMPT_TEMPLATE": request.app.state.config.IMAGE_PROMPT_GENERATION_PROMPT_TEMPLATE,
  46. "ENABLE_AUTOCOMPLETE_GENERATION": request.app.state.config.ENABLE_AUTOCOMPLETE_GENERATION,
  47. "AUTOCOMPLETE_GENERATION_INPUT_MAX_LENGTH": request.app.state.config.AUTOCOMPLETE_GENERATION_INPUT_MAX_LENGTH,
  48. "TAGS_GENERATION_PROMPT_TEMPLATE": request.app.state.config.TAGS_GENERATION_PROMPT_TEMPLATE,
  49. "ENABLE_TAGS_GENERATION": request.app.state.config.ENABLE_TAGS_GENERATION,
  50. "ENABLE_SEARCH_QUERY_GENERATION": request.app.state.config.ENABLE_SEARCH_QUERY_GENERATION,
  51. "ENABLE_RETRIEVAL_QUERY_GENERATION": request.app.state.config.ENABLE_RETRIEVAL_QUERY_GENERATION,
  52. "QUERY_GENERATION_PROMPT_TEMPLATE": request.app.state.config.QUERY_GENERATION_PROMPT_TEMPLATE,
  53. "TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE": request.app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE,
  54. }
  55. class TaskConfigForm(BaseModel):
  56. TASK_MODEL: Optional[str]
  57. TASK_MODEL_EXTERNAL: Optional[str]
  58. TITLE_GENERATION_PROMPT_TEMPLATE: str
  59. IMAGE_PROMPT_GENERATION_PROMPT_TEMPLATE: str
  60. ENABLE_AUTOCOMPLETE_GENERATION: bool
  61. AUTOCOMPLETE_GENERATION_INPUT_MAX_LENGTH: int
  62. TAGS_GENERATION_PROMPT_TEMPLATE: str
  63. ENABLE_TAGS_GENERATION: bool
  64. ENABLE_SEARCH_QUERY_GENERATION: bool
  65. ENABLE_RETRIEVAL_QUERY_GENERATION: bool
  66. QUERY_GENERATION_PROMPT_TEMPLATE: str
  67. TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE: str
  68. @router.post("/config/update")
  69. async def update_task_config(
  70. request: Request, form_data: TaskConfigForm, user=Depends(get_admin_user)
  71. ):
  72. request.app.state.config.TASK_MODEL = form_data.TASK_MODEL
  73. request.app.state.config.TASK_MODEL_EXTERNAL = form_data.TASK_MODEL_EXTERNAL
  74. request.app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE = (
  75. form_data.TITLE_GENERATION_PROMPT_TEMPLATE
  76. )
  77. request.app.state.config.IMAGE_PROMPT_GENERATION_PROMPT_TEMPLATE = (
  78. form_data.IMAGE_PROMPT_GENERATION_PROMPT_TEMPLATE
  79. )
  80. request.app.state.config.ENABLE_AUTOCOMPLETE_GENERATION = (
  81. form_data.ENABLE_AUTOCOMPLETE_GENERATION
  82. )
  83. request.app.state.config.AUTOCOMPLETE_GENERATION_INPUT_MAX_LENGTH = (
  84. form_data.AUTOCOMPLETE_GENERATION_INPUT_MAX_LENGTH
  85. )
  86. request.app.state.config.TAGS_GENERATION_PROMPT_TEMPLATE = (
  87. form_data.TAGS_GENERATION_PROMPT_TEMPLATE
  88. )
  89. request.app.state.config.ENABLE_TAGS_GENERATION = form_data.ENABLE_TAGS_GENERATION
  90. request.app.state.config.ENABLE_SEARCH_QUERY_GENERATION = (
  91. form_data.ENABLE_SEARCH_QUERY_GENERATION
  92. )
  93. request.app.state.config.ENABLE_RETRIEVAL_QUERY_GENERATION = (
  94. form_data.ENABLE_RETRIEVAL_QUERY_GENERATION
  95. )
  96. request.app.state.config.QUERY_GENERATION_PROMPT_TEMPLATE = (
  97. form_data.QUERY_GENERATION_PROMPT_TEMPLATE
  98. )
  99. request.app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE = (
  100. form_data.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE
  101. )
  102. return {
  103. "TASK_MODEL": request.app.state.config.TASK_MODEL,
  104. "TASK_MODEL_EXTERNAL": request.app.state.config.TASK_MODEL_EXTERNAL,
  105. "TITLE_GENERATION_PROMPT_TEMPLATE": request.app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE,
  106. "IMAGE_PROMPT_GENERATION_PROMPT_TEMPLATE": request.app.state.config.IMAGE_PROMPT_GENERATION_PROMPT_TEMPLATE,
  107. "ENABLE_AUTOCOMPLETE_GENERATION": request.app.state.config.ENABLE_AUTOCOMPLETE_GENERATION,
  108. "AUTOCOMPLETE_GENERATION_INPUT_MAX_LENGTH": request.app.state.config.AUTOCOMPLETE_GENERATION_INPUT_MAX_LENGTH,
  109. "TAGS_GENERATION_PROMPT_TEMPLATE": request.app.state.config.TAGS_GENERATION_PROMPT_TEMPLATE,
  110. "ENABLE_TAGS_GENERATION": request.app.state.config.ENABLE_TAGS_GENERATION,
  111. "ENABLE_SEARCH_QUERY_GENERATION": request.app.state.config.ENABLE_SEARCH_QUERY_GENERATION,
  112. "ENABLE_RETRIEVAL_QUERY_GENERATION": request.app.state.config.ENABLE_RETRIEVAL_QUERY_GENERATION,
  113. "QUERY_GENERATION_PROMPT_TEMPLATE": request.app.state.config.QUERY_GENERATION_PROMPT_TEMPLATE,
  114. "TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE": request.app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE,
  115. }
  116. @router.post("/title/completions")
  117. async def generate_title(
  118. request: Request, form_data: dict, user=Depends(get_verified_user)
  119. ):
  120. if getattr(request.state, "direct", False) and hasattr(request.state, "model"):
  121. models = {
  122. request.state.model["id"]: request.state.model,
  123. }
  124. else:
  125. models = request.app.state.MODELS
  126. model_id = form_data["model"]
  127. if model_id not in models:
  128. raise HTTPException(
  129. status_code=status.HTTP_404_NOT_FOUND,
  130. detail="Model not found",
  131. )
  132. # Check if the user has a custom task model
  133. # If the user has a custom task model, use that model
  134. task_model_id = get_task_model_id(
  135. model_id,
  136. request.app.state.config.TASK_MODEL,
  137. request.app.state.config.TASK_MODEL_EXTERNAL,
  138. models,
  139. )
  140. log.debug(
  141. f"generating chat title using model {task_model_id} for user {user.email} "
  142. )
  143. if request.app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE != "":
  144. template = request.app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE
  145. else:
  146. template = DEFAULT_TITLE_GENERATION_PROMPT_TEMPLATE
  147. messages = form_data["messages"]
  148. # Remove reasoning details from the messages
  149. for message in messages:
  150. message["content"] = re.sub(
  151. r"<details\s+type=\"reasoning\"[^>]*>.*?<\/details>",
  152. "",
  153. message["content"],
  154. flags=re.S,
  155. ).strip()
  156. content = title_generation_template(
  157. template,
  158. messages,
  159. {
  160. "name": user.name,
  161. "location": user.info.get("location") if user.info else None,
  162. },
  163. )
  164. payload = {
  165. "model": task_model_id,
  166. "messages": [{"role": "user", "content": content}],
  167. "stream": False,
  168. **(
  169. {"max_tokens": 1000}
  170. if models[task_model_id]["owned_by"] == "ollama"
  171. else {
  172. "max_completion_tokens": 1000,
  173. }
  174. ),
  175. "metadata": {
  176. **(request.state.metadata if hasattr(request.state, "metadata") else {}),
  177. "task": str(TASKS.TITLE_GENERATION),
  178. "task_body": form_data,
  179. "chat_id": form_data.get("chat_id", None),
  180. },
  181. }
  182. try:
  183. return await generate_chat_completion(request, form_data=payload, user=user)
  184. except Exception as e:
  185. log.error("Exception occurred", exc_info=True)
  186. return JSONResponse(
  187. status_code=status.HTTP_400_BAD_REQUEST,
  188. content={"detail": "An internal error has occurred."},
  189. )
  190. @router.post("/tags/completions")
  191. async def generate_chat_tags(
  192. request: Request, form_data: dict, user=Depends(get_verified_user)
  193. ):
  194. if not request.app.state.config.ENABLE_TAGS_GENERATION:
  195. return JSONResponse(
  196. status_code=status.HTTP_200_OK,
  197. content={"detail": "Tags generation is disabled"},
  198. )
  199. if getattr(request.state, "direct", False) and hasattr(request.state, "model"):
  200. models = {
  201. request.state.model["id"]: request.state.model,
  202. }
  203. else:
  204. models = request.app.state.MODELS
  205. model_id = form_data["model"]
  206. if model_id not in models:
  207. raise HTTPException(
  208. status_code=status.HTTP_404_NOT_FOUND,
  209. detail="Model not found",
  210. )
  211. # Check if the user has a custom task model
  212. # If the user has a custom task model, use that model
  213. task_model_id = get_task_model_id(
  214. model_id,
  215. request.app.state.config.TASK_MODEL,
  216. request.app.state.config.TASK_MODEL_EXTERNAL,
  217. models,
  218. )
  219. log.debug(
  220. f"generating chat tags using model {task_model_id} for user {user.email} "
  221. )
  222. if request.app.state.config.TAGS_GENERATION_PROMPT_TEMPLATE != "":
  223. template = request.app.state.config.TAGS_GENERATION_PROMPT_TEMPLATE
  224. else:
  225. template = DEFAULT_TAGS_GENERATION_PROMPT_TEMPLATE
  226. content = tags_generation_template(
  227. template, form_data["messages"], {"name": user.name}
  228. )
  229. payload = {
  230. "model": task_model_id,
  231. "messages": [{"role": "user", "content": content}],
  232. "stream": False,
  233. "metadata": {
  234. **(request.state.metadata if hasattr(request.state, "metadata") else {}),
  235. "task": str(TASKS.TAGS_GENERATION),
  236. "task_body": form_data,
  237. "chat_id": form_data.get("chat_id", None),
  238. },
  239. }
  240. try:
  241. return await generate_chat_completion(request, form_data=payload, user=user)
  242. except Exception as e:
  243. log.error(f"Error generating chat completion: {e}")
  244. return JSONResponse(
  245. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
  246. content={"detail": "An internal error has occurred."},
  247. )
  248. @router.post("/image_prompt/completions")
  249. async def generate_image_prompt(
  250. request: Request, form_data: dict, user=Depends(get_verified_user)
  251. ):
  252. if getattr(request.state, "direct", False) and hasattr(request.state, "model"):
  253. models = {
  254. request.state.model["id"]: request.state.model,
  255. }
  256. else:
  257. models = request.app.state.MODELS
  258. model_id = form_data["model"]
  259. if model_id not in models:
  260. raise HTTPException(
  261. status_code=status.HTTP_404_NOT_FOUND,
  262. detail="Model not found",
  263. )
  264. # Check if the user has a custom task model
  265. # If the user has a custom task model, use that model
  266. task_model_id = get_task_model_id(
  267. model_id,
  268. request.app.state.config.TASK_MODEL,
  269. request.app.state.config.TASK_MODEL_EXTERNAL,
  270. models,
  271. )
  272. log.debug(
  273. f"generating image prompt using model {task_model_id} for user {user.email} "
  274. )
  275. if request.app.state.config.IMAGE_PROMPT_GENERATION_PROMPT_TEMPLATE != "":
  276. template = request.app.state.config.IMAGE_PROMPT_GENERATION_PROMPT_TEMPLATE
  277. else:
  278. template = DEFAULT_IMAGE_PROMPT_GENERATION_PROMPT_TEMPLATE
  279. content = image_prompt_generation_template(
  280. template,
  281. form_data["messages"],
  282. user={
  283. "name": user.name,
  284. },
  285. )
  286. payload = {
  287. "model": task_model_id,
  288. "messages": [{"role": "user", "content": content}],
  289. "stream": False,
  290. "metadata": {
  291. **(request.state.metadata if hasattr(request.state, "metadata") else {}),
  292. "task": str(TASKS.IMAGE_PROMPT_GENERATION),
  293. "task_body": form_data,
  294. "chat_id": form_data.get("chat_id", None),
  295. },
  296. }
  297. try:
  298. return await generate_chat_completion(request, form_data=payload, user=user)
  299. except Exception as e:
  300. log.error("Exception occurred", exc_info=True)
  301. return JSONResponse(
  302. status_code=status.HTTP_400_BAD_REQUEST,
  303. content={"detail": "An internal error has occurred."},
  304. )
  305. @router.post("/queries/completions")
  306. async def generate_queries(
  307. request: Request, form_data: dict, user=Depends(get_verified_user)
  308. ):
  309. type = form_data.get("type")
  310. if type == "web_search":
  311. if not request.app.state.config.ENABLE_SEARCH_QUERY_GENERATION:
  312. raise HTTPException(
  313. status_code=status.HTTP_400_BAD_REQUEST,
  314. detail=f"Search query generation is disabled",
  315. )
  316. elif type == "retrieval":
  317. if not request.app.state.config.ENABLE_RETRIEVAL_QUERY_GENERATION:
  318. raise HTTPException(
  319. status_code=status.HTTP_400_BAD_REQUEST,
  320. detail=f"Query generation is disabled",
  321. )
  322. if getattr(request.state, "direct", False) and hasattr(request.state, "model"):
  323. models = {
  324. request.state.model["id"]: request.state.model,
  325. }
  326. else:
  327. models = request.app.state.MODELS
  328. model_id = form_data["model"]
  329. if model_id not in models:
  330. raise HTTPException(
  331. status_code=status.HTTP_404_NOT_FOUND,
  332. detail="Model not found",
  333. )
  334. # Check if the user has a custom task model
  335. # If the user has a custom task model, use that model
  336. task_model_id = get_task_model_id(
  337. model_id,
  338. request.app.state.config.TASK_MODEL,
  339. request.app.state.config.TASK_MODEL_EXTERNAL,
  340. models,
  341. )
  342. log.debug(
  343. f"generating {type} queries using model {task_model_id} for user {user.email}"
  344. )
  345. if (request.app.state.config.QUERY_GENERATION_PROMPT_TEMPLATE).strip() != "":
  346. template = request.app.state.config.QUERY_GENERATION_PROMPT_TEMPLATE
  347. else:
  348. template = DEFAULT_QUERY_GENERATION_PROMPT_TEMPLATE
  349. content = query_generation_template(
  350. template, form_data["messages"], {"name": user.name}
  351. )
  352. payload = {
  353. "model": task_model_id,
  354. "messages": [{"role": "user", "content": content}],
  355. "stream": False,
  356. "metadata": {
  357. **(request.state.metadata if hasattr(request.state, "metadata") else {}),
  358. "task": str(TASKS.QUERY_GENERATION),
  359. "task_body": form_data,
  360. "chat_id": form_data.get("chat_id", None),
  361. },
  362. }
  363. try:
  364. return await generate_chat_completion(request, form_data=payload, user=user)
  365. except Exception as e:
  366. return JSONResponse(
  367. status_code=status.HTTP_400_BAD_REQUEST,
  368. content={"detail": str(e)},
  369. )
  370. @router.post("/auto/completions")
  371. async def generate_autocompletion(
  372. request: Request, form_data: dict, user=Depends(get_verified_user)
  373. ):
  374. if not request.app.state.config.ENABLE_AUTOCOMPLETE_GENERATION:
  375. raise HTTPException(
  376. status_code=status.HTTP_400_BAD_REQUEST,
  377. detail=f"Autocompletion generation is disabled",
  378. )
  379. type = form_data.get("type")
  380. prompt = form_data.get("prompt")
  381. messages = form_data.get("messages")
  382. if request.app.state.config.AUTOCOMPLETE_GENERATION_INPUT_MAX_LENGTH > 0:
  383. if (
  384. len(prompt)
  385. > request.app.state.config.AUTOCOMPLETE_GENERATION_INPUT_MAX_LENGTH
  386. ):
  387. raise HTTPException(
  388. status_code=status.HTTP_400_BAD_REQUEST,
  389. detail=f"Input prompt exceeds maximum length of {request.app.state.config.AUTOCOMPLETE_GENERATION_INPUT_MAX_LENGTH}",
  390. )
  391. if getattr(request.state, "direct", False) and hasattr(request.state, "model"):
  392. models = {
  393. request.state.model["id"]: request.state.model,
  394. }
  395. else:
  396. models = request.app.state.MODELS
  397. model_id = form_data["model"]
  398. if model_id not in models:
  399. raise HTTPException(
  400. status_code=status.HTTP_404_NOT_FOUND,
  401. detail="Model not found",
  402. )
  403. # Check if the user has a custom task model
  404. # If the user has a custom task model, use that model
  405. task_model_id = get_task_model_id(
  406. model_id,
  407. request.app.state.config.TASK_MODEL,
  408. request.app.state.config.TASK_MODEL_EXTERNAL,
  409. models,
  410. )
  411. log.debug(
  412. f"generating autocompletion using model {task_model_id} for user {user.email}"
  413. )
  414. if (request.app.state.config.AUTOCOMPLETE_GENERATION_PROMPT_TEMPLATE).strip() != "":
  415. template = request.app.state.config.AUTOCOMPLETE_GENERATION_PROMPT_TEMPLATE
  416. else:
  417. template = DEFAULT_AUTOCOMPLETE_GENERATION_PROMPT_TEMPLATE
  418. content = autocomplete_generation_template(
  419. template, prompt, messages, type, {"name": user.name}
  420. )
  421. payload = {
  422. "model": task_model_id,
  423. "messages": [{"role": "user", "content": content}],
  424. "stream": False,
  425. "metadata": {
  426. **(request.state.metadata if hasattr(request.state, "metadata") else {}),
  427. "task": str(TASKS.AUTOCOMPLETE_GENERATION),
  428. "task_body": form_data,
  429. "chat_id": form_data.get("chat_id", None),
  430. },
  431. }
  432. try:
  433. return await generate_chat_completion(request, form_data=payload, user=user)
  434. except Exception as e:
  435. log.error(f"Error generating chat completion: {e}")
  436. return JSONResponse(
  437. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
  438. content={"detail": "An internal error has occurred."},
  439. )
  440. @router.post("/emoji/completions")
  441. async def generate_emoji(
  442. request: Request, form_data: dict, user=Depends(get_verified_user)
  443. ):
  444. if getattr(request.state, "direct", False) and hasattr(request.state, "model"):
  445. models = {
  446. request.state.model["id"]: request.state.model,
  447. }
  448. else:
  449. models = request.app.state.MODELS
  450. model_id = form_data["model"]
  451. if model_id not in models:
  452. raise HTTPException(
  453. status_code=status.HTTP_404_NOT_FOUND,
  454. detail="Model not found",
  455. )
  456. # Check if the user has a custom task model
  457. # If the user has a custom task model, use that model
  458. task_model_id = get_task_model_id(
  459. model_id,
  460. request.app.state.config.TASK_MODEL,
  461. request.app.state.config.TASK_MODEL_EXTERNAL,
  462. models,
  463. )
  464. log.debug(f"generating emoji using model {task_model_id} for user {user.email} ")
  465. template = DEFAULT_EMOJI_GENERATION_PROMPT_TEMPLATE
  466. content = emoji_generation_template(
  467. template,
  468. form_data["prompt"],
  469. {
  470. "name": user.name,
  471. "location": user.info.get("location") if user.info else None,
  472. },
  473. )
  474. payload = {
  475. "model": task_model_id,
  476. "messages": [{"role": "user", "content": content}],
  477. "stream": False,
  478. **(
  479. {"max_tokens": 4}
  480. if models[task_model_id]["owned_by"] == "ollama"
  481. else {
  482. "max_completion_tokens": 4,
  483. }
  484. ),
  485. "chat_id": form_data.get("chat_id", None),
  486. "metadata": {
  487. **(request.state.metadata if hasattr(request.state, "metadata") else {}),
  488. "task": str(TASKS.EMOJI_GENERATION),
  489. "task_body": form_data,
  490. },
  491. }
  492. try:
  493. return await generate_chat_completion(request, form_data=payload, user=user)
  494. except Exception as e:
  495. return JSONResponse(
  496. status_code=status.HTTP_400_BAD_REQUEST,
  497. content={"detail": str(e)},
  498. )
  499. @router.post("/moa/completions")
  500. async def generate_moa_response(
  501. request: Request, form_data: dict, user=Depends(get_verified_user)
  502. ):
  503. if getattr(request.state, "direct", False) and hasattr(request.state, "model"):
  504. models = {
  505. request.state.model["id"]: request.state.model,
  506. }
  507. else:
  508. models = request.app.state.MODELS
  509. model_id = form_data["model"]
  510. if model_id not in models:
  511. raise HTTPException(
  512. status_code=status.HTTP_404_NOT_FOUND,
  513. detail="Model not found",
  514. )
  515. # Check if the user has a custom task model
  516. # If the user has a custom task model, use that model
  517. task_model_id = get_task_model_id(
  518. model_id,
  519. request.app.state.config.TASK_MODEL,
  520. request.app.state.config.TASK_MODEL_EXTERNAL,
  521. models,
  522. )
  523. log.debug(f"generating MOA model {task_model_id} for user {user.email} ")
  524. template = DEFAULT_MOA_GENERATION_PROMPT_TEMPLATE
  525. content = moa_response_generation_template(
  526. template,
  527. form_data["prompt"],
  528. form_data["responses"],
  529. )
  530. payload = {
  531. "model": task_model_id,
  532. "messages": [{"role": "user", "content": content}],
  533. "stream": form_data.get("stream", False),
  534. "metadata": {
  535. **(request.state.metadata if hasattr(request.state, "metadata") else {}),
  536. "chat_id": form_data.get("chat_id", None),
  537. "task": str(TASKS.MOA_RESPONSE_GENERATION),
  538. "task_body": form_data,
  539. },
  540. }
  541. try:
  542. return await generate_chat_completion(request, form_data=payload, user=user)
  543. except Exception as e:
  544. return JSONResponse(
  545. status_code=status.HTTP_400_BAD_REQUEST,
  546. content={"detail": str(e)},
  547. )