Skip to content
Closed
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions apps/promptions-chat/.env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
# Copy this file to .env and add your OpenAI API key
VITE_OPENAI_API_KEY=your_openai_api_key_here
# Optional: only set for Azure or other custom OpenAI-compatible endpoints.
# Omit for standard OpenAI API usage.
VITE_OPENAI_BASE_URL=your_openai_base_url_here
Comment thread
jack-williams marked this conversation as resolved.
# Optional: API version is typically Azure-specific/custom-endpoint specific.
# Omit for standard OpenAI API usage.
VITE_OPENAI_API_VERSION=2024-12-01-preview
VITE_OPENAI_MODEL=gpt-4.1-mini
12 changes: 10 additions & 2 deletions apps/promptions-chat/src/services/ChatService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface ChatMessage {

export class ChatService {
private client: OpenAI;
private model: string;

constructor() {
// In a real application, you'd want to handle the API key more securely
Expand All @@ -19,8 +20,15 @@ export class ChatService {
);
}

const baseURL = import.meta.env.VITE_OPENAI_BASE_URL || process.env.OPENAI_BASE_URL;
const apiVersion = import.meta.env.VITE_OPENAI_API_VERSION || process.env.OPENAI_API_VERSION;
this.model = import.meta.env.VITE_OPENAI_MODEL || process.env.OPENAI_MODEL || "gpt-4.1";
Comment on lines +23 to +25
Copy link

Copilot AI Apr 29, 2026

Choose a reason for hiding this comment

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

process.env.* fallbacks here will never resolve in this Vite app because vite.config.ts defines process.env as an empty object at build time. This makes the OPENAI_* fallbacks dead code and can mislead users into thinking Node-style env vars are supported; consider removing the process.env branches (or changing the Vite define strategy) and adjusting the error/help text accordingly.

Copilot uses AI. Check for mistakes.

this.client = new OpenAI({
apiKey,
...(baseURL ? { baseURL } : {}),
...(apiVersion ? { defaultQuery: { "api-version": apiVersion } } : {}),
...(baseURL ? { defaultHeaders: { "api-key": apiKey } } : {}),
Comment on lines +25 to +31
Copy link

Copilot AI Apr 29, 2026

Choose a reason for hiding this comment

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

defaultHeaders: { "api-key": apiKey } is Azure OpenAI–specific, but it's currently enabled for any custom baseURL. For non-Azure OpenAI-compatible endpoints, sending an api-key header can be incorrect/unexpected; consider gating the Azure header/query behavior behind a dedicated flag (or only enabling it when an Azure-specific setting like VITE_OPENAI_API_VERSION is provided) rather than tying it solely to baseURL.

Suggested change
this.model = import.meta.env.VITE_OPENAI_MODEL || process.env.OPENAI_MODEL || "gpt-4.1";
this.client = new OpenAI({
apiKey,
...(baseURL ? { baseURL } : {}),
...(apiVersion ? { defaultQuery: { "api-version": apiVersion } } : {}),
...(baseURL ? { defaultHeaders: { "api-key": apiKey } } : {}),
const isAzureOpenAI = Boolean(apiVersion);
this.model = import.meta.env.VITE_OPENAI_MODEL || process.env.OPENAI_MODEL || "gpt-4.1";
this.client = new OpenAI({
apiKey,
...(baseURL ? { baseURL } : {}),
...(isAzureOpenAI ? { defaultQuery: { "api-version": apiVersion } } : {}),
...(isAzureOpenAI ? { defaultHeaders: { "api-key": apiKey } } : {}),

Copilot uses AI. Check for mistakes.
dangerouslyAllowBrowser: true, // Only for demo purposes - use a backend in production
});
}
Expand All @@ -35,7 +43,7 @@ export class ChatService {
try {
const stream = await this.client.chat.completions.create(
{
model: "gpt-4.1",
model: this.model,
messages: messages as OpenAI.Chat.Completions.ChatCompletionMessageParam[],
stream: true,
temperature: 0.7,
Expand Down Expand Up @@ -64,7 +72,7 @@ export class ChatService {
async sendMessage(messages: ChatMessage[]): Promise<string> {
try {
const response = await this.client.chat.completions.create({
model: "gpt-3.5-turbo",
model: this.model,
messages: messages as OpenAI.Chat.Completions.ChatCompletionMessageParam[],
temperature: 0.7,
max_tokens: 1000,
Expand Down
3 changes: 3 additions & 0 deletions apps/promptions-chat/src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

interface ImportMetaEnv {
readonly VITE_OPENAI_API_KEY: string;
readonly VITE_OPENAI_BASE_URL?: string;
readonly VITE_OPENAI_API_VERSION?: string;
readonly VITE_OPENAI_MODEL?: string;
// more env variables...
}

Expand Down