(basePrompt: string, prompt: string, input: string, temperature = "1.0")
| 18 | * @returns The string output received from the model endpoint. |
| 19 | */ |
| 20 | export default async function runModel(basePrompt: string, prompt: string, input: string, temperature = "1.0") { |
| 21 | const preferences = getPreferenceValues<ExtensionPreferences>(); |
| 22 | |
| 23 | const preferenceModel: Model = { |
| 24 | endpoint: preferences.modelEndpoint, |
| 25 | authType: preferences.authType, |
| 26 | apiKey: preferences.apiKey, |
| 27 | inputSchema: preferences.inputSchema, |
| 28 | outputKeyPath: preferences.outputKeyPath, |
| 29 | outputTiming: preferences.outputTiming, |
| 30 | lengthLimit: preferences.lengthLimit, |
| 31 | temperature: temperature, |
| 32 | name: "", |
| 33 | description: "", |
| 34 | favorited: false, |
| 35 | id: "", |
| 36 | icon: "", |
| 37 | iconColor: "", |
| 38 | notes: "", |
| 39 | isDefault: false, |
| 40 | }; |
| 41 | |
| 42 | const fallbackModel = { ...RAYCAST_AI_FALLBACK_MODEL, temperature: temperature }; |
| 43 | |
| 44 | const temp = preferences.includeTemperature |
| 45 | ? parseFloat(temperature) == undefined |
| 46 | ? 1.0 |
| 47 | : parseFloat(temperature) |
| 48 | : 1.0; |
| 49 | |
| 50 | return Promise.resolve(LocalStorage.allItems()).then(async (items) => { |
| 51 | const models: Model[] = Object.entries(items) |
| 52 | .filter(([key]) => key.startsWith("--model-")) |
| 53 | .map(([, value]) => JSON.parse(value)); |
| 54 | |
| 55 | const defaultModel = models.find((model) => model.isDefault); |
| 56 | const targetModel = defaultModel || (preferenceModel.endpoint == "" ? fallbackModel : preferenceModel); |
| 57 | const raycastModel = |
| 58 | RAYCAST_AI_REPRESENTATIONS.includes(targetModel.endpoint.toLowerCase() as RaycastAIRepresentation) || |
| 59 | targetModel.endpoint == "" || |
| 60 | targetModel.apiKey == "N/A" || |
| 61 | preferenceModel.endpoint == ""; |
| 62 | |
| 63 | // If the endpoint is a URL, use the fetch hook |
| 64 | const headers: { [key: string]: string } = { |
| 65 | method: "POST", |
| 66 | "Content-Type": "application/json", |
| 67 | }; |
| 68 | |
| 69 | // Add the authentication header if necessary |
| 70 | if (targetModel.apiKey.length != 0) { |
| 71 | if (targetModel.authType == "apiKey") { |
| 72 | headers["Authorization"] = `Api-Key ${targetModel.apiKey.trim()}`; |
| 73 | } else if (targetModel.authType == "bearerToken") { |
| 74 | headers["Authorization"] = `Bearer ${targetModel.apiKey.trim()}`; |
| 75 | } else if (targetModel.authType == "x-api-key") { |
| 76 | headers["X-API-Key"] = `${targetModel.apiKey.trim()}`; |
| 77 | } |
no test coverage detected