-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.php
55 lines (46 loc) · 1.53 KB
/
connection.php
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
<?php
// 设置API令牌和账户ID
$api_token = 'YOUR_API_TOKEN';
$account_id = 'YOUR_ACCOUNT_ID';
// 获取POST数据
$request = file_get_contents('php://input');
$data = json_decode($request, true);
// 如果没有提供对话数据,返回错误信息
if (!isset($data['conversation']) || empty($data['conversation'])) {
echo json_encode(['error' => 'No conversation data provided']);
exit();
}
// 设置请求URL和数据
$url = "https://api.cloudflare.com/client/v4/accounts/$account_id/ai/run/@cf/meta/llama-3-8b-instruct";
$conversation = $data['conversation'];
$messages = array_map(function($entry) {
return ['role' => $entry['role'], 'content' => $entry['content']];
}, $conversation);
$requestData = json_encode(['messages' => $messages]);
// 初始化cURL
$ch = curl_init($url);
// 设置cURL选项
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
"Authorization: Bearer $api_token"
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestData);
// 执行cURL请求
$response = curl_exec($ch);
// 检查cURL错误
if ($response === false) {
echo json_encode(['error' => curl_error($ch)]);
} else {
// 输出响应
$responseData = json_decode($response, true);
if (isset($responseData['result'])) {
echo json_encode(['response' => $responseData['result']['response']]);
} else {
echo json_encode(['error' => 'Invalid response from AI']);
}
}
// 关闭cURL会话
curl_close($ch);
?>