Source code for smarter.apps.provider.services.text_completion.utils

# pylint: disable=duplicate-code
# pylint: disable=E1101
"""Utility functions for the OpenAI Lambda functions."""

import base64
import sys  # libraries for error management
import traceback  # libraries for error management
from typing import Optional, Union

from smarter.apps.provider.services.text_completion.const import (
    LANGCHAIN_MESSAGE_HISTORY_ROLES,
)
from smarter.common.conf import smarter_settings
from smarter.common.exceptions import SmarterValueError
from smarter.lib import (  # library for interacting with JSON data https://www.json.org/json-en.html
    json,
    logging,
)
from smarter.lib.django import waffle
from smarter.lib.django.waffle import SmarterWaffleSwitches
from smarter.lib.typing import SmarterDictType

from .const import OpenAIMessageKeys
from .validators import (
    validate_endpoint,
    validate_max_completion_tokens,
    validate_messages,
    validate_object_types,
    validate_request_body,
    validate_temperature,
)


[docs] def should_log(level): """Check if logging should be done based on the waffle switch.""" return waffle.switch_is_active(SmarterWaffleSwitches.PROMPT_LOGGING)
logger = logging.getSmarterLogger(__name__, condition_func=should_log)
[docs] def http_response_factory(status: int, body, debug_mode: bool = False) -> Union[list, SmarterDictType]: """ Generate a standardized JSON return dictionary for all possible response scenarios. :param status: an HTTP response code. see https://developer.mozilla.org/en-US/docs/Web/HTTP/Status :param body: a JSON dict of http response for status 200, an error dict otherwise. Both are serialized into the "body", so that an error's message reaches the client. :param debug_mode: if True, log the response to the CloudWatch log for this Lambda. See https://docs.aws.amazon.com/lambda/latest/dg/python-handler.html """ if status < 100 or status > 599: raise SmarterValueError(f"Invalid HTTP response code received: {status}") retval = { "isBase64Encoded": False, "statusCode": status, "headers": {"Content-Type": "application/json"}, } if status != 200: logger.error("Error: %s", body) if debug_mode: retval["body"] = body # log our output to the CloudWatch log for this Lambda logger.info(json.dumps({"retval": retval})) try: # see https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html retval["body"] = json.dumps(body) except (TypeError, ValueError) as exc: logger.error("Failed to serialize response body: %s", exc) retval["body"] = str(body) return retval
[docs] def exception_response_factory( exception, request_meta_data: Optional[SmarterDictType] = None ) -> Union[list, SmarterDictType]: """ Generate a standardized error response dictionary that includes the Python exception type and stack trace. exception: a descendant of Python Exception class """ exc_info = sys.exc_info() retval = { "request_meta_data": request_meta_data, "error": str(exception), "description": "".join(traceback.format_exception(*exc_info)), } return retval
[docs] def get_request_body(data) -> SmarterDictType: """ Returns the request body as a dictionary. Args: event: The event object containing the request body. Returns: A dictionary representing the request body. """ if hasattr(data, "isBase64Encoded") and bool(data["isBase64Encoded"]): # pylint: disable=line-too-long # https://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20 # https://stackoverflow.com/questions/53340627/typeerror-expected-bytes-like-object-not-str request_body = str(data["body"]).encode("ascii") request_body = base64.b64decode(request_body) else: request_body = data if not isinstance(request_body, dict): try: request_body = json.loads(request_body) except json.JSONDecodeError as exc: raise SmarterValueError(f"Invalid JSON request body: {exc}") from exc except TypeError as exc: raise SmarterValueError(f"Invalid request body type: {exc}") from exc validate_request_body(request_body=request_body) if hasattr(request_body, "temperature"): temperature = request_body["temperature"] validate_temperature(temperature=temperature) if hasattr(request_body, "max_completion_tokens"): max_completion_tokens = request_body["max_completion_tokens"] validate_max_completion_tokens(max_completion_tokens=max_completion_tokens) if hasattr(request_body, "end_point"): end_point = request_body["end_point"] validate_endpoint(end_point=end_point) if hasattr(request_body, "object_type"): object_type = request_body["object_type"] validate_object_types(object_type=object_type) validate_messages(request_body=request_body) # an assistant message that only calls tools may omit its content. The rest of the # pipeline reads every message's content, so it is null, as OpenAI returns it. for message in request_body["messages"]: message.setdefault(OpenAIMessageKeys.MESSAGE_CONTENT_KEY, None) return request_body
[docs] def parse_request(request_body: SmarterDictType): """Parse the request body and return the endpoint, model, messages, and input_text.""" messages: Optional[list[SmarterDictType]] = request_body.get("messages") input_text: Optional[str] = request_body.get("input_text") prompt_history: Optional[list[SmarterDictType]] = request_body.get("prompt_history") if not messages and not input_text: raise SmarterValueError("A value for either messages or input_text is required") if messages is not None and not isinstance(messages, list): try: messages = json.loads(messages) except json.JSONDecodeError as exc: raise SmarterValueError(f"Invalid JSON messages: {exc}") from exc if not isinstance(messages, list): raise SmarterValueError("Messages must be a list") for message in messages: if not isinstance(message, dict): raise SmarterValueError("Each message must be a dictionary") if "role" not in message or "content" not in message: raise SmarterValueError("Each message must contain 'role' and 'content' keys") if prompt_history and input_text: # memory-enabled request assumed to be destined for langchain_passthrough # we'll need to rebuild the messages list from the prompt_history messages = [] for prompt in prompt_history: messages.append({"role": prompt["sender"], "content": prompt["message"]}) messages.append({"role": "user", "content": input_text}) if isinstance(messages, list) and not input_text: # we need to extract the most recent prompt for the user role input_text = get_content_for_role(messages, "user") return messages, input_text
[docs] def get_content_for_role(messages: list, role: str) -> str: """ Get the content of the most recent message for a given role. :param messages: The messages list; each message must have a ``role`` key. :param role: The role to look for, e.g. ``user``. :returns: The ``content`` of the last message with that role (None if that message has no content), or an empty string if no message has that role. """ retval = [d.get("content") for d in messages if d["role"] == role] try: return retval[-1] except IndexError: return ""
[docs] def get_message_history(messages: list) -> list: """Get the text content from the messages list for a given role.""" message_history = [ {"role": d["role"], "content": d.get("content")} for d in messages if d["role"] in LANGCHAIN_MESSAGE_HISTORY_ROLES ] return message_history
[docs] def get_messages_for_role(messages: list, role: str) -> list: """ Get the content of every message for a given role. :param messages: The messages list; each message must have a ``role`` key. :param role: The role to look for, e.g. ``user``. :returns: The ``content`` of each message with that role, in order; None for a message without content. """ retval = [d.get("content") for d in messages if d["role"] == role] return retval
[docs] def ensure_system_role_present( messages: list[SmarterDictType], default_system_role: str = smarter_settings.llm_default_system_role # type: ignore ) -> list: """ Ensure that a system role is present in the messages list. If no message has the ``system`` role, a system message with ``default_system_role`` as its content is inserted at the front of the list, in place. :param messages: The messages list; each message must have ``role`` and ``content`` keys. :param default_system_role: The content of the system message to insert when none is present. :returns: The same list, with a system message. :raises SmarterValueError: If ``messages`` is not a list of dicts with ``role`` and ``content`` keys, or ``default_system_role`` is not a string. """ if not isinstance(messages, list): raise SmarterValueError("Messages must be a list") if not all(isinstance(d, dict) for d in messages): raise SmarterValueError("Each message must be a dictionary") if not all( OpenAIMessageKeys.MESSAGE_ROLE_KEY in d and OpenAIMessageKeys.MESSAGE_CONTENT_KEY in d for d in messages ): raise SmarterValueError("Each message must contain 'role' and 'content' keys") if not isinstance(default_system_role, str): raise SmarterValueError("Default system role must be a string") if not any(d[OpenAIMessageKeys.MESSAGE_ROLE_KEY] == OpenAIMessageKeys.SYSTEM_MESSAGE_KEY for d in messages): # expected for the first prompt of a session, whose messages have no system role yet. logger.debug("adding the default system role") messages.insert( 0, { OpenAIMessageKeys.MESSAGE_ROLE_KEY: OpenAIMessageKeys.SYSTEM_MESSAGE_KEY, OpenAIMessageKeys.MESSAGE_CONTENT_KEY: default_system_role, }, ) return messages
[docs] def pair_tool_messages(messages: list[SmarterDictType]) -> list[SmarterDictType]: """ Return the messages with every tool call paired with its tool reply, as OpenAI requires. The OpenAI chat completions API refuses, with a 400, a ``tool`` message that does not answer a ``tool_calls`` entry of the assistant message immediately before it, and an assistant ``tool_calls`` entry that no ``tool`` message answers. A message thread can break this rule when it comes from a client, such as the chat window, that keeps the tool replies but not the assistant's ``tool_calls``, which happens when the prompt history has not been persisted yet. So: - a ``tool`` message is kept only if it answers a tool call of the preceding assistant message. - a tool call that no ``tool`` message answers is removed from its assistant message. The messages themselves are not modified: an assistant message that changes is copied. :param messages: The message thread. :return: The message thread, in which every tool call is paired with its reply. """ role_key, tool_calls_key = OpenAIMessageKeys.MESSAGE_ROLE_KEY, "tool_calls" retval: list[SmarterDictType] = [] i = 0 while i < len(messages): message = messages[i] role = message.get(role_key) if role == OpenAIMessageKeys.TOOL_MESSAGE_KEY: # an orphan: it does not follow an assistant message with tool calls. logger.warning("removed a tool message without a tool call: %s", message.get("tool_call_id")) i += 1 continue tool_calls = message.get(tool_calls_key) if role == OpenAIMessageKeys.ASSISTANT_MESSAGE_KEY else None if not tool_calls: retval.append(message) i += 1 continue # the tool messages that immediately follow the assistant message j = i + 1 replies: list[SmarterDictType] = [] while j < len(messages) and messages[j].get(role_key) == OpenAIMessageKeys.TOOL_MESSAGE_KEY: replies.append(messages[j]) j += 1 call_ids = [call.get("id") for call in tool_calls if isinstance(call, dict)] replies = [reply for reply in replies if reply.get("tool_call_id") in call_ids] answered = {reply.get("tool_call_id") for reply in replies} paired_calls = [call for call in tool_calls if isinstance(call, dict) and call.get("id") in answered] if len(paired_calls) != len(tool_calls): logger.warning("removed %s tool calls without a tool message", len(tool_calls) - len(paired_calls)) message = dict(message) if paired_calls: message[tool_calls_key] = paired_calls else: del message[tool_calls_key] if message.get(OpenAIMessageKeys.MESSAGE_CONTENT_KEY) is None: message[OpenAIMessageKeys.MESSAGE_CONTENT_KEY] = "" retval.append(message) retval.extend(replies) i = j return retval
[docs] def message_characters(message: SmarterDictType) -> int: """ Return the size of a message, in characters: its content and its tool calls. :param message: A message of a thread. :return: The number of characters. """ content = message.get(OpenAIMessageKeys.MESSAGE_CONTENT_KEY) if isinstance(content, str): size = len(content) elif content: size = len(json.dumps(content)) else: size = 0 tool_calls = message.get("tool_calls") if tool_calls: size += len(json.dumps(tool_calls)) return size
[docs] def cap_message_history(messages: list[SmarterDictType], max_characters: int) -> list[SmarterDictType]: """ Return the messages without the oldest turns of the conversation, so that they fit in ``max_characters``. A turn is a ``user`` message and every message that follows it until the next ``user`` message, e.g. the assistant's tool calls, their ``tool`` replies and its answer. Whole turns are left out, so every tool call stays paired with its reply. The ``system`` messages and the latest turn are always kept, even if they alone are longer than ``max_characters``. The messages themselves are not modified, and the list is a new one if any turn is left out. :param messages: The message thread, oldest first. :param max_characters: The most characters of content and tool calls to keep. See :func:`message_characters`. :return: The message thread, without its oldest turns if it was too long. """ role_key = OpenAIMessageKeys.MESSAGE_ROLE_KEY total = sum(message_characters(message) for message in messages) if total <= max_characters: return messages turn_starts = [ i for i, message in enumerate(messages) if message.get(role_key) == OpenAIMessageKeys.USER_MESSAGE_KEY ] if len(turn_starts) < 2: return messages # leave out the messages before each later turn's start, oldest first, until the rest fit. cut = 0 dropped = 0 for start in turn_starts[1:]: dropped += sum( message_characters(message) for message in messages[cut:start] if message.get(role_key) != OpenAIMessageKeys.SYSTEM_MESSAGE_KEY ) cut = start if total - dropped <= max_characters: break retval = [ message for i, message in enumerate(messages) if i >= cut or message.get(role_key) == OpenAIMessageKeys.SYSTEM_MESSAGE_KEY ] logger.info( "left out %s of %s messages, %s of %s characters, to fit in %s characters", len(messages) - len(retval), len(messages), dropped, total, max_characters, ) return retval