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#
- From
pyfect.ai.ai_error:AiError - From
pyfect.persistence:PersistenceError - From
pyfect.schema:SchemaError
Values#
| Attribute | Type | Description |
|---|---|---|
empty | Effect[Service] | A chat with no history. |
PERSISTENCE_KEY | Literal['effect/ai/Chat/Persisted'] = 'effect/ai/Chat/Persisted' | The provider-options key holding a persisted message's |
Classes#
Functions#
from_prompt#
from_prompt(prompt) -> Effect[Service]A chat whose history is prompt.
| Parameter | Type | Description |
|---|---|---|
prompt | P.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.
| Parameter | Type | Description |
|---|---|---|
data | object |
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.
| Parameter | Type | Description |
|---|---|---|
data | str |
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.
| Parameter | Type | Description |
|---|---|---|
store_id | str |
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.
| Parameter | Type | Description |
|---|---|---|
store_id | str |
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))