I’m building an AI bot and I want to use function calling but I don’t understand how to do it
<code>from openai import OpenAI
import json
from datetime import datetime
client = OpenAI(api_key="key")
def get_current_datetime():
return {"current_datetime": datetime.now().isoformat()}
functions = [
{
"name": "get_current_datetime",
"description": "Возвращает текущую дату и время в формате ISO 8601.",
"parameters": {
"type": "object",
"properties": {}
}
}
]
messages = [
{"role": "system", "content": "Ты можешь вызывать функции для получения информации."},
{"role": "user", "content": "Какое сейчас время?"}
]
response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
functions=functions,
function_call="auto"
)
print(response)
</code>
<code>from openai import OpenAI
import json
from datetime import datetime
client = OpenAI(api_key="key")
def get_current_datetime():
return {"current_datetime": datetime.now().isoformat()}
functions = [
{
"name": "get_current_datetime",
"description": "Возвращает текущую дату и время в формате ISO 8601.",
"parameters": {
"type": "object",
"properties": {}
}
}
]
messages = [
{"role": "system", "content": "Ты можешь вызывать функции для получения информации."},
{"role": "user", "content": "Какое сейчас время?"}
]
response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
functions=functions,
function_call="auto"
)
print(response)
</code>
from openai import OpenAI
import json
from datetime import datetime
client = OpenAI(api_key="key")
def get_current_datetime():
return {"current_datetime": datetime.now().isoformat()}
functions = [
{
"name": "get_current_datetime",
"description": "Возвращает текущую дату и время в формате ISO 8601.",
"parameters": {
"type": "object",
"properties": {}
}
}
]
messages = [
{"role": "system", "content": "Ты можешь вызывать функции для получения информации."},
{"role": "user", "content": "Какое сейчас время?"}
]
response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
functions=functions,
function_call="auto"
)
print(response)
To begin with, I created a simple function that returns the date and everything seems to be correct, but openai returns none
New contributor
J AR is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.