changes
This commit is contained in:
64
fusion_accounting/tests/test_api_live.py
Normal file
64
fusion_accounting/tests/test_api_live.py
Normal file
@@ -0,0 +1,64 @@
|
||||
import anthropic
|
||||
import json
|
||||
import sys
|
||||
|
||||
api_key = sys.argv[1]
|
||||
model = sys.argv[2] if len(sys.argv) > 2 else 'claude-sonnet-4-6'
|
||||
print(f'API Key: {api_key[:12]}...{api_key[-4:]}')
|
||||
print(f'Model: {model}')
|
||||
|
||||
client = anthropic.Anthropic(api_key=api_key)
|
||||
|
||||
print()
|
||||
print('--- Test 1: Basic API Call ---')
|
||||
try:
|
||||
r = client.messages.create(model=model, max_tokens=100, messages=[{'role': 'user', 'content': 'Say hello in one sentence.'}])
|
||||
print(f'OK: {r.content[0].text}')
|
||||
print(f'Tokens: {r.usage.input_tokens} in, {r.usage.output_tokens} out')
|
||||
except Exception as e:
|
||||
print(f'FAILED: {e}')
|
||||
sys.exit(1)
|
||||
|
||||
print()
|
||||
print('--- Test 2: Tool Calling ---')
|
||||
try:
|
||||
tools = [{'name': 'get_account_balance', 'description': 'Get balance of an account by code', 'input_schema': {'type': 'object', 'properties': {'account_code': {'type': 'string', 'description': 'Account code'}}, 'required': ['account_code']}}]
|
||||
r = client.messages.create(model=model, max_tokens=300, system='You are an accounting AI. Always use tools to look up data before answering.', messages=[{'role': 'user', 'content': 'Look up the balance on account 2005.'}], tools=tools)
|
||||
print(f'Stop reason: {r.stop_reason}')
|
||||
tool_id = None
|
||||
for b in r.content:
|
||||
if b.type == 'text':
|
||||
print(f'Text: {b.text}')
|
||||
elif b.type == 'tool_use':
|
||||
print(f'TOOL CALL: {b.name}({json.dumps(b.input)}) id={b.id}')
|
||||
tool_id = b.id
|
||||
if r.stop_reason == 'tool_use':
|
||||
print('RESULT: Tool calling WORKING')
|
||||
else:
|
||||
print('RESULT: No tool call (model answered directly)')
|
||||
except Exception as e:
|
||||
print(f'FAILED: {e}')
|
||||
sys.exit(1)
|
||||
|
||||
print()
|
||||
print('--- Test 3: Tool Result Round-Trip ---')
|
||||
try:
|
||||
if not tool_id:
|
||||
tool_id = 'toolu_test123'
|
||||
msgs = [
|
||||
{'role': 'user', 'content': 'Look up account 2005 balance.'},
|
||||
{'role': 'assistant', 'content': [{'type': 'tool_use', 'id': tool_id, 'name': 'get_account_balance', 'input': {'account_code': '2005'}}]},
|
||||
{'role': 'user', 'content': [{'type': 'tool_result', 'tool_use_id': tool_id, 'content': json.dumps({'balance': -15234.56, 'name': 'HST Collected'})}]}
|
||||
]
|
||||
r2 = client.messages.create(model=model, max_tokens=200, system='You are an accounting AI. Report findings in Canadian dollars.', messages=msgs, tools=tools)
|
||||
for b in r2.content:
|
||||
if b.type == 'text':
|
||||
print(f'AI: {b.text}')
|
||||
print(f'Tokens: {r2.usage.input_tokens} in, {r2.usage.output_tokens} out')
|
||||
print('RESULT: Multi-turn tool flow WORKING')
|
||||
except Exception as e:
|
||||
print(f'FAILED: {e}')
|
||||
sys.exit(1)
|
||||
|
||||
print()
|
||||
print('=== ALL TESTS PASSED ===')
|
||||
107
fusion_accounting/tests/test_claude_api.py
Normal file
107
fusion_accounting/tests/test_claude_api.py
Normal file
@@ -0,0 +1,107 @@
|
||||
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 ===')
|
||||
Reference in New Issue
Block a user