-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathOllamaTextGenerationModel.php
More file actions
96 lines (85 loc) · 2.69 KB
/
OllamaTextGenerationModel.php
File metadata and controls
96 lines (85 loc) · 2.69 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
declare( strict_types=1 );
namespace Fueled\AiProviderForOllama\Models;
use Fueled\AiProviderForOllama\Models\Traits\OllamaRequestOptionsTrait;
use Fueled\AiProviderForOllama\Provider\OllamaProvider;
use WordPress\AiClient\Providers\Http\DTO\Request;
use WordPress\AiClient\Providers\Http\DTO\RequestOptions;
use WordPress\AiClient\Providers\Http\Enums\HttpMethodEnum;
use WordPress\AiClient\Providers\OpenAiCompatibleImplementation\AbstractOpenAiCompatibleTextGenerationModel;
/**
* Class for an Ollama text generation model using the OpenAI-compatible chat completions API.
*
* TODO: Could look to use the native API instead of the OpenAI-compatible API.
*
* @since 1.0.0
*/
class OllamaTextGenerationModel extends AbstractOpenAiCompatibleTextGenerationModel {
use OllamaRequestOptionsTrait;
/**
* Prepares the response format parameter for Ollama's OpenAI-compatible API.
*
* Ollama's OpenAI-compatible API uses the same response_format key as OpenAI,
* but schema mode expects the schema to be nested at json_schema.schema.
*
* @since x.x.x
*
* @param array<string, mixed>|null $output_schema The output schema.
* @return array<string, mixed> The prepared response format parameter.
*/
protected function prepareResponseFormatParam( ?array $output_schema ): array {
if ( is_array( $output_schema ) ) {
return array(
'type' => 'json_schema',
'json_schema' => array(
'name' => 'response_schema',
'schema' => $output_schema,
),
);
}
return array(
'type' => 'json_object',
);
}
/**
* {@inheritDoc}
*
* @since 1.0.0
*/
protected function createRequest(
HttpMethodEnum $method,
string $path,
array $headers = array(),
$data = null
): Request {
$request_options = $this->prepareRequestOptionsForTextGeneration();
// Keep transport-only timeout options out of the OpenAI-compatible payload.
if ( is_array( $data ) ) {
unset( $data['ollama.request_timeout'], $data['ollama.connect_timeout'] );
}
// Ollama supports OpenAI-compatible endpoints at /v1/.
$path = ltrim( (string) preg_replace( '#^v1/?#', '', ltrim( $path, '/' ) ), '/' );
$path = '/v1/' . $path;
return new Request(
$method,
OllamaProvider::url( $path ),
$headers,
$data,
$request_options
);
}
/**
* Prepares request options for text generation with a longer default timeout.
*
* Supported custom options:
* - ollama.request_timeout (seconds)
* - ollama.connect_timeout (seconds)
*
* @since x.x.x
*
* @return \WordPress\AiClient\Providers\Http\DTO\RequestOptions Prepared request options.
*/
private function prepareRequestOptionsForTextGeneration(): RequestOptions {
return $this->prepareRequestOptions( 60.0, 10.0 );
}
}