oliviadb-agent-framework 0.1.0__tar.gz
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.
- oliviadb_agent_framework-0.1.0/PKG-INFO +8 -0
- oliviadb_agent_framework-0.1.0/pyproject.toml +16 -0
- oliviadb_agent_framework-0.1.0/setup.cfg +4 -0
- oliviadb_agent_framework-0.1.0/src/oliviadb_agent_framework/__init__.py +99 -0
- oliviadb_agent_framework-0.1.0/src/oliviadb_agent_framework.egg-info/PKG-INFO +8 -0
- oliviadb_agent_framework-0.1.0/src/oliviadb_agent_framework.egg-info/SOURCES.txt +8 -0
- oliviadb_agent_framework-0.1.0/src/oliviadb_agent_framework.egg-info/dependency_links.txt +1 -0
- oliviadb_agent_framework-0.1.0/src/oliviadb_agent_framework.egg-info/requires.txt +2 -0
- oliviadb_agent_framework-0.1.0/src/oliviadb_agent_framework.egg-info/top_level.txt +1 -0
- oliviadb_agent_framework-0.1.0/tests/test_agent_framework.py +61 -0
|
@@ -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,16 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
[build-system]
|
|
3
|
+
requires = ["setuptools>=68"]
|
|
4
|
+
build-backend = "setuptools.build_meta"
|
|
5
|
+
|
|
6
|
+
[project]
|
|
7
|
+
name = "oliviadb-agent-framework"
|
|
8
|
+
version = "0.1.0"
|
|
9
|
+
description = "Microsoft Agent Framework history and memory providers backed by OliviaDB"
|
|
10
|
+
license = "Apache-2.0"
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
# Built and tested against agent-framework-core 1.26.0.
|
|
13
|
+
dependencies = ["oliviadb>=0.1.0", "agent-framework-core>=1.26,<2"]
|
|
14
|
+
|
|
15
|
+
[tool.setuptools.packages.find]
|
|
16
|
+
where = ["src"]
|
|
@@ -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,8 @@
|
|
|
1
|
+
pyproject.toml
|
|
2
|
+
src/oliviadb_agent_framework/__init__.py
|
|
3
|
+
src/oliviadb_agent_framework.egg-info/PKG-INFO
|
|
4
|
+
src/oliviadb_agent_framework.egg-info/SOURCES.txt
|
|
5
|
+
src/oliviadb_agent_framework.egg-info/dependency_links.txt
|
|
6
|
+
src/oliviadb_agent_framework.egg-info/requires.txt
|
|
7
|
+
src/oliviadb_agent_framework.egg-info/top_level.txt
|
|
8
|
+
tests/test_agent_framework.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
oliviadb_agent_framework
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""Against a real oliviadb-server (OLIVIADB_SERVER_URL, OLIVIADB_API_KEY): history kept per session and
|
|
3
|
+
reloaded by a new agent object, and long-term memory recalled into a later run."""
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import os
|
|
7
|
+
import sys
|
|
8
|
+
import unittest
|
|
9
|
+
import uuid
|
|
10
|
+
|
|
11
|
+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
|
|
12
|
+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "examples"))
|
|
13
|
+
URL = os.environ.get("OLIVIADB_SERVER_URL")
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def client():
|
|
17
|
+
from oliviadb import OliviaDbClient
|
|
18
|
+
|
|
19
|
+
return OliviaDbClient(URL, api_key=os.environ["OLIVIADB_API_KEY"])
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@unittest.skipUnless(URL, "OLIVIADB_SERVER_URL not set")
|
|
23
|
+
class Providers(unittest.TestCase):
|
|
24
|
+
def test_history_provider_round_trip(self):
|
|
25
|
+
from agent_framework import Message
|
|
26
|
+
from oliviadb_agent_framework import OliviaDBHistoryProvider
|
|
27
|
+
|
|
28
|
+
p = OliviaDBHistoryProvider(client(), root=("maf-test", uuid.uuid4().hex))
|
|
29
|
+
|
|
30
|
+
async def go():
|
|
31
|
+
await p.save_messages("s1", [Message("user", ["hi"]), Message("assistant", ["hello"])])
|
|
32
|
+
await p.save_messages("s1", [Message("user", ["bye"])])
|
|
33
|
+
return await p.get_messages("s1"), await p.get_messages("s2")
|
|
34
|
+
|
|
35
|
+
a, b = asyncio.run(go())
|
|
36
|
+
self.assertEqual([(m.role, m.text) for m in a], [("user", "hi"), ("assistant", "hello"), ("user", "bye")])
|
|
37
|
+
self.assertEqual(b, [])
|
|
38
|
+
|
|
39
|
+
def test_agent_history_and_memory(self):
|
|
40
|
+
from helpdesk import agent
|
|
41
|
+
|
|
42
|
+
root = ("maf-ex", uuid.uuid4().hex)
|
|
43
|
+
thread = "t-" + uuid.uuid4().hex
|
|
44
|
+
|
|
45
|
+
async def go():
|
|
46
|
+
a = agent(client(), root, thread)
|
|
47
|
+
s = a.create_session(session_id="ticket-1")
|
|
48
|
+
r1 = (await a.run("My printer model is LX-200.", session=s)).text
|
|
49
|
+
# A new agent object (a restarted process) resumes the session by id.
|
|
50
|
+
a2 = agent(client(), root, thread)
|
|
51
|
+
r2 = (await a2.run("Which printer do I have?", session=a2.create_session(session_id="ticket-1"))).text
|
|
52
|
+
return r1, r2
|
|
53
|
+
|
|
54
|
+
r1, r2 = asyncio.run(go())
|
|
55
|
+
self.assertTrue(r1.startswith("1 messages"), r1)
|
|
56
|
+
self.assertTrue(r2.startswith("3 messages"), r2) # the earlier exchange came from OliviaDB
|
|
57
|
+
self.assertIn("LX-200", r2) # and long-term memory recalled it into the instructions
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
if __name__ == "__main__":
|
|
61
|
+
unittest.main()
|