NousResearch / Hermes-Function-Calling

MIT License
464 stars 73 forks source link

Function schema and toolcall output are not JSON #3

Closed zakkor closed 2 days ago

zakkor commented 3 months ago

Hi there, thanks for the model and repo!

I noticed that the given system prompt examples for function schema definitions:

{'type': 'function', 'function': {'name': 'get_stock_fundamentals', 'description': 'get_stock_fundamentals(symbol: str) -> dict - Get fundamental data for a given stock symbol using yfinance API.\n\n    Args:\n    symbol (str): The stock symbol.\n\n    Returns:\n    dict: A dictionary containing fundamental data.', 'parameters': {'type': 'object', 'properties': {'symbol': {'type': 'string'}}, 'required': ['symbol']}}}

As well as the actual output from tool calls:

{'arguments': {'symbol': 'TSLA'}, 'name': 'get_stock_fundamentals'}

Are not valid JSON, because JSON uses double quotes, not single quotes. I saw in your example code that you parse the output first as JSON, and if that doesn't work, by parsing the Python AST value. I'm guessing these representations are Python dicts. Additionally, some of the example function calling outputs have mixed JSON and Python AST formats inside the same response.

This is kind of problematic because most languages don't provide utilities for parsing Python AST (whereas JSON is universal), which makes this unusable outside of Python. In the case of mixed JSON/Python responses, it's even harder to parse.

interstellarninja commented 3 months ago

Hi thanks for pointing out the issue.

Yes the model was trained with function signatures and function calls with both single quotes & double quotes.

With regards to parsing with both json.loads() and AST, we just want to make it more general so that if model returns python dictionary or json object with some invalid tokens, we can still parse them.

Please explicitly prompt the model to use json object with double quotes and it should return function calls with valid json objects.