Skip to content

Allow Usage of OpenAI Compatible APIs #2194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/client/src/pages/dashboard/settings/_sections/openai.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ const formSchema = z.object({
apiKey: z
.string()
// eslint-disable-next-line lingui/no-unlocalized-strings
.regex(/^sk-.+$/, "That doesn't look like a valid OpenAI API key.")
.min(1, "API key cannot be empty.") //allow api keys like hf-.. and gsk_..
.default(""),
Comment on lines +25 to 26
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance API key validation for better error handling.

While removing the strict sk- prefix check allows for different API providers, the current validation is too permissive. Consider adding format validation to catch obvious errors early.

 apiKey: z
   .string()
-  .min(1, "API key cannot be empty.") //allow api keys like hf-.. and gsk_..
+  .min(1, "API key cannot be empty.")
+  .max(256, "API key is too long")
+  .regex(
+    /^(sk-|hf-|gsk_).+$/,
+    "API key must start with 'sk-', 'hf-', or 'gsk_'"
+  )
   .default(""),
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
.min(1, "API key cannot be empty.") //allow api keys like hf-.. and gsk_..
.default(""),
.min(1, "API key cannot be empty.")
.max(256, "API key is too long")
.regex(
/^(sk-|hf-|gsk_).+$/,
"API key must start with 'sk-', 'hf-', or 'gsk_'"
)
.default(""),

baseURL: z
.string()
// eslint-disable-next-line lingui/no-unlocalized-strings
.regex(/^https?:\/\/[^/]+\/?$/, "That doesn't look like a valid URL")
.regex(/^https?:\/\/[^\s]+$/, "That doesn't look like a valid URL") //allow different openai compatible endpoints like https://api.groq.com/openai/v1 and https://api-inference.huggingface.co/v1/
.or(z.literal(""))
.default(""),
Comment on lines +30 to 32
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Strengthen URL validation for OpenAI-compatible endpoints.

The current URL validation can be improved in several ways:

  1. Optimize the regex as suggested by ESLint
  2. Add validation for common OpenAI-compatible paths
 baseURL: z
   .string()
-  .regex(/^https?:\/\/[^\s]+$/, "That doesn't look like a valid URL")
+  .regex(/^https?:\/\/\S+$/, "That doesn't look like a valid URL")
+  .refine(
+    (url) => !url || url.endsWith('/v1') || url.endsWith('/openai/v1'),
+    "URL should end with '/v1' or '/openai/v1'"
+  )
   .or(z.literal(""))
   .default(""),
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
.regex(/^https?:\/\/[^\s]+$/, "That doesn't look like a valid URL") //allow different openai compatible endpoints like https://api.groq.com/openai/v1 and https://api-inference.huggingface.co/v1/
.or(z.literal(""))
.default(""),
.regex(/^https?:\/\/\S+$/, "That doesn't look like a valid URL") //allow different openai compatible endpoints like https://api.groq.com/openai/v1 and https://api-inference.huggingface.co/v1/
.refine(
(url) => !url || url.endsWith('/v1') || url.endsWith('/openai/v1'),
"URL should end with '/v1' or '/openai/v1'"
)
.or(z.literal(""))
.default(""),
🧰 Tools
🪛 ESLint

[error] 30-30: /^https?://[^\s]+$/ can be optimized to /^https?://\S+$/.

(unicorn/better-regex)

model: z.string().default(DEFAULT_MODEL),
Expand Down