Skip to content

Instantly share code, notes, and snippets.

@ochafik
Last active March 22, 2024 19:52
Show Gist options
  • Select an option

  • Save ochafik/07ff7eb402e7a6fd0770f76c82eb87b4 to your computer and use it in GitHub Desktop.

Select an option

Save ochafik/07ff7eb402e7a6fd0770f76c82eb87b4 to your computer and use it in GitHub Desktop.
OpenAI tool call in chat completion
# https://cookbook.openai.com/examples/how_to_call_functions_with_chat_models
#export OPENAI_API_KEY=...
export ENDPOINT=https://api.openai.com/v1/chat/completions
#export ENDPOINT=http://localhost:8080/v1/chat/completions
curl "$ENDPOINT" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-3.5-turbo",
"tools": [{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"format": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The temperature unit to use. Infer this from the users location."
}
},
"required": ["location", "format"]
}
}
}, {
"type": "function",
"function": {
"name": "get_n_day_weather_forecast",
"description": "Get an N-day weather forecast",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"format": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The temperature unit to use. Infer this from the users location."
},
"num_days": {
"type": "integer",
"description": "The number of days to forecast"
}
},
"required": ["location", "format", "num_days"]
}
}
}],
"messages": [
{"role": "system", "content": "Do not make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous."},
{"role": "user", "content": "what is the weather going to be like in San Francisco and Glasgow over the next 4 days"}
]
}'
# {
# "id": "chatcmpl-95fBKQR3zjCZw3kTi4TBZZh1l9rcG",
# "object": "chat.completion",
# "created": 1711136970,
# "model": "gpt-3.5-turbo-0125",
# "choices": [
# {
# "index": 0,
# "message": {
# "role": "assistant",
# "content": null,
# "tool_calls": [
# {
# "id": "call_08f1msKRNolnlSOcieNBkNY2",
# "type": "function",
# "function": {
# "name": "get_n_day_weather_forecast",
# "arguments": "{\"location\": \"San Francisco\", \"format\": \"celsius\", \"num_days\": 4}"
# }
# },
# {
# "id": "call_5zAoALg3GxyelJb56MycNMvb",
# "type": "function",
# "function": {
# "name": "get_n_day_weather_forecast",
# "arguments": "{\"location\": \"Glasgow\", \"format\": \"celsius\", \"num_days\": 4}"
# }
# }
# ]
# },
# "logprobs": null,
# "finish_reason": "tool_calls"
# }
# ],
# "usage": {
# "prompt_tokens": 204,
# "completion_tokens": 74,
# "total_tokens": 278
# },
# "system_fingerprint": "fp_3bc1b5746c"
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment