API Reference¶
Indices and tables¶
Manage the application creation and configuration process.¶
- promptly.app.create_app(config=None) Flask [source]¶
Create and configure an instance of the Flask application.
- promptly.app.load_env_vars(base_path: str)[source]¶
Load the current dotenv as system environment variable.
Configuration module for the application.¶
Provides the base configuration and specific configurations for development, testing, and production. Includes the ‘config’ dictionary for easy switching between different configurations.
- class promptly.config.Config[source]¶
Bases:
object
Base config, uses staging database server.
- static init_app(app: Flask)[source]¶
Initialize the application with configuration-specific settings.
This method takes a Flask application instance and configures it with settings defined in the configuration object.
- Parameters:
app (
flask.Flask
) – The Flask application instance.
Usage:
config_obj = DevelopmentConfig() config_obj.init_app(app)
A module with utility functions.¶
- promptly.utils.PROMPTLY_THREAD_TIMEOUT: float = 60.0¶
The maximum amount of time, in seconds, that the
threaded_execute()
function will wait for the completion of the function it is executing in a separate thread.This constant uses the value of the
PROMPTLY_THREAD_TIMEOUT
environment variable, if set, or defaults to 60 seconds.
- promptly.utils.strtobool(value: str) bool [source]¶
Convert a string representation of truth to true (1) or false (0).
String values are case-insensitive and can be any of: ‘y’, ‘yes’, ‘t’, ‘true’, ‘on’, ‘1’, ‘n’, ‘no’, ‘f’, ‘false’, ‘off’, ‘0’.
- Parameters:
value (str) – The string to convert to a boolean.
- Returns:
The boolean representation of the string.
- Return type:
- Raises:
ValueError – If the value does not represent a truth value.
Usage:
bool_value = strtobool('True') # bool_value is now True
- promptly.utils.try_parse_int(value: str) int [source]¶
Try to convert a string to an integer.
This function attempts to convert a string to an integer. If the conversion fails due to a
ValueError
orTypeError
, it returnsNone
.
- promptly.utils.threaded_execute(func, *args, timeout=None, **kwargs)[source]¶
Worker thread for timed request execution.
This function will continuously attempt to execute
func
until it succeeds without encountering aconcurrent.futures.TimeoutError
. If aconcurrent.futures.TimeoutError
occurs, a warning is logged, and the function is re-invoked with the same arguments.- Parameters:
func – The function to be executed.
args – Positional arguments to pass to
func
.timeout – The time, in seconds, within which
func
should complete execution. Defaults toPROMPTLY_THREAD_TIMEOUT
.kwargs – Keyword arguments to pass to
func
.
- Returns:
The return value of
func
, if it completes successfully within the allotted time.- Raises:
Any exceptions raised by
func
, except forconcurrent.futures.TimeoutError
which is caught and logged.
The api blueprint module for the application.¶
This module provides a simplified interface to the OpenAI API.¶
- promptly.api.openai_eval.completion(*args, **kwargs) Dict[str, Any] [source]¶
Generates a text completion using the OpenAI API.
This function uses an exponential backoff strategy to handle the following exceptions:
openai.error.ServiceUnavailableError
openai.error.APIError
openai.error.RateLimitError
openai.error.APIConnectionError
openai.error.Timeout
The backoff delay starts at 1 second and increases by a factor of 1.5 with each retry, capping at a maximum delay of 60 seconds between retries.
Any arguments or keyword arguments are forwarded directly to the
openai.ChatCompletion.create
method. The OpenAI API key is read from theOPENAI_API_KEY
environment variable by any import openai’s__init__.py
file.
Module for serving chat API request.¶
This module contains a blueprint for a Flask application to handle chat API requests. It processes a user’s chat message and returns the model’s response.
- promptly.api.views.conversation()[source]¶
Process a user’s chat message and return the model’s response.
Extracts the user’s message from the request form, sends it to OpenAI for processing, extracts the response from OpenAI’s reply, and sends the response back to the user.
- Returns:
A JSON object containing the model’s response to the user’s message.
- Return type:
- Raises:
Exception –
openai.error.OpenAIError
for issues with the OpenAI call.
The chat blueprint module for the application.¶
The chat module for the application.¶
- promptly.chat.views.chat_index() Response [source]¶
Redirect to the appropriate chat page based on cookie value.
This function checks if a
chat_id
exists in cookies and if so, redirects the user to that specific chat page. Otherwise, a new chat page is rendered.- Returns:
Redirect response or a call to
chat()
function.- Return type:
- promptly.chat.views.chat(chat_id=None) Response [source]¶
Render the chat page of the application.
- Parameters:
chat_id (int) – The ID of the chat. If not exists, a new chat is created.
- Returns:
The rendered chat page template.
- Return type:
- promptly.chat.views.history() str [source]¶
Render the chat history page of the application.
- Returns:
The rendered chat history template.
- Return type:
The template filters module for the chat.¶
- promptly.chat.template_filters.human_readable_date(value: datetime) str [source]¶
Convert a datetime object to a human-readable date.
This function takes a datetime object and returns a string that represents a more human-readable and context-sensitive representation of the time. The output will be one of the following:
Today
Yesterday
Previous 7 Days
Previous 30 Days
Previous Year
Older
- Parameters:
value (datetime.datetime) – The datetime object to be converted.
- Returns:
A string representing the human-readable date.
- Return type:
The main blueprint module for the application.¶
The promptly.main
module provides the routes and errors handlers
definition for the application.
General routes and views for the application.¶
This module contains the general routes and views for the main section of the Promptly application, including the homepage and maintenance mode handling.
- promptly.main.views.PROMPTLY_MAINTENANCE_MODE: bool = False¶
The maintenance mode flag for the application.
This constant uses
PROMPTLY_MAINTENANCE_MODE
environment variable to determine if the application is in maintenance mode. The value of the environment variable is converted to a boolean value using thestrtobool()
function.
- promptly.main.views.maintained()[source]¶
Check if the application is in maintenance mode.
This function is called before each request to the application. If the environment variable
PROMPTLY_MAINTENANCE_MODE
is set to a truthy value, the function will abort the request with a 503 Service Unavailable status.- Raises:
HTTPException: 503 Service Unavailable, if the app is in maintenance mode.
Application models and convenient imports.¶
The promptly.models
module provides application models and imports
necessary modules for ease of access.
Service classes for interacting with external systems.¶
The promptly.services
module contains service classes and functions
that encapsulate the logic for interacting with external systems such as the
OpenAI API.
OpenAI Service Wrapper.¶
The promptly.services.openai_service
module provides a service for
interacting with the OpenAI’s API through the OpenAIService
class.
It handles query construction, API calls, and response extraction.
- class promptly.services.openai_service.OpenAIService[source]¶
Bases:
object
A service class for interacting with the OpenAI API.
- Attributes:
- model (str): The model to be used by OpenAI. Default is
gpt-3.5-turbo
or can be set via the environment variableOPENAI_MODEL
. For details on available models, see the OpenAI API documentation.