payload.py 12 KB

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