orhanerday / open-ai

OpenAI PHP SDK : Most downloaded, forked, contributed, huge community supported, and used PHP (Laravel , Symfony, Yii, Cake PHP or any PHP framework) SDK for OpenAI GPT-3 and DALL-E. It also supports chatGPT-like streaming. (ChatGPT AI is supported)
https://orhanerday.gitbook.io/openai-php-api-1/
MIT License
2.24k stars 286 forks source link

Function calling #129

Open lakshaysethi opened 9 months ago

lakshaysethi commented 9 months ago

Describe the feature or improvement you're requesting

does this library not support function calling? Tried but could not get this to work

as also noted in

https://github.com/orhanerday/open-ai/issues/98 https://platform.openai.com/docs/api-reference/chat/create image

curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
  "model": "gpt-3.5-turbo",
  "messages": [
    {
      "role": "user",
      "content": "What is the weather like in Boston?"
    }
  ],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_current_weather",
        "description": "Get the current weather in a given location",
        "parameters": {
          "type": "object",
          "properties": {
            "location": {
              "type": "string",
              "description": "The city and state, e.g. San Francisco, CA"
            },
            "unit": {
              "type": "string",
              "enum": ["celsius", "fahrenheit"]
            }
          },
          "required": ["location"]
        }
      }
    }
  ],
  "tool_choice": "auto"
}'

Additional context

No response

genolve commented 7 months ago

Not sure about the direct call with curl but the library worked for me, using gpt-4-turbo-preview, and adding a 'tools' param into args for $open_ai->chat(). You need to be real careful with the JSON If using json_decode as it will silently fail on JSON errors unless you specifically check:

$jData = stripslashes(html_entity_decode($tools)); $toolsO = json_decode($jData,true); if ($toolsO === null && json_last_error() !== JSON_ERROR_NONE){ echo '{"success":false,"message": "OpenAI tools JSON parse fail:'.json_last_error().'"}'; exit; }