|
15 | 15 | """ |
16 | 16 | Anthropic structured output + tool use example for CAMEL. |
17 | 17 |
|
18 | | -This single example focuses on the most important combined scenario: |
19 | | -Anthropic tool calling and ``response_format`` in the same request. |
20 | | -
|
21 | 18 | Required environment variable: |
22 | 19 | export ANTHROPIC_API_KEY="<your-anthropic-api-key>" |
23 | 20 |
|
24 | 21 | Optional environment variables: |
25 | 22 | export ANTHROPIC_API_BASE_URL="<your-anthropic-compatible-base-url>" |
26 | | - export ANTHROPIC_USE_BETA_STRUCTURED_OUTPUTS="true" |
| 23 | + export ANTHROPIC_MODEL="<your-anthropic-model>" |
27 | 24 |
|
28 | 25 | Run: |
29 | 26 | python3 examples/models/anthropic_structured_output_example.py |
|
35 | 32 | from pydantic import BaseModel, Field |
36 | 33 |
|
37 | 34 | from camel.agents import ChatAgent |
38 | | -from camel.configs import AnthropicConfig |
39 | 35 | from camel.models import AnthropicModel |
| 36 | +from camel.types import ModelType |
| 37 | +from camel.utils import OpenAITokenCounter |
40 | 38 |
|
41 | 39 |
|
42 | 40 | class TripDecision(BaseModel): |
@@ -124,22 +122,18 @@ def build_anthropic_model() -> AnthropicModel: |
124 | 122 | ) |
125 | 123 |
|
126 | 124 | base_url = os.environ.get("ANTHROPIC_API_BASE_URL") |
127 | | - use_beta_for_structured_outputs = ( |
128 | | - os.environ.get("ANTHROPIC_USE_BETA_STRUCTURED_OUTPUTS", "") |
129 | | - .strip() |
130 | | - .lower() |
131 | | - == "true" |
| 125 | + model_type = os.environ.get("ANTHROPIC_MODEL") or ( |
| 126 | + "anthropic/claude-sonnet-4.5" |
132 | 127 | ) |
133 | 128 |
|
134 | 129 | return AnthropicModel( |
135 | | - model_type="anthropic/claude-sonnet-4.5", |
| 130 | + model_type=model_type, |
136 | 131 | api_key=api_key, |
137 | 132 | url=base_url, |
138 | | - use_beta_for_structured_outputs=use_beta_for_structured_outputs, |
139 | | - model_config_dict=AnthropicConfig( |
140 | | - temperature=0.0, |
141 | | - max_tokens=800, |
142 | | - ).as_dict(), |
| 133 | + # Third-party Anthropic-compatible platforms may require a local |
| 134 | + # fallback token counter instead of Anthropic's count_tokens API. |
| 135 | + token_counter=OpenAITokenCounter(ModelType.GPT_4O_MINI), |
| 136 | + model_config_dict={"max_tokens": 800}, |
143 | 137 | ) |
144 | 138 |
|
145 | 139 |
|
@@ -187,3 +181,30 @@ def main() -> None: |
187 | 181 |
|
188 | 182 | if __name__ == "__main__": |
189 | 183 | main() |
| 184 | + |
| 185 | +""" |
| 186 | +=============================================================================== |
| 187 | +=== Raw Content === |
| 188 | +{"recommended_area":"Higashiyama","estimated_total_budget_rmb":1100,"must_visit_spot":"Kiyomizu-dera","transport_tip":"Take a bus or taxi early in the morning to avoid crowds and save time-consider a Kyoto Bus One-Day Pass for unlimited rides."} |
| 189 | +
|
| 190 | +=== Parsed Object === |
| 191 | +recommended_area='Higashiyama' estimated_total_budget_rmb=1100 |
| 192 | +must_visit_spot='Kiyomizu-dera' transport_tip='Take a bus or taxi early in |
| 193 | +the morning to avoid crowds and save time-consider a Kyoto Bus One-Day Pass |
| 194 | +for unlimited rides.' |
| 195 | +
|
| 196 | +=== Parsed JSON === |
| 197 | +{ |
| 198 | + "recommended_area": "Higashiyama", |
| 199 | + "estimated_total_budget_rmb": 1100, |
| 200 | + "must_visit_spot": "Kiyomizu-dera", |
| 201 | + "transport_tip": "Take a bus or taxi early in the morning to avoid crowds and save time-consider a Kyoto Bus One-Day Pass for unlimited rides." |
| 202 | +} |
| 203 | +
|
| 204 | +=== Tool Calls === |
| 205 | +- lookup_kyoto_area args={'area': 'Higashiyama'} result={'must_visit_spot': |
| 206 | +'Kiyomizu-dera', 'transport_tip': 'Take a bus or taxi early in the morning.'} |
| 207 | +- estimate_kyoto_budget args={'days': 2, 'hotel_tier': 'budget'} |
| 208 | +result={'total_budget_rmb': 1100} |
| 209 | +=============================================================================== |
| 210 | +""" |
0 commit comments