| 7 | const completions_url = "https://api.openai.com/v1/completions" |
| 8 | |
| 9 | export async function testOpenAIAPI(OPENAI_API_KEY: string, model: string) { |
| 10 | // Create request |
| 11 | const base = { |
| 12 | model, |
| 13 | temperature, |
| 14 | max_tokens, |
| 15 | stream: true, |
| 16 | } |
| 17 | |
| 18 | let body |
| 19 | let url |
| 20 | |
| 21 | if (model == "gpt-4" || model == "gpt-3.5-turbo") { |
| 22 | url = chat_url |
| 23 | body = JSON.stringify({ |
| 24 | ...base, |
| 25 | messages: [ |
| 26 | { |
| 27 | role: "system", |
| 28 | content: "You are an expert in quantum mechanics & computing.", |
| 29 | }, |
| 30 | { |
| 31 | role: "user", |
| 32 | content: |
| 33 | "Can you please provide an extensive & in-depth overview of Quantum Cryptography, and the current research being done?", |
| 34 | }, |
| 35 | ], |
| 36 | }) |
| 37 | } else { |
| 38 | url = completions_url |
| 39 | body = JSON.stringify({ |
| 40 | ...base, |
| 41 | prompt: |
| 42 | "Can you please provide an extensive & in-depth overview of Quantum Cryptography, and the current research being done?", |
| 43 | }) |
| 44 | } |
| 45 | |
| 46 | // Send request |
| 47 | let generationAttempts = 0 |
| 48 | const maxGenerationAttempts = 3 |
| 49 | let finishReason = null |
| 50 | |
| 51 | let startDate |
| 52 | |
| 53 | let ttfb |
| 54 | let duration |
| 55 | |
| 56 | while (generationAttempts < maxGenerationAttempts) { |
| 57 | generationAttempts++ |
| 58 | |
| 59 | startDate = Date.now() |
| 60 | |
| 61 | const response = await fetch(url, { |
| 62 | method: "POST", |
| 63 | headers: { |
| 64 | "Content-Type": "application/json", |
| 65 | Authorization: `Bearer ${OPENAI_API_KEY}`, |
| 66 | }, |