payload.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. from open_webui.utils.task import prompt_template, prompt_variables_template
  2. from open_webui.utils.misc import (
  3. add_or_update_system_message,
  4. )
  5. from typing import Callable, Optional
  6. # inplace function: form_data is modified
  7. def apply_model_system_prompt_to_body(
  8. params: dict, form_data: dict, metadata: Optional[dict] = None, user=None
  9. ) -> dict:
  10. system = params.get("system", None)
  11. if not system:
  12. return form_data
  13. # Metadata (WebUI Usage)
  14. if metadata:
  15. variables = metadata.get("variables", {})
  16. if variables:
  17. system = prompt_variables_template(system, variables)
  18. # Legacy (API Usage)
  19. if user:
  20. template_params = {
  21. "user_name": user.name,
  22. "user_location": user.info.get("location") if user.info else None,
  23. }
  24. else:
  25. template_params = {}
  26. system = prompt_template(system, **template_params)
  27. form_data["messages"] = add_or_update_system_message(
  28. system, form_data.get("messages", [])
  29. )
  30. return form_data
  31. # inplace function: form_data is modified
  32. def apply_model_params_to_body(
  33. params: dict, form_data: dict, mappings: dict[str, Callable]
  34. ) -> dict:
  35. if not params:
  36. return form_data
  37. for key, cast_func in mappings.items():
  38. if (value := params.get(key)) is not None:
  39. form_data[key] = cast_func(value)
  40. return form_data
  41. # inplace function: form_data is modified
  42. def apply_model_params_to_body_openai(params: dict, form_data: dict) -> dict:
  43. mappings = {
  44. "temperature": float,
  45. "top_p": float,
  46. "max_tokens": int,
  47. "frequency_penalty": float,
  48. "reasoning_effort": str,
  49. "seed": lambda x: x,
  50. "stop": lambda x: [bytes(s, "utf-8").decode("unicode_escape") for s in x],
  51. }
  52. return apply_model_params_to_body(params, form_data, mappings)
  53. def apply_model_params_to_body_ollama(params: dict, form_data: dict) -> dict:
  54. # Convert OpenAI parameter names to Ollama parameter names if needed.
  55. name_differences = {
  56. "max_tokens": "num_predict",
  57. }
  58. for key, value in name_differences.items():
  59. if (param := params.get(key, None)) is not None:
  60. # Copy the parameter to new name then delete it, to prevent Ollama warning of invalid option provided
  61. params[value] = params[key]
  62. del params[key]
  63. opts = [
  64. "temperature",
  65. "top_p",
  66. "seed",
  67. "mirostat",
  68. "mirostat_eta",
  69. "mirostat_tau",
  70. "num_ctx",
  71. "num_batch",
  72. "num_keep",
  73. "num_predict",
  74. "repeat_last_n",
  75. "top_k",
  76. "min_p",
  77. "typical_p",
  78. "repeat_penalty",
  79. "presence_penalty",
  80. "frequency_penalty",
  81. "penalize_newline",
  82. "stop",
  83. "numa",
  84. "num_gpu",
  85. "main_gpu",
  86. "low_vram",
  87. "vocab_only",
  88. "use_mmap",
  89. "use_mlock",
  90. "num_thread",
  91. ]
  92. mappings = {i: lambda x: x for i in opts}
  93. form_data = apply_model_params_to_body(params, form_data, mappings)
  94. return form_data
  95. def convert_messages_openai_to_ollama(messages: list[dict]) -> list[dict]:
  96. ollama_messages = []
  97. for message in messages:
  98. # Initialize the new message structure with the role
  99. new_message = {"role": message["role"]}
  100. content = message.get("content", [])
  101. # Check if the content is a string (just a simple message)
  102. if isinstance(content, str):
  103. # If the content is a string, it's pure text
  104. new_message["content"] = content
  105. else:
  106. # Otherwise, assume the content is a list of dicts, e.g., text followed by an image URL
  107. content_text = ""
  108. images = []
  109. # Iterate through the list of content items
  110. for item in content:
  111. # Check if it's a text type
  112. if item.get("type") == "text":
  113. content_text += item.get("text", "")
  114. # Check if it's an image URL type
  115. elif item.get("type") == "image_url":
  116. img_url = item.get("image_url", {}).get("url", "")
  117. if img_url:
  118. # If the image url starts with data:, it's a base64 image and should be trimmed
  119. if img_url.startswith("data:"):
  120. img_url = img_url.split(",")[-1]
  121. images.append(img_url)
  122. # Add content text (if any)
  123. if content_text:
  124. new_message["content"] = content_text.strip()
  125. # Add images (if any)
  126. if images:
  127. new_message["images"] = images
  128. # Append the new formatted message to the result
  129. ollama_messages.append(new_message)
  130. return ollama_messages
  131. def convert_payload_openai_to_ollama(openai_payload: dict) -> dict:
  132. """
  133. Converts a payload formatted for OpenAI's API to be compatible with Ollama's API endpoint for chat completions.
  134. Args:
  135. openai_payload (dict): The payload originally designed for OpenAI API usage.
  136. Returns:
  137. dict: A modified payload compatible with the Ollama API.
  138. """
  139. ollama_payload = {}
  140. # Mapping basic model and message details
  141. ollama_payload["model"] = openai_payload.get("model")
  142. ollama_payload["messages"] = convert_messages_openai_to_ollama(
  143. openai_payload.get("messages")
  144. )
  145. ollama_payload["stream"] = openai_payload.get("stream", False)
  146. if "tools" in openai_payload:
  147. ollama_payload["tools"] = openai_payload["tools"]
  148. if "format" in openai_payload:
  149. ollama_payload["format"] = openai_payload["format"]
  150. # If there are advanced parameters in the payload, format them in Ollama's options field
  151. ollama_options = {}
  152. if openai_payload.get("options"):
  153. ollama_payload["options"] = openai_payload["options"]
  154. ollama_options = openai_payload["options"]
  155. # Handle parameters which map directly
  156. for param in ["temperature", "top_p", "seed"]:
  157. if param in openai_payload:
  158. ollama_options[param] = openai_payload[param]
  159. # Mapping OpenAI's `max_tokens` -> Ollama's `num_predict`
  160. if "max_completion_tokens" in openai_payload:
  161. ollama_options["num_predict"] = openai_payload["max_completion_tokens"]
  162. elif "max_tokens" in openai_payload:
  163. ollama_options["num_predict"] = openai_payload["max_tokens"]
  164. # Handle frequency / presence_penalty, which needs renaming and checking
  165. if "frequency_penalty" in openai_payload:
  166. ollama_options["repeat_penalty"] = openai_payload["frequency_penalty"]
  167. if "presence_penalty" in openai_payload and "penalty" not in ollama_options:
  168. # We are assuming presence penalty uses a similar concept in Ollama, which needs custom handling if exists.
  169. ollama_options["new_topic_penalty"] = openai_payload["presence_penalty"]
  170. # Add options to payload if any have been set
  171. if ollama_options:
  172. ollama_payload["options"] = ollama_options
  173. if "metadata" in openai_payload:
  174. ollama_payload["metadata"] = openai_payload["metadata"]
  175. return ollama_payload