人工智能通过分析用户输入的自然语言来判断用户需求,调用不同的方法
openai在最近(2023.6.13)的一次更新中,重点介绍了Function calling的能力更新,简单理解就是在人工智能模型在获取到用户输入的自然语言后,可以基于分析判断调用不同的能力,以下面的例子为例:
1、当用户输入:北京天气怎么样?推荐些适合的户外活动吧 时,会调用get_current_weather方法获取北京当天的天气情况,再将天气数据和用户问题发给模型做总结后反馈给用户
2、当用户输入:北京未来几天的的天气怎么样?根据每天的天气情况推荐一些适合的户外活动吧时,模型会判断调用get_days_current_weather方法获取北京未来几天的天气情况,再将天气数据和用户问题发给模型做总结后反馈给用户
3、当用户输入:目前的天气预报是基于什么原理做的预测呢? 模型判断没有合适的方法可以调用,会直接返回问题的答案
调用流程可以参考下面的代码,使用了(github.com/openai-php/…) openai-php/client v0.6.1来实现
public function function_call($prompt)
{
$yourApiKey = '';
try {
$client = OpenAI::client($yourApiKey);
if(isset($client)){
$response = $client->chat()->create([
'model' => 'gpt-3.5-turbo-0613',
'messages' => [
['role' => 'user', 'content' => $prompt],
],
'functions' => [
[
'name' => 'get_current_weather',
'description' => '获取给定位置的当前天气',
'parameters' => [
'type' => 'object',
'properties' => [
'location' => [
'type' => 'string',
'description' => '城市和省或者区和市,例如北京市海淀区',
],
'unit' => [
'type' => 'string',
'enum' => ['celsius', 'fahrenheit']
],
],
'required' => ['location'],
],
],
[
'name' => 'get_days_current_weather',
'description' => '获取给定位置未来几天的天气',
'parameters' => [
'type' => 'object',
'properties' => [
'location' => [
'type' => 'string',
'description' => '城市和省或者区和市,例如北京市海淀区',
],
'unit' => [
'type' => 'string',
'enum' => ['celsius', 'fahrenheit']
],
],
'required' => ['location'],
],
]
]
]);
foreach ($response->choices as $result) {
$result->index; // 0
$result->message->role; // 'assistant'
$content = $result->message->content; // null
if(isset($result->message->functionCall->name)){
$name = $result->message->functionCall->name; // 'get_current_weather'
}
if(isset($result->message->functionCall->arguments)){
$arguments = $result->message->functionCall->arguments; // "{\n "location": "Boston, MA"\n}"
}
if(isset($result->finishReason)){
$finishReason = $result->finishReason; // 'function_call'
}
if(isset($content)){
}
if(isset($name)){
}
if(isset($arguments)){
}
if(isset($finishReason)){
}
}
if($finishReason=="function_call"){
if(isset($arguments)){
$arr = json_decode(json_decode(json_encode($arguments)));
if(isset($arr->location)){
}
}
if(isset($name)){
$content_json = $this->systemservice->$name($arr->location);
}
unset($response);
//$stream = $client->chat()->createStreamed([
$response = $client->chat()->create([
'model' => 'gpt-3.5-turbo-0613',
'messages' => [
['role' => 'user', 'content' => $prompt],
['role' => 'function', 'name' => 'get_current_weather', 'content' => $content_json ],
],
]);
foreach ($response->choices as $result) {
$result->index; // 0
$result->message->role; // 'assistant'
$content = $result->message->content; // null
if(isset($result->message->functionCall->name)){
$name = $result->message->functionCall->name; // 'get_current_weather'
}
if(isset($result->message->functionCall->arguments)){
$arguments = $result->message->functionCall->arguments; // "{\n "location": "Boston, MA"\n}"
}
if(isset($result->finishReason)){
$finishReason = $result->finishReason; // 'function_call'
}
if(isset($name)){
}
if(isset($arguments)){
}
if(isset($content)){
}
if(isset($finishReason)){
}
}
}
}else{
}
} catch (\OpenAI\Exceptions\ErrorException $e) {
$type = $e->getErrorType(); // "invalid_request_error"
$errorCode = $e->getErrorCode(); // "invalid_api_key"
$errorMessage = $e->getErrorMessage();
if(isset($type)){
}
if(isset($errorCode)){
}
if(isset($errorMessage)){
}
}
}
可以在微信小程序搜索“AIWorkFlow”,自己亲自试试[微笑]