Source file openrouter_options.ml
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
open Melange_json.Primitives
(** Reasoning effort levels matching upstream OpenRouter API. *)
type reasoning_effort =
| Xhigh
| High
| Medium
| Low
| Minimal
| None_
type reasoning_config = {
enabled : bool option;
exclude : bool option;
budget : reasoning_budget;
}
and reasoning_budget =
| Max_tokens of int
| Effort of reasoning_effort
| No_budget
type cache_control = {
type_ : string;
ttl : string option;
}
type debug_config = { echo_upstream_body : bool option }
type web_search_options = {
max_results : int option;
search_prompt : string option;
engine : string option;
search_context_size : string option;
}
type usage_config = { include_ : bool }
type max_price = {
prompt : float option;
completion : float option;
image : float option;
audio : float option;
request : float option;
}
type throughput_percentile = {
percentile : float;
min_provider_count : int option;
}
type latency_percentile = {
percentile : float;
max_provider_count : int option;
}
type throughput_preference =
| Throughput_value of float
| Throughput_percentile of throughput_percentile
type latency_preference =
| Latency_value of float
| Latency_percentile of latency_percentile
type provider_prefs = {
order : string list;
allow_fallbacks : bool option;
require_parameters : bool option;
data_collection : string option;
only : string list;
ignore_ : string list;
quantizations : string list;
sort : string option;
max_price : max_price option;
zdr : bool option;
preferred_min_throughput : throughput_preference option;
preferred_max_latency : latency_preference option;
enforce_distillable_text : bool option;
}
type plugin =
| Web_search of web_search_plugin_config option
| File_parser of file_parser_plugin_config option
| Auto_router of auto_router_plugin_config option
| Moderation
| Response_healing
and web_search_plugin_config = {
max_results : int option;
search_prompt : string option;
engine : string option;
include_domains : string list;
exclude_domains : string list;
}
and file_parser_plugin_config = {
max_files : int option;
pdf_engine : string option;
}
and auto_router_plugin_config = { allowed_models : string list }
type t = {
models : string list;
logit_bias : (int * float) list;
logprobs : [ `Bool of bool | `Int of int ] option;
parallel_tool_calls : bool option;
user : string option;
reasoning : reasoning_config option;
include_reasoning : bool option;
plugins : plugin list;
web_search_options : web_search_options option;
provider : provider_prefs option;
cache_control : cache_control option;
debug : debug_config option;
usage : usage_config option;
extra_body : (string * Yojson.Basic.t) list;
strict_json_schema : bool;
system_message_mode : Model_catalog.system_message_mode option;
}
let default =
{
models = [];
logit_bias = [];
logprobs = None;
parallel_tool_calls = None;
user = None;
reasoning = None;
include_reasoning = None;
plugins = [];
web_search_options = None;
provider = None;
cache_control = None;
debug = None;
usage = None;
extra_body = [];
strict_json_schema = true;
system_message_mode = None;
}
type _ Ai_provider.Provider_options.key += Openrouter : t Ai_provider.Provider_options.key
let to_provider_options opts = Ai_provider.Provider_options.set Openrouter opts Ai_provider.Provider_options.empty
let of_provider_options opts = Ai_provider.Provider_options.find Openrouter opts
let json_strings ss = `List (List.map (fun s -> `String s) ss)
let reasoning_effort_to_string = function
| Xhigh -> "xhigh"
| High -> "high"
| Medium -> "medium"
| Low -> "low"
| Minimal -> "minimal"
| None_ -> "none"
let reasoning_config_to_json (rc : reasoning_config) =
let fields = [] in
let fields =
match rc.enabled with
| Some b -> ("enabled", `Bool b) :: fields
| None -> fields
in
let fields =
match rc.exclude with
| Some b -> ("exclude", `Bool b) :: fields
| None -> fields
in
let fields =
match rc.budget with
| Max_tokens n -> ("max_tokens", `Int n) :: fields
| Effort e -> ("effort", `String (reasoning_effort_to_string e)) :: fields
| No_budget -> fields
in
`Assoc (List.rev fields)
let cache_control_to_json (cc : cache_control) =
let fields = [ "type", `String cc.type_ ] in
let fields =
match cc.ttl with
| Some ttl -> fields @ [ "ttl", `String ttl ]
| None -> fields
in
`Assoc fields
let debug_config_to_json (dc : debug_config) =
match dc.echo_upstream_body with
| Some b -> `Assoc [ "echo_upstream_body", `Bool b ]
| None -> `Assoc []
let web_search_options_to_json (wso : web_search_options) =
let fields = [] in
let fields =
match wso.max_results with
| Some n -> ("max_results", `Int n) :: fields
| None -> fields
in
let fields =
match wso.search_prompt with
| Some s -> ("search_prompt", `String s) :: fields
| None -> fields
in
let fields =
match wso.engine with
| Some e -> ("engine", `String e) :: fields
| None -> fields
in
let fields =
match wso.search_context_size with
| Some s -> ("search_context_size", `String s) :: fields
| None -> fields
in
`Assoc (List.rev fields)
let usage_config_to_json (uc : usage_config) = `Assoc [ "include", `Bool uc.include_ ]
let max_price_to_json (mp : max_price) =
let add_opt name value fields =
match value with
| Some v -> (name, `Float v) :: fields
| None -> fields
in
let fields =
[]
|> add_opt "prompt" mp.prompt
|> add_opt "completion" mp.completion
|> add_opt "image" mp.image
|> add_opt "audio" mp.audio
|> add_opt "request" mp.request
in
`Assoc (List.rev fields)
let provider_prefs_to_json (pp : provider_prefs) =
let fields = [] in
let fields =
match pp.order with
| [] -> fields
| order -> ("order", json_strings order) :: fields
in
let fields =
match pp.allow_fallbacks with
| Some b -> ("allow_fallbacks", `Bool b) :: fields
| None -> fields
in
let fields =
match pp.require_parameters with
| Some b -> ("require_parameters", `Bool b) :: fields
| None -> fields
in
let fields =
match pp.data_collection with
| Some s -> ("data_collection", `String s) :: fields
| None -> fields
in
let fields =
match pp.only with
| [] -> fields
| only -> ("only", json_strings only) :: fields
in
let fields =
match pp.ignore_ with
| [] -> fields
| ignore_ -> ("ignore", json_strings ignore_) :: fields
in
let fields =
match pp.quantizations with
| [] -> fields
| qs -> ("quantizations", json_strings qs) :: fields
in
let fields =
match pp.sort with
| Some s -> ("sort", `String s) :: fields
| None -> fields
in
let fields =
match pp.max_price with
| Some mp -> ("max_price", max_price_to_json mp) :: fields
| None -> fields
in
let fields =
match pp.zdr with
| Some b -> ("zdr", `Bool b) :: fields
| None -> fields
in
let fields =
match pp.preferred_min_throughput with
| Some (Throughput_value v) -> ("preferred_min_throughput", `Float v) :: fields
| Some (Throughput_percentile { percentile; min_provider_count }) ->
let pf = [ "percentile", `Float percentile ] in
let pf =
match min_provider_count with
| Some n -> pf @ [ "min_provider_count", `Int n ]
| None -> pf
in
("preferred_min_throughput", `Assoc pf) :: fields
| None -> fields
in
let fields =
match pp.preferred_max_latency with
| Some (Latency_value v) -> ("preferred_max_latency", `Float v) :: fields
| Some (Latency_percentile { percentile; max_provider_count }) ->
let pf = [ "percentile", `Float percentile ] in
let pf =
match max_provider_count with
| Some n -> pf @ [ "max_provider_count", `Int n ]
| None -> pf
in
("preferred_max_latency", `Assoc pf) :: fields
| None -> fields
in
let fields =
match pp.enforce_distillable_text with
| Some b -> ("enforce_distillable_text", `Bool b) :: fields
| None -> fields
in
`Assoc (List.rev fields)
let plugin_to_json = function
| Web_search None -> `Assoc [ "id", `String "web" ]
| Web_search (Some config) ->
let fields = [ "id", `String "web" ] in
let fields =
match config.max_results with
| Some n -> fields @ [ "max_results", `Int n ]
| None -> fields
in
let fields =
match config.search_prompt with
| Some s -> fields @ [ "search_prompt", `String s ]
| None -> fields
in
let fields =
match config.engine with
| Some e -> fields @ [ "engine", `String e ]
| None -> fields
in
let fields =
match config.include_domains with
| [] -> fields
| ds -> fields @ [ "include_domains", json_strings ds ]
in
let fields =
match config.exclude_domains with
| [] -> fields
| ds -> fields @ [ "exclude_domains", json_strings ds ]
in
`Assoc fields
| File_parser None -> `Assoc [ "id", `String "file-parser" ]
| File_parser (Some config) ->
let fields = [ "id", `String "file-parser" ] in
let fields =
match config.max_files with
| Some n -> fields @ [ "max_files", `Int n ]
| None -> fields
in
let fields =
match config.pdf_engine with
| Some e -> fields @ [ "pdf", `Assoc [ "engine", `String e ] ]
| None -> fields
in
`Assoc fields
| Auto_router None -> `Assoc [ "id", `String "auto-router" ]
| Auto_router (Some config) ->
let fields = [ "id", `String "auto-router" ] in
let fields =
match config.allowed_models with
| [] -> fields
| models -> fields @ [ "allowed_models", json_strings models ]
in
`Assoc fields
| Moderation -> `Assoc [ "id", `String "moderation" ]
| Response_healing -> `Assoc [ "id", `String "response-healing" ]
let plugins_to_json plugins = List.map plugin_to_json plugins
let logit_bias_to_json bias = `Assoc (List.map (fun (token_id, value) -> string_of_int token_id, `Float value) bias)
(** Returns [(logprobs_json, top_logprobs_json option)].
[`Bool b] -> [(`Bool b, None)];
[`Int n] -> [(`Bool true, Some (`Int n))]. *)
let logprobs_to_json = function
| `Bool b -> `Bool b, None
| `Int n -> `Bool true, Some (`Int n)