Skip to main content

No project description provided

Project description

Anthropic extensions

Get Pocket Anthropic Tool Spec

from hyperpocket_anthropic import PocketAnthropic

pocket = PocketAnthropic(tools=[
    "https://github.com/my-org/some-awesome-tool",
    "https://github.com/vessl-ai/hyperpocket/tree/main/tools/slack/get-message",
    "https://github.com/vessl-ai/hyperpocket/tree/main/tools/slack/post-message",
]
)

# get anthropic compatible tool specs from pocket 
tool_specs = pocket.get_anthropic_tool_specs()

Tool Injection

import os

from anthropic import Anthropic

messages = [
    {
        "role": "user",
        "content": "get slack message",
    }
]

llm = Anthropic()
response = llm.messages.create(
    model=os.getenv("ANTHROPIC_MODEL_NAME"),
    max_tokens=500,
    messages=messages,
    tools=tool_specs,  # pass the tool specs as an tools argument
)

Tool Call

response = llm.messages.create(
    model=os.getenv("ANTHROPIC_MODEL_NAME"),
    max_tokens=500,
    messages=messages,
    tools=tool_specs,  # pass the tool specs as an tools argument
)

tool_result_blocks = []
for block in response.content:
    if block.type == "tool_use":
        tool_result_block = pocket.invoke(block)
        tool_result_blocks.append(tool_result_block)

messages.append({"role": "user", "content": tool_result_blocks})

response_after_tool_call = llm.messages.create(
    model=os.getenv("ANTHROPIC_MODEL_NAME"),
    max_tokens=500,
    messages=messages,
    tools=tool_specs,  # pass the tool specs as an tools argument
)

# ...

Full Code

import os

from anthropic import Anthropic

from hyperpocket_anthropic import PocketAnthropic

pocket = PocketAnthropic(tools=[
    "https://github.com/my-org/some-awesome-tool",
    "https://github.com/vessl-ai/hyperpocket/tree/main/tools/slack/get-message",
    "https://github.com/vessl-ai/hyperpocket/tree/main/tools/slack/post-message",
]
)

# get anthropic compatible tool specs from pocket 
tool_specs = pocket.get_anthropic_tool_specs()
messages = [
    {
        "role": "user",
        "content": "get slack message",
    }
]

llm = Anthropic()
response = llm.messages.create(
    model=os.getenv("ANTHROPIC_MODEL_NAME"),
    max_tokens=500,
    messages=messages,
    tools=tool_specs,  # pass the tool specs as an tools argument
)

tool_result_blocks = []
for block in response.content:
    if block.type == "tool_use":
        tool_result_block = pocket.invoke(block)  # tool call by pocket
        tool_result_blocks.append(tool_result_block)

messages.append({"role": "user", "content": tool_result_blocks})

response_after_tool_call = llm.messages.create(
    model=os.getenv("ANTHROPIC_MODEL_NAME"),
    max_tokens=500,
    messages=messages,
    tools=tool_specs,  # pass the tool specs as an tools argument
)

Examples

import os

from anthropic import Anthropic

from hyperpocket_anthropic import PocketAnthropic

client = Anthropic()
pocket = PocketAnthropic(tools=[
    "https://github.com/my-org/some-awesome-tool",
    "https://github.com/vessl-ai/hyperpocket/tree/main/tools/slack/get-message",
    "https://github.com/vessl-ai/hyperpocket/tree/main/tools/slack/post-message",
]
)

tool_specs = pocket.get_anthropic_tool_specs()

messages = []
while True:
    print("user(q to quit) : ", end="")
    user_input = input()
    if user_input == "q":
        break
    if user_input == "":
        continue

    messages.append({"role": "user", "content": user_input})

    while True:
        response = client.messages.create(
            model=os.getenv("ANTHROPIC_MODEL_NAME"),
            max_tokens=500,
            messages=messages,
            tools=tool_specs,
        )

        messages.append({"role": "assistant", "content": response.content})

        tool_result_blocks = []
        for block in response.content:
            if block.type == "text":
                print("[ai] response : ", block.text)

            elif block.type == "tool_use":
                tool_result_block = pocket.invoke(block)
                tool_result_blocks.append(tool_result_block)

                print("[tool] response : ", tool_result_block["content"])

        messages.append({"role": "user", "content": tool_result_blocks})

        if response.stop_reason != "tool_use":
            break

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hyperpocket_anthropic-0.4.0.tar.gz (59.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hyperpocket_anthropic-0.4.0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file hyperpocket_anthropic-0.4.0.tar.gz.

File metadata

File hashes

Hashes for hyperpocket_anthropic-0.4.0.tar.gz
Algorithm Hash digest
SHA256 097b83f2966ad5c36fcb775327674d103e8b50acd217c2b8eace8095d11094fb
MD5 fd33522abde00a7de7086fcc35080a77
BLAKE2b-256 30f6dcc24d4e3d64b7e4f79a0104976fb8763d57d1794c0a3319969f3d97e2c4

See more details on using hashes here.

File details

Details for the file hyperpocket_anthropic-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for hyperpocket_anthropic-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5dc9eebd1c8f5da48a3e8765ed32001c29cdc05b8d0f09256428177d82fd0037
MD5 948f00476f4934f1ac51c3924c6e4c56
BLAKE2b-256 3ca946f7e3dd4b0581a139efab33cf0514f92af7db36dbfd4c24c35bb40f151e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page