Skip to main content

AI

Ask text and image prompts easily to AI.

Basic use

The main prompt can be a text, a file, or a list containing both.

info

We currently support PNG (.png), JPEG (.jpeg and .jpg), WEBP (.webp), non-animated GIF (.gif) and PDFs (.pdf) as files.

from abstra.ai import prompt

ans = prompt("When Python was created?")
from abstra.ai import prompt

ans = prompt(["Summarize this document", "./my_doc.pdf"])

Specify answer format

Use the format parameter to enforce the AI response to follow a specific format.

Types

from abstra.ai import prompt

ans = prompt(
"Python is a strongly typing programming language created in 1989.",
format={
"year": int,
"reason": str,
"is_commonly_known": bool,
"quality_score": float,
"typing": ["strong", "weak"]
}
)

Accepted types:

  • int
  • str
  • bool
  • float
  • ["foo", "bar"] (enum)

JSON Schema

from abstra.ai import prompt

ans = prompt(
"Python is a strongly typing programming language created in 1989.",
format={
"year": { "type": "number" },
"typing": { "type": "string", "enum": ["strong", "weak"] }
}
)

See more in https://json-schema.org/

Add system instructions

Use the instructions parameter to provide specific guidelines for the AI to follow when responding to the main prompt.

from abstra.ai import prompt

txt = "With Abstra's workflows, it's easy to automate processes"

ans = prompt(
f"Translate this text to portuguese: {txt}",
instructions=["Don't translate the word workflow"]
)

Examples

Get information from invoices

from abstra.forms import read_file
from abstra.ai import prompt

invoice_file = read_file("Update your invoice here")
ans = prompt(
["Here is an invoice", invoice_file],
format={
"value": { "type": "number", "description": "Value of the service in dollars" },
"CNPJ": { "type": "string" }
}
)

value = ans["value"]
cnpj = ans["CNPJ"]

Parameters

ParameterDescriptionType
promptMain prompt(s) to be asked to the AIOne or many str, pathlib.Path, io.IOBase or base64 string
instructionsSystem instructions to be considered to answer the prompts. Defaults to []str | list[str]
formatThe format of AI response using JSON Schema. Defaults to None, implying the response will be a string.JSON Schema
temperatureDefine how random the output will be. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. When not specified, defaults to 1.number between 0 and 2