|
| 1 | +# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. ========= |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. ========= |
| 14 | + |
| 15 | +from camel.agents import ChatAgent |
| 16 | +from camel.configs import AnthropicConfig |
| 17 | +from camel.models import ModelFactory |
| 18 | +from camel.toolkits import FunctionTool |
| 19 | +from camel.types import ModelPlatformType, ModelType |
| 20 | + |
| 21 | +""" |
| 22 | +Claude Sonnet 4.5 Example |
| 23 | +
|
| 24 | +Please set the environment variable: |
| 25 | +export ANTHROPIC_API_KEY="your-api-key-here" |
| 26 | +""" |
| 27 | + |
| 28 | +print("Testing Claude Sonnet 4.5 Integration...") |
| 29 | + |
| 30 | +# Create Claude Sonnet 4.5 model |
| 31 | +model = ModelFactory.create( |
| 32 | + model_platform=ModelPlatformType.ANTHROPIC, |
| 33 | + model_type=ModelType.CLAUDE_4_5_SONNET, |
| 34 | + model_config_dict=AnthropicConfig(temperature=0.2).as_dict(), |
| 35 | +) |
| 36 | + |
| 37 | +# Define system message |
| 38 | +sys_msg = "You are a helpful AI assistant powered by Claude Sonnet 4.5." |
| 39 | + |
| 40 | +# Set agent |
| 41 | +camel_agent = ChatAgent(system_message=sys_msg, model=model) |
| 42 | + |
| 43 | +print(f"Model Type: {model.model_type}") |
| 44 | +print(f"Model Platform: {ModelPlatformType.ANTHROPIC}") |
| 45 | +print(f"Is Anthropic: {model.model_type.is_anthropic}") |
| 46 | +print(f"Token Limit: {model.model_type.token_limit}") |
| 47 | + |
| 48 | +# Test basic conversation |
| 49 | +user_msg = """Hello Claude Sonnet 4.5! Can you tell me about your \ |
| 50 | +capabilities and how you differ from previous versions?""" |
| 51 | + |
| 52 | +print(f"\nUser: {user_msg}") |
| 53 | +print("Assistant:", end=" ") |
| 54 | + |
| 55 | +try: |
| 56 | + response = camel_agent.step(user_msg) |
| 57 | + print(response.msgs[0].content) |
| 58 | + print("\n✅ Basic conversation test PASSED") |
| 59 | +except Exception as e: |
| 60 | + print(f"\n❌ Basic conversation test FAILED: {e}") |
| 61 | + |
| 62 | + |
| 63 | +# Test with tool calling |
| 64 | +def calculate_circle_area(radius: float) -> float: |
| 65 | + """Calculate the area of a circle given its radius.""" |
| 66 | + import math |
| 67 | + |
| 68 | + return math.pi * radius * radius |
| 69 | + |
| 70 | + |
| 71 | +# Create agent with tools |
| 72 | +tool_agent = ChatAgent( |
| 73 | + system_message=sys_msg, |
| 74 | + model=model, |
| 75 | + tools=[FunctionTool(calculate_circle_area)], |
| 76 | +) |
| 77 | + |
| 78 | +print("\n" + "=" * 50) |
| 79 | +print("Testing Claude Sonnet 4.5 with tool calling:") |
| 80 | + |
| 81 | +user_msg = ( |
| 82 | + "Please use the calculate_circle_area tool to find the area " |
| 83 | + "of a circle with radius 5." |
| 84 | +) |
| 85 | + |
| 86 | +print(f"\nUser: {user_msg}") |
| 87 | +print("Assistant:", end=" ") |
| 88 | + |
| 89 | +try: |
| 90 | + response = tool_agent.step(user_msg) |
| 91 | + print(response.msgs[0].content) |
| 92 | + |
| 93 | + # Check if tool was called |
| 94 | + if response.info and response.info.get("tool_calls"): |
| 95 | + print("\n✅ Tool calling test PASSED") |
| 96 | + print(f"Tool calls: {response.info['tool_calls']}") |
| 97 | + else: |
| 98 | + print( |
| 99 | + "\n⚠️ Tool calling may not have been used (expected for this test)" |
| 100 | + ) |
| 101 | + |
| 102 | +except Exception as e: |
| 103 | + print(f"\n❌ Tool calling test FAILED: {e}") |
| 104 | + |
| 105 | +print("\n" + "=" * 50) |
| 106 | +print("Claude Sonnet 4.5 integration test completed!") |
| 107 | +print( |
| 108 | + "If you see this message without errors, " |
| 109 | + "the integration is working correctly." |
| 110 | +) |
0 commit comments