oliviadb-agent-framework 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""Microsoft Agent Framework providers on OliviaDB (interfaces of agent-framework-core 1.26.0):
|
|
3
|
+
|
|
4
|
+
* `OliviaDBHistoryProvider` (`HistoryProvider`): a session's conversation in OliviaDB agent state,
|
|
5
|
+
so sessions survive the process and can be resumed by id anywhere;
|
|
6
|
+
* `OliviaDBMemoryProvider` (`ContextProvider`): long-term memory: before a run, what OliviaDB
|
|
7
|
+
recalls for the input is added to the instructions; after it, the exchange is
|
|
8
|
+
remembered as an episode.
|
|
9
|
+
|
|
10
|
+
agent = Agent(client, "You are helpful.",
|
|
11
|
+
context_providers=[OliviaDBHistoryProvider(oliviadb), OliviaDBMemoryProvider(oliviadb)])
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import asyncio
|
|
17
|
+
import hashlib
|
|
18
|
+
from collections.abc import Sequence
|
|
19
|
+
from typing import Any
|
|
20
|
+
|
|
21
|
+
from agent_framework import AgentSession, ContextProvider, HistoryProvider, Message, SessionContext
|
|
22
|
+
from oliviadb import OliviaDbClient
|
|
23
|
+
|
|
24
|
+
__all__ = ["OliviaDBHistoryProvider", "OliviaDBMemoryProvider"]
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class OliviaDBHistoryProvider(HistoryProvider):
|
|
28
|
+
DEFAULT_SOURCE_ID = "oliviadb_history"
|
|
29
|
+
|
|
30
|
+
def __init__(
|
|
31
|
+
self,
|
|
32
|
+
client: OliviaDbClient,
|
|
33
|
+
source_id: str | None = None,
|
|
34
|
+
*,
|
|
35
|
+
root: Sequence[str] = ("agent-framework", "history"),
|
|
36
|
+
**kw: Any,
|
|
37
|
+
):
|
|
38
|
+
super().__init__(source_id or self.DEFAULT_SOURCE_ID, **kw)
|
|
39
|
+
self.c = client
|
|
40
|
+
self.root = list(root)
|
|
41
|
+
|
|
42
|
+
def _ns(self, session_id: str | None) -> list[str]:
|
|
43
|
+
return [*self.root, session_id or "-"]
|
|
44
|
+
|
|
45
|
+
async def get_messages(
|
|
46
|
+
self, session_id: str | None, *, state: dict[str, Any] | None = None, **kwargs: Any
|
|
47
|
+
) -> list[Message]:
|
|
48
|
+
def go():
|
|
49
|
+
out, after = [], None
|
|
50
|
+
while True:
|
|
51
|
+
page = self.c.state_list(self._ns(session_id), after=after, limit=500)
|
|
52
|
+
out.extend(Message.from_dict(i["value"]) for i in page)
|
|
53
|
+
if len(page) < 500:
|
|
54
|
+
return out
|
|
55
|
+
after = page[-1]["key"]
|
|
56
|
+
|
|
57
|
+
return await asyncio.to_thread(go)
|
|
58
|
+
|
|
59
|
+
async def save_messages(
|
|
60
|
+
self, session_id: str | None, messages: Sequence[Message], *, state: dict[str, Any] | None = None, **kwargs: Any
|
|
61
|
+
) -> None:
|
|
62
|
+
def go():
|
|
63
|
+
for m in messages:
|
|
64
|
+
self.c.state_append(self._ns(session_id), m.to_dict())
|
|
65
|
+
|
|
66
|
+
await asyncio.to_thread(go)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class OliviaDBMemoryProvider(ContextProvider):
|
|
70
|
+
DEFAULT_SOURCE_ID = "oliviadb_memory"
|
|
71
|
+
|
|
72
|
+
def __init__(
|
|
73
|
+
self, client: OliviaDbClient, source_id: str | None = None, *, thread: str | None = None, budget: int = 400
|
|
74
|
+
):
|
|
75
|
+
super().__init__(source_id or self.DEFAULT_SOURCE_ID)
|
|
76
|
+
self.c = client
|
|
77
|
+
self.thread = thread
|
|
78
|
+
self.budget = budget
|
|
79
|
+
|
|
80
|
+
async def before_run(
|
|
81
|
+
self, *, agent: Any, session: AgentSession, context: SessionContext, state: dict[str, Any]
|
|
82
|
+
) -> None:
|
|
83
|
+
query = " ".join(m.text for m in context.input_messages if m.text)
|
|
84
|
+
if not query:
|
|
85
|
+
return
|
|
86
|
+
r = await asyncio.to_thread(self.c.recall, query, self.budget, self.thread or session.session_id)
|
|
87
|
+
if r.get("text"):
|
|
88
|
+
context.extend_instructions(self.source_id, "What you remember (cite refs like [m1]):\n" + r["text"])
|
|
89
|
+
|
|
90
|
+
async def after_run(
|
|
91
|
+
self, *, agent: Any, session: AgentSession, context: SessionContext, state: dict[str, Any]
|
|
92
|
+
) -> None:
|
|
93
|
+
msgs = list(context.input_messages) + (list(context.response.messages) if context.response else [])
|
|
94
|
+
text = "\n".join(f"{m.role}: {m.text}" for m in msgs if m.text)
|
|
95
|
+
if not text:
|
|
96
|
+
return
|
|
97
|
+
thread = self.thread or session.session_id or "agent-framework"
|
|
98
|
+
step = hashlib.sha256((thread + text).encode()).hexdigest()[:32]
|
|
99
|
+
await asyncio.to_thread(self.c.remember, thread, text, step)
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: oliviadb-agent-framework
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Microsoft Agent Framework history and memory providers backed by OliviaDB
|
|
5
|
+
License-Expression: Apache-2.0
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Requires-Dist: oliviadb>=0.1.0
|
|
8
|
+
Requires-Dist: agent-framework-core<2,>=1.26
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
oliviadb_agent_framework/__init__.py,sha256=CnGPkSIGMkTA45QzmxE2DROHRFAF31YHS0agjod-tTA,3822
|
|
2
|
+
oliviadb_agent_framework-0.1.0.dist-info/METADATA,sha256=YOZC1qr0ONYClSHo9j23YpAiViurZSNOiBoPicYlKtM,282
|
|
3
|
+
oliviadb_agent_framework-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
4
|
+
oliviadb_agent_framework-0.1.0.dist-info/top_level.txt,sha256=BWMMOKwoy1-1p_TJMYnfcRZDoRYSmDh5x_3kJuB-Kik,25
|
|
5
|
+
oliviadb_agent_framework-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
oliviadb_agent_framework
|