エージェントメモリー¶
エージェントメモリーSDKは、エージェントアプリケーションのチャット形式の履歴を保持します。 このSDKでは、関連する会話をグループ化するMemorySpace、1つの会話を表すSession、およびセッション内のターンやメッセージを記録するEventの3つのタイプを提供しています。
プレミアム機能
DataRobot's Agentic AI capabilities are a premium feature; contact your DataRobot representative for enablement information.
メモリーの用語¶
エージェントメモリーSDKでは、以下の用語を使用しています。
MemorySpace:セッションとそのイベントを格納するコンテナ。 メモリー空間は、チャット形式のセッションおよびそこに記録されたイベントに対して、分離の境界を提供します。Session:メモリー空間内での単一のチャット会話。 各セッションでは、参加者、オプションのメタデータ、および順序付けられたイベントのストリームを追跡します。Event:セッション内の1ターン。 イベントは通常、ユーザーのメッセージ、エージェントの回答、またはアプリケーション定義のアクションを表します。 イベントは、セッション内でsequence_idによって順序付けられます。Lifecycle strategy:セッションとそのイベントが保持される期間を規定するサーバー側のルール。 セッションの作成時に指定しない場合、サーバーはデフォルトの戦略を適用します。
推奨されるワークフローは次のとおりです。
datarobot.models.memory.MemorySpaceを作成して、関連する会話を保存します。- そのメモリー空間で、会話ごとに1つずつ、1つ以上の
datarobot.models.memory.Sessionオブジェクトを作成します。 - 会話の進行に合わせて
datarobot.models.memory.Eventのレコードをセッションに追加し、修正が必要な場合はイベントを更新します。
メモリー空間¶
メモリー空間は最上位のコンテナです。 セッションを保持し、呼び出し元のユーザーとテナントにスコープが設定されます。
メモリー空間の作成と変更¶
MemorySpace.createを使用してメモリー空間を作成し、返されたオブジェクトに対してupdateを呼び出すことで、その説明や関連付けられたLLMモデルの名前を変更できます。 オプションのフィールドをクリアするには、Noneを渡してください。
>>> from datarobot.models.memory import MemorySpace
>>>
>>> # Create a new memory space
>>> memory_space = MemorySpace.create(
... description="Customer support assistant memory",
... llm_model_name="gpt-4o",
... )
>>> memory_space.id
'deadbeef-cafe-babe-feed-cafebabe0000'
>>>
>>> # Update the description; leave llm_model_name unchanged
>>> memory_space.update(description="Customer support assistant -- production")
>>> memory_space.description
'Customer support assistant -- production'
>>>
>>> # Clear the LLM model name by passing None
>>> memory_space.update(llm_model_name=None)
>>> memory_space.llm_model_name is None
True
メモリー空間の取得¶
現在のユーザーがアクセス可能なメモリー空間を一覧表示したり、IDを指定して特定のメモリー空間を取得したりできます。
>>> from datarobot.models.memory import MemorySpace
>>>
>>> # Returns one server-paginated page; use offset and limit to walk further pages
>>> memory_spaces = MemorySpace.list()
>>> next_page = MemorySpace.list(offset=len(memory_spaces), limit=20)
>>>
>>> # Fetch a specific memory space by ID
>>> memory_space = MemorySpace.get("deadbeef-cafe-babe-feed-cafebabe0000")
メモリー空間の削除¶
メモリー空間を削除すると、その中のセッションとイベントがすべて削除されます。 この操作は元に戻せません。
>>> memory_space.delete()
セッションとイベント¶
セッションは1つの会話を表します。 イベントはセッションに順番に追加されます。各イベントには、セッション内での位置を反映した、サーバー割り当てのsequence_idが付与されています。
セッションの作成とイベントの追加¶
Session.createを使用してSessionを作成し、親のmemory_space_idとparticipantsリストを渡します。 会話の進行に合わせて、Session.post_eventを使用してイベントを追加します。 emitter辞書は、イベントを生成したエンティティを識別します。通常、人間のターンでは{"type": "user", "id": <participant_id>}、エージェントの回答では{"type": "agent", "id": <agent_id>}となります。
1セッションにつき1人の参加者
複数参加者のセッションはサポートされていません。 participantsには要素が1つのリストを渡してください。サーバーは、参加者が2人以上のセッションを拒否します。
>>> from datarobot.models.memory import Session
>>>
>>> participant_id = "ba5eba11deadbeefcafebabe"
>>> agent_id = "agent-001"
>>>
>>> # Create a session in the memory space
>>> session = Session.create(
... memory_space_id=memory_space.id,
... participants=[participant_id],
... description="Order status inquiry",
... metadata={"channel": "web", "priority": "high"},
... )
>>>
>>> # Append a user message
>>> user_event = session.post_event(
... body={"content": "Can you check the status of my order?"},
... emitter={"type": "user", "id": participant_id},
... event_type="message",
... )
>>> user_event.sequence_id
0
>>>
>>> # Append an agent reply
>>> agent_event = session.post_event(
... body={"content": "Let me check that for you."},
... emitter={"type": "agent", "id": agent_id},
... event_type="message",
... )
>>> agent_event.sequence_id
1
イベントの更新¶
イベントの本文、タイプ、またはエミッターを変更するには、Session.update_eventを使用します。 対象のイベントはsequence_idで特定します。 同時書き込みを防ぐには、イベントのcreated_atタイムスタンプを渡します。イベントがそのタイムスタンプ以降に変更されている場合、サーバーは更新を拒否します。呼び出し元は、再試行する前にイベントを再読み込みする必要があります。
>>> # The agent's answer needs to be replaced after fetching real data
>>> updated = session.update_event(
... sequence_id=agent_event.sequence_id,
... body={"content": "Your order has shipped and is scheduled to arrive within two business days."},
... created_at=agent_event.created_at, # Optimistic concurrency check
... )
>>> updated.body
{'content': 'Your order has shipped and is scheduled to arrive within two business days.'}
イベントの一覧表示¶
Session.eventsを使用してイベントを読み戻します。 last_nを使用すると最新のイベントを取得でき、offsetとlimitを使用すると最初からページ単位で取得できます。 この2つは互いに排他的です。 event_typeでフィルターすると、特定の種類のイベントのみを取得できます。
>>> # Fetch the most recent 50 message events
>>> recent = session.events(last_n=50, event_type="message")
>>>
>>> # Read the first 100 events from the start of the session
>>> first_page = session.events(offset=0, limit=100)
>>>
>>> # Walk subsequent pages with offset
>>> next_page = session.events(offset=len(first_page), limit=100)
セッションの更新または削除¶
Session.updateを使用して、セッションの説明やメタデータを更新します。 フィールドをクリアするにはNoneを渡してください。 Session.deleteを使用して、セッションとそのイベントを削除します。
>>> session.update(metadata={"channel": "web", "priority": "low"})
>>> session.delete()