微软openapi大模型调用function函数联网
2025-06-07 15:00:31
### 该功能使用webman2.0开发
#### php环境:8.3.21
#### 调用流程
##### 1、第一次直接调用大模型
##### 2、判断大模型是否返回function标识
##### 3、如果有返回function,那么启动联网博查搜索
#####4、把博查搜索结果丢给大模型,再次返回结果给客户端
<br>
## 下面是php类示例
<?php
/**
* 微软大模型
* @author zzk
*/
namespace app\util;
use support\Request;
use think\facade\Db;
use GuzzleHttp\Client;
class Openai
{
private $apiKey = null;
public function __construct(string $apiKey)
{
$this->apiKey = $apiKey;
}
/**
* gpt-4.1-mini对话(不带联网)
* @param array $message
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function gpt41Mini(array $message){
$url = 'https://xxxxx.openai.azure.com/openai/deployments/gpt-4.1-mini/chat/completions?api-version=2025-01-01-preview';
$header = [
'Content-Type' => 'application/json',
'api-key' => $this->apiKey
];
$client = new Client();
$result = $client->post($url,[
'headers' => $header,
'json' => $message
]);
return json_decode($result->getBody()->getContents(),true);
}
/**
* gpt-4o-mini对话
* @param array $message
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function gpt4oMini(array $message){
//美国东部
$url = 'https://xxxxxx.azure.com/openai/deployments/o4-mini/chat/completions?api-version=2025-01-01-preview';
$header = [
'Content-Type' => 'application/json',
'Authorization' => "Bearer ".$this->apiKey,
// 'api-key' => $this->apiKey,
];
$client = new Client();
$result = $client->post($url,[
'headers' => $header,
'json' => $message
]);
return json_decode($result->getBody()->getContents(),true);
}
/**
* gpt-4.1-mini对话(带联网)
* @param array $message
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function gpt41MiniSearch(array $message){
$sendMessage = $message;
$sendMessage['functions'] = [
[
"name" => "search",
"description" => "联网搜索",
"parameters" => [
"type" => "object",
"properties" => [
'query' => [
'type' => 'string',
'description' => '需要搜索的关键词,例如:"2025年苹果全球开发者大会主题'
]
],
"required" => ["query"]
]
]
];
$sendMessage['function_call'] = 'auto';
$url = 'https://xxxxx.azure.com/openai/deployments/gpt-4.1-mini/chat/completions?api-version=2025-01-01-preview';
$header = [
'Content-Type' => 'application/json',
'api-key' => $this->apiKey
];
$client = new Client();
$result = $client->post($url,[
'headers' => $header,
'json' => $sendMessage
]);
// return json_decode($result->getBody()->getContents(),true);
$response = json_decode($result->getBody()->getContents(),true);
$functionCall = $response['choices'][0]['message']['function_call'] ?? null;
if (!$functionCall) {
// 模型直接回答,无需搜索
return $response;
}
//联网搜索
$arguments = json_decode($functionCall['arguments'],true);
$keyword = $arguments['query'] ?? '';
$bocha = $this->bochaaiSearch($keyword);
if($bocha == false){
return $response;
}
$message['messages'][] = $response['choices'][0]['message'];
$message['messages'][] = [
'role' => 'function',
'name' => $functionCall['name'],
'content' => json_encode($bocha)
];
//再次请求大模型
$result = $client->post($url,[
'headers' => $header,
'json' => $message
]);
return json_decode($result->getBody()->getContents(),true);
}
/**
* 博查搜索
* 文档:https://bocha-ai.feishu.cn/wiki/RXEOw02rFiwzGSkd9mUcqoeAnNK
* @param $keyword
* @return false|mixed
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function bochaaiSearch($keyword){
if(empty($keyword)){
return false;
}
$url = 'https://api.bochaai.com/v1/web-search';
$header = [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer sk-xxxxxxxx',
];
$json = [
'query' => $keyword,
'count' => 20,
'freshness' => 'oneWeek'
];
$client = new Client();
$result = $client->post($url,[
'headers' => $header,
'json' => $json
]);
return json_decode($result->getBody()->getContents(),true);
}
}
##调用Openapi类方法示例
//发送到微软大模型
$openapiBody = [
'max_completion_tokens' => 1000,
'model' => 'gpt-4.1-mini',
'messages' => [
['role'=>'system','content'=>"# 角色\n你是aido'],
['role'=>'user','content'=>'今天天气怎么样'],
]
];
$openapi = new Openai('you key');
$result = $openapi->gpt41MiniSearch($openapiBody);
$resultCon = $result['choices'][0]['message']['content'] ?? ''; //返回结果文本