Compare the two responses to see if they're similar enough.
(anthropic_response, proxy_response, check_tools=False)
| 206 | return response |
| 207 | |
| 208 | def compare_responses(anthropic_response, proxy_response, check_tools=False): |
| 209 | """Compare the two responses to see if they're similar enough.""" |
| 210 | anthropic_json = anthropic_response.json() |
| 211 | proxy_json = proxy_response.json() |
| 212 | |
| 213 | print("\n--- Anthropic Response Structure ---") |
| 214 | print(json.dumps({k: v for k, v in anthropic_json.items() if k != "content"}, indent=2)) |
| 215 | |
| 216 | print("\n--- Proxy Response Structure ---") |
| 217 | print(json.dumps({k: v for k, v in proxy_json.items() if k != "content"}, indent=2)) |
| 218 | |
| 219 | # Basic structure verification with more flexibility |
| 220 | # The proxy might map values differently, so we're more lenient in our checks |
| 221 | assert proxy_json.get("role") == "assistant", "Proxy role is not 'assistant'" |
| 222 | assert proxy_json.get("type") == "message", "Proxy type is not 'message'" |
| 223 | |
| 224 | # Check if stop_reason is reasonable (might be different between Anthropic and our proxy) |
| 225 | valid_stop_reasons = ["end_turn", "max_tokens", "stop_sequence", "tool_use", None] |
| 226 | assert proxy_json.get("stop_reason") in valid_stop_reasons, "Invalid stop reason" |
| 227 | |
| 228 | # Check content exists and has valid structure |
| 229 | assert "content" in anthropic_json, "No content in Anthropic response" |
| 230 | assert "content" in proxy_json, "No content in Proxy response" |
| 231 | |
| 232 | anthropic_content = anthropic_json["content"] |
| 233 | proxy_content = proxy_json["content"] |
| 234 | |
| 235 | # Make sure content is a list and has at least one item |
| 236 | assert isinstance(anthropic_content, list), "Anthropic content is not a list" |
| 237 | assert isinstance(proxy_content, list), "Proxy content is not a list" |
| 238 | assert len(proxy_content) > 0, "Proxy content is empty" |
| 239 | |
| 240 | # If we're checking for tool uses |
| 241 | if check_tools: |
| 242 | # Check if content has tool use |
| 243 | anthropic_tool = None |
| 244 | proxy_tool = None |
| 245 | |
| 246 | # Find tool use in Anthropic response |
| 247 | for item in anthropic_content: |
| 248 | if item.get("type") == "tool_use": |
| 249 | anthropic_tool = item |
| 250 | break |
| 251 | |
| 252 | # Find tool use in Proxy response |
| 253 | for item in proxy_content: |
| 254 | if item.get("type") == "tool_use": |
| 255 | proxy_tool = item |
| 256 | break |
| 257 | |
| 258 | # At least one of them should have a tool use |
| 259 | if anthropic_tool is not None: |
| 260 | print("\n---------- ANTHROPIC TOOL USE ----------") |
| 261 | print(json.dumps(anthropic_tool, indent=2)) |
| 262 | |
| 263 | if proxy_tool is not None: |
| 264 | print("\n---------- PROXY TOOL USE ----------") |
| 265 | print(json.dumps(proxy_tool, indent=2)) |