keganhollern nsarrazin HF Staff commited on
Commit
3360a5d
·
unverified ·
1 Parent(s): 4c06898

feat(openai): added support for o1 reasoning models (#1618)

Browse files

* fix(openai): systemRoleSupported model configuration for openai endpoints

* feat(openai): max_completion_tokens now used over max_tokens for chat_completeions endpoint.

* fix: lint

* feat(docs): add o1 example

* fix: make parameter default to false and fix type checks

---------

Co-authored-by: Nathan Sarrazin <[email protected]>

docs/source/configuration/models/providers/openai.md CHANGED
@@ -53,6 +53,25 @@ MODELS=`[{
53
  }]`
54
  ```
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  You may also consume any model provider that provides compatible OpenAI API endpoint. For example, you may self-host [Portkey](https://github.com/Portkey-AI/gateway) gateway and experiment with Claude or GPTs offered by Azure OpenAI. Example for Claude from Anthropic:
57
 
58
  ```ini
 
53
  }]`
54
  ```
55
 
56
+ We also support models in the `o1` family. You need to add a few more options ot the config: Here is an example for `o1-mini`:
57
+
58
+ ```ini
59
+ MODELS=`[
60
+ {
61
+ "name": "o1-mini",
62
+ "description": "ChatGPT o1-mini",
63
+ "systemRoleSupported": false,
64
+ "parameters": {
65
+ "max_new_tokens": 2048,
66
+ },
67
+ "endpoints" : [{
68
+ "type": "openai",
69
+ "useCompletionTokens": true,
70
+ }]
71
+ }
72
+ ]
73
+ ```
74
+
75
  You may also consume any model provider that provides compatible OpenAI API endpoint. For example, you may self-host [Portkey](https://github.com/Portkey-AI/gateway) gateway and experiment with Claude or GPTs offered by Azure OpenAI. Example for Claude from Anthropic:
76
 
77
  ```ini
src/lib/server/endpoints/openai/endpointOai.ts CHANGED
@@ -111,6 +111,8 @@ export const endpointOAIParametersSchema = z.object({
111
  }),
112
  })
113
  .default({}),
 
 
114
  });
115
 
116
  export async function endpointOai(
@@ -125,6 +127,7 @@ export async function endpointOai(
125
  defaultQuery,
126
  multimodal,
127
  extraBody,
 
128
  } = endpointOAIParametersSchema.parse(input);
129
 
130
  let OpenAI;
@@ -199,6 +202,14 @@ export async function endpointOai(
199
  messagesOpenAI[0].content = preprompt ?? "";
200
  }
201
 
 
 
 
 
 
 
 
 
202
  if (toolResults && toolResults.length > 0) {
203
  const toolCallRequests: OpenAI.Chat.Completions.ChatCompletionAssistantMessageParam = {
204
  role: "assistant",
@@ -241,7 +252,9 @@ export async function endpointOai(
241
  model: model.id ?? model.name,
242
  messages: messagesOpenAI,
243
  stream: true,
244
- max_tokens: parameters?.max_new_tokens,
 
 
245
  stop: parameters?.stop,
246
  temperature: parameters?.temperature,
247
  top_p: parameters?.top_p,
 
111
  }),
112
  })
113
  .default({}),
114
+ /* enable use of max_completion_tokens in place of max_tokens */
115
+ useCompletionTokens: z.boolean().default(false),
116
  });
117
 
118
  export async function endpointOai(
 
127
  defaultQuery,
128
  multimodal,
129
  extraBody,
130
+ useCompletionTokens,
131
  } = endpointOAIParametersSchema.parse(input);
132
 
133
  let OpenAI;
 
202
  messagesOpenAI[0].content = preprompt ?? "";
203
  }
204
 
205
+ // if system role is not supported, convert first message to a user message.
206
+ if (!model.systemRoleSupported && messagesOpenAI?.[0]?.role === "system") {
207
+ messagesOpenAI[0] = {
208
+ ...messagesOpenAI[0],
209
+ role: "user",
210
+ };
211
+ }
212
+
213
  if (toolResults && toolResults.length > 0) {
214
  const toolCallRequests: OpenAI.Chat.Completions.ChatCompletionAssistantMessageParam = {
215
  role: "assistant",
 
252
  model: model.id ?? model.name,
253
  messages: messagesOpenAI,
254
  stream: true,
255
+ ...(useCompletionTokens
256
+ ? { max_completion_tokens: parameters?.max_new_tokens }
257
+ : { max_tokens: parameters?.max_new_tokens }),
258
  stop: parameters?.stop,
259
  temperature: parameters?.temperature,
260
  top_p: parameters?.top_p,