|
| 1 | +<?php |
| 2 | +namespace Tests\Feature; |
| 3 | + |
| 4 | +use DeepSeek\DeepSeekClient; |
| 5 | +use DeepSeek\Enums\Requests\HTTPState; |
| 6 | +use Mockery; |
| 7 | +use Mockery\{LegacyMockInterface,MockInterface}; |
| 8 | +use Tests\Feature\ClientDependency\FakeResponse; |
| 9 | + |
| 10 | + |
| 11 | +function get_weather($city) |
| 12 | +{ |
| 13 | + $city = strtolower($city); |
| 14 | + $city = match($city){ |
| 15 | + "cairo" => ["temperature"=> 22, "condition" => "Sunny"], |
| 16 | + "gharbia" => ["temperature"=> 23, "condition" => "Sunny"], |
| 17 | + "sharkia" => ["temperature"=> 24, "condition" => "Sunny"], |
| 18 | + "beheira" => ["temperature"=> 21, "condition" => "Sunny"], |
| 19 | + default => "not found city name." |
| 20 | + }; |
| 21 | + return json_encode($city); |
| 22 | +} |
| 23 | + |
| 24 | +test('Test function calling with fake responses.', function () { |
| 25 | + // Arrange |
| 26 | + $fake = new FakeResponse(); |
| 27 | + |
| 28 | + /** @var DeepSeekClient&LegacyMockInterface&MockInterface */ |
| 29 | + $mockClient = Mockery::mock(DeepSeekClient::class); |
| 30 | + |
| 31 | + $mockClient->shouldReceive('build')->andReturn($mockClient); |
| 32 | + $mockClient->shouldReceive('setTools')->andReturn($mockClient); |
| 33 | + $mockClient->shouldReceive('query')->andReturn($mockClient); |
| 34 | + $mockClient->shouldReceive('run')->once()->andReturn($fake->toolFunctionCalling()); |
| 35 | + |
| 36 | + // Act |
| 37 | + $response = $mockClient::build('your-api-key') |
| 38 | + ->query('What is the weather like in Cairo?') |
| 39 | + ->setTools([ |
| 40 | + [ |
| 41 | + "type" => "function", |
| 42 | + "function" => [ |
| 43 | + "name" => "get_weather", |
| 44 | + "description" => "Get the current weather in a given city", |
| 45 | + "parameters" => [ |
| 46 | + "type" => "object", |
| 47 | + "properties" => [ |
| 48 | + "city" => [ |
| 49 | + "type" => "string", |
| 50 | + "description" => "The city name", |
| 51 | + ], |
| 52 | + ], |
| 53 | + "required" => ["city"], |
| 54 | + ], |
| 55 | + ], |
| 56 | + ], |
| 57 | + ] |
| 58 | + )->run(); |
| 59 | + |
| 60 | + // Assert |
| 61 | + expect($fake->toolFunctionCalling())->toEqual($response); |
| 62 | + |
| 63 | + //------------------------------------------ |
| 64 | + |
| 65 | + // Arrange |
| 66 | + $response = json_decode($response, true); |
| 67 | + $message = $response['choices'][0]['message']; |
| 68 | + |
| 69 | + $firstFunction = $message['tool_calls'][0]; |
| 70 | + if ($firstFunction['function']['name'] == "get_weather") |
| 71 | + { |
| 72 | + $weather_data = get_weather($firstFunction['function']['arguments']['city']); |
| 73 | + } |
| 74 | + |
| 75 | + $mockClient->shouldReceive('queryCallTool')->andReturn($mockClient); |
| 76 | + $mockClient->shouldReceive('queryTool')->andReturn($mockClient); |
| 77 | + $mockClient->shouldReceive('run')->andReturn($fake->resultToolFunctionCalling()); |
| 78 | + |
| 79 | + // Act |
| 80 | + $response2 = $mockClient->queryCallTool( |
| 81 | + $message['tool_calls'], |
| 82 | + $message['content'], |
| 83 | + $message['role'] |
| 84 | + )->queryTool( |
| 85 | + $firstFunction['id'], |
| 86 | + $weather_data, |
| 87 | + 'tool' |
| 88 | + )->run(); |
| 89 | + |
| 90 | + // Assert |
| 91 | + expect($fake->resultToolFunctionCalling())->toEqual($response2); |
| 92 | +}); |
| 93 | + |
| 94 | +test('Test function calling use base data with real responses.', function () { |
| 95 | + // Arrange |
| 96 | + $client = DeepSeekClient::build('your-api-key') |
| 97 | + ->query('What is the weather like in Cairo?') |
| 98 | + ->setTools([ |
| 99 | + [ |
| 100 | + "type" => "function", |
| 101 | + "function" => [ |
| 102 | + "name" => "get_weather", |
| 103 | + "description" => "Get the current weather in a given city", |
| 104 | + "parameters" => [ |
| 105 | + "type" => "object", |
| 106 | + "properties" => [ |
| 107 | + "city" => [ |
| 108 | + "type" => "string", |
| 109 | + "description" => "The city name", |
| 110 | + ], |
| 111 | + ], |
| 112 | + "required" => ["city"], |
| 113 | + ], |
| 114 | + ], |
| 115 | + ], |
| 116 | + ] |
| 117 | + ); |
| 118 | + |
| 119 | + // Act |
| 120 | + $response = $client->run(); |
| 121 | + $result = $client->getResult(); |
| 122 | + |
| 123 | + // Assert |
| 124 | + expect($response)->not()->toBeEmpty($response) |
| 125 | + ->and($result->getStatusCode())->toEqual(HTTPState::OK->value); |
| 126 | + |
| 127 | + //----------------------------------------------------------------- |
| 128 | + |
| 129 | + // Arrange |
| 130 | + $response = json_decode($response, true); |
| 131 | + $message = $response['choices'][0]['message']; |
| 132 | + $firstFunction = $message['tool_calls'][0]; |
| 133 | + if ($firstFunction['function']['name'] == "get_weather") |
| 134 | + { |
| 135 | + $weather_data = get_weather($firstFunction['function']['arguments']['city']); |
| 136 | + } |
| 137 | + |
| 138 | + $response2 = $client->queryToolCall( |
| 139 | + $message['tool_calls'], |
| 140 | + $message['content'], |
| 141 | + $message['role'] |
| 142 | + )->queryTool( |
| 143 | + $firstFunction['id'], |
| 144 | + $weather_data, |
| 145 | + 'tool' |
| 146 | + ); |
| 147 | + |
| 148 | + // Act |
| 149 | + $response2 = $response2->run(); |
| 150 | + $result2 = $client->getResult(); |
| 151 | + |
| 152 | + // Assert |
| 153 | + expect($response2)->not()->toBeEmpty($response2) |
| 154 | + ->and($result2->getStatusCode())->toEqual(HTTPState::OK->value); |
| 155 | +}); |
0 commit comments