Utils

Utility functions for the OpenAI Lambda functions.

smarter.apps.provider.services.text_completion.utils.cap_message_history(messages, max_characters)[source]

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.

Parameters:
Return type:

list[dict[str, Any]]

Returns:

The message thread, without its oldest turns if it was too long.

smarter.apps.provider.services.text_completion.utils.ensure_system_role_present(messages, default_system_role='You are a helpful llmclient. When given the opportunity to utilize function calling, you should always do so. This will allow you to provide the best possible responses to the user. If you are unable to provide a response, you should prompt the user for more information. If you are still unable to provide a response, you should inform the user that you are unable to help them at this time.')[source]

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.

Parameters:
  • messages (list[dict[str, Any]]) – The messages list; each message must have role and content keys.

  • default_system_role (str) – The content of the system message to insert when none is present.

Return type:

list

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.

smarter.apps.provider.services.text_completion.utils.exception_response_factory(exception, request_meta_data=None)[source]

Generate a standardized error response dictionary that includes the Python exception type and stack trace.

exception: a descendant of Python Exception class

Return type:

list | dict[str, Any]

smarter.apps.provider.services.text_completion.utils.get_content_for_role(messages, role)[source]

Get the content of the most recent message for a given role.

Parameters:
  • messages (list) – The messages list; each message must have a role key.

  • role (str) – The role to look for, e.g. user.

Return type:

str

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.

smarter.apps.provider.services.text_completion.utils.get_message_history(messages)[source]

Get the text content from the messages list for a given role.

Return type:

list

smarter.apps.provider.services.text_completion.utils.get_messages_for_role(messages, role)[source]

Get the content of every message for a given role.

Parameters:
  • messages (list) – The messages list; each message must have a role key.

  • role (str) – The role to look for, e.g. user.

Return type:

list

Returns:

The content of each message with that role, in order; None for a message without content.

smarter.apps.provider.services.text_completion.utils.get_request_body(data)[source]

Returns the request body as a dictionary.

Parameters:

event – The event object containing the request body.

Return type:

dict[str, Any]

Returns:

A dictionary representing the request body.

smarter.apps.provider.services.text_completion.utils.http_response_factory(status, body, debug_mode=False)[source]

Generate a standardized JSON return dictionary for all possible response scenarios.

Parameters:
  • status (int) – an HTTP response code. see https://developer.mozilla.org/en-US/docs/Web/HTTP/Status

  • 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.

  • debug_mode (bool) – if True, log the response to the CloudWatch log for this Lambda.

Return type:

list | dict[str, Any]

See https://docs.aws.amazon.com/lambda/latest/dg/python-handler.html

smarter.apps.provider.services.text_completion.utils.message_characters(message)[source]

Return the size of a message, in characters: its content and its tool calls.

Parameters:

message (dict[str, Any]) – A message of a thread.

Return type:

int

Returns:

The number of characters.

smarter.apps.provider.services.text_completion.utils.pair_tool_messages(messages)[source]

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.

Parameters:

messages (list[dict[str, Any]]) – The message thread.

Return type:

list[dict[str, Any]]

Returns:

The message thread, in which every tool call is paired with its reply.

smarter.apps.provider.services.text_completion.utils.parse_request(request_body)[source]

Parse the request body and return the endpoint, model, messages, and input_text.

smarter.apps.provider.services.text_completion.utils.should_log(level)[source]

Check if logging should be done based on the waffle switch.