import anthropic import json import subprocess import sys def get_db_param(key): result = subprocess.run( ['docker', 'exec', 'odoo-dev-db', 'psql', '-U', 'odoo', '-d', 'westin-v19', '-t', '-A', '-c', f"SELECT value FROM ir_config_parameter WHERE key = '{key}'"], capture_output=True, text=True ) return result.stdout.strip() api_key = get_db_param('fusion_accounting.anthropic_api_key') if not api_key: print('ERROR: No API key found in database') sys.exit(1) print(f'API Key found: {api_key[:12]}...{api_key[-4:]}') model = get_db_param('fusion_accounting.claude_model') or 'claude-sonnet-4-6' print(f'Model: {model}') client = anthropic.Anthropic(api_key=api_key) # Test 1: Basic API call print('\n--- Test 1: Basic API Call ---') try: response = client.messages.create( model=model, max_tokens=100, messages=[{'role': 'user', 'content': 'Say hello in one sentence.'}] ) print(f'Status: OK') print(f'Response: {response.content[0].text}') print(f'Tokens: {response.usage.input_tokens} in, {response.usage.output_tokens} out') except Exception as e: print(f'FAILED: {e}') sys.exit(1) # Test 2: Tool calling print('\n--- Test 2: Tool Calling ---') try: tools = [{ 'name': 'get_account_balance', 'description': 'Get the balance of an accounting account by code', 'input_schema': { 'type': 'object', 'properties': { 'account_code': {'type': 'string', 'description': 'Account code like 1000, 2005'}, }, 'required': ['account_code'] } }] response = client.messages.create( model=model, max_tokens=300, system='You are an accounting assistant. Use tools to look up data.', messages=[{'role': 'user', 'content': 'What is the balance on account 2005 (HST Collected)?'}], tools=tools, ) print(f'Stop reason: {response.stop_reason}') for block in response.content: if block.type == 'text': print(f'Text: {block.text}') elif block.type == 'tool_use': print(f'Tool call: {block.name}({json.dumps(block.input)})') print(f'Tool ID: {block.id}') print(f'Tokens: {response.usage.input_tokens} in, {response.usage.output_tokens} out') if response.stop_reason == 'tool_use': print('Tool calling: WORKING') else: print('Tool calling: Model responded with text (functional but did not use tool)') except Exception as e: print(f'FAILED: {e}') sys.exit(1) # Test 3: Multi-turn with tool result print('\n--- Test 3: Tool Result Round-Trip ---') try: messages = [ {'role': 'user', 'content': 'What is the HST balance on account 2005?'}, {'role': 'assistant', 'content': [ {'type': 'tool_use', 'id': 'test_123', 'name': 'get_account_balance', 'input': {'account_code': '2005'}} ]}, {'role': 'user', 'content': [ {'type': 'tool_result', 'tool_use_id': 'test_123', 'content': json.dumps({'balance': -15234.56, 'name': 'HST Collected'})} ]} ] response = client.messages.create( model=model, max_tokens=200, system='You are an accounting assistant. Report findings concisely in Canadian dollars.', messages=messages, tools=tools, ) for block in response.content: if block.type == 'text': print(f'AI Response: {block.text}') print(f'Tokens: {response.usage.input_tokens} in, {response.usage.output_tokens} out') print('Multi-turn tool flow: WORKING') except Exception as e: print(f'FAILED: {e}') sys.exit(1) print('\n=== ALL TESTS PASSED ===')