Skip to content

Commit b1692d7

Browse files
committed
format fix
1 parent 72195ef commit b1692d7

File tree

2 files changed

+181
-142
lines changed

2 files changed

+181
-142
lines changed

examples/models/claude_example.py

Lines changed: 0 additions & 142 deletions
This file was deleted.
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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+
import os
16+
17+
from camel.agents import ChatAgent
18+
from camel.configs import AnthropicConfig
19+
from camel.models import ModelFactory
20+
from camel.toolkits import FunctionTool, TerminalToolkit
21+
from camel.types import ModelPlatformType, ModelType
22+
23+
"""
24+
Claude Model Example
25+
26+
Please set the environment variable:
27+
export ANTHROPIC_API_KEY="your-api-key-here"
28+
"""
29+
30+
# Define system message
31+
sys_msg = "You are a helpful AI assistant"
32+
33+
# Get current script directory
34+
base_dir = os.path.dirname(os.path.abspath(__file__))
35+
# Define workspace directory for the toolkit
36+
workspace_dir = os.path.join(
37+
os.path.dirname(os.path.dirname(base_dir)), "workspace"
38+
)
39+
40+
# Set model config
41+
tools = [
42+
*TerminalToolkit(working_directory=workspace_dir).get_tools(),
43+
]
44+
# Create Claude Opus 4.5 model
45+
model_opus_4_5 = ModelFactory.create(
46+
model_platform=ModelPlatformType.ANTHROPIC,
47+
model_type=ModelType.CLAUDE_OPUS_4_5,
48+
model_config_dict=AnthropicConfig(temperature=0.2).as_dict(),
49+
)
50+
51+
user_msg = """
52+
Create an interactive HTML webpage that allows users to play with a
53+
Rubik's Cube, and saved it to local file.
54+
"""
55+
56+
camel_agent_pro = ChatAgent(
57+
system_message=sys_msg, model=model_opus_4_5, tools=tools
58+
)
59+
response_pro = camel_agent_pro.step(user_msg)
60+
print(response_pro.msgs[0].content)
61+
'''
62+
===============================================================================
63+
The interactive Rubik's Cube HTML file has been created successfully! Here's
64+
what I built:
65+
66+
## 📁 File: `rubiks_cube.html` (23KB)
67+
68+
### Features:
69+
70+
🎮 **Interactive 3D Cube**
71+
- Fully rendered 3D Rubik's Cube using CSS transforms
72+
- Drag to rotate the view (mouse or touch)
73+
- All 6 faces visible with proper colors
74+
75+
🔄 **Face Rotations**
76+
- **F/B** - Front/Back face
77+
- **U/D** - Up/Down face
78+
- **L/R** - Left/Right face
79+
- **'** versions for counter-clockwise rotations
80+
81+
⚡ **Actions**
82+
- **Scramble** - Randomly mix the cube with 20 moves
83+
- **Reset** - Return to solved state
84+
- **Undo** - Reverse the last move
85+
86+
📐 **Net View**
87+
- 2D unfolded view of all cube faces for easier tracking
88+
89+
⌨️ **Keyboard Support**
90+
- Press F, B, R, L, U, D keys to rotate faces
91+
- Hold Shift for counter-clockwise
92+
93+
📱 **Responsive Design**
94+
- Works on desktop and mobile devices
95+
- Touch support for rotating the view
96+
97+
### To use:
98+
Simply open `rubiks_cube.html` in any modern web browser!
99+
100+
Process finished with exit code 0
101+
===============================================================================
102+
'''
103+
104+
# Create Claude Sonnet 4.5 model
105+
model = ModelFactory.create(
106+
model_platform=ModelPlatformType.ANTHROPIC,
107+
model_type=ModelType.CLAUDE_SONNET_4_5,
108+
model_config_dict=AnthropicConfig(temperature=0.2).as_dict(),
109+
)
110+
111+
# Set agent
112+
camel_agent = ChatAgent(system_message=sys_msg, model=model)
113+
114+
print(f"Model Type: {model.model_type}")
115+
print(f"Model Platform: {ModelPlatformType.ANTHROPIC}")
116+
print(f"Is Anthropic: {model.model_type.is_anthropic}")
117+
print(f"Token Limit: {model.model_type.token_limit}")
118+
119+
# Test basic conversation
120+
user_msg = """Hello Claude Sonnet 4.5! Can you tell me about your \
121+
capabilities and how you differ from previous versions?"""
122+
123+
print(f"\nUser: {user_msg}")
124+
print("Assistant:", end=" ")
125+
126+
try:
127+
response = camel_agent.step(user_msg)
128+
print(response.msgs[0].content)
129+
print("\n✅ Basic conversation test PASSED")
130+
except Exception as e:
131+
print(f"\n❌ Basic conversation test FAILED: {e}")
132+
133+
134+
# Test with tool calling
135+
def calculate_circle_area(radius: float) -> float:
136+
"""Calculate the area of a circle given its radius."""
137+
import math
138+
139+
return math.pi * radius * radius
140+
141+
142+
# Create agent with tools
143+
tool_agent = ChatAgent(
144+
system_message=sys_msg,
145+
model=model,
146+
tools=[FunctionTool(calculate_circle_area)],
147+
)
148+
149+
print("\n" + "=" * 50)
150+
print("Testing Claude Sonnet 4.5 with tool calling:")
151+
152+
user_msg = (
153+
"Please use the calculate_circle_area tool to find the area "
154+
"of a circle with radius 5."
155+
)
156+
157+
print(f"\nUser: {user_msg}")
158+
print("Assistant:", end=" ")
159+
160+
try:
161+
response = tool_agent.step(user_msg)
162+
print(response.msgs[0].content)
163+
164+
# Check if tool was called
165+
if response.info and response.info.get("tool_calls"):
166+
print("\n✅ Tool calling test PASSED")
167+
print(f"Tool calls: {response.info['tool_calls']}")
168+
else:
169+
print(
170+
"\n⚠️ Tool calling may not have been used (expected for this test)"
171+
)
172+
173+
except Exception as e:
174+
print(f"\n❌ Tool calling test FAILED: {e}")
175+
176+
print("\n" + "=" * 50)
177+
print("Claude Sonnet 4.5 integration test completed!")
178+
print(
179+
"If you see this message without errors, "
180+
"the integration is working correctly."
181+
)

0 commit comments

Comments
 (0)