GitHub

API reference for chat.

Chat (Effect's effect/ai/Chat): a conversation that keeps its history. Each call sends the history plus the new prompt to the LanguageModel and appends the prompt and the response to the history; calls on one chat run one at a time::

chat = yield from chat_.empty
yield from chat.generate_text("Hi, I'm Ada")
reply = yield from chat.generate_text("What is my name?")

export / export_json and from_export / from_json save and restore a history. Persistence (layer_persisted) keeps chats in a BackingPersistence store by id, saving after every call and tagging each new message with a message id under PERSISTENCE_KEY.

Re-exports

Values

AttributeTypeDescription
emptyEffect[Service]

A chat with no history.

PERSISTENCE_KEYLiteral['effect/ai/Chat/Persisted'] = 'effect/ai/Chat/Persisted'

The provider-options key holding a persisted message's messageId.

Classes

Functions

from_prompt

from_prompt(prompt) -> Effect[Service]

A chat whose history is prompt.

ParameterTypeDescription
promptP.RawInput

Returns Effect[Service]

def from_prompt(prompt: P.RawInput) -> Effect[Service]:
    """A chat whose history is ``prompt``."""
    return fx.sync(lambda: Service(Ref(P.make(prompt))))

from_export

from_export(data) -> Effect[Service, SchemaError]

A chat from export's wire form.

ParameterTypeDescription
dataobject

Returns Effect[Service, SchemaError]

def from_export(data: object) -> Effect[Service, SchemaError]:
    """A chat from ``export``'s wire form."""
    return _decode(data).flat_map(from_prompt)

from_json

from_json(data) -> Effect[Service, SchemaError]

A chat from export_json's text.

ParameterTypeDescription
datastr

Returns Effect[Service, SchemaError]

def from_json(data: str) -> Effect[Service, SchemaError]:
    """A chat from ``export_json``'s text."""
    return _decode_json(data).flat_map(from_prompt)

make_persisted

make_persisted(*, store_id) -> Effect[PersistenceService, Never, BackingPersistence | Scope]

Chats in the backing store store_id.

ParameterTypeDescription
store_idstr

Returns Effect[PersistenceService, Never, BackingPersistence | Scope]

def make_persisted(
    *, store_id: str
) -> Effect[PersistenceService, Never, BackingPersistence | Scope]:
    """Chats in the backing store ``store_id``."""
    return (
        BackingPersistence()
        .flat_map(lambda backing: backing.make(store_id))
        .map(PersistenceService)
    )

layer_persisted

layer_persisted(*, store_id) -> Layer[Persistence, Never, BackingPersistence]

Persistence in the backing store store_id.

ParameterTypeDescription
store_idstr

Returns Layer[Persistence, Never, BackingPersistence]

def layer_persisted(*, store_id: str) -> Layer[Persistence, Never, BackingPersistence]:
    """``Persistence`` in the backing store ``store_id``."""
    return layer_effect(Persistence, make_persisted(store_id=store_id))