From ec4903f3236bd79f9df4b1383e9195593cd6c8cc Mon Sep 17 00:00:00 2001 From: vanguy765 Date: Sat, 29 Mar 2025 17:51:04 -0700 Subject: [PATCH] New Blank Copy of Repo --- src/handlers/custom-llm/basic.ts | 43 ++++++++++++++++------ src/handlers/custom-llm/openai-advanced.ts | 23 ++++++++++++ src/handlers/custom-llm/openai-sse.ts | 19 ++++++++++ 3 files changed, 73 insertions(+), 12 deletions(-) diff --git a/src/handlers/custom-llm/basic.ts b/src/handlers/custom-llm/basic.ts index 8a2d2f0..3e6f388 100644 --- a/src/handlers/custom-llm/basic.ts +++ b/src/handlers/custom-llm/basic.ts @@ -1,29 +1,46 @@ +/** + * Mock OpenAI Chat Completion API Handler + * + * This endpoint simulates the behavior of OpenAI's chat completion API. + * It's typically used during development and testing to avoid consuming + * real API credits. Instead of making actual API calls, it returns a + * mock response that mirrors OpenAI's response structure. + * + * @param {Request} req - Express request object containing chat parameters + * @param {Response} res - Express response object + * @returns {Promise} Responds with a mock chat completion + */ import { Request, Response } from 'express'; import OpenAI from 'openai'; import { envConfig } from '../../config/env.config'; +// Initialize OpenAI client with API key from environment config const openai = new OpenAI({ apiKey: envConfig.openai.apiKey }); export const basic = async (req: Request, res: Response) => { try { + // Destructure request body to get OpenAI API parameters const { - model, - messages, - max_tokens, - temperature, - stream, - call, - ...restParams + model, // The model to use (e.g., gpt-3.5-turbo) + messages, // Array of conversation messages + max_tokens, // Maximum tokens in response + temperature, // Randomness of response (0-1) + stream, // Whether to stream response + call, // Custom parameter + ...restParams // Catch any additional parameters } = req.body; + + // Construct mock response following OpenAI's response structure const response = { - id: 'chatcmpl-8mcLf78g0quztp4BMtwd3hEj58Uof', - object: 'chat.completion', - created: Math.floor(Date.now() / 1000), - model: 'gpt-3.5-turbo-0613', - system_fingerprint: null, + id: 'chatcmpl-8mcLf78g0quztp4BMtwd3hEj58Uof', // Mock completion ID + object: 'chat.completion', // Type of response + created: Math.floor(Date.now() / 1000), // Current timestamp in seconds + model: 'gpt-3.5-turbo-0613', // Hardcoded model version + system_fingerprint: null, // OpenAI's system identifier choices: [ { index: 0, + // Echo back the last message's content or empty string if no messages delta: { content: messages?.[messages.length - 1]?.content ?? '' }, logprobs: null, finish_reason: 'stop', @@ -31,8 +48,10 @@ export const basic = async (req: Request, res: Response) => { ], }; + // Return mock response with 201 Created status res.status(201).json(response); } catch (e) { + // Log any errors and return 500 Internal Server Error console.log(e); res.status(500).json({ error: e }); } diff --git a/src/handlers/custom-llm/openai-advanced.ts b/src/handlers/custom-llm/openai-advanced.ts index dd2a8cd..123cd62 100644 --- a/src/handlers/custom-llm/openai-advanced.ts +++ b/src/handlers/custom-llm/openai-advanced.ts @@ -4,6 +4,23 @@ import { envConfig } from '../../config/env.config'; const openai = new OpenAI({ apiKey: envConfig.openai.apiKey }); +/** + * Express handler for enhanced OpenAI chat completions with prompt improvement + * This endpoint is used when: + * 1. You want to automatically enhance user prompts for better results + * 2. You need the flexibility of both streaming and non-streaming responses + * + * The handler first processes the original prompt through GPT-3.5-turbo-instruct + * to create a more detailed version, then uses this enhanced prompt for the final completion. + * + * @param req - Express request object containing: + * - model: OpenAI model to use (defaults to gpt-3.5-turbo) + * - messages: Array of chat messages + * - max_tokens: Maximum tokens to generate (defaults to 150) + * - temperature: Sampling temperature (defaults to 0.7) + * - stream: Boolean flag for streaming mode + * @param res - Express response object for returning completion data + */ export const openaiAdvanced = async (req: Request, res: Response) => { try { const { @@ -16,7 +33,10 @@ export const openaiAdvanced = async (req: Request, res: Response) => { ...restParams } = req.body; + // Extract the last message from the conversation const lastMessage = messages?.[messages.length - 1]; + + // Generate an enhanced version of the prompt using GPT-3.5-turbo-instruct const prompt = await openai.completions.create({ model: 'gpt-3.5-turbo-instruct', prompt: ` @@ -28,12 +48,14 @@ export const openaiAdvanced = async (req: Request, res: Response) => { temperature: 0.7, }); + // Create a new message array with the enhanced prompt const modifiedMessage = [ ...messages.slice(0, messages.length - 1), { ...lastMessage, content: prompt.choices[0].text }, ]; if (stream) { + // Handle streaming mode with enhanced prompt const completionStream = await openai.chat.completions.create({ model: model || 'gpt-3.5-turbo', ...restParams, @@ -51,6 +73,7 @@ export const openaiAdvanced = async (req: Request, res: Response) => { } res.end(); } else { + // Handle non-streaming mode with enhanced prompt const completion = await openai.chat.completions.create({ model: model || 'gpt-3.5-turbo', ...restParams, diff --git a/src/handlers/custom-llm/openai-sse.ts b/src/handlers/custom-llm/openai-sse.ts index 6741d66..409ff89 100644 --- a/src/handlers/custom-llm/openai-sse.ts +++ b/src/handlers/custom-llm/openai-sse.ts @@ -4,8 +4,23 @@ import { envConfig } from '../../config/env.config'; const openai = new OpenAI({ apiKey: envConfig.openai.apiKey }); +/** + * Express handler for OpenAI chat completions with Server-Sent Events (SSE) support + * This endpoint is typically used when: + * 1. You need real-time streaming responses from OpenAI's chat completions + * 2. You want to handle both streaming and non-streaming requests in one endpoint + * + * @param req - Express request object containing: + * - model: OpenAI model to use (defaults to gpt-3.5-turbo) + * - messages: Array of chat messages + * - max_tokens: Maximum tokens to generate (defaults to 150) + * - temperature: Sampling temperature (defaults to 0.7) + * - stream: Boolean flag for streaming mode + * @param res - Express response object for returning completion data + */ export const openaiSSE = async (req: Request, res: Response) => { try { + // Destructure request body to get configuration parameters const { model, messages, @@ -19,6 +34,7 @@ export const openaiSSE = async (req: Request, res: Response) => { console.log(req.body); if (stream) { + // Handle streaming mode - uses SSE for real-time responses const completionStream = await openai.chat.completions.create({ model: model || 'gpt-3.5-turbo', ...restParams, @@ -28,15 +44,18 @@ export const openaiSSE = async (req: Request, res: Response) => { stream: true, } as OpenAI.Chat.ChatCompletionCreateParamsStreaming); + // Set headers for SSE connection res.setHeader('Content-Type', 'text/event-stream'); res.setHeader('Cache-Control', 'no-cache'); res.setHeader('Connection', 'keep-alive'); + // Stream each chunk of the response to the client for await (const data of completionStream) { res.write(`data: ${JSON.stringify(data)}\n\n`); } res.end(); } else { + // Handle non-streaming mode - returns complete response at once const completion = await openai.chat.completions.create({ model: model || 'gpt-3.5-turbo', ...restParams,