payload.py 9.9 KB

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