Utils
Utility functions for the OpenAI Lambda functions.
- 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.
- Return type:
- 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
- smarter.apps.provider.services.text_completion.utils.get_content_for_role(messages, role)[source]
Get the text content from the messages list for a given role.
- Return type:
- 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:
- smarter.apps.provider.services.text_completion.utils.get_messages_for_role(messages, role)[source]
Get the text content from the messages list for a given role.
- Return type:
- smarter.apps.provider.services.text_completion.utils.get_request_body(data)[source]
Returns the request body as a dictionary.
- 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/Statusbody – 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:
See https://docs.aws.amazon.com/lambda/latest/dg/python-handler.html
- 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
toolmessage that does not answer atool_callsentry of the assistant message immediately before it, and an assistanttool_callsentry that notoolmessage 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’stool_calls, which happens when the prompt history has not been persisted yet. So:a
toolmessage is kept only if it answers a tool call of the preceding assistant message.a tool call that no
toolmessage answers is removed from its assistant message.
The messages themselves are not modified: an assistant message that changes is copied.