Tests
import json
from openai.types.chat import ChatCompletionMessage
from openai.types.chat.chat_completion_message_tool_call import ChatCompletionMessageToolCall, Function
msg = ChatCompletionMessage(
role='assistant',
content=None,
tool_calls=[
ChatCompletionMessageToolCall(
id='call_001',
type='function',
function=Function(name='get_meteo', arguments='{"ville": "Paris"}')
)
]
)
result = parser_tool_calls(msg)
assert len(result) == 1, 'Doit retourner une liste avec 1 élément'
assert result[0]['nom'] == 'get_meteo', 'Le nom de la fonction doit etre get_meteo'
assert result[0]['arguments'] == {'ville': 'Paris'}, 'Les arguments doivent etre un dictionnaire Python'
assert result[0]['id'] == 'call_001', 'L id doit etre call_001'
msg2 = ChatCompletionMessage(
role='assistant',
content=None,
tool_calls=[
ChatCompletionMessageToolCall(id='call_A', type='function', function=Function(name='fn1', arguments='{"x": 1}')),
ChatCompletionMessageToolCall(id='call_B', type='function', function=Function(name='fn2', arguments='{"y": "hello"}')),
]
)
result2 = parser_tool_calls(msg2)
assert len(result2) == 2, 'Doit gerer plusieurs tool calls'
assert result2[0]['nom'] == 'fn1', 'Premier appel doit etre fn1'
assert result2[1]['arguments'] == {'y': 'hello'}, 'Les arguments du deuxieme appel doivent etre corrects'