#!/usr/bin/env node import { AxAI, AxAIOpenAIResponsesModel, AxGen, AxSignature } from '@ax-llm/ax'; // Mathematical reasoning example with o3 async function runMathExample() { console.log('šŸ“ Example 1: Mathematical reasoning with o3'); console.log('----------------------------------------'); try { const ai = new AxAI({ name: 'openai-responses', apiKey: process.env.OPENAI_APIKEY || '', config: { model: AxAIOpenAIResponsesModel.O3, reasoningEffort: 'medium', temperature: 0.7, stream: false, }, }); console.log(`šŸ¤– Using model: ${AxAIOpenAIResponsesModel.O3}`); console.log('šŸ”§ Reasoning effort: medium'); const signature = new AxSignature( `question:string -> answer:string "step-by-step solution", thought:string "reasoning process", usage:string "model usage stats"` ); const gen = new AxGen< { question: string }, { answer: string; thought?: string; usage?: string } >(signature); const result = await gen.forward(ai, { question: 'Solve this step by step: If a train travels 120 km in 1.5 hours, and then increases its speed by 20 km/h for the next 2 hours, what is the total distance traveled?', }); console.log('āœ… Response:'); console.log(result.answer); if (result.thought) { console.log('\n🧠 Reasoning process:'); console.log(result.thought); } console.log('\nšŸ“Š Usage:'); if (result.usage) { console.log(result.usage); } else { console.log(' Usage stats not available'); } } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : String(error); if (errorMessage.includes('400') || errorMessage.includes('Bad Request')) { console.log('āš ļø o3 model not yet available on this API key'); console.log(' This is expected - o3 is in limited preview'); } else { console.error('āŒ Error:', errorMessage); } } } // Code generation example with o4-mini async function runCodeExample() { console.log('\nšŸ“ Example 2: Code generation with o4-mini'); console.log('-------------------------------------------'); try { const ai = new AxAI({ name: 'openai-responses', apiKey: process.env.OPENAI_APIKEY || '', config: { model: AxAIOpenAIResponsesModel.O4Mini, reasoningEffort: 'low', temperature: 0.3, stream: false, }, }); console.log(`šŸ¤– Using model: ${AxAIOpenAIResponsesModel.O4Mini}`); console.log('šŸ”§ Reasoning effort: low'); const signature = new AxSignature( `task:string -> code:string "typescript function", explanation:string "how it works", thought:string "reasoning process"` ); const gen = new AxGen< { task: string }, { code: string; explanation: string; thought?: string } >(signature); const result = await gen.forward(ai, { task: 'Create a TypeScript function that calculates the factorial of a number using recursion', }); console.log('āœ… Code generated:'); console.log(result.code); console.log('\nšŸ“– Explanation:'); console.log(result.explanation); if (result.thought) { console.log('\n🧠 Reasoning process:'); console.log(result.thought); } } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : String(error); if (errorMessage.includes('400') || errorMessage.includes('Bad Request')) { console.log('āš ļø o4-mini model not yet available on this API key'); console.log(' This is expected - o4 models are in limited preview'); } else { console.error('āŒ Error:', errorMessage); } } } // Logic reasoning example with o3-mini async function runLogicExample() { console.log('\nšŸ“ Example 3: Logic reasoning with o3-mini'); console.log('------------------------------------------'); try { const ai = new AxAI({ name: 'openai-responses', apiKey: process.env.OPENAI_APIKEY || '', config: { model: AxAIOpenAIResponsesModel.O3Mini, reasoningEffort: 'high', temperature: 0.1, stream: false, }, }); console.log(`šŸ¤– Using model: ${AxAIOpenAIResponsesModel.O3Mini}`); console.log('šŸ”§ Reasoning effort: high'); const signature = new AxSignature( `premise:string -> conclusion:string "logical deduction", confidence:string "high, medium, low", thought:string "reasoning process"` ); const gen = new AxGen< { premise: string }, { conclusion: string; confidence: string; thought?: string } >(signature); const result = await gen.forward(ai, { premise: 'All birds can fly. Penguins are birds. However, penguins cannot fly. What logical conclusion can we draw?', }); console.log('āœ… Logical conclusion:'); console.log(result.conclusion); console.log('\nšŸŽÆ Confidence:', result.confidence); if (result.thought) { console.log('\n🧠 Reasoning process:'); console.log(result.thought); } } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : String(error); if (errorMessage.includes('400') || errorMessage.includes('Bad Request')) { console.log('āš ļø o3-mini model not yet available on this API key'); console.log(' This is expected - o3 models are in limited preview'); } else { console.error('āŒ Error:', errorMessage); } } } async function main() { console.log('🧠 OpenAI Responses API with o3/o4 Models Example'); console.log('=================================================='); if (!process.env.OPENAI_APIKEY) { console.error('āŒ Please set OPENAI_APIKEY environment variable'); process.exit(1); } // Run all examples await runMathExample(); await runCodeExample(); await runLogicExample(); console.log('\nšŸŽ‰ Examples complete!'); console.log('\nā„¹ļø Available reasoning models:'); console.log(` • ${AxAIOpenAIResponsesModel.O3} - Advanced reasoning model`); console.log( ` • ${AxAIOpenAIResponsesModel.O3Mini} - Efficient reasoning model` ); console.log( ` • ${AxAIOpenAIResponsesModel.O4Mini} - Latest mini reasoning model` ); console.log('\nšŸ“š These models are currently in limited preview.'); console.log(' Contact OpenAI for access to o3/o4 models.'); } main().catch(console.error);