|
| 1 | +## Function Calling |
| 2 | + |
| 3 | +Function Calling allows the model to call external tools to enhance its capabilities.[[1]](https://api-docs.deepseek.com/guides/function_calling) |
| 4 | + |
| 5 | +#### 1. Define the tools used by the model and pass them with each message passed to the model, Receive query messages from the end user and pass them to the model with the defined tools. |
| 6 | +- example function `get_weather($city)`. |
| 7 | +```php |
| 8 | +function get_weather($city) |
| 9 | +{ |
| 10 | + $city = strtolower($city); |
| 11 | + $city = match($city){ |
| 12 | + "cairo" => ["temperature"=> 22, "condition" => "Sunny"], |
| 13 | + "gharbia" => ["temperature"=> 23, "condition" => "Sunny"], |
| 14 | + "sharkia" => ["temperature"=> 24, "condition" => "Sunny"], |
| 15 | + "beheira" => ["temperature"=> 21, "condition" => "Sunny"], |
| 16 | + default => "not found city name." |
| 17 | + }; |
| 18 | + return json_encode($city); |
| 19 | +} |
| 20 | +``` |
| 21 | +The user requests the weather in Cairo. |
| 22 | +```php |
| 23 | +$client = DeepSeekClient::build('your-api-key') |
| 24 | + ->query('What is the weather like in Cairo?') |
| 25 | + ->setTools([ |
| 26 | + [ |
| 27 | + "type" => "function", |
| 28 | + "function" => [ |
| 29 | + "name" => "get_weather", |
| 30 | + "description" => "Get the current weather in a given city", |
| 31 | + "parameters" => [ |
| 32 | + "type" => "object", |
| 33 | + "properties" => [ |
| 34 | + "city" => [ |
| 35 | + "type" => "string", |
| 36 | + "description" => "The city name", |
| 37 | + ], |
| 38 | + ], |
| 39 | + "required" => ["city"], |
| 40 | + ], |
| 41 | + ], |
| 42 | + ], |
| 43 | + ] |
| 44 | +); |
| 45 | + |
| 46 | +$response = $client->run(); |
| 47 | + |
| 48 | +``` |
| 49 | + |
| 50 | +Output response like. |
| 51 | +```json |
| 52 | +{ |
| 53 | + "id": "chat_12345", |
| 54 | + "object": "chat.completion", |
| 55 | + "created": 1677654321, |
| 56 | + "model": "deepseek-chat", |
| 57 | + "choices": [ |
| 58 | + { |
| 59 | + "index": 0, |
| 60 | + "message": { |
| 61 | + "role": "assistant", |
| 62 | + "content": null, |
| 63 | + "tool_calls": [ |
| 64 | + { |
| 65 | + "id": "call_12345", |
| 66 | + "type": "function", |
| 67 | + "function": { |
| 68 | + "name": "get_weather", |
| 69 | + "arguments": "{\"city\": \"Cairo\"}" |
| 70 | + } |
| 71 | + } |
| 72 | + ] |
| 73 | + }, |
| 74 | + "finish_reason": "tool_calls" |
| 75 | + } |
| 76 | + ] |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +#### 2. Receive the response and check if it has called one or more tools to execute it in the system ,And execute the tool called by the model. |
| 81 | +The deepseek api responds to the system and requests the execution of the tool responsible for fetching the weather status. |
| 82 | +```php |
| 83 | + |
| 84 | +$response = $client->run(); |
| 85 | + |
| 86 | +$response = json_decode($response, true); |
| 87 | +$message = $response['choices'][0]['message']; |
| 88 | +$firstFunction = $message['tool_calls'][0]; |
| 89 | +if ($firstFunction['function']['name'] == "get_weather") |
| 90 | +{ |
| 91 | + $weather_data = get_weather($firstFunction['function']['arguments']['city']); |
| 92 | +} |
| 93 | + |
| 94 | +``` |
| 95 | + |
| 96 | +#### 3. Coordinate the results and send the previous response with the results of the executed tools. |
| 97 | +Formats the response, and sends it back to the form. |
| 98 | +```php |
| 99 | +$response2 = $client->queryToolCall( |
| 100 | + $message['tool_calls'], |
| 101 | + $message['content'], |
| 102 | + $message['role'] |
| 103 | + )->queryTool( |
| 104 | + $firstFunction['id'], |
| 105 | + $weather_data |
| 106 | +); |
| 107 | +``` |
| 108 | + |
| 109 | +Request like |
| 110 | +```json |
| 111 | +{ |
| 112 | + "messages": [ |
| 113 | + { |
| 114 | + "role": "user", |
| 115 | + "content": "What is the weather like in Cairo?" |
| 116 | + }, |
| 117 | + { |
| 118 | + "content": "What is the weather like in Cairo?", |
| 119 | + "tool_calls": [ |
| 120 | + { |
| 121 | + "id": "930c60df-3ec75f81e00e", |
| 122 | + "type": "function", |
| 123 | + "function": { |
| 124 | + "name": "get_weather", |
| 125 | + "arguments": { |
| 126 | + "city": "Cairo" |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + ], |
| 131 | + "role": "assistant" |
| 132 | + }, |
| 133 | + { |
| 134 | + "role": "tool", |
| 135 | + "tool_call_id": "930c60df-3ec75f81e00e", |
| 136 | + "content": "{\"temperature\":22,\"condition\":\"Sunny\"}" |
| 137 | + } |
| 138 | + ], |
| 139 | + "model": "deepseek-chat", |
| 140 | + "stream": false, |
| 141 | + "temperature": 1.3, |
| 142 | + "tools": [ |
| 143 | + { |
| 144 | + "type": "function", |
| 145 | + "function": { |
| 146 | + "name": "get_weather", |
| 147 | + "description": "Get the current weather in a given city", |
| 148 | + "parameters": { |
| 149 | + "type": "object", |
| 150 | + "properties": { |
| 151 | + "city": { |
| 152 | + "type": "string", |
| 153 | + "description": "The city name" |
| 154 | + } |
| 155 | + }, |
| 156 | + "required": [ |
| 157 | + "city" |
| 158 | + ] |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + ] |
| 163 | +} |
| 164 | +``` |
| 165 | + |
| 166 | +#### 4. Receive the final response from the model and pass it to the end user. |
| 167 | +The deepseek api responds with the final response, which is the weather status according to the data passed to it in the example. |
| 168 | +```php |
| 169 | + |
| 170 | +$response2 = $response2->run(); |
| 171 | +echo $response2; |
| 172 | +``` |
| 173 | +Output response like :- |
| 174 | +```json |
| 175 | +{ |
| 176 | + "id": "chat_67890", |
| 177 | + "object": "chat.completion", |
| 178 | + "created": 1677654322, |
| 179 | + "model": "deepseek-chat", |
| 180 | + "choices": [ |
| 181 | + { |
| 182 | + "index": 0, |
| 183 | + "message": { |
| 184 | + "role": "assistant", |
| 185 | + "content": "The weather in Cairo is 22℃." |
| 186 | + }, |
| 187 | + "finish_reason": "stop" |
| 188 | + } |
| 189 | + ] |
| 190 | +} |
| 191 | +``` |
| 192 | + |
0 commit comments