Alteration of time with calling custom function

Hello,
I encounter an issue with assistants api when dealing with time.
i have a custom function that is helping to assistant to access current time.

# custom function
def get_current_time():
    current_time = datetime.now()
    minutes = current_time.minute
    hour = current_time.hour
    day = current_time.day
    month = current_time.month
    year = current_time.year
    data = {'minutes': minutes,
            'hours': hour,
            'day': day,
            'month': month,
            "year": year}    
    json_data = json.dumps(data)
    return json_data

# Assistant code
if run.status == 'requires_action':
    for tool_call in run.required_action.submit_tool_outputs.tool_calls:
         if tool_call.function.name == 'get_current_time':
                        output = get_current_time()
                        client.beta.threads.runs.submit_tool_outputs(
                            thread_id=session["thread_id"],
                            run_id=run.id,
                            tool_outputs=[{
                                "tool_call_id": tool_call.id,
                                "output": json.dumps(output)
                            }]
                        )

but when it returns response, it is altering year and month: "datetime":"2023-11-27T18:32:00".

Result had to be "datetime":"2024-11-19T18:32:00".
I’m using gpt-4o-mini as model for assistant.

I want to know if there is a way to solve this problem.

Just use the additional_instructions parameter for a run to automatically place the time and date as part of the context. There’s no need for a distracting complex function to always be there that consumes more tokens than the actual data.

The AI will interpret and understand the date with higher quality, but you will need to give more instructions on the presentation format and the reason it needs to be output in a particular format, and where. Then you are using the -mini model, which has a mini version of following any provided context at all.

from datetime import datetime

def get_formatted_datetime() -> str:
    """Returns the current date and time in the specified format."""
    now = datetime.now()
    formatted_datetime = f'{{"datetime_now":"{now.strftime("%Y-%m-%dT%H:%M:%S")}"}}'
    return formatted_datetime

Thank you

I did add time, that were given by get_current_time(), to every user message and know it does every thing as planned.

1 Like