|
| 1 | +/** |
| 2 | + * LLM API Module |
| 3 | + * Handles all communication with the ch.at API |
| 4 | + */ |
| 5 | + |
| 6 | +const API_URL = "https://ch.at/v1/chat/completions"; |
| 7 | + |
| 8 | +/** |
| 9 | + * Makes a chat completion request to the ch.at API |
| 10 | + * @param {string} prompt - The prompt to send to the API |
| 11 | + * @returns {Promise<string>} - The API response text |
| 12 | + * @throws {Error} - Throws error with message and optional httpStatus property |
| 13 | + */ |
| 14 | +async function llm(prompt) { |
| 15 | + const temperature = 0.65; |
| 16 | + const maxTokens = 300; |
| 17 | + const response = await fetch(API_URL, { |
| 18 | + method: "POST", |
| 19 | + headers: { "Content-Type": "application/json" }, |
| 20 | + body: JSON.stringify({ |
| 21 | + messages: [{ role: "user", content: prompt }], |
| 22 | + temperature, |
| 23 | + max_tokens: maxTokens, |
| 24 | + }), |
| 25 | + }); |
| 26 | + |
| 27 | + if (!response.ok) { |
| 28 | + const errorData = await response |
| 29 | + .json() |
| 30 | + .catch(() => ({ error: { message: response.statusText } })); |
| 31 | + const err = new Error( |
| 32 | + errorData.error?.message || `HTTP error ${response.status}`, |
| 33 | + ); |
| 34 | + err.httpStatus = response.status; |
| 35 | + throw err; |
| 36 | + } |
| 37 | + |
| 38 | + const data = await response.json(); |
| 39 | + const textResponse = data.choices?.[0]?.message?.content; |
| 40 | + |
| 41 | + if (!textResponse) { |
| 42 | + throw new Error("Could not parse response from API"); |
| 43 | + } |
| 44 | + |
| 45 | + return textResponse; |
| 46 | +} |
| 47 | + |
| 48 | +export async function getSynonyms(word, count = 8) { |
| 49 | + const prompt = `You are a sophisticated thesaurus that provides ${count} diverse alternatives for any input. |
| 50 | +
|
| 51 | +Input: "${word}" |
| 52 | +
|
| 53 | +Instructions: |
| 54 | +1. If the input is a SINGLE WORD: |
| 55 | + - Provide ${count} diverse synonyms |
| 56 | + - Mix common everyday words and rare archaic words |
| 57 | +
|
| 58 | + <example> |
| 59 | + "happy": |
| 60 | + Joyful |
| 61 | + Elated |
| 62 | + Euphoric |
| 63 | + Blissful |
| 64 | + Cheerful |
| 65 | + Content |
| 66 | + Jubilant |
| 67 | + Exultant |
| 68 | + </example> |
| 69 | +
|
| 70 | +2. If the input is a PHRASE or MULTIPLE WORDS: |
| 71 | + - Provide ${count} synonymous phrases or expressions |
| 72 | +
|
| 73 | + <example> |
| 74 | + "break down": |
| 75 | + Fall apart |
| 76 | + Collapse |
| 77 | + Deteriorate |
| 78 | + Malfunction |
| 79 | + Disintegrate |
| 80 | + Deconstruct |
| 81 | + Breach |
| 82 | + Dissolve |
| 83 | + </example> |
| 84 | +
|
| 85 | +Always return exactly ${count} synonyms, newline separated. Your response will be interpreted by a script, so include only the newline-separated list without any other text. Do not use commas, asterisks, or dashes.`; |
| 86 | + |
| 87 | + return await llm(prompt); |
| 88 | +} |
| 89 | + |
| 90 | +export async function getDefinition(word) { |
| 91 | + const prompt = `You are a comprehensive dictionary that defines any input intelligently. |
| 92 | +
|
| 93 | +Input: "${word}" |
| 94 | +
|
| 95 | +Instructions: |
| 96 | +1. If the input is a REAL WORD: |
| 97 | + - Provide (part of speech) followed by concise definition |
| 98 | + - For multiple meanings, separate with semicolons |
| 99 | + - Use clear, accessible language |
| 100 | +
|
| 101 | +2. If the input is a PHRASE: |
| 102 | + - Provide (phrase) followed by explanation of meaning/usage |
| 103 | + - Focus on the overall concept or idiom meaning |
| 104 | +
|
| 105 | +Examples: |
| 106 | +"bank" → "(noun) A financial institution; the edge of a river" |
| 107 | +"sprint" → "(verb) To run at full speed over a short distance; (noun) A short, fast run" |
| 108 | +"break down" → "(phrase) To stop functioning; to analyze in detail" |
| 109 | +
|
| 110 | +Return only the definition without introductory text.`; |
| 111 | + |
| 112 | + return await llm(prompt); |
| 113 | +} |
| 114 | + |
| 115 | +export function formatError(error) { |
| 116 | + if ( |
| 117 | + error.name === "TypeError" && |
| 118 | + error.message.toLowerCase().includes("fetch") |
| 119 | + ) { |
| 120 | + return "Network error. Please check your internet connection."; |
| 121 | + } |
| 122 | + |
| 123 | + if (error.httpStatus) { |
| 124 | + return `API Error (${error.httpStatus}): ${error.message}`; |
| 125 | + } |
| 126 | + |
| 127 | + return `Error: ${error.message || "Unknown API error."}`; |
| 128 | +} |
0 commit comments