Python Function Runtime
Python Function Runtime
The Python Function Runtime allows you to author functions (such as prompt builders and completion handlers) using a limited version of the Python programming language. The runtime is only intended for writing pure functions and is not a general software runtime. Think of it as a templating engine on steroids. Rather than worrying about network requests, concurrency and other software engineering concerns, you can just focus on minimal scripting to achieve the desired translations.
- Compatible with Python 3.12 syntax
- Each assistant gets one python module in which to author functions referenced directly by the Flow or lower-level helper functions
- To aide in debugging,
print
output is visible in the Debug Workbench -> Inspector -> Console area - The
import
keyword is not allowed. - The only module you have access to is the pre-imported
quiq
module that contains various helper functions documented below - There's no way to make network requests from within your functions. However, search results and webhook results accumulate on the
context
argument that is passed into your assistant functions
Reference Materials
quiq.base64
encode
def encode(bites, url=False)
Encode a bytes-like object using python's base64 standard library
Arguments:
bites
bytes - a bytes-like object to base64 encodeurl
bool - whether or not to encode to a base64 URL scheme
Returns:
Base64 encoded bytes
decode
def decode(bites, url=False)
Decode a bytes-like object using python's base64 standard library
Arguments:
bites
bytes - a bytes-like object to base64 decodeurl
bool - whether or not to decode from a base64 URL scheme
Returns:
Bytes decoded from base64
encode_to_string
def encode_to_string(string, url=False, charset="utf-8")
Base64 encode a string using python's base64 standard library
Arguments:
string
str|bytes - a str (or bytes) to base64 encodeurl
- (bool): whether or not this is to be encoded to a base64 URL schemecharset
str - defaults to utf-8
Returns:
The base64 encoded string generated from the input argument
decode_to_string
def decode_to_string(string, url=False, charset="utf-8")
Base64 decode a string using python's base64 standard library
Arguments:
string
str|bytes - a str (or bytes) to base64 decodeurl
bool - whether or not this is to be decoded from a base64 URL schemecharset
str - defaults to utf-8
Returns:
The string decoded from the base64 input.
quiq.ctx
get_search_results
def get_search_results(resource_id, max_distance=None, max_records=None)
Easily find search results for a given search resource.
Finds search results by search resource id with the ability to apply a distance filter.
Arguments:
resource_id
str - The id of the search indexmax_distance
float - Optional max distance between the search result and the search stringmax_records
int - Optional maximum number of records to return
Returns:
A list of dataset records, the schema of which varies based on the dataset.
format_search_results
def format_search_results(resource_id,
format_str,
records=None,
separator="\n\n",
max_distance=None,
max_records=None)
Easily format search result into a string (typically for inclusion in prompts)
Finds and formats search results by search resource id with the ability to apply a distance filter.
Example invocation assuming dataset records ahve the properties title
and body
:
quiq.ctx.format_search_results('my-search-resource', "{title} - {body}")
Arguments:
resource_id
str - The id of the search resourceformat_str
str - A python f-string (format string) that will be used to format the search recordsrecords
- (list): Overrides the list of records to format. By default will be populated for you usingget_search_results
separator
- (str): Separator to be used between records in the final formatted string.max_distance
float - Optional max distance between the search result and the search stringmax_records
int - Optional maximum number of records to return
Returns:
A string representation of the matching search results
quiq.dt
timestamp_to_iso
def timestamp_to_iso(timestamp, tz=None)
Convert timestamp to an ISO 8601 string
Converts timestamps into ISO 8601 strings. Will handle either second or millisecond timestamps. By default the ISO
strings will be in UTC but you can override that.
Arguments:
timestamp
- the timestamp in seconds or milliseconds. Can be a float or inttz
- Optional tz, which must be a legal key into the IANA zone_info DB. Defaults to UTC.
Returns:
An ISO 8601 string
iso_to_timestamp
def iso_to_timestamp(iso_string)
Convert an ISO 8601 string to a timestamp
Arguments:
iso_string
str - A legal ISO 8601 datetime string
Returns:
A unix timestamp (in seconds) as a float
format_timestamp
def format_timestamp(timestamp, format_str, tz=None)
Format a timestamp according to a format string.
See the python docs for details on supported format strings
Arguments:
timestamp
- the timestamp in seconds or milliseconds. Can be a float or intformat_str
- the formatting string to apply to the timestamptz
- Optional tz, which must be a legal key into the IANA zone_info DB. Defaults to UTC.
Returns:
The formatted string
quiq.json
loads
def loads(s)
Deserialize a JSON string into an object
dumps
def dumps(obj, indent=None)
Serialize an object to a JSON string
quiq.jwt
get_unverified_claimset
def get_unverified_claimset(encoded)
Read the claimset of a JWT without validating its signature
Arguments:
encoded
- the encoded JWT
Returns:
Claims dictionary
get_unverified_headers
def get_unverified_headers(encoded)
Read the headers of a JWT without validating its signature
Arguments:
encoded
- the encoded JWT
Returns:
Headers dictionary
quiq.re
Wrappers around commonly used regex functions
findall
def findall(pattern, string, ignore_case=False)
Find all occurences of a regex pattern in a string
sub
def sub(pattern, repl, string, count=0, ignore_case=False)
Substitute occurences of a regex pattern in a string with a replacement
quiq.tokens
count
def count(llm, prompt)
Count the tokens used by the specified prompt. Only works for OpenAI ChatCompletion models
quiq.urllib.parse
quote
def quote(string, safe='/', encoding=None, errors=None)
Replace special characters in string using the %xx escape
quote_plus
def quote_plus(string, safe='', encoding=None, errors=None)
Like quote(), but also replace spaces with plus signs
quiq.yaml
safe_load
def safe_load(data)
Deserialize YAML into an object
dump
def dump(data)
Serialize an object to YAML
Updated 4 months ago