-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChatRequest.php
More file actions
157 lines (139 loc) · 5.99 KB
/
Copy pathChatRequest.php
File metadata and controls
157 lines (139 loc) · 5.99 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
declare(strict_types=1);
namespace fholbrook\Openrouter\DTO;
final class ChatRequest
{
public function __construct(
/**
* Chat Object. When provided prompt is ignored.
*
* @var Chat|null
*/
public ?Chat $chat = null,
/**
* Prompt string data. If "chat" is specified, this is ignored.
*
* @var string|null
*/
public ?string $prompt = null,
/**
* Model name. If "model" is unspecified, uses the user's default.
* For more info: https://openrouter.ai/docs#models
*
* @var string|null
*/
public ?string $model = null,
/**
* The format of the output, e.g. json, text, srt, verbose_json ...
*
* @var ResponseFormat|null
*/
public ?ResponseFormat $responseFormat = null,
/**
* Stop generation immediately if the model encounters any token specified in the stop array|string.
*
* @var array|string|null
*/
public array|string|null $stop = null,
/**
* Enable streaming.
*
* @var bool|null
*/
public ?bool $stream = null,
/**
* See LLM Parameters (https://openrouter.ai/docs#parameters) for following:
*/
public ?int $maxTokens = 1024, // Range: [1, context_length) The maximum number of tokens that can be generated in the completion. Default 1024.
public ?float $temperature = null, // Range: [0, 2] Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
public ?float $topP = null, // Range: (0, 1] An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass.
public ?float $topK = null, // Range: [1, Infinity) Not available for OpenAI models
public ?float $frequencyPenalty = null, // Range: [-2, 2] Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.
public ?float $presencePenalty = null, // Range: [-2, 2] Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.
public ?float $repetitionPenalty = null, // Range: (0, 2]
public ?int $seed = null, // OpenAI only. This feature is in Beta. If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result.
// Function-calling
/**
* Only natively supported by OpenAI models. For others, we submit a YAML-formatted string with these tools at the end of the prompt.
*
* @var string|array|null
*/
public string|array|null $toolChoice = null, // none|auto or ToolCallData as {"type": "function", "function": {"name": "my_function"}}
/**
* Tool calls (also known as function calling) allow you to give an LLM access to external tools.
*
* @var ToolCall[]|null
*/
public ?array $tools = null,
// Additional optional parameters
/**
* Modify the likelihood of specified tokens appearing in the completion. e.g. {"50256": -100}
*/
public ?array $logitBias = null,
// OpenRouter-only parameters
/**
* See "Prompt Transforms" section: https://openrouter.ai/docs#transforms
*
* @var array|null
*/
public ?array $transforms = null,
/**
* The models array, which lets you automatically try other models if the primary model's providers are down,
* rate-limited, or refuse to reply due to content moderation required by all providers.
*
* @var array|null
*/
public ?array $models = null,
/**
* @var string|null
*/
public ?string $route = null,
/**
* See "Provider Routing" section: https://openrouter.ai/docs#provider-routing
*
* @var ProviderPreferences|null
*/
public ?ProviderPreferences $provider = null,
/**
* Enable think tokens.
* Default: false
*
* @var bool|null
*/
public ?bool $includeReasoning = false,
)
{
}
public function toArray(bool $includeMeta = false): array
{
if ($this->chat && count($this->chat->messages)>0) {
$this->prompt = null;
}
return array_filter(
[
'messages' => ($this->chat && count($this->chat->messages)>0) ? $this->chat->toArray($includeMeta)['messages'] : null,
'prompt' => $this->prompt,
'model' => $this->model,
'responseFormat' => $this->responseFormat ? $this->responseFormat->toArray() : null,
'stop' => $this->stop,
'stream' => $this->stream,
'maxTokens' => $this->maxTokens,
'temperature' => $this->temperature,
'topP' => $this->topP,
'topK' => $this->topK,
'frequencyPenalty' => $this->frequencyPenalty,
'presencePenalty' => $this->presencePenalty,
'repetitionPenalty' => $this->repetitionPenalty,
'seed' => $this->seed,
'toolChoice' => $this->toolChoice,
'tools' => $this->tools ? array_map(fn($tool) => $tool->toArray(), $this->tools) : null,
'logitBias' => $this->logitBias,
'transforms' => $this->transforms,
'models' => $this->models,
'route' => $this->route,
'provider' => $this->provider ? $this->provider->toArray() : null,
'includeReasoning' => $this->includeReasoning,
]
);
}
}