Skip to content

Instantly share code, notes, and snippets.

@dexit
Forked from PeterUtekal/index.php
Created December 3, 2024 16:13
Show Gist options
  • Save dexit/90aee015c614427892489486aee108ff to your computer and use it in GitHub Desktop.
Save dexit/90aee015c614427892489486aee108ff to your computer and use it in GitHub Desktop.
OpenAI Functions
$response = $client->chat()->create([
'model' => 'gpt-3.5-turbo-16k-0613',
'messages' => $messages,
'functions' => [
[
'name' => 'search_job_post',
'description' => 'Search for a job based on a search query.',
'parameters' => [
'type' => 'object',
'properties' => [
'query' => [
'type' => 'string',
'description' => 'The search string, e.g. Software engineer in london',
],
],
'required' => ['query'],
],
]
]
]);
if(isset($response->choices[0]->message->functionCall)){
$functionCall = $response->choices[0]->message->functionCall;
$function = $functionCall->name;
$args = json_decode($functionCall->arguments);
switch($function){
case 'search_job_post':
$query = $args->query;
$job = queryAdzuna($query, $conn);
if($job == 'no_response'){
$messages[] = ['role' => 'assistant', 'content' => 'Unfortunately I could not find a job under "' . $query .'"'];
echo json_encode(['message' => 'Unfortunately I could not find a job under "' . $query .'"']);
exit;
}
$messages[] = ['role' => 'assistant', 'content' => null, 'function_call' => $functionCall];
$messages[] = ['role' => 'function', 'name' => 'search_job_post', 'content' => json_encode($job)];
$response2 = $client->chat()->create([
'model' => 'gpt-3.5-turbo-16k-0613',
'messages' => $messages,
'functions' => [
[
'name' => 'search_job_post',
'description' => 'Search for a job based on a search query.',
'parameters' => [
'type' => 'object',
'properties' => [
'query' => [
'type' => 'string',
'description' => 'The search string, e.g. Software engineer in london',
],
],
'required' => ['query'],
],
]
]
]);
$aiResponse = $response2->choices[0]->message->content;
echo json_encode(['job' => json_encode($job), 'function_call' => 'search_job_post', 'message' => $aiResponse, 'chatHistory' => $chatHistory]);
break;
}
}else{
$aiResponse = $response->choices[0]->message->content;
$messages[] = ['role' => 'assistant', 'content' => $aiResponse];
echo json_encode(['message' => $aiResponse, 'chatHistory' => $chatHistory]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment