payload.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. from open_webui.utils.task import prompt_template, prompt_variables_template
  2. from open_webui.utils.misc import (
  3. deep_update,
  4. add_or_update_system_message,
  5. replace_system_message_content,
  6. )
  7. from typing import Callable, Optional
  8. import json
  9. # inplace function: form_data is modified
  10. def apply_system_prompt_to_body(
  11. system: Optional[str],
  12. form_data: dict,
  13. metadata: Optional[dict] = None,
  14. user=None,
  15. replace: bool = False,
  16. ) -> dict:
  17. if not system:
  18. return form_data
  19. # Metadata (WebUI Usage)
  20. if metadata:
  21. variables = metadata.get("variables", {})
  22. if variables:
  23. system = prompt_variables_template(system, variables)
  24. # Legacy (API Usage)
  25. system = prompt_template(system, user)
  26. if replace:
  27. form_data["messages"] = replace_system_message_content(
  28. system, form_data.get("messages", [])
  29. )
  30. else:
  31. form_data["messages"] = add_or_update_system_message(
  32. system, form_data.get("messages", [])
  33. )
  34. return form_data
  35. # inplace function: form_data is modified
  36. def apply_model_params_to_body(
  37. params: dict, form_data: dict, mappings: dict[str, Callable]
  38. ) -> dict:
  39. if not params:
  40. return form_data
  41. for key, value in params.items():
  42. if value is not None:
  43. if key in mappings:
  44. cast_func = mappings[key]
  45. if isinstance(cast_func, Callable):
  46. form_data[key] = cast_func(value)
  47. else:
  48. form_data[key] = value
  49. return form_data
  50. def remove_open_webui_params(params: dict) -> dict:
  51. """
  52. Removes OpenWebUI specific parameters from the provided dictionary.
  53. Args:
  54. params (dict): The dictionary containing parameters.
  55. Returns:
  56. dict: The modified dictionary with OpenWebUI parameters removed.
  57. """
  58. open_webui_params = {
  59. "stream_response": bool,
  60. "stream_delta_chunk_size": int,
  61. "function_calling": str,
  62. "reasoning_tags": list,
  63. "system": str,
  64. }
  65. for key in list(params.keys()):
  66. if key in open_webui_params:
  67. del params[key]
  68. return params
  69. # inplace function: form_data is modified
  70. def apply_model_params_to_body_openai(params: dict, form_data: dict) -> dict:
  71. params = remove_open_webui_params(params)
  72. custom_params = params.pop("custom_params", {})
  73. if custom_params:
  74. # Attempt to parse custom_params if they are strings
  75. for key, value in custom_params.items():
  76. if isinstance(value, str):
  77. try:
  78. # Attempt to parse the string as JSON
  79. custom_params[key] = json.loads(value)
  80. except json.JSONDecodeError:
  81. # If it fails, keep the original string
  82. pass
  83. # If there are custom parameters, we need to apply them first
  84. params = deep_update(params, custom_params)
  85. mappings = {
  86. "temperature": float,
  87. "top_p": float,
  88. "min_p": float,
  89. "max_tokens": int,
  90. "frequency_penalty": float,
  91. "presence_penalty": float,
  92. "reasoning_effort": str,
  93. "seed": lambda x: x,
  94. "stop": lambda x: [bytes(s, "utf-8").decode("unicode_escape") for s in x],
  95. "logit_bias": lambda x: x,
  96. "response_format": dict,
  97. }
  98. return apply_model_params_to_body(params, form_data, mappings)
  99. def apply_model_params_to_body_ollama(params: dict, form_data: dict) -> dict:
  100. params = remove_open_webui_params(params)
  101. custom_params = params.pop("custom_params", {})
  102. if custom_params:
  103. # Attempt to parse custom_params if they are strings
  104. for key, value in custom_params.items():
  105. if isinstance(value, str):
  106. try:
  107. # Attempt to parse the string as JSON
  108. custom_params[key] = json.loads(value)
  109. except json.JSONDecodeError:
  110. # If it fails, keep the original string
  111. pass
  112. # If there are custom parameters, we need to apply them first
  113. params = deep_update(params, custom_params)
  114. # Convert OpenAI parameter names to Ollama parameter names if needed.
  115. name_differences = {
  116. "max_tokens": "num_predict",
  117. }
  118. for key, value in name_differences.items():
  119. if (param := params.get(key, None)) is not None:
  120. # Copy the parameter to new name then delete it, to prevent Ollama warning of invalid option provided
  121. params[value] = params[key]
  122. del params[key]
  123. # See https://github.com/ollama/ollama/blob/main/docs/api.md#request-8
  124. mappings = {
  125. "temperature": float,
  126. "top_p": float,
  127. "seed": lambda x: x,
  128. "mirostat": int,
  129. "mirostat_eta": float,
  130. "mirostat_tau": float,
  131. "num_ctx": int,
  132. "num_batch": int,
  133. "num_keep": int,
  134. "num_predict": int,
  135. "repeat_last_n": int,
  136. "top_k": int,
  137. "min_p": float,
  138. "repeat_penalty": float,
  139. "presence_penalty": float,
  140. "frequency_penalty": float,
  141. "stop": lambda x: [bytes(s, "utf-8").decode("unicode_escape") for s in x],
  142. "num_gpu": int,
  143. "use_mmap": bool,
  144. "use_mlock": bool,
  145. "num_thread": int,
  146. }
  147. def parse_json(value: str) -> dict:
  148. """
  149. Parses a JSON string into a dictionary, handling potential JSONDecodeError.
  150. """
  151. try:
  152. return json.loads(value)
  153. except Exception as e:
  154. return value
  155. ollama_root_params = {
  156. "format": lambda x: parse_json(x),
  157. "keep_alive": lambda x: parse_json(x),
  158. "think": bool,
  159. }
  160. for key, value in ollama_root_params.items():
  161. if (param := params.get(key, None)) is not None:
  162. # Copy the parameter to new name then delete it, to prevent Ollama warning of invalid option provided
  163. form_data[key] = value(param)
  164. del params[key]
  165. # Unlike OpenAI, Ollama does not support params directly in the body
  166. form_data["options"] = apply_model_params_to_body(
  167. params, (form_data.get("options", {}) or {}), mappings
  168. )
  169. return form_data
  170. def convert_messages_openai_to_ollama(messages: list[dict]) -> list[dict]:
  171. ollama_messages = []
  172. for message in messages:
  173. # Initialize the new message structure with the role
  174. new_message = {"role": message["role"]}
  175. content = message.get("content", [])
  176. tool_calls = message.get("tool_calls", None)
  177. tool_call_id = message.get("tool_call_id", None)
  178. # Check if the content is a string (just a simple message)
  179. if isinstance(content, str) and not tool_calls:
  180. # If the content is a string, it's pure text
  181. new_message["content"] = content
  182. # If message is a tool call, add the tool call id to the message
  183. if tool_call_id:
  184. new_message["tool_call_id"] = tool_call_id
  185. elif tool_calls:
  186. # If tool calls are present, add them to the message
  187. ollama_tool_calls = []
  188. for tool_call in tool_calls:
  189. ollama_tool_call = {
  190. "index": tool_call.get("index", 0),
  191. "id": tool_call.get("id", None),
  192. "function": {
  193. "name": tool_call.get("function", {}).get("name", ""),
  194. "arguments": json.loads(
  195. tool_call.get("function", {}).get("arguments", {})
  196. ),
  197. },
  198. }
  199. ollama_tool_calls.append(ollama_tool_call)
  200. new_message["tool_calls"] = ollama_tool_calls
  201. # Put the content to empty string (Ollama requires an empty string for tool calls)
  202. new_message["content"] = ""
  203. else:
  204. # Otherwise, assume the content is a list of dicts, e.g., text followed by an image URL
  205. content_text = ""
  206. images = []
  207. # Iterate through the list of content items
  208. for item in content:
  209. # Check if it's a text type
  210. if item.get("type") == "text":
  211. content_text += item.get("text", "")
  212. # Check if it's an image URL type
  213. elif item.get("type") == "image_url":
  214. img_url = item.get("image_url", {}).get("url", "")
  215. if img_url:
  216. # If the image url starts with data:, it's a base64 image and should be trimmed
  217. if img_url.startswith("data:"):
  218. img_url = img_url.split(",")[-1]
  219. images.append(img_url)
  220. # Add content text (if any)
  221. if content_text:
  222. new_message["content"] = content_text.strip()
  223. # Add images (if any)
  224. if images:
  225. new_message["images"] = images
  226. # Append the new formatted message to the result
  227. ollama_messages.append(new_message)
  228. return ollama_messages
  229. def convert_payload_openai_to_ollama(openai_payload: dict) -> dict:
  230. """
  231. Converts a payload formatted for OpenAI's API to be compatible with Ollama's API endpoint for chat completions.
  232. Args:
  233. openai_payload (dict): The payload originally designed for OpenAI API usage.
  234. Returns:
  235. dict: A modified payload compatible with the Ollama API.
  236. """
  237. ollama_payload = {}
  238. # Mapping basic model and message details
  239. ollama_payload["model"] = openai_payload.get("model")
  240. ollama_payload["messages"] = convert_messages_openai_to_ollama(
  241. openai_payload.get("messages")
  242. )
  243. ollama_payload["stream"] = openai_payload.get("stream", False)
  244. if "tools" in openai_payload:
  245. ollama_payload["tools"] = openai_payload["tools"]
  246. # If there are advanced parameters in the payload, format them in Ollama's options field
  247. if openai_payload.get("options"):
  248. ollama_payload["options"] = openai_payload["options"]
  249. ollama_options = openai_payload["options"]
  250. def parse_json(value: str) -> dict:
  251. """
  252. Parses a JSON string into a dictionary, handling potential JSONDecodeError.
  253. """
  254. try:
  255. return json.loads(value)
  256. except Exception as e:
  257. return value
  258. ollama_root_params = {
  259. "format": lambda x: parse_json(x),
  260. "keep_alive": lambda x: parse_json(x),
  261. "think": bool,
  262. }
  263. # Ollama's options field can contain parameters that should be at the root level.
  264. for key, value in ollama_root_params.items():
  265. if (param := ollama_options.get(key, None)) is not None:
  266. # Copy the parameter to new name then delete it, to prevent Ollama warning of invalid option provided
  267. ollama_payload[key] = value(param)
  268. del ollama_options[key]
  269. # Re-Mapping OpenAI's `max_tokens` -> Ollama's `num_predict`
  270. if "max_tokens" in ollama_options:
  271. ollama_options["num_predict"] = ollama_options["max_tokens"]
  272. del ollama_options["max_tokens"]
  273. # Ollama lacks a "system" prompt option. It has to be provided as a direct parameter, so we copy it down.
  274. # Comment: Not sure why this is needed, but we'll keep it for compatibility.
  275. if "system" in ollama_options:
  276. ollama_payload["system"] = ollama_options["system"]
  277. del ollama_options["system"]
  278. ollama_payload["options"] = ollama_options
  279. # If there is the "stop" parameter in the openai_payload, remap it to the ollama_payload.options
  280. if "stop" in openai_payload:
  281. ollama_options = ollama_payload.get("options", {})
  282. ollama_options["stop"] = openai_payload.get("stop")
  283. ollama_payload["options"] = ollama_options
  284. if "metadata" in openai_payload:
  285. ollama_payload["metadata"] = openai_payload["metadata"]
  286. if "response_format" in openai_payload:
  287. response_format = openai_payload["response_format"]
  288. format_type = response_format.get("type", None)
  289. schema = response_format.get(format_type, None)
  290. if schema:
  291. format = schema.get("schema", None)
  292. ollama_payload["format"] = format
  293. return ollama_payload
  294. def convert_embedding_payload_openai_to_ollama(openai_payload: dict) -> dict:
  295. """
  296. Convert an embeddings request payload from OpenAI format to Ollama format.
  297. Args:
  298. openai_payload (dict): The original payload designed for OpenAI API usage.
  299. Returns:
  300. dict: A payload compatible with the Ollama API embeddings endpoint.
  301. """
  302. ollama_payload = {"model": openai_payload.get("model")}
  303. input_value = openai_payload.get("input")
  304. # Ollama expects 'input' as a list, and 'prompt' as a single string.
  305. if isinstance(input_value, list):
  306. ollama_payload["input"] = input_value
  307. ollama_payload["prompt"] = "\n".join(str(x) for x in input_value)
  308. else:
  309. ollama_payload["input"] = [input_value]
  310. ollama_payload["prompt"] = str(input_value)
  311. # Optionally forward other fields if present
  312. for optional_key in ("options", "truncate", "keep_alive"):
  313. if optional_key in openai_payload:
  314. ollama_payload[optional_key] = openai_payload[optional_key]
  315. return ollama_payload