1
This commit is contained in:
parent
c584dcac4e
commit
fff16d2578
46
api.php
46
api.php
@ -27,9 +27,9 @@ function get_env($key, $default = null) {
|
|||||||
// --- Main Logic ---
|
// --- Main Logic ---
|
||||||
|
|
||||||
// 1. Get API Key
|
// 1. Get API Key
|
||||||
$apiKey = get_env('GEMINI_API_KEY');
|
$apiKey = get_env('MISTRAL_API_KEY');
|
||||||
if (!$apiKey) {
|
if (!$apiKey) {
|
||||||
echo json_encode(['reply' => 'Ошибка: API-ключ не найден. Пожалуйста, добавьте его в файл .env']);
|
echo json_encode(['reply' => 'Ошибка: API-ключ не найден. Пожалуйста, добавьте его в файл .env с именем MISTRAL_API_KEY.']);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,28 +50,33 @@ if (!file_exists($knowledgeBasePath)) {
|
|||||||
}
|
}
|
||||||
$knowledgeBase = file_get_contents($knowledgeBasePath);
|
$knowledgeBase = file_get_contents($knowledgeBasePath);
|
||||||
|
|
||||||
// 4. Prepare the prompt for the AI
|
// 4. Prepare the messages for the AI
|
||||||
$prompt = "Ты — ИИ-ассистент, специалист по внутренней системе управления складом под названием HUB. Твоя задача — отвечать на вопросы пользователя, основываясь ИСКЛЮЧИТЕЛЬНО на предоставленной базе знаний. Не придумывай ничего от себя. Если ответа в базе знаний нет, вежливо сообщи, что ты можешь отвечать только на вопросы, связанные с системой HUB.\n\nВот база знаний:\n---\n" . $knowledgeBase . "\n---\n\nВопрос пользователя: \"" . $userMessage . "\"";
|
$systemPrompt = "Ты — ИИ-ассистент, специалист по внутренней системе управления складом под названием HUB. Твоя задача — отвечать на вопросы пользователя, основываясь ИСКЛЮЧИТЕЛЬНО на предоставленной базе знаний. Не придумывай ничего от себя. Если ответа в базе знаний нет, вежливо сообщи, что ты можешь отвечать только на вопросы, связанные с системой HUB.\n\nВот база знаний:\n---\n" . $knowledgeBase;
|
||||||
|
|
||||||
// 5. Call the Gemini API
|
// 5. Call the Mistral API
|
||||||
$url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=' . $apiKey;
|
$url = 'https://api.mistral.ai/v1/chat/completions';
|
||||||
|
|
||||||
$data = [
|
$data = [
|
||||||
'contents' => [
|
'model' => 'mistral-small-latest', // Or another suitable model
|
||||||
|
'messages' => [
|
||||||
[
|
[
|
||||||
'parts' => [
|
'role' => 'system',
|
||||||
['text' => $prompt]
|
'content' => $systemPrompt
|
||||||
]
|
],
|
||||||
|
[
|
||||||
|
'role' => 'user',
|
||||||
|
'content' => $userMessage
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
|
|
||||||
$options = [
|
$options = [
|
||||||
'http' => [
|
'http' => [
|
||||||
'header' => "Content-type: application/json\r\n",
|
'header' => "Content-Type: application/json\n" .
|
||||||
|
"Authorization: Bearer " . $apiKey . "\n",
|
||||||
'method' => 'POST',
|
'method' => 'POST',
|
||||||
'content' => json_encode($data),
|
'content' => json_encode($data),
|
||||||
'ignore_errors' => true // To see the error message from the API
|
'ignore_errors' => true
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
|
|
||||||
@ -81,30 +86,29 @@ $http_response_header = $http_response_header ?? [];
|
|||||||
|
|
||||||
// 6. Process the response
|
// 6. Process the response
|
||||||
if ($response === FALSE) {
|
if ($response === FALSE) {
|
||||||
$error = 'Не удалось связаться с API. ';
|
$error = 'Не удалось связаться с API Mistral. ';
|
||||||
// Check for more specific errors if possible
|
|
||||||
$last_error = error_get_last();
|
$last_error = error_get_last();
|
||||||
if ($last_error) {
|
if ($last_error) {
|
||||||
$error .= $last_error['message'];
|
$error .= $last_error['message'];
|
||||||
}
|
}
|
||||||
// Try to get the HTTP status and response body
|
|
||||||
$status_line = $http_response_header[0] ?? 'HTTP/1.1 500 Internal Server Error';
|
$status_line = $http_response_header[0] ?? 'HTTP/1.1 500 Internal Server Error';
|
||||||
preg_match('{HTTP/\S+\s(\d+)}', $status_line, $match);
|
preg_match('{HTTP/\S+\s(\d+)}', $status_line, $match);
|
||||||
$status = $match[1] ?? 500;
|
$status = $match[1] ?? 500;
|
||||||
|
|
||||||
error_log("Gemini API Error: Status $status, Response: $response");
|
error_log("Mistral API Error: Status $status, Response: $response");
|
||||||
|
|
||||||
echo json_encode(['reply' => "Ошибка при обращении к сервису ИИ. Статус: $status. Пожалуйста, проверьте ключ API и настройки сервера."]);
|
echo json_encode(['reply' => "Ошибка при обращении к сервису ИИ (Mistral). Статус: $status. Пожалуйста, проверьте ключ API и настройки сервера."]);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$result = json_decode($response, true);
|
$result = json_decode($response, true);
|
||||||
|
|
||||||
if (isset($result['candidates'][0]['content']['parts'][0]['text'])) {
|
if (isset($result['choices'][0]['message']['content'])) {
|
||||||
$reply = $result['candidates'][0]['content']['parts'][0]['text'];
|
$reply = $result['choices'][0]['message']['content'];
|
||||||
echo json_encode(['reply' => $reply]);
|
echo json_encode(['reply' => $reply]);
|
||||||
} else {
|
} else {
|
||||||
// Log the actual error response from the API for debugging
|
// Log the actual error response from the API for debugging
|
||||||
error_log("Gemini API - Unexpected response structure: " . $response);
|
error_log("Mistral API - Unexpected response structure: " . $response);
|
||||||
echo json_encode(['reply' => 'Получен неожиданный ответ от сервиса ИИ. Возможно, проблема с конфигурацией или ключом API.']);
|
$errorMessage = $result['message'] ?? 'Неизвестная ошибка.';
|
||||||
|
echo json_encode(['reply' => 'Получен неожиданный ответ от сервиса ИИ (Mistral). ' . $errorMessage]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user