fix: fall back to plain generate() when web_search tool returns HTTP error
mistral-large-latest may not support the web_search tool type on all API tiers; catch the exception and retry without web search so the pipeline does not crash with needs_review. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
4739a0b1fe
commit
00dc232426
1 changed files with 33 additions and 25 deletions
|
|
@ -74,26 +74,30 @@ final class MistralClient implements OllamaClientInterface
|
||||||
{
|
{
|
||||||
$messages = [['role' => 'user', 'content' => $prompt]];
|
$messages = [['role' => 'user', 'content' => $prompt]];
|
||||||
|
|
||||||
$response = $this->httpClient->request('POST', $this->mistralBaseUrl.'/v1/chat/completions', [
|
try {
|
||||||
'headers' => ['Authorization' => 'Bearer '.$this->mistralApiKey],
|
$response = $this->httpClient->request('POST', $this->mistralBaseUrl.'/v1/chat/completions', [
|
||||||
'json' => [
|
'headers' => ['Authorization' => 'Bearer '.$this->mistralApiKey],
|
||||||
'model' => $model,
|
'json' => [
|
||||||
'tools' => [['type' => 'web_search']],
|
'model' => $model,
|
||||||
'messages' => $messages,
|
'tools' => [['type' => 'web_search']],
|
||||||
],
|
'messages' => $messages,
|
||||||
'timeout' => 120,
|
],
|
||||||
]);
|
'timeout' => 120,
|
||||||
|
]);
|
||||||
|
|
||||||
|
/** @var array{choices: array{0: array{finish_reason: string, message: array{content: ?string, tool_calls?: list<array{id: string, function: array{name: string, arguments: string}}>}}}} $data */
|
||||||
|
$data = $response->toArray();
|
||||||
|
} catch (\Throwable) {
|
||||||
|
// Model does not support web_search tool — fall back to plain generation
|
||||||
|
return $this->generate($model, $prompt);
|
||||||
|
}
|
||||||
|
|
||||||
/** @var array{choices: array{0: array{finish_reason: string, message: array{content: ?string, tool_calls?: list<array{id: string, function: array{name: string, arguments: string}}>}}}} $data */
|
|
||||||
$data = $response->toArray();
|
|
||||||
$choice = $data['choices'][0];
|
$choice = $data['choices'][0];
|
||||||
|
|
||||||
if ('tool_calls' !== $choice['finish_reason']) {
|
if ('tool_calls' !== $choice['finish_reason']) {
|
||||||
return (string) $choice['message']['content'];
|
return (string) $choice['message']['content'];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Append assistant's tool call turn, then return empty tool results so
|
|
||||||
// Mistral's servers can complete the search and produce the final answer.
|
|
||||||
$messages[] = $choice['message'];
|
$messages[] = $choice['message'];
|
||||||
foreach ($choice['message']['tool_calls'] ?? [] as $toolCall) {
|
foreach ($choice['message']['tool_calls'] ?? [] as $toolCall) {
|
||||||
$messages[] = [
|
$messages[] = [
|
||||||
|
|
@ -103,20 +107,24 @@ final class MistralClient implements OllamaClientInterface
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
$final = $this->httpClient->request('POST', $this->mistralBaseUrl.'/v1/chat/completions', [
|
try {
|
||||||
'headers' => ['Authorization' => 'Bearer '.$this->mistralApiKey],
|
$final = $this->httpClient->request('POST', $this->mistralBaseUrl.'/v1/chat/completions', [
|
||||||
'json' => [
|
'headers' => ['Authorization' => 'Bearer '.$this->mistralApiKey],
|
||||||
'model' => $model,
|
'json' => [
|
||||||
'tools' => [['type' => 'web_search']],
|
'model' => $model,
|
||||||
'messages' => $messages,
|
'tools' => [['type' => 'web_search']],
|
||||||
],
|
'messages' => $messages,
|
||||||
'timeout' => 120,
|
],
|
||||||
]);
|
'timeout' => 120,
|
||||||
|
]);
|
||||||
|
|
||||||
/** @var array{choices: array{0: array{message: array{content: string}}}} $finalData */
|
/** @var array{choices: array{0: array{message: array{content: string}}}} $finalData */
|
||||||
$finalData = $final->toArray();
|
$finalData = $final->toArray();
|
||||||
|
|
||||||
return $finalData['choices'][0]['message']['content'];
|
return $finalData['choices'][0]['message']['content'];
|
||||||
|
} catch (\Throwable) {
|
||||||
|
return $this->generate($model, $prompt);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function guessMimeType(string $path): string
|
private function guessMimeType(string $path): string
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue