Skip to content

Commit 49ea186

Browse files
committed
feat(integration): support claude
1 parent 1cdd628 commit 49ea186

File tree

5 files changed

+114
-1
lines changed

5 files changed

+114
-1
lines changed

examples/integration/claude/main.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import asyncio
2+
3+
import anthropic
4+
5+
from npiai import FunctionTool, function
6+
from npiai.utils.test_utils import DebugContext
7+
from npiai.integration.claude import get_claude_tools
8+
9+
10+
class MyTool(FunctionTool):
11+
name = "Fibonacci"
12+
description = "My first NPi tool"
13+
14+
def __init__(self):
15+
super().__init__()
16+
17+
@function
18+
def fibonacci(self, n: int) -> int:
19+
"""
20+
Get the nth Fibonacci number.
21+
22+
Args:
23+
n: The index of the Fibonacci number in the sequence.
24+
"""
25+
if n == 0:
26+
return 0
27+
if n == 1:
28+
return 1
29+
return self.fibonacci(n - 1) + self.fibonacci(n - 2)
30+
31+
32+
async def main():
33+
async with MyTool() as tool:
34+
ctx = DebugContext()
35+
client = anthropic.Anthropic()
36+
messages = [
37+
{
38+
"role": "user",
39+
"content": "What's the 10-th fibonacci number?",
40+
}
41+
]
42+
43+
response = client.messages.create(
44+
model="claude-3-5-sonnet-20241022",
45+
max_tokens=1024,
46+
messages=messages,
47+
tools=get_claude_tools(tool),
48+
)
49+
50+
for content in response.content:
51+
match content.type:
52+
case "text":
53+
print(content.text)
54+
case "tool_use":
55+
result = await tool.exec(ctx, content.name, content.input)
56+
print(
57+
f"Calling {content.name} with {content.input} returned {result}"
58+
)
59+
60+
61+
if __name__ == "__main__":
62+
asyncio.run(main())

npiai/integration/claude/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .get_claude_tools import get_claude_tools
2+
3+
__all__ = ["get_claude_tools"]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from npiai.core import BaseTool
2+
from npiai.utils import sanitize_schema
3+
4+
__EMPTY_INPUT_SCHEMA__ = {"type": "object", "properties": {}, "required": []}
5+
6+
7+
def get_claude_tools(tool: BaseTool):
8+
tools = []
9+
10+
for fn_reg in tool.unpack_functions():
11+
tool = {
12+
"name": fn_reg.name,
13+
"description": fn_reg.description,
14+
"input_schema": (
15+
sanitize_schema(fn_reg.model, strict=False)
16+
if fn_reg.model
17+
else __EMPTY_INPUT_SCHEMA__
18+
),
19+
}
20+
21+
tools.append(tool)
22+
23+
return tools

poetry.lock

Lines changed: 25 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pytest-asyncio = "^0.24.0"
5454
pytest-dotenv = "^0.5.2"
5555
pytest-repeat = "^0.9.3"
5656
csv-diff = "^1.2"
57+
anthropic = "^0.42.0"
5758

5859
[build-system]
5960
requires = ["poetry-core"]

0 commit comments

Comments
 (0)