-
Notifications
You must be signed in to change notification settings - Fork 3
Fix initialization error when only OPENAI_API_KEY is configured #2
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { NextRequest, NextResponse } from 'next/server'; | ||
| import { openai, createDirectOpenAI } from '@/lib/openai'; | ||
| import { openai, createDirectOpenAI, createAzureOpenAI } from '@/lib/openai'; | ||
| import { buildPrompt, getAllStyles } from '@/lib/styles'; | ||
| import { TransliterationStyle } from '@/types/transliteration'; | ||
|
|
||
|
|
@@ -25,11 +25,40 @@ export async function POST(req: NextRequest) { | |
| const userPrompt = reverse ? `Romanized: ${text}` : `${text}`; | ||
|
|
||
| let transliteration = ""; | ||
|
|
||
| // Check if we should use direct OpenAI for all requests | ||
| const useDirectOpenAI = !openai && process.env.OPENAI_API_KEY; | ||
|
|
||
| if (useDirectOpenAI) { | ||
| // Use direct OpenAI for all styles when Azure is not configured | ||
| const directClient = createDirectOpenAI(); | ||
| const model = style === TransliterationStyle.SHARIASOURCE && !reverse ? "gpt-4.1" : "gpt-4"; | ||
|
|
||
| const completion = await directClient.chat.completions.create({ | ||
| model, | ||
| temperature: 0, | ||
| messages: [ | ||
| { role: "system", content: systemPrompt }, | ||
| { role: "user", content: userPrompt } | ||
| ], | ||
| ...(style === TransliterationStyle.SHARIASOURCE && !reverse ? { | ||
| response_format: { "type": "text" }, | ||
| max_completion_tokens: 2048, | ||
| top_p: 1, | ||
| frequency_penalty: 0, | ||
| presence_penalty: 0 | ||
| } : {}) | ||
| }); | ||
|
|
||
| if (style === TransliterationStyle.SHARIASOURCE && !reverse) { | ||
| const result = completion.choices[0]?.message.content?.trim() ?? ""; | ||
| transliteration = style === TransliterationStyle.SHARIASOURCE && !reverse | ||
| ? result.replace(/\*/g, '') | ||
| : result; | ||
| } else if (style === TransliterationStyle.SHARIASOURCE && !reverse) { | ||
| // Try Azure OpenAI first, fallback to direct OpenAI if it fails | ||
| try { | ||
| const completion = await openai.chat.completions.create({ | ||
| const azureClient = openai || createAzureOpenAI(); | ||
| const completion = await azureClient.chat.completions.create({ | ||
|
Comment on lines
+60
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix: Same parameter issue applies to the Azure call. Suggest removing - const azureClient = openai || createAzureOpenAI();
+ const azureClient = openai || createAzureOpenAI();
const completion = await azureClient.chat.completions.create({
model: process.env.AZURE_4_1_DEPLOYMENT || "snapsolve-gpt4.1",
temperature: 0,
messages: [
{ role: "system", content: systemPrompt },
{ role: "user", content: userPrompt }
],
- response_format: {
- "type": "text"
- },
- max_completion_tokens: 2048,
+ // Default is text for Chat Completions; response_format can be omitted.
+ max_tokens: 2048,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0
});If you intend to leverage the newer Responses API for GPT‑4.1, we can refactor this call path to Also applies to: 68-75 🤖 Prompt for AI Agents |
||
| model: process.env.AZURE_4_1_DEPLOYMENT || "snapsolve-gpt4.1", | ||
| temperature: 0, | ||
| messages: [ | ||
|
|
@@ -81,7 +110,8 @@ export async function POST(req: NextRequest) { | |
| } | ||
| } else { | ||
| // Standard single call for other styles | ||
| const completion = await openai.chat.completions.create({ | ||
| const azureClient = openai || createAzureOpenAI(); | ||
| const completion = await azureClient.chat.completions.create({ | ||
| model: process.env.AZURE_4_1_DEPLOYMENT || "snapsolve-gpt4.1", | ||
| temperature: 0, | ||
| messages: [ | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,18 +1,24 @@ | ||||||||||||||
| import { AzureOpenAI, OpenAI } from 'openai'; | ||||||||||||||
|
|
||||||||||||||
| if (!process.env.AZURE_SECRET_KEY) { | ||||||||||||||
| throw new Error('Missing AZURE_SECRET_KEY environment variable'); | ||||||||||||||
| } | ||||||||||||||
| export const createAzureOpenAI = () => { | ||||||||||||||
| if (!process.env.AZURE_SECRET_KEY) { | ||||||||||||||
| throw new Error('Missing AZURE_SECRET_KEY environment variable'); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| if (!process.env.AZURE_ENDPOINT_URL) { | ||||||||||||||
| throw new Error('Missing AZURE_ENDPOINT_URL environment variable'); | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+3
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainMake Azure API version configurable (+ verify validity of '2024-10-21') Hard-coding the apiVersion can cause breakage if Azure changes requirements. Prefer an env override with a sane default. Also, please double-check that '2024-10-21' is a valid Azure OpenAI API version for your deployments. Apply this minimal change: return new AzureOpenAI({
apiKey: process.env.AZURE_SECRET_KEY,
endpoint: process.env.AZURE_ENDPOINT_URL,
- apiVersion: '2024-10-21',
+ apiVersion: process.env.AZURE_OPENAI_API_VERSION ?? '2024-10-21',
});If helpful, I can follow up with a small doc PR to mention AZURE_OPENAI_API_VERSION support in README. Optionally verify the latest stable API version: Also applies to: 12-17 🌐 Web query: 💡 Result: The latest generally available Azure OpenAI API version that supports GPT‑4.1 chat/completions and the Responses API is 2024-10-21[1][2]. This API version supersedes the previous GA version 2024-06-01 and is currently the stable release for production use in Azure AI Foundry. Key Details:
Code Example for Chat Completions: import os
from openai import AzureOpenAI
client = AzureOpenAI(
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
api_version="2024-10-21", # Latest GA
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT")
)
response = client.chat.completions.create(
model="gpt-4.1",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the largest mammal?"}
]
)
print(response.model_dump_json(indent=2))[2] Code Example for Responses API: import os
from openai import AzureOpenAI
client = AzureOpenAI(
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
api_version="2024-10-21", # Latest GA
azure_endpoint="https://YOUR-RESOURCE-NAME.openai.azure.com"
)
response = client.responses.create(
model="gpt-4.1-nano",
input="This is a test."
)
print(response.model_dump_json(indent=2))[1] Important Notes:
Summary Table
This means for stable, production Azure deployments of GPT‑4.1 with chat or Responses API, use API version 2024-10-21[1][2][9]. Citations:
Parameterize Azure OpenAI API Version Verified that 2024-10-21 is the current GA version supporting GPT-4.1 chat/completions and the Responses API. To guard against future API–version changes, let’s make it configurable via an environment variable: • Update lib/openai.ts (around lines 12–13): return new AzureOpenAI({
apiKey: process.env.AZURE_SECRET_KEY,
endpoint: process.env.AZURE_ENDPOINT_URL,
- apiVersion: '2024-10-21',
+ apiVersion: process.env.AZURE_OPENAI_API_VERSION ?? '2024-10-21',
});• Add validation for 🤖 Prompt for AI Agents |
||||||||||||||
|
|
||||||||||||||
| if (!process.env.AZURE_ENDPOINT_URL) { | ||||||||||||||
| throw new Error('Missing AZURE_ENDPOINT_URL environment variable'); | ||||||||||||||
| } | ||||||||||||||
| return new AzureOpenAI({ | ||||||||||||||
| apiKey: process.env.AZURE_SECRET_KEY, | ||||||||||||||
| endpoint: process.env.AZURE_ENDPOINT_URL, | ||||||||||||||
| apiVersion: '2024-10-21', | ||||||||||||||
| }); | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| export const openai = new AzureOpenAI({ | ||||||||||||||
| apiKey: process.env.AZURE_SECRET_KEY, | ||||||||||||||
| endpoint: process.env.AZURE_ENDPOINT_URL, | ||||||||||||||
| apiVersion: '2024-10-21', | ||||||||||||||
| }); | ||||||||||||||
| export const openai = process.env.AZURE_SECRET_KEY && process.env.AZURE_ENDPOINT_URL | ||||||||||||||
| ? createAzureOpenAI() | ||||||||||||||
| : null; | ||||||||||||||
|
|
||||||||||||||
| export const createDirectOpenAI = () => { | ||||||||||||||
| if (!process.env.OPENAI_API_KEY) { | ||||||||||||||
|
|
||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix:
max_completion_tokensis not a valid Chat Completions parameter; usemax_tokensFor the Chat Completions API, the correct parameter is
max_tokens. Usingmax_completion_tokensmay be ignored or cause errors depending on SDK/runtime.const completion = await directClient.chat.completions.create({ model, temperature: 0, messages: [ { role: "system", content: systemPrompt }, { role: "user", content: userPrompt } ], - ...(style === TransliterationStyle.SHARIASOURCE && !reverse ? { - response_format: { "type": "text" }, - max_completion_tokens: 2048, + ...(style === TransliterationStyle.SHARIASOURCE && !reverse ? { + // response_format is optional for Chat Completions; default is text. + max_tokens: 2048, top_p: 1, frequency_penalty: 0, presence_penalty: 0 } : {}) });Optional: Since default output is text for Chat Completions, you can remove
response_formatentirely here.📝 Committable suggestion
🤖 Prompt for AI Agents