Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(context): introduce memory layer #95

Merged
merged 10 commits into from
Jul 31, 2024
10 changes: 5 additions & 5 deletions examples/agent/calendar_negotiator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ def load_google_credentials(secret_file: str, scopes: List[str], token_file: str


class Negotiator(FunctionTool):
name = "calendar_negotiator"
description = "Schedule meetings with others using gmail and google calendar"
system_prompt = PROMPT

def __init__(self, secret_file: str, token_file: str = ""):
super().__init__(
name="calendar_negotiator",
description="Schedule meetings with others using gmail and google calendar",
system_prompt=PROMPT,
)
super().__init__()
if token_file == "":
token_file = secret_file.replace("secret", "token")

Expand Down
10 changes: 5 additions & 5 deletions examples/agent/twitter_discord_crawler/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@


class TwitterDiscordCrawler(FunctionTool):
name = "twitter_crawler"
description = "Retrieve data from Twitter and send messages to Discord"
system_prompt = PROMPT

def __init__(self):
super().__init__(
name="twitter_crawler",
description="Retrieve data from Twitter and send messages to Discord",
system_prompt=PROMPT,
)
super().__init__()

self.use_hitl(ConsoleHandler())

Expand Down
43 changes: 43 additions & 0 deletions examples/memory/memory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from typing import Annotated
from npiai import FunctionTool, function, FromContext, agent, Context
from npiai.hitl_handler import ConsoleHandler
import asyncio


class MyTool(FunctionTool):
name = "my_tool"
description = "My first NPi tool"

@function
def get_test_id(
self,
test_id: Annotated[int, FromContext(query="{user}'s test id")],
):
"""
Get test id

Args:
test_id: test id
"""
return test_id


async def main():
async with agent.wrap(MyTool()) as tool:
tool.use_hitl(ConsoleHandler())
ctx = Context()
ctx.bind(tool)
result = await tool.chat(ctx=ctx, instruction="What's the test id for @Alice?")
print(f"Result: {result}")

result = await tool.chat(ctx=ctx, instruction="What's the test id for @Bob?")
print(f"Result: {result}")

result = await tool.chat(
ctx=ctx, instruction="What are the test id's for @Alice and @Bob?"
)
print(f"Result: {result}")


if __name__ == "__main__":
asyncio.run(main())
8 changes: 4 additions & 4 deletions examples/quickstart/my_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@


class MyTool(FunctionTool):
name = "Fibonacci"
description = "My first NPi tool"

def __init__(self):
super().__init__(
name="Fibonacci",
description="My first NPi tool",
)
super().__init__()

@function
def fibonacci(self, n: int) -> int:
Expand Down
14 changes: 11 additions & 3 deletions npiai/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from .core.tool import FunctionTool, BrowserTool, function, AgentTool, BrowserAgentTool
from .core.hitl import HITL
from .llm import OpenAI, LLM
from npiai.core.tool import (
FunctionTool,
BrowserTool,
function,
AgentTool,
BrowserAgentTool,
)
from npiai.core.hitl import HITL
from npiai.llm import OpenAI, LLM
from npiai.core.base import Context
from npiai.types import FromContext

__all__ = [
"BrowserTool",
Expand All @@ -13,4 +20,5 @@
"OpenAI",
"LLM",
"Context",
"FromContext",
]
2 changes: 1 addition & 1 deletion npiai/agent/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from npiai.core.tool import wrap
from .wrap import wrap

__all__ = [
"wrap",
Expand Down
22 changes: 22 additions & 0 deletions npiai/agent/wrap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import overload
from npiai import FunctionTool, BrowserTool, AgentTool, BrowserAgentTool, LLM


@overload
def wrap(tool: FunctionTool, llm: LLM = None) -> AgentTool: ...


@overload
def wrap(tool: BrowserTool, llm: LLM = None) -> BrowserAgentTool: ...


def wrap(
tool: FunctionTool | BrowserTool, llm: LLM = None
) -> AgentTool | BrowserAgentTool:
if isinstance(tool, BrowserTool):
return BrowserAgentTool(tool, llm)

if isinstance(tool, FunctionTool):
return AgentTool(tool, llm)

raise TypeError(f"app must be an instance of FunctionTool or BrowserTool")
2 changes: 1 addition & 1 deletion npiai/cloud/_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(
self.tools = {}
self._endpoint = endpoint
for tool in tool_cls:
self.tools[tool.get_name()] = tool
self.tools[tool.name] = tool
self.port = port
self.app = FastAPI()
self.ctx_mgr = ContextManager()
Expand Down
1 change: 1 addition & 0 deletions npiai/constant/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CTX_QUERY_POSTFIX = "__ctx_query"
Loading