middleware.py 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  1. import time
  2. import logging
  3. import sys
  4. import os
  5. import base64
  6. import asyncio
  7. from aiocache import cached
  8. from typing import Any, Optional
  9. import random
  10. import json
  11. import html
  12. import inspect
  13. import re
  14. from uuid import uuid4
  15. from concurrent.futures import ThreadPoolExecutor
  16. from fastapi import Request
  17. from fastapi import BackgroundTasks
  18. from starlette.responses import Response, StreamingResponse
  19. from open_webui.models.chats import Chats
  20. from open_webui.models.users import Users
  21. from open_webui.socket.main import (
  22. get_event_call,
  23. get_event_emitter,
  24. get_active_status_by_user_id,
  25. )
  26. from open_webui.routers.tasks import (
  27. generate_queries,
  28. generate_title,
  29. generate_image_prompt,
  30. generate_chat_tags,
  31. )
  32. from open_webui.routers.retrieval import process_web_search, SearchForm
  33. from open_webui.routers.images import image_generations, GenerateImageForm
  34. from open_webui.utils.webhook import post_webhook
  35. from open_webui.models.users import UserModel
  36. from open_webui.models.functions import Functions
  37. from open_webui.models.models import Models
  38. from open_webui.retrieval.utils import get_sources_from_files
  39. from open_webui.utils.chat import generate_chat_completion
  40. from open_webui.utils.task import (
  41. get_task_model_id,
  42. rag_template,
  43. tools_function_calling_generation_template,
  44. )
  45. from open_webui.utils.misc import (
  46. get_message_list,
  47. add_or_update_system_message,
  48. add_or_update_user_message,
  49. get_last_user_message,
  50. get_last_assistant_message,
  51. prepend_to_first_user_message_content,
  52. )
  53. from open_webui.utils.tools import get_tools
  54. from open_webui.utils.plugin import load_function_module_by_id
  55. from open_webui.tasks import create_task
  56. from open_webui.config import (
  57. CACHE_DIR,
  58. DEFAULT_TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE,
  59. DEFAULT_CODE_INTERPRETER_PROMPT,
  60. )
  61. from open_webui.env import (
  62. SRC_LOG_LEVELS,
  63. GLOBAL_LOG_LEVEL,
  64. BYPASS_MODEL_ACCESS_CONTROL,
  65. ENABLE_REALTIME_CHAT_SAVE,
  66. )
  67. from open_webui.constants import TASKS
  68. logging.basicConfig(stream=sys.stdout, level=GLOBAL_LOG_LEVEL)
  69. log = logging.getLogger(__name__)
  70. log.setLevel(SRC_LOG_LEVELS["MAIN"])
  71. async def chat_completion_filter_functions_handler(request, body, model, extra_params):
  72. skip_files = None
  73. def get_filter_function_ids(model):
  74. def get_priority(function_id):
  75. function = Functions.get_function_by_id(function_id)
  76. if function is not None and hasattr(function, "valves"):
  77. # TODO: Fix FunctionModel
  78. return (function.valves if function.valves else {}).get("priority", 0)
  79. return 0
  80. filter_ids = [
  81. function.id for function in Functions.get_global_filter_functions()
  82. ]
  83. if "info" in model and "meta" in model["info"]:
  84. filter_ids.extend(model["info"]["meta"].get("filterIds", []))
  85. filter_ids = list(set(filter_ids))
  86. enabled_filter_ids = [
  87. function.id
  88. for function in Functions.get_functions_by_type("filter", active_only=True)
  89. ]
  90. filter_ids = [
  91. filter_id for filter_id in filter_ids if filter_id in enabled_filter_ids
  92. ]
  93. filter_ids.sort(key=get_priority)
  94. return filter_ids
  95. filter_ids = get_filter_function_ids(model)
  96. for filter_id in filter_ids:
  97. filter = Functions.get_function_by_id(filter_id)
  98. if not filter:
  99. continue
  100. if filter_id in request.app.state.FUNCTIONS:
  101. function_module = request.app.state.FUNCTIONS[filter_id]
  102. else:
  103. function_module, _, _ = load_function_module_by_id(filter_id)
  104. request.app.state.FUNCTIONS[filter_id] = function_module
  105. # Check if the function has a file_handler variable
  106. if hasattr(function_module, "file_handler"):
  107. skip_files = function_module.file_handler
  108. # Apply valves to the function
  109. if hasattr(function_module, "valves") and hasattr(function_module, "Valves"):
  110. valves = Functions.get_function_valves_by_id(filter_id)
  111. function_module.valves = function_module.Valves(
  112. **(valves if valves else {})
  113. )
  114. if hasattr(function_module, "inlet"):
  115. try:
  116. inlet = function_module.inlet
  117. # Create a dictionary of parameters to be passed to the function
  118. params = {"body": body} | {
  119. k: v
  120. for k, v in {
  121. **extra_params,
  122. "__model__": model,
  123. "__id__": filter_id,
  124. }.items()
  125. if k in inspect.signature(inlet).parameters
  126. }
  127. if "__user__" in params and hasattr(function_module, "UserValves"):
  128. try:
  129. params["__user__"]["valves"] = function_module.UserValves(
  130. **Functions.get_user_valves_by_id_and_user_id(
  131. filter_id, params["__user__"]["id"]
  132. )
  133. )
  134. except Exception as e:
  135. print(e)
  136. if inspect.iscoroutinefunction(inlet):
  137. body = await inlet(**params)
  138. else:
  139. body = inlet(**params)
  140. except Exception as e:
  141. print(f"Error: {e}")
  142. raise e
  143. if skip_files and "files" in body.get("metadata", {}):
  144. del body["metadata"]["files"]
  145. return body, {}
  146. async def chat_completion_tools_handler(
  147. request: Request, body: dict, user: UserModel, models, extra_params: dict
  148. ) -> tuple[dict, dict]:
  149. async def get_content_from_response(response) -> Optional[str]:
  150. content = None
  151. if hasattr(response, "body_iterator"):
  152. async for chunk in response.body_iterator:
  153. data = json.loads(chunk.decode("utf-8"))
  154. content = data["choices"][0]["message"]["content"]
  155. # Cleanup any remaining background tasks if necessary
  156. if response.background is not None:
  157. await response.background()
  158. else:
  159. content = response["choices"][0]["message"]["content"]
  160. return content
  161. def get_tools_function_calling_payload(messages, task_model_id, content):
  162. user_message = get_last_user_message(messages)
  163. history = "\n".join(
  164. f"{message['role'].upper()}: \"\"\"{message['content']}\"\"\""
  165. for message in messages[::-1][:4]
  166. )
  167. prompt = f"History:\n{history}\nQuery: {user_message}"
  168. return {
  169. "model": task_model_id,
  170. "messages": [
  171. {"role": "system", "content": content},
  172. {"role": "user", "content": f"Query: {prompt}"},
  173. ],
  174. "stream": False,
  175. "metadata": {"task": str(TASKS.FUNCTION_CALLING)},
  176. }
  177. # If tool_ids field is present, call the functions
  178. metadata = body.get("metadata", {})
  179. tool_ids = metadata.get("tool_ids", None)
  180. log.debug(f"{tool_ids=}")
  181. if not tool_ids:
  182. return body, {}
  183. skip_files = False
  184. sources = []
  185. task_model_id = get_task_model_id(
  186. body["model"],
  187. request.app.state.config.TASK_MODEL,
  188. request.app.state.config.TASK_MODEL_EXTERNAL,
  189. models,
  190. )
  191. tools = get_tools(
  192. request,
  193. tool_ids,
  194. user,
  195. {
  196. **extra_params,
  197. "__model__": models[task_model_id],
  198. "__messages__": body["messages"],
  199. "__files__": metadata.get("files", []),
  200. },
  201. )
  202. log.info(f"{tools=}")
  203. specs = [tool["spec"] for tool in tools.values()]
  204. tools_specs = json.dumps(specs)
  205. if request.app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE != "":
  206. template = request.app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE
  207. else:
  208. template = DEFAULT_TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE
  209. tools_function_calling_prompt = tools_function_calling_generation_template(
  210. template, tools_specs
  211. )
  212. log.info(f"{tools_function_calling_prompt=}")
  213. payload = get_tools_function_calling_payload(
  214. body["messages"], task_model_id, tools_function_calling_prompt
  215. )
  216. try:
  217. response = await generate_chat_completion(request, form_data=payload, user=user)
  218. log.debug(f"{response=}")
  219. content = await get_content_from_response(response)
  220. log.debug(f"{content=}")
  221. if not content:
  222. return body, {}
  223. try:
  224. content = content[content.find("{") : content.rfind("}") + 1]
  225. if not content:
  226. raise Exception("No JSON object found in the response")
  227. result = json.loads(content)
  228. async def tool_call_handler(tool_call):
  229. log.debug(f"{tool_call=}")
  230. tool_function_name = tool_call.get("name", None)
  231. if tool_function_name not in tools:
  232. return body, {}
  233. tool_function_params = tool_call.get("parameters", {})
  234. try:
  235. required_params = (
  236. tools[tool_function_name]
  237. .get("spec", {})
  238. .get("parameters", {})
  239. .get("required", [])
  240. )
  241. tool_function = tools[tool_function_name]["callable"]
  242. tool_function_params = {
  243. k: v
  244. for k, v in tool_function_params.items()
  245. if k in required_params
  246. }
  247. tool_output = await tool_function(**tool_function_params)
  248. except Exception as e:
  249. tool_output = str(e)
  250. if isinstance(tool_output, str):
  251. if tools[tool_function_name]["citation"]:
  252. sources.append(
  253. {
  254. "source": {
  255. "name": f"TOOL:{tools[tool_function_name]['toolkit_id']}/{tool_function_name}"
  256. },
  257. "document": [tool_output],
  258. "metadata": [
  259. {
  260. "source": f"TOOL:{tools[tool_function_name]['toolkit_id']}/{tool_function_name}"
  261. }
  262. ],
  263. }
  264. )
  265. else:
  266. sources.append(
  267. {
  268. "source": {},
  269. "document": [tool_output],
  270. "metadata": [
  271. {
  272. "source": f"TOOL:{tools[tool_function_name]['toolkit_id']}/{tool_function_name}"
  273. }
  274. ],
  275. }
  276. )
  277. if tools[tool_function_name]["file_handler"]:
  278. skip_files = True
  279. # check if "tool_calls" in result
  280. if result.get("tool_calls"):
  281. for tool_call in result.get("tool_calls"):
  282. await tool_call_handler(tool_call)
  283. else:
  284. await tool_call_handler(result)
  285. except Exception as e:
  286. log.exception(f"Error: {e}")
  287. content = None
  288. except Exception as e:
  289. log.exception(f"Error: {e}")
  290. content = None
  291. log.debug(f"tool_contexts: {sources}")
  292. if skip_files and "files" in body.get("metadata", {}):
  293. del body["metadata"]["files"]
  294. return body, {"sources": sources}
  295. async def chat_web_search_handler(
  296. request: Request, form_data: dict, extra_params: dict, user
  297. ):
  298. event_emitter = extra_params["__event_emitter__"]
  299. await event_emitter(
  300. {
  301. "type": "status",
  302. "data": {
  303. "action": "web_search",
  304. "description": "Generating search query",
  305. "done": False,
  306. },
  307. }
  308. )
  309. messages = form_data["messages"]
  310. user_message = get_last_user_message(messages)
  311. queries = []
  312. try:
  313. res = await generate_queries(
  314. request,
  315. {
  316. "model": form_data["model"],
  317. "messages": messages,
  318. "prompt": user_message,
  319. "type": "web_search",
  320. },
  321. user,
  322. )
  323. response = res["choices"][0]["message"]["content"]
  324. try:
  325. bracket_start = response.find("{")
  326. bracket_end = response.rfind("}") + 1
  327. if bracket_start == -1 or bracket_end == -1:
  328. raise Exception("No JSON object found in the response")
  329. response = response[bracket_start:bracket_end]
  330. queries = json.loads(response)
  331. queries = queries.get("queries", [])
  332. except Exception as e:
  333. queries = [response]
  334. except Exception as e:
  335. log.exception(e)
  336. queries = [user_message]
  337. if len(queries) == 0:
  338. await event_emitter(
  339. {
  340. "type": "status",
  341. "data": {
  342. "action": "web_search",
  343. "description": "No search query generated",
  344. "done": True,
  345. },
  346. }
  347. )
  348. return form_data
  349. searchQuery = queries[0]
  350. await event_emitter(
  351. {
  352. "type": "status",
  353. "data": {
  354. "action": "web_search",
  355. "description": 'Searching "{{searchQuery}}"',
  356. "query": searchQuery,
  357. "done": False,
  358. },
  359. }
  360. )
  361. try:
  362. # Offload process_web_search to a separate thread
  363. loop = asyncio.get_running_loop()
  364. with ThreadPoolExecutor() as executor:
  365. results = await loop.run_in_executor(
  366. executor,
  367. lambda: process_web_search(
  368. request,
  369. SearchForm(
  370. **{
  371. "query": searchQuery,
  372. }
  373. ),
  374. user,
  375. ),
  376. )
  377. if results:
  378. await event_emitter(
  379. {
  380. "type": "status",
  381. "data": {
  382. "action": "web_search",
  383. "description": "Searched {{count}} sites",
  384. "query": searchQuery,
  385. "urls": results["filenames"],
  386. "done": True,
  387. },
  388. }
  389. )
  390. files = form_data.get("files", [])
  391. files.append(
  392. {
  393. "collection_name": results["collection_name"],
  394. "name": searchQuery,
  395. "type": "web_search_results",
  396. "urls": results["filenames"],
  397. }
  398. )
  399. form_data["files"] = files
  400. else:
  401. await event_emitter(
  402. {
  403. "type": "status",
  404. "data": {
  405. "action": "web_search",
  406. "description": "No search results found",
  407. "query": searchQuery,
  408. "done": True,
  409. "error": True,
  410. },
  411. }
  412. )
  413. except Exception as e:
  414. log.exception(e)
  415. await event_emitter(
  416. {
  417. "type": "status",
  418. "data": {
  419. "action": "web_search",
  420. "description": 'Error searching "{{searchQuery}}"',
  421. "query": searchQuery,
  422. "done": True,
  423. "error": True,
  424. },
  425. }
  426. )
  427. return form_data
  428. async def chat_image_generation_handler(
  429. request: Request, form_data: dict, extra_params: dict, user
  430. ):
  431. __event_emitter__ = extra_params["__event_emitter__"]
  432. await __event_emitter__(
  433. {
  434. "type": "status",
  435. "data": {"description": "Generating an image", "done": False},
  436. }
  437. )
  438. messages = form_data["messages"]
  439. user_message = get_last_user_message(messages)
  440. prompt = user_message
  441. negative_prompt = ""
  442. if request.app.state.config.ENABLE_IMAGE_PROMPT_GENERATION:
  443. try:
  444. res = await generate_image_prompt(
  445. request,
  446. {
  447. "model": form_data["model"],
  448. "messages": messages,
  449. },
  450. user,
  451. )
  452. response = res["choices"][0]["message"]["content"]
  453. try:
  454. bracket_start = response.find("{")
  455. bracket_end = response.rfind("}") + 1
  456. if bracket_start == -1 or bracket_end == -1:
  457. raise Exception("No JSON object found in the response")
  458. response = response[bracket_start:bracket_end]
  459. response = json.loads(response)
  460. prompt = response.get("prompt", [])
  461. except Exception as e:
  462. prompt = user_message
  463. except Exception as e:
  464. log.exception(e)
  465. prompt = user_message
  466. system_message_content = ""
  467. try:
  468. images = await image_generations(
  469. request=request,
  470. form_data=GenerateImageForm(**{"prompt": prompt}),
  471. user=user,
  472. )
  473. await __event_emitter__(
  474. {
  475. "type": "status",
  476. "data": {"description": "Generated an image", "done": True},
  477. }
  478. )
  479. for image in images:
  480. await __event_emitter__(
  481. {
  482. "type": "message",
  483. "data": {"content": f"![Generated Image]({image['url']})\n"},
  484. }
  485. )
  486. system_message_content = "<context>User is shown the generated image, tell the user that the image has been generated</context>"
  487. except Exception as e:
  488. log.exception(e)
  489. await __event_emitter__(
  490. {
  491. "type": "status",
  492. "data": {
  493. "description": f"An error occured while generating an image",
  494. "done": True,
  495. },
  496. }
  497. )
  498. system_message_content = "<context>Unable to generate an image, tell the user that an error occured</context>"
  499. if system_message_content:
  500. form_data["messages"] = add_or_update_system_message(
  501. system_message_content, form_data["messages"]
  502. )
  503. return form_data
  504. async def chat_completion_files_handler(
  505. request: Request, body: dict, user: UserModel
  506. ) -> tuple[dict, dict[str, list]]:
  507. sources = []
  508. if files := body.get("metadata", {}).get("files", None):
  509. try:
  510. queries_response = await generate_queries(
  511. request,
  512. {
  513. "model": body["model"],
  514. "messages": body["messages"],
  515. "type": "retrieval",
  516. },
  517. user,
  518. )
  519. queries_response = queries_response["choices"][0]["message"]["content"]
  520. try:
  521. bracket_start = queries_response.find("{")
  522. bracket_end = queries_response.rfind("}") + 1
  523. if bracket_start == -1 or bracket_end == -1:
  524. raise Exception("No JSON object found in the response")
  525. queries_response = queries_response[bracket_start:bracket_end]
  526. queries_response = json.loads(queries_response)
  527. except Exception as e:
  528. queries_response = {"queries": [queries_response]}
  529. queries = queries_response.get("queries", [])
  530. except Exception as e:
  531. queries = []
  532. if len(queries) == 0:
  533. queries = [get_last_user_message(body["messages"])]
  534. try:
  535. # Offload get_sources_from_files to a separate thread
  536. loop = asyncio.get_running_loop()
  537. with ThreadPoolExecutor() as executor:
  538. sources = await loop.run_in_executor(
  539. executor,
  540. lambda: get_sources_from_files(
  541. files=files,
  542. queries=queries,
  543. embedding_function=request.app.state.EMBEDDING_FUNCTION,
  544. k=request.app.state.config.TOP_K,
  545. reranking_function=request.app.state.rf,
  546. r=request.app.state.config.RELEVANCE_THRESHOLD,
  547. hybrid_search=request.app.state.config.ENABLE_RAG_HYBRID_SEARCH,
  548. ),
  549. )
  550. except Exception as e:
  551. log.exception(e)
  552. log.debug(f"rag_contexts:sources: {sources}")
  553. return body, {"sources": sources}
  554. def apply_params_to_form_data(form_data, model):
  555. params = form_data.pop("params", {})
  556. if model.get("ollama"):
  557. form_data["options"] = params
  558. if "format" in params:
  559. form_data["format"] = params["format"]
  560. if "keep_alive" in params:
  561. form_data["keep_alive"] = params["keep_alive"]
  562. else:
  563. if "seed" in params:
  564. form_data["seed"] = params["seed"]
  565. if "stop" in params:
  566. form_data["stop"] = params["stop"]
  567. if "temperature" in params:
  568. form_data["temperature"] = params["temperature"]
  569. if "max_tokens" in params:
  570. form_data["max_tokens"] = params["max_tokens"]
  571. if "top_p" in params:
  572. form_data["top_p"] = params["top_p"]
  573. if "frequency_penalty" in params:
  574. form_data["frequency_penalty"] = params["frequency_penalty"]
  575. if "reasoning_effort" in params:
  576. form_data["reasoning_effort"] = params["reasoning_effort"]
  577. return form_data
  578. async def process_chat_payload(request, form_data, metadata, user, model):
  579. form_data = apply_params_to_form_data(form_data, model)
  580. log.debug(f"form_data: {form_data}")
  581. event_emitter = get_event_emitter(metadata)
  582. event_call = get_event_call(metadata)
  583. extra_params = {
  584. "__event_emitter__": event_emitter,
  585. "__event_call__": event_call,
  586. "__user__": {
  587. "id": user.id,
  588. "email": user.email,
  589. "name": user.name,
  590. "role": user.role,
  591. },
  592. "__metadata__": metadata,
  593. "__request__": request,
  594. }
  595. # Initialize events to store additional event to be sent to the client
  596. # Initialize contexts and citation
  597. models = request.app.state.MODELS
  598. events = []
  599. sources = []
  600. user_message = get_last_user_message(form_data["messages"])
  601. model_knowledge = model.get("info", {}).get("meta", {}).get("knowledge", False)
  602. if model_knowledge:
  603. await event_emitter(
  604. {
  605. "type": "status",
  606. "data": {
  607. "action": "knowledge_search",
  608. "query": user_message,
  609. "done": False,
  610. },
  611. }
  612. )
  613. knowledge_files = []
  614. for item in model_knowledge:
  615. if item.get("collection_name"):
  616. knowledge_files.append(
  617. {
  618. "id": item.get("collection_name"),
  619. "name": item.get("name"),
  620. "legacy": True,
  621. }
  622. )
  623. elif item.get("collection_names"):
  624. knowledge_files.append(
  625. {
  626. "name": item.get("name"),
  627. "type": "collection",
  628. "collection_names": item.get("collection_names"),
  629. "legacy": True,
  630. }
  631. )
  632. else:
  633. knowledge_files.append(item)
  634. files = form_data.get("files", [])
  635. files.extend(knowledge_files)
  636. form_data["files"] = files
  637. variables = form_data.pop("variables", None)
  638. features = form_data.pop("features", None)
  639. if features:
  640. if "web_search" in features and features["web_search"]:
  641. form_data = await chat_web_search_handler(
  642. request, form_data, extra_params, user
  643. )
  644. if "image_generation" in features and features["image_generation"]:
  645. form_data = await chat_image_generation_handler(
  646. request, form_data, extra_params, user
  647. )
  648. if "code_interpreter" in features and features["code_interpreter"]:
  649. form_data["messages"] = add_or_update_user_message(
  650. DEFAULT_CODE_INTERPRETER_PROMPT, form_data["messages"]
  651. )
  652. try:
  653. form_data, flags = await chat_completion_filter_functions_handler(
  654. request, form_data, model, extra_params
  655. )
  656. except Exception as e:
  657. raise Exception(f"Error: {e}")
  658. tool_ids = form_data.pop("tool_ids", None)
  659. files = form_data.pop("files", None)
  660. # Remove files duplicates
  661. if files:
  662. files = list({json.dumps(f, sort_keys=True): f for f in files}.values())
  663. metadata = {
  664. **metadata,
  665. "tool_ids": tool_ids,
  666. "files": files,
  667. }
  668. form_data["metadata"] = metadata
  669. try:
  670. form_data, flags = await chat_completion_tools_handler(
  671. request, form_data, user, models, extra_params
  672. )
  673. sources.extend(flags.get("sources", []))
  674. except Exception as e:
  675. log.exception(e)
  676. try:
  677. form_data, flags = await chat_completion_files_handler(request, form_data, user)
  678. sources.extend(flags.get("sources", []))
  679. except Exception as e:
  680. log.exception(e)
  681. # If context is not empty, insert it into the messages
  682. if len(sources) > 0:
  683. context_string = ""
  684. for source_idx, source in enumerate(sources):
  685. source_id = source.get("source", {}).get("name", "")
  686. if "document" in source:
  687. for doc_idx, doc_context in enumerate(source["document"]):
  688. metadata = source.get("metadata")
  689. doc_source_id = None
  690. if metadata:
  691. doc_source_id = metadata[doc_idx].get("source", source_id)
  692. if source_id:
  693. context_string += f"<source><source_id>{doc_source_id if doc_source_id is not None else source_id}</source_id><source_context>{doc_context}</source_context></source>\n"
  694. else:
  695. # If there is no source_id, then do not include the source_id tag
  696. context_string += f"<source><source_context>{doc_context}</source_context></source>\n"
  697. context_string = context_string.strip()
  698. prompt = get_last_user_message(form_data["messages"])
  699. if prompt is None:
  700. raise Exception("No user message found")
  701. if (
  702. request.app.state.config.RELEVANCE_THRESHOLD == 0
  703. and context_string.strip() == ""
  704. ):
  705. log.debug(
  706. f"With a 0 relevancy threshold for RAG, the context cannot be empty"
  707. )
  708. # Workaround for Ollama 2.0+ system prompt issue
  709. # TODO: replace with add_or_update_system_message
  710. if model["owned_by"] == "ollama":
  711. form_data["messages"] = prepend_to_first_user_message_content(
  712. rag_template(
  713. request.app.state.config.RAG_TEMPLATE, context_string, prompt
  714. ),
  715. form_data["messages"],
  716. )
  717. else:
  718. form_data["messages"] = add_or_update_system_message(
  719. rag_template(
  720. request.app.state.config.RAG_TEMPLATE, context_string, prompt
  721. ),
  722. form_data["messages"],
  723. )
  724. # If there are citations, add them to the data_items
  725. sources = [source for source in sources if source.get("source", {}).get("name", "")]
  726. if len(sources) > 0:
  727. events.append({"sources": sources})
  728. if model_knowledge:
  729. await event_emitter(
  730. {
  731. "type": "status",
  732. "data": {
  733. "action": "knowledge_search",
  734. "query": user_message,
  735. "done": True,
  736. "hidden": True,
  737. },
  738. }
  739. )
  740. return form_data, events
  741. async def process_chat_response(
  742. request, response, form_data, user, events, metadata, tasks
  743. ):
  744. async def background_tasks_handler():
  745. message_map = Chats.get_messages_by_chat_id(metadata["chat_id"])
  746. message = message_map.get(metadata["message_id"]) if message_map else None
  747. if message:
  748. messages = get_message_list(message_map, message.get("id"))
  749. if tasks and messages:
  750. if TASKS.TITLE_GENERATION in tasks:
  751. if tasks[TASKS.TITLE_GENERATION]:
  752. res = await generate_title(
  753. request,
  754. {
  755. "model": message["model"],
  756. "messages": messages,
  757. "chat_id": metadata["chat_id"],
  758. },
  759. user,
  760. )
  761. if res and isinstance(res, dict):
  762. if len(res.get("choices", [])) == 1:
  763. title_string = (
  764. res.get("choices", [])[0]
  765. .get("message", {})
  766. .get("content", message.get("content", "New Chat"))
  767. )
  768. else:
  769. title_string = ""
  770. title_string = title_string[
  771. title_string.find("{") : title_string.rfind("}") + 1
  772. ]
  773. try:
  774. title = json.loads(title_string).get(
  775. "title", "New Chat"
  776. )
  777. except Exception as e:
  778. title = ""
  779. if not title:
  780. title = messages[0].get("content", "New Chat")
  781. Chats.update_chat_title_by_id(metadata["chat_id"], title)
  782. await event_emitter(
  783. {
  784. "type": "chat:title",
  785. "data": title,
  786. }
  787. )
  788. elif len(messages) == 2:
  789. title = messages[0].get("content", "New Chat")
  790. Chats.update_chat_title_by_id(metadata["chat_id"], title)
  791. await event_emitter(
  792. {
  793. "type": "chat:title",
  794. "data": message.get("content", "New Chat"),
  795. }
  796. )
  797. if TASKS.TAGS_GENERATION in tasks and tasks[TASKS.TAGS_GENERATION]:
  798. res = await generate_chat_tags(
  799. request,
  800. {
  801. "model": message["model"],
  802. "messages": messages,
  803. "chat_id": metadata["chat_id"],
  804. },
  805. user,
  806. )
  807. if res and isinstance(res, dict):
  808. if len(res.get("choices", [])) == 1:
  809. tags_string = (
  810. res.get("choices", [])[0]
  811. .get("message", {})
  812. .get("content", "")
  813. )
  814. else:
  815. tags_string = ""
  816. tags_string = tags_string[
  817. tags_string.find("{") : tags_string.rfind("}") + 1
  818. ]
  819. try:
  820. tags = json.loads(tags_string).get("tags", [])
  821. Chats.update_chat_tags_by_id(
  822. metadata["chat_id"], tags, user
  823. )
  824. await event_emitter(
  825. {
  826. "type": "chat:tags",
  827. "data": tags,
  828. }
  829. )
  830. except Exception as e:
  831. pass
  832. event_emitter = None
  833. event_caller = None
  834. if (
  835. "session_id" in metadata
  836. and metadata["session_id"]
  837. and "chat_id" in metadata
  838. and metadata["chat_id"]
  839. and "message_id" in metadata
  840. and metadata["message_id"]
  841. ):
  842. event_emitter = get_event_emitter(metadata)
  843. event_caller = get_event_call(metadata)
  844. # Non-streaming response
  845. if not isinstance(response, StreamingResponse):
  846. if event_emitter:
  847. if "selected_model_id" in response:
  848. Chats.upsert_message_to_chat_by_id_and_message_id(
  849. metadata["chat_id"],
  850. metadata["message_id"],
  851. {
  852. "selectedModelId": response["selected_model_id"],
  853. },
  854. )
  855. if response.get("choices", [])[0].get("message", {}).get("content"):
  856. content = response["choices"][0]["message"]["content"]
  857. if content:
  858. await event_emitter(
  859. {
  860. "type": "chat:completion",
  861. "data": response,
  862. }
  863. )
  864. title = Chats.get_chat_title_by_id(metadata["chat_id"])
  865. await event_emitter(
  866. {
  867. "type": "chat:completion",
  868. "data": {
  869. "done": True,
  870. "content": content,
  871. "title": title,
  872. },
  873. }
  874. )
  875. # Save message in the database
  876. Chats.upsert_message_to_chat_by_id_and_message_id(
  877. metadata["chat_id"],
  878. metadata["message_id"],
  879. {
  880. "content": content,
  881. },
  882. )
  883. # Send a webhook notification if the user is not active
  884. if get_active_status_by_user_id(user.id) is None:
  885. webhook_url = Users.get_user_webhook_url_by_id(user.id)
  886. if webhook_url:
  887. post_webhook(
  888. webhook_url,
  889. f"{title} - {request.app.state.config.WEBUI_URL}/c/{metadata['chat_id']}\n\n{content}",
  890. {
  891. "action": "chat",
  892. "message": content,
  893. "title": title,
  894. "url": f"{request.app.state.config.WEBUI_URL}/c/{metadata['chat_id']}",
  895. },
  896. )
  897. await background_tasks_handler()
  898. return response
  899. else:
  900. return response
  901. # Non standard response
  902. if not any(
  903. content_type in response.headers["Content-Type"]
  904. for content_type in ["text/event-stream", "application/x-ndjson"]
  905. ):
  906. return response
  907. # Streaming response
  908. if event_emitter and event_caller:
  909. task_id = str(uuid4()) # Create a unique task ID.
  910. model_id = form_data.get("model", "")
  911. Chats.upsert_message_to_chat_by_id_and_message_id(
  912. metadata["chat_id"],
  913. metadata["message_id"],
  914. {
  915. "model": model_id,
  916. },
  917. )
  918. # Handle as a background task
  919. async def post_response_handler(response, events):
  920. def serialize_content_blocks(content_blocks, raw=False):
  921. content = ""
  922. for block in content_blocks:
  923. if block["type"] == "text":
  924. content = f"{content}{block['content'].strip()}\n"
  925. elif block["type"] == "reasoning":
  926. reasoning_display_content = "\n".join(
  927. (f"> {line}" if not line.startswith(">") else line)
  928. for line in block["content"].splitlines()
  929. )
  930. reasoning_duration = block.get("duration", None)
  931. if reasoning_duration:
  932. if raw:
  933. content = f'{content}\n<{block["tag"]}>{block["content"]}</{block["tag"]}>\n'
  934. else:
  935. content = f'{content}\n<details type="reasoning" done="true" duration="{reasoning_duration}">\n<summary>Thought for {reasoning_duration} seconds</summary>\n{reasoning_display_content}\n</details>\n'
  936. else:
  937. if raw:
  938. content = f'{content}\n<{block["tag"]}>{block["content"]}</{block["tag"]}>\n'
  939. else:
  940. content = f'{content}\n<details type="reasoning" done="false">\n<summary>Thinking…</summary>\n{reasoning_display_content}\n</details>\n'
  941. elif block["type"] == "code_interpreter":
  942. attributes = block.get("attributes", {})
  943. output = block.get("output", None)
  944. lang = attributes.get("lang", "")
  945. if output:
  946. output = html.escape(json.dumps(output))
  947. if raw:
  948. content = f'{content}\n<code_interpreter type="code" lang="{lang}">\n{block["content"]}\n</code_interpreter>\n```output\n{output}\n```\n'
  949. else:
  950. content = f'{content}\n<details type="code_interpreter" done="true" output="{output}">\n<summary>Analyzed</summary>\n```{lang}\n{block["content"]}\n```\n</details>\n'
  951. else:
  952. if raw:
  953. content = f'{content}\n<code_interpreter type="code" lang="{lang}">\n{block["content"]}\n</code_interpreter>\n'
  954. else:
  955. content = f'{content}\n<details type="code_interpreter" done="false">\n<summary>Analyzing...</summary>\n```{lang}\n{block["content"]}\n```\n</details>\n'
  956. else:
  957. block_content = str(block["content"]).strip()
  958. content = f"{content}{block['type']}: {block_content}\n"
  959. return content
  960. def tag_content_handler(content_type, tags, content, content_blocks):
  961. end_flag = False
  962. def extract_attributes(tag_content):
  963. """Extract attributes from a tag if they exist."""
  964. attributes = {}
  965. # Match attributes in the format: key="value" (ignores single quotes for simplicity)
  966. matches = re.findall(r'(\w+)\s*=\s*"([^"]+)"', tag_content)
  967. for key, value in matches:
  968. attributes[key] = value
  969. return attributes
  970. if content_blocks[-1]["type"] == "text":
  971. for tag in tags:
  972. # Match start tag e.g., <tag> or <tag attr="value">
  973. start_tag_pattern = rf"<{tag}(.*?)>"
  974. match = re.search(start_tag_pattern, content)
  975. if match:
  976. # Extract attributes in the tag (if present)
  977. attributes = extract_attributes(match.group(1))
  978. # Remove the start tag from the currently handling text block
  979. content_blocks[-1]["content"] = content_blocks[-1][
  980. "content"
  981. ].replace(match.group(0), "")
  982. if not content_blocks[-1]["content"]:
  983. content_blocks.pop()
  984. # Append the new block
  985. content_blocks.append(
  986. {
  987. "type": content_type,
  988. "tag": tag,
  989. "attributes": attributes,
  990. "content": "",
  991. "started_at": time.time(),
  992. }
  993. )
  994. break
  995. elif content_blocks[-1]["type"] == content_type:
  996. tag = content_blocks[-1]["tag"]
  997. # Match end tag e.g., </tag>
  998. end_tag_pattern = rf"</{tag}>"
  999. if re.search(end_tag_pattern, content):
  1000. block_content = content_blocks[-1]["content"]
  1001. # Strip start and end tags from the content
  1002. start_tag_pattern = rf"<{tag}(.*?)>"
  1003. block_content = re.sub(
  1004. start_tag_pattern, "", block_content
  1005. ).strip()
  1006. block_content = re.sub(
  1007. end_tag_pattern, "", block_content
  1008. ).strip()
  1009. if block_content:
  1010. end_flag = True
  1011. content_blocks[-1]["content"] = block_content
  1012. content_blocks[-1]["ended_at"] = time.time()
  1013. content_blocks[-1]["duration"] = int(
  1014. content_blocks[-1]["ended_at"]
  1015. - content_blocks[-1]["started_at"]
  1016. )
  1017. # Reset the content_blocks by appending a new text block
  1018. content_blocks.append(
  1019. {
  1020. "type": "text",
  1021. "content": "",
  1022. }
  1023. )
  1024. # Clean processed content
  1025. content = re.sub(
  1026. rf"<{tag}(.*?)>(.|\n)*?</{tag}>",
  1027. "",
  1028. content,
  1029. flags=re.DOTALL,
  1030. )
  1031. else:
  1032. # Remove the block if content is empty
  1033. content_blocks.pop()
  1034. return content, content_blocks, end_flag
  1035. message = Chats.get_message_by_id_and_message_id(
  1036. metadata["chat_id"], metadata["message_id"]
  1037. )
  1038. content = message.get("content", "") if message else ""
  1039. content_blocks = [
  1040. {
  1041. "type": "text",
  1042. "content": content,
  1043. }
  1044. ]
  1045. # We might want to disable this by default
  1046. DETECT_REASONING = True
  1047. DETECT_CODE_INTERPRETER = metadata.get("features", {}).get(
  1048. "code_interpreter", False
  1049. )
  1050. reasoning_tags = ["think", "reason", "reasoning", "thought", "Thought"]
  1051. code_interpreter_tags = ["code_interpreter"]
  1052. try:
  1053. for event in events:
  1054. await event_emitter(
  1055. {
  1056. "type": "chat:completion",
  1057. "data": event,
  1058. }
  1059. )
  1060. # Save message in the database
  1061. Chats.upsert_message_to_chat_by_id_and_message_id(
  1062. metadata["chat_id"],
  1063. metadata["message_id"],
  1064. {
  1065. **event,
  1066. },
  1067. )
  1068. async def stream_body_handler(response):
  1069. nonlocal content
  1070. nonlocal content_blocks
  1071. async for line in response.body_iterator:
  1072. line = line.decode("utf-8") if isinstance(line, bytes) else line
  1073. data = line
  1074. # Skip empty lines
  1075. if not data.strip():
  1076. continue
  1077. # "data:" is the prefix for each event
  1078. if not data.startswith("data:"):
  1079. continue
  1080. # Remove the prefix
  1081. data = data[len("data:") :].strip()
  1082. try:
  1083. data = json.loads(data)
  1084. if "selected_model_id" in data:
  1085. model_id = data["selected_model_id"]
  1086. Chats.upsert_message_to_chat_by_id_and_message_id(
  1087. metadata["chat_id"],
  1088. metadata["message_id"],
  1089. {
  1090. "selectedModelId": model_id,
  1091. },
  1092. )
  1093. else:
  1094. choices = data.get("choices", [])
  1095. if not choices:
  1096. continue
  1097. value = choices[0].get("delta", {}).get("content")
  1098. if value:
  1099. content = f"{content}{value}"
  1100. content_blocks[-1]["content"] = (
  1101. content_blocks[-1]["content"] + value
  1102. )
  1103. if DETECT_REASONING:
  1104. content, content_blocks, _ = (
  1105. tag_content_handler(
  1106. "reasoning",
  1107. reasoning_tags,
  1108. content,
  1109. content_blocks,
  1110. )
  1111. )
  1112. if DETECT_CODE_INTERPRETER:
  1113. content, content_blocks, end = (
  1114. tag_content_handler(
  1115. "code_interpreter",
  1116. code_interpreter_tags,
  1117. content,
  1118. content_blocks,
  1119. )
  1120. )
  1121. if end:
  1122. break
  1123. if ENABLE_REALTIME_CHAT_SAVE:
  1124. # Save message in the database
  1125. Chats.upsert_message_to_chat_by_id_and_message_id(
  1126. metadata["chat_id"],
  1127. metadata["message_id"],
  1128. {
  1129. "content": serialize_content_blocks(
  1130. content_blocks
  1131. ),
  1132. },
  1133. )
  1134. else:
  1135. data = {
  1136. "content": serialize_content_blocks(
  1137. content_blocks
  1138. ),
  1139. }
  1140. await event_emitter(
  1141. {
  1142. "type": "chat:completion",
  1143. "data": data,
  1144. }
  1145. )
  1146. except Exception as e:
  1147. done = "data: [DONE]" in line
  1148. if done:
  1149. pass
  1150. else:
  1151. log.debug("Error: ", e)
  1152. continue
  1153. # Clean up the last text block
  1154. if content_blocks[-1]["type"] == "text":
  1155. content_blocks[-1]["content"] = content_blocks[-1][
  1156. "content"
  1157. ].strip()
  1158. if not content_blocks[-1]["content"]:
  1159. content_blocks.pop()
  1160. await event_emitter(
  1161. {
  1162. "type": "chat:completion",
  1163. "data": {
  1164. "content": serialize_content_blocks(content_blocks),
  1165. },
  1166. }
  1167. )
  1168. if response.background:
  1169. await response.background()
  1170. await stream_body_handler(response)
  1171. if DETECT_CODE_INTERPRETER:
  1172. MAX_RETRIES = 5
  1173. retries = 0
  1174. while (
  1175. content_blocks[-1]["type"] == "code_interpreter"
  1176. and retries < MAX_RETRIES
  1177. ):
  1178. retries += 1
  1179. log.debug(f"Attempt count: {retries}")
  1180. output = ""
  1181. try:
  1182. if content_blocks[-1]["attributes"].get("type") == "code":
  1183. output = await event_caller(
  1184. {
  1185. "type": "execute:python",
  1186. "data": {
  1187. "id": str(uuid4()),
  1188. "code": content_blocks[-1]["content"],
  1189. },
  1190. }
  1191. )
  1192. if isinstance(output, dict):
  1193. stdout = output.get("stdout", "")
  1194. if stdout:
  1195. stdoutLines = stdout.split("\n")
  1196. for idx, line in enumerate(stdoutLines):
  1197. if "data:image/png;base64" in line:
  1198. id = str(uuid4())
  1199. # ensure the path exists
  1200. os.makedirs(
  1201. os.path.join(CACHE_DIR, "images"),
  1202. exist_ok=True,
  1203. )
  1204. image_path = os.path.join(
  1205. CACHE_DIR,
  1206. f"images/{id}.png",
  1207. )
  1208. with open(image_path, "wb") as f:
  1209. f.write(
  1210. base64.b64decode(
  1211. line.split(",")[1]
  1212. )
  1213. )
  1214. stdoutLines[idx] = (
  1215. f"![Output Image {idx}](/cache/images/{id}.png)"
  1216. )
  1217. output["stdout"] = "\n".join(stdoutLines)
  1218. except Exception as e:
  1219. output = str(e)
  1220. content_blocks[-1]["output"] = output
  1221. content_blocks.append(
  1222. {
  1223. "type": "text",
  1224. "content": "",
  1225. }
  1226. )
  1227. await event_emitter(
  1228. {
  1229. "type": "chat:completion",
  1230. "data": {
  1231. "content": serialize_content_blocks(content_blocks),
  1232. },
  1233. }
  1234. )
  1235. try:
  1236. res = await generate_chat_completion(
  1237. request,
  1238. {
  1239. "model": model_id,
  1240. "stream": True,
  1241. "messages": [
  1242. *form_data["messages"],
  1243. {
  1244. "role": "assistant",
  1245. "content": serialize_content_blocks(
  1246. content_blocks, raw=True
  1247. ),
  1248. },
  1249. ],
  1250. },
  1251. user,
  1252. )
  1253. if isinstance(res, StreamingResponse):
  1254. await stream_body_handler(res)
  1255. else:
  1256. break
  1257. except Exception as e:
  1258. log.debug(e)
  1259. break
  1260. title = Chats.get_chat_title_by_id(metadata["chat_id"])
  1261. data = {
  1262. "done": True,
  1263. "content": serialize_content_blocks(content_blocks),
  1264. "title": title,
  1265. }
  1266. if not ENABLE_REALTIME_CHAT_SAVE:
  1267. # Save message in the database
  1268. Chats.upsert_message_to_chat_by_id_and_message_id(
  1269. metadata["chat_id"],
  1270. metadata["message_id"],
  1271. {
  1272. "content": serialize_content_blocks(content_blocks),
  1273. },
  1274. )
  1275. # Send a webhook notification if the user is not active
  1276. if get_active_status_by_user_id(user.id) is None:
  1277. webhook_url = Users.get_user_webhook_url_by_id(user.id)
  1278. if webhook_url:
  1279. post_webhook(
  1280. webhook_url,
  1281. f"{title} - {request.app.state.config.WEBUI_URL}/c/{metadata['chat_id']}\n\n{content}",
  1282. {
  1283. "action": "chat",
  1284. "message": content,
  1285. "title": title,
  1286. "url": f"{request.app.state.config.WEBUI_URL}/c/{metadata['chat_id']}",
  1287. },
  1288. )
  1289. await event_emitter(
  1290. {
  1291. "type": "chat:completion",
  1292. "data": data,
  1293. }
  1294. )
  1295. await background_tasks_handler()
  1296. except asyncio.CancelledError:
  1297. print("Task was cancelled!")
  1298. await event_emitter({"type": "task-cancelled"})
  1299. if not ENABLE_REALTIME_CHAT_SAVE:
  1300. # Save message in the database
  1301. Chats.upsert_message_to_chat_by_id_and_message_id(
  1302. metadata["chat_id"],
  1303. metadata["message_id"],
  1304. {
  1305. "content": serialize_content_blocks(content_blocks),
  1306. },
  1307. )
  1308. if response.background is not None:
  1309. await response.background()
  1310. # background_tasks.add_task(post_response_handler, response, events)
  1311. task_id, _ = create_task(post_response_handler(response, events))
  1312. return {"status": True, "task_id": task_id}
  1313. else:
  1314. # Fallback to the original response
  1315. async def stream_wrapper(original_generator, events):
  1316. def wrap_item(item):
  1317. return f"data: {item}\n\n"
  1318. for event in events:
  1319. yield wrap_item(json.dumps(event))
  1320. async for data in original_generator:
  1321. yield data
  1322. return StreamingResponse(
  1323. stream_wrapper(response.body_iterator, events),
  1324. headers=dict(response.headers),
  1325. background=response.background,
  1326. )