aism

Give

Provide data for the LLM.


Giving

To give data to the LLM, use the give() method:

ai.give(
    data1,
    data2,
    data3,
    ...,

    label=label  # optional
)

All the data provided must be structured. See Structured data.

The give() method returns a new session, which allows you to chain give() methods to provide more context or run AI inference.

You can use it the context-manager style.

with ai.give(data) as session:
    # ...it's now a convention to not use .give() 
    # in this block. think of session as immutable
    session.fill(SomeModel)  # some ai inference

This is generally not recommended for bigger projects, but still works.

session = ai.give(data)

# some ai inference
session.fill(SomeModel)

Structured data

When using aism, you must provide structured data. By definition:

StructuredItem = Union[
    str,
    int,
    float,
    bool,
    Sequence[StructuredItem],
    dict[str, StructuredItem],
    StructuredDataclassInstance,
    PydanticModelLike,
    Datetime,
]

In short, the contents you give to aism must be native types (or Pydantic models), and it must be well-structured.

It converts structured data into text with a simple API:

from aism.data import resolve_data

resolve_data(
    ...  # data here
)

str, int, float, bool

These are the most simple types. Not structured, but requisite.

(
    ai
    .give("Hi")  # ok
    .give(1000)  # ok
    .give(1.23)  # ok
    .give(True)  # ok
)

Sequence[...]

You can create a sequence of any structured type mentioned in this column. Since Sequence is covariant, types like list & tuple work fine. However, a set does not work (as of now).

For example:

(
    ai
    .give(["money", "ties"])  # ok
    .give((-1.23, "oh, no"))  # ok
    .give({ 1.0, 2.0, 3.0 })  # error
)

dict[str, ...]

To make Python more compatible with JSON, dictionary keys should be string. This is to prevent weird structures like dict[list, ...] (which is a bad practice).

(
    ai
    .give({ "money": 100 })  # ok
    .give({ "money": { "ties": 420 } })  # ok
    .give({ 1: 10 })  # error
    .give({ [1, 2, 3]: -1 })  # error
)

StructuredDataclassInstance

This is a Protocol for dataclasses.

This does not apply to Pydantic models, since they do not support __dataclass_fields__.

@dataclass
class Person:
    name: str
    id_: int

person = Person(name="Walter", id_=69420)
ai.give(person)  # ok

PydanticModelLike

This is a Protocol for Pydantic models. The pydantic dependency is not required for this to work, respectfully.

class Person(BaseModel):
    name: str
    id_: int

person = Person(name="Walter", id_=69420)
ai.give(person)  # ok

Datetime

Support for Python's datetime object from the datetime module. (See the uppercase D? Ye.)

from datetime import datetime  # object

dt = datetime.now()
ai.give(dt)  # ok

Labeling

You can also label the contents you've given using the label keyword-only argument.

ai.give(important_data, label="Very important data (top-secret)")