From cf365cd2a891e960d543580bfe4daf2b329182ed Mon Sep 17 00:00:00 2001 From: Bill DiStefano Date: Wed, 26 Nov 2025 11:47:10 -0500 Subject: [PATCH] support for OpenAI API compatible endpoints --- apps/promptions-chat/.env.example | 10 +++++++++- apps/promptions-chat/src/services/ChatService.ts | 8 ++++++-- apps/promptions-image/.env.example | 10 +++++++++- apps/promptions-image/src/services/ImageService.ts | 6 +++++- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/apps/promptions-chat/.env.example b/apps/promptions-chat/.env.example index 19d97c9..383181f 100644 --- a/apps/promptions-chat/.env.example +++ b/apps/promptions-chat/.env.example @@ -1,2 +1,10 @@ -# Copy this file to .env and add your OpenAI API key +# Copy this file to .env + +# Required: Set your API key VITE_OPENAI_API_KEY=your_openai_api_key_here + +# Optional: Set base URL for the OpenAI-compatible API endpoint (if blank, defaults to OpenAI API endpoint) +# VITE_OPENAI_BASE_URL= + +# Optional: Set model to use (if blank, defaults to gpt-3.5-turbo) +# VITE_OPENAI_MODEL= diff --git a/apps/promptions-chat/src/services/ChatService.ts b/apps/promptions-chat/src/services/ChatService.ts index a60c3f4..ebf5d0c 100644 --- a/apps/promptions-chat/src/services/ChatService.ts +++ b/apps/promptions-chat/src/services/ChatService.ts @@ -7,11 +7,14 @@ 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 // For development, you can set VITE_OPENAI_API_KEY in your .env file const apiKey = import.meta.env.VITE_OPENAI_API_KEY || process.env.OPENAI_API_KEY; + const baseURL = import.meta.env.VITE_OPENAI_BASE_URL || process.env.OPENAI_BASE_URL; + this.model = import.meta.env.VITE_OPENAI_MODEL || process.env.OPENAI_MODEL || "gpt-3.5-turbo"; if (!apiKey) { throw new Error( @@ -21,6 +24,7 @@ export class ChatService { this.client = new OpenAI({ apiKey, + baseURL, dangerouslyAllowBrowser: true, // Only for demo purposes - use a backend in production }); } @@ -35,7 +39,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, @@ -64,7 +68,7 @@ export class ChatService { async sendMessage(messages: ChatMessage[]): Promise { 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, diff --git a/apps/promptions-image/.env.example b/apps/promptions-image/.env.example index 19d97c9..383181f 100644 --- a/apps/promptions-image/.env.example +++ b/apps/promptions-image/.env.example @@ -1,2 +1,10 @@ -# Copy this file to .env and add your OpenAI API key +# Copy this file to .env + +# Required: Set your API key VITE_OPENAI_API_KEY=your_openai_api_key_here + +# Optional: Set base URL for the OpenAI-compatible API endpoint (if blank, defaults to OpenAI API endpoint) +# VITE_OPENAI_BASE_URL= + +# Optional: Set model to use (if blank, defaults to gpt-3.5-turbo) +# VITE_OPENAI_MODEL= diff --git a/apps/promptions-image/src/services/ImageService.ts b/apps/promptions-image/src/services/ImageService.ts index 533e32f..3a668c0 100644 --- a/apps/promptions-image/src/services/ImageService.ts +++ b/apps/promptions-image/src/services/ImageService.ts @@ -3,9 +3,12 @@ import { ImageGenerationParams, GeneratedImage } from "../types"; export class ImageService { private client: OpenAI; + private chatModel: string; constructor() { const apiKey = import.meta.env.VITE_OPENAI_API_KEY || process.env.OPENAI_API_KEY; + const baseURL = import.meta.env.VITE_OPENAI_BASE_URL || process.env.OPENAI_BASE_URL; + this.chatModel = import.meta.env.VITE_OPENAI_MODEL || process.env.OPENAI_MODEL || "gpt-3.5-turbo"; if (!apiKey) { throw new Error( @@ -15,6 +18,7 @@ export class ImageService { this.client = new OpenAI({ apiKey, + baseURL, dangerouslyAllowBrowser: true, // Only for demo purposes - use a backend in production }); } @@ -61,7 +65,7 @@ export class ImageService { try { const stream = await this.client.chat.completions.create( { - model: "gpt-4.1", + model: this.chatModel, messages: messages as OpenAI.Chat.Completions.ChatCompletionMessageParam[], stream: true, temperature: 0.7,