Created
March 19, 2025 11:02
-
-
Save Nunocky/80d7231251e6c8343e62f915f2f7c74f to your computer and use it in GitHub Desktop.
Claude tool use
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import sys | |
from itertools import count | |
import anthropic | |
from tool_text_revert import ( | |
TOOL_TEXT_REVERT_NAME, | |
process_tool_text_revert, | |
tool_text_revert, | |
) | |
from tool_word_count import ( | |
TOOL_WORD_COUNT_NAME, | |
process_tool_word_count, | |
tool_word_count, | |
) | |
client = anthropic.Anthropic() | |
tools = [tool_word_count, tool_text_revert] | |
# user_input = "Hello World!" | |
user_input = "こんにちは世界!" # 一般的な日本語の挨拶 | |
messages = [] | |
messages.append({"role": "user", "content": f"{user_input}"}) | |
print("初期メッセージ送信...", file=sys.stderr) | |
response = client.messages.create( | |
model="claude-3-opus-20240229", # より高性能なOpusモデルに変更 | |
max_tokens=1000, | |
temperature=0.0, | |
system="""You MUST follow these exact steps in order: | |
1. Use the word_count tool on the input text to count the number of characters. | |
2. After receiving the character count result, you MUST use the text_revert tool on the SAME input text. | |
3. Do not respond with any text or complete your response until you have used BOTH tools. | |
This is critical: you MUST use both tools, first word_count then text_revert, on the same input.""", | |
messages=messages, | |
tools=tools, | |
) | |
messages.append({"role": "assistant", "content": response.content}) | |
stop_reason = response.stop_reason | |
print(f"応答: {response}", file=sys.stderr) | |
print(f"停止理由: {stop_reason}", file=sys.stderr) | |
# 結果を保存する変数 | |
character_count = None | |
reverted_text = None | |
# ツール使用ループ | |
loop_count = 0 | |
while stop_reason == "tool_use" and loop_count < 5: # 無限ループ防止 | |
loop_count += 1 | |
print(f"ループ {loop_count}", file=sys.stderr) | |
tools_used_this_round = [] | |
# 各ツール使用を処理 | |
for tool_use_block in response.content: | |
if tool_use_block.type != "tool_use": | |
continue | |
tools_used_this_round.append(tool_use_block.name) | |
if tool_use_block.name == TOOL_WORD_COUNT_NAME: | |
print("ツール word_count の実行", file=sys.stderr) | |
text = tool_use_block.input["text"] | |
count = process_tool_word_count(text) | |
character_count = count | |
# ツールの結果を追加 | |
tool_result = { | |
"role": "user", | |
"content": [ | |
{ | |
"type": "tool_result", | |
"tool_use_id": tool_use_block.id, | |
"content": str(count), | |
} | |
], | |
} | |
messages.append(tool_result) | |
print(f"word_count 結果: {count}", file=sys.stderr) | |
elif tool_use_block.name == TOOL_TEXT_REVERT_NAME: | |
print("ツール text_revert の実行", file=sys.stderr) | |
text = tool_use_block.input["text"] | |
reverted_text = process_tool_text_revert(text) | |
# ツールの結果を追加 | |
tool_result = { | |
"role": "user", | |
"content": [ | |
{ | |
"type": "tool_result", | |
"tool_use_id": tool_use_block.id, | |
"content": reverted_text, | |
} | |
], | |
} | |
messages.append(tool_result) | |
print(f"text_revert 結果: {reverted_text}", file=sys.stderr) | |
print(f"このラウンドで使用されたツール: {tools_used_this_round}", file=sys.stderr) | |
# 次の応答を取得 (もしまだツールを使用する必要があれば) | |
print("次の応答を取得...", file=sys.stderr) | |
response = client.messages.create( | |
model="claude-3-opus-20240229", # より高性能なOpusモデルに変更 | |
max_tokens=1000, | |
temperature=0.0, | |
messages=messages, | |
tools=tools, # ツールを引き続き提供 | |
) | |
messages.append({"role": "assistant", "content": response.content}) | |
stop_reason = response.stop_reason | |
print(f"応答: {response}", file=sys.stderr) | |
print(f"停止理由: {stop_reason}", file=sys.stderr) | |
# すべてのツール使用が完了したら、最終的な結果を表示 | |
print("\n両方のツールが使用されました。最終結果:", file=sys.stderr) | |
print(f"文字数: {character_count}", file=sys.stderr) | |
print(f"反転テキスト: {reverted_text}", file=sys.stderr) | |
# 結果をJSON形式で表示するためのプロンプト | |
if character_count is not None and reverted_text is not None: | |
# 直接JSONオブジェクトを作成して出力 (これはstdoutに出力) | |
import json | |
json_result = json.dumps( | |
{"character_count": character_count, "reverted_text": reverted_text}, indent=2 | |
) | |
# JSONだけはstdoutに出力 | |
print(json_result) | |
else: | |
print( | |
"エラー: 一部のツールが使用されなかったため、JSON出力を生成できませんでした。", | |
file=sys.stderr, | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
TOOL_TEXT_REVERT_NAME = "text_revert" | |
TOOL_TEXT_REVERT_DESCRIPTION = "Revert the text." | |
def process_tool_text_revert(text: str) -> str: | |
return text[::-1] | |
tool_text_revert = { | |
"name": TOOL_TEXT_REVERT_NAME, | |
"description": TOOL_TEXT_REVERT_DESCRIPTION, | |
"input_schema": { | |
"type": "object", | |
"properties": { | |
"text": {"type": "string", "description": "text to revert"}, | |
}, | |
"required": ["text"], | |
}, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
TOOL_WORD_COUNT_NAME = "word_count" | |
TOOL_WORD_COUNT_DESCRIPTION = "Count the number of words in a text." | |
def process_tool_word_count(text: str) -> int: | |
return len(text) | |
tool_word_count = { | |
"name": TOOL_WORD_COUNT_NAME, | |
"description": TOOL_WORD_COUNT_DESCRIPTION, | |
"input_schema": { | |
"type": "object", | |
"properties": { | |
"text": {"type": "string", "description": "text to count words in"}, | |
}, | |
"required": ["text"], | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment