Fine Tuning a function call

Hello, devs!

I am building an AI assistant focusing on using many function calls to handle different actions in my application. Defining your functions in the chatCompletions API adds up to your tokens count which eventually incurs high usage costs.

Going through the docs recently specifically the fine-tune section I noticed you can now fine-tune with function calls. I followed the instructions, set up my fine-tine jsonl file, and created a ft model. Below is a sample which consist of some system messages to structure the model and some other messages which define a function call response together with the functions definition.

{"messages":[{"role":"system","content":"Melo AI is the AI assistant to the Melo Dashboard."},{"role":"user","content":"Who are you?"},{"role":"assistant","content":"Hello there! I am Melo AI here to assist you. You can ask me to show you you sales revenue for the past month."}]}
{"messages":[{"role":"system","content":"Melo AI is the AI assistant to the Melo Dashboard."},{"role":"user","content":"Who are you?"},{"role":"assistant","content":"Hello there! I am Melo AI here to assist you. You can ask me about your inventory summary for a facility"}]}
{"messages":[{"role":"system","content":"Melo AI is the AI assistant to the Melo Dashboard."},{"role":"user","content":"Who are you?"},{"role":"assistant","content":"Hello there! I am Melo AI here to assist you. You can ask me to show you your top 10 products in a certain zone"}]}
{"messages":[{"role":"system","content":"Melo AI is the AI assistant to the Melo Dashboard."},{"role":"user","content":"What can I ask you?"},{"role":"assistant","content":"Hello there! I am Melo AI here to assist you. You can ask me to do any of the following;\n\nShow you your top 10 products in a certain zone\nWho are my top 10 customers?\nShow me a chart total sales revenue for the past month."}]}
{"messages":[{"role":"system","content":"Melo AI is the AI assistant to the Melo Dashboard."},{"role":"user","content":"Show me the customer aquisition rate for the past year on a chart"},{"role":"assistant","function_call":{"name":"get_analytics_time_series_chart_from_prompt","arguments":"{\"prompt-reply\": \"Here is a line chart showing your customer acquisition rate over the past year\", \"name\": \"Customer Acquisition Rate for 2023\", \"request.chart\": \"line_chart\", \"request.timePeriod\": \"yearly\", \"request.timeRange\": \"['2023-01-01T00:00:00.000Z', '2023-12-31T00:00:00.000Z']\", \"request.metricList\": \"[{'metric': 'customer_acquisition_rate_periodic'}]\"}"}}],"functions":[{"name":"get_analytics_time_series_chart_from_prompt","description":"Get the time series analytics metric into a chart. That is any analytic metric that can be represented in a time series chart, for example Line Chart, Stepline Chart, Area Chart, Area Spline Chart, Stacked Area Chart, Bar Chart. Note that the time range in the prompt should not be more than 100 days, 12 months, 3 years and 6 quarters Examples of these metrics include, customer acquisition rate, total sales revenue, average order value, total orders, order fill rate.","parameters":{"type":"object","properties":{"name":{"type":"string","description":"name or title of the chart"},"description":{"type":"string","description":"The description of the chart. Should not be more than 30 words"},"prompt-reply":{"type":"string","description":"A reply to the prompt"},"request.chart":{"type":"string","description":"The type of chart","enum":["line_chart","stepline_chart","area_chart","area_spline_chart","stacked_area_chart","bar_chart"]},"request.timePeriod":{"type":"string","description":"The time period the metric is being broken into","enum":["daily","weekly","monthly","yearly"]},"request.timeRange":{"type":"array","items":{"type":"string"},"description":"The converted time range to a tuple of datetimes. Note that the timezome is UTC. e.g ['1990-01-01T00:00:00.000Z', '1990-01-01T00:00:00.000Z']. Note that weeks(should begin from the monday of the week). Months and years should begin from the first day, example month should be 2022-01-01T00:00:00.000Z not 2022-01-29T00:00:00.000Z"},"request.metricList":{"type":"array","items":{"type":"string"},"description":"The the metrics to a list this shown below. e.g [{'metric': 'total_sales_revenue_periodic'}, {'metric': 'customer_acquisition_rate_periodic'}] The metrics need to be among these; customer_acquisition_rate_periodic, total_sales_revenue_periodic, average_order_value_periodic, total_orders_periodic, order_fill_rate_periodic. If the request does not among these metrics then dont call this tool function"}},"required":["prompt-reply","name","description","request.chart","request.timePeriod","request.timeRange","request.metricList"]}}]}

Now after doing all of that, I can’t get my function call response if I query the model with a prompt that suggests that it should return the function call with the right arguments as seen below

async def run_chat_prompt(message_history):
    completion = openai_client.chat.completions.create(
        model=GPT_MODEL,
        messages=message_history,
    )
    print(f"COMPLETIONS >> {completion}")
    reply_content = completion.choices[len(completion.choices)-1]
    return reply_content

How can I get the function call response directly from the model without defining it in the chat completions API? The idea here is to reduce the prompt tokens with fine-tuning a function call.

Hi! Welcome to the forum!

That’s not really how fine-tunes work, although in theory (if you stretch the theory) it could theoretically maybe work.

How about trying to combine some functions instead? Do you really need a separate function for every little thing? maybe you can compact it onto one, and use an enum as an argument that decides which function will actuall be called by your backend?

1 Like

You were deceived by the creative mistruths of the fine-tune documentation language.

You must include at least one function specification in the API call, or the ability for the AI to invoke functions is just completely disabled.

2 Likes

Do you if i have 5 functions in the fine-tune jsonl file I need to add at least one in the completions API and it can call the other four from a prompt?