1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| <?php
require __DIR__ . '/vendor/autoload.php';
$dotenv = \Dotenv\Dotenv::createImmutable(__DIR__); $dotenv->load();
print_r(ask('我想知道台北的天氣如何?'));
function ask(string $question): array { $client = OpenAI::client($_ENV['OPENAI_API_KEY']); $response = $client->chat()->create([ 'model' => 'gpt-3.5-turbo-0613', 'messages' => [ [ 'role' => 'user', 'content' => $question, ], [ 'role' => 'assistant', 'content' => null, 'function_call' => [ 'name' => 'get_current_weather', 'arguments' => 'Taipei', ], ], [ 'role' => 'function', 'name' => 'get_current_weather', 'content' => json_encode([ 'temperature' => 22, 'unit' => 'celsius', 'description' => 'Sunny', ]), ], ], 'temperature' => 1, 'functions' => [ [ 'name' => 'get_wether', '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', ], ], ], ], ]); return $response['choices'][0]['message']; }
|