Skip to main content

No project description provided

Project description

Anthropic extensions

Get Pocket Anthropic Tool Spec

import hyperpocket as pk
from pocket_anthropic import PocketAnthropic

pocket = PocketAnthropic(tools=[
    *pk.curated_tools.SLACK,  # SLACK = [slack_get_message, slack_post_message, ..]
    *pk.curated_tools.LINEAR,
    "https://github.com/my-org/some-awesome-tool"]
)

# 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

import hyperpocket as pk
from pocket_anthropic import PocketAnthropic

pocket = PocketAnthropic(tools=[
    *pk.curated_tools.SLACK,  # SLACK = [slack_get_message, slack_post_message, ..]
    *pk.curated_tools.LINEAR,
    "https://github.com/my-org/some-awesome-tool"]
)

# 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

import hyperpocket as pk
from pocket_anthropic import PocketAnthropic

client = Anthropic()
pocket = PocketAnthropic(
    tools=[
        *pk.curated_tools.SLACK,  # SLACK = [slack_get_message, slack_post_message, ..]
        *pk.curated_tools.LINEAR,
        "https://github.com/my-org/some-awesome-tool"]
)

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.1.7.tar.gz (2.6 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.1.7-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: hyperpocket_anthropic-0.1.7.tar.gz
  • Upload date:
  • Size: 2.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.11.10 Darwin/24.2.0

File hashes

Hashes for hyperpocket_anthropic-0.1.7.tar.gz
Algorithm Hash digest
SHA256 9411130cd6428fe11ca7aa33cb72a1bc4a5fe50c8fbdd68d330373aee3d66f64
MD5 dc57f5befa5faa9f2f8f5c1d4501bf6c
BLAKE2b-256 b52fce8998ac4289396f7eee3c1396123ba61f0a3a80a3ca33070751920446b6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for hyperpocket_anthropic-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 3326ea2d010170190419bb214c63818956faf8f3e0626ed3feb960f0a0fb695b
MD5 93f349211018047da570f040a5dc59f7
BLAKE2b-256 d748e25e16bd68b84bfab345468927898f8610d1c54f6032457f08f4c6cbd789

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