|
| 1 | +import React, { useState, useEffect } from "react" |
| 2 | +import ShiningButton from "@components/ShiningButton" |
| 3 | +import ModalDialog from "@components/ModalDialog" // Assume this is available |
| 4 | +import toast from "react-hot-toast" |
| 5 | + |
| 6 | +const ModelSelection = ({ onProceed }) => { |
| 7 | + const [availableModels, setAvailableModels] = useState([]) |
| 8 | + const [selectedModel, setSelectedModel] = useState("") |
| 9 | + const [apiKeyDialog, setApiKeyDialog] = useState(false) |
| 10 | + const [apiKey, setApiKey] = useState("") |
| 11 | + const [selectedProvider, setSelectedProvider] = useState("") |
| 12 | + |
| 13 | + const cloudModels = [ |
| 14 | + { name: "OpenAI", value: "openai" }, |
| 15 | + { name: "Gemini", value: "gemini" }, |
| 16 | + { name: "Claude", value: "claude" } |
| 17 | + ] |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + const fetchModels = async () => { |
| 21 | + try { |
| 22 | + const models = await window.electron.invoke("get-ollama-models") |
| 23 | + setAvailableModels(models) |
| 24 | + // Set a default model: first local model or first cloud model |
| 25 | + if (models.length > 0) { |
| 26 | + setSelectedModel(models[0]) |
| 27 | + await saveModel(models[0]) |
| 28 | + } else { |
| 29 | + setSelectedModel(cloudModels[0].value) |
| 30 | + await handleCloudModelSelection(cloudModels[0].value) |
| 31 | + } |
| 32 | + } catch (error) { |
| 33 | + toast.error("Error fetching available models.") |
| 34 | + } |
| 35 | + } |
| 36 | + fetchModels() |
| 37 | + }, []) |
| 38 | + |
| 39 | + const saveModel = async (model) => { |
| 40 | + try { |
| 41 | + await window.electron.invoke("set-db-data", { |
| 42 | + data: { selectedModel: model } |
| 43 | + }) |
| 44 | + } catch (error) { |
| 45 | + toast.error("Error saving model selection.") |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + const handleModelChange = async (model) => { |
| 50 | + setSelectedModel(model) |
| 51 | + if (cloudModels.some((m) => m.value === model)) { |
| 52 | + await handleCloudModelSelection(model) |
| 53 | + } else { |
| 54 | + await saveModel(model) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + const handleCloudModelSelection = async (model) => { |
| 59 | + setSelectedProvider(model) |
| 60 | + const hasKey = await window.electron.invoke("check-api-key", model) |
| 61 | + if (!hasKey) { |
| 62 | + setApiKeyDialog(true) |
| 63 | + } else { |
| 64 | + await saveModel(model) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + const handleApiKeySubmit = async () => { |
| 69 | + try { |
| 70 | + await window.electron.invoke("set-api-key", { |
| 71 | + provider: selectedProvider, |
| 72 | + apiKey |
| 73 | + }) |
| 74 | + await saveModel(selectedProvider) |
| 75 | + setApiKeyDialog(false) |
| 76 | + setApiKey("") |
| 77 | + toast.success("API key saved and model selected.") |
| 78 | + } catch (error) { |
| 79 | + toast.error("Error saving API key.") |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return ( |
| 84 | + <div className="w-4/5 max-w-lg font-Poppins bg-matteblack p-10 rounded-2xl shadow-2xl text-white"> |
| 85 | + <h2 className="text-3xl font-bold mb-6 text-center"> |
| 86 | + Select AI Model |
| 87 | + </h2> |
| 88 | + <p className="text-gray-300 mb-4 text-center"> |
| 89 | + Choose a model to power your knowledge graph. |
| 90 | + </p> |
| 91 | + <select |
| 92 | + value={selectedModel} |
| 93 | + onChange={(e) => handleModelChange(e.target.value)} |
| 94 | + className="bg-transparent border-2 border-gray-400 text-white p-2 rounded-xl pr-4 w-full mb-4" |
| 95 | + > |
| 96 | + {cloudModels.map((model) => ( |
| 97 | + <option |
| 98 | + className="bg-matteblack" |
| 99 | + key={model.value} |
| 100 | + value={model.value} |
| 101 | + > |
| 102 | + {model.name} (Cloud) |
| 103 | + </option> |
| 104 | + ))} |
| 105 | + {availableModels.map((model) => ( |
| 106 | + <option className="bg-matteblack" key={model} value={model}> |
| 107 | + {model} (Local) |
| 108 | + </option> |
| 109 | + ))} |
| 110 | + {availableModels.length === 0 && ( |
| 111 | + <option className="bg-matteblack" value="" disabled> |
| 112 | + No local models available. |
| 113 | + </option> |
| 114 | + )} |
| 115 | + </select> |
| 116 | + {apiKeyDialog && ( |
| 117 | + <ModalDialog |
| 118 | + title={`Enter API Key for ${selectedProvider}`} |
| 119 | + description="Please enter your API key to use this model." |
| 120 | + onCancel={() => setApiKeyDialog(false)} |
| 121 | + onConfirm={handleApiKeySubmit} |
| 122 | + confirmButtonText="Save" |
| 123 | + showInput={true} |
| 124 | + inputValue={apiKey} |
| 125 | + onInputChange={setApiKey} |
| 126 | + /> |
| 127 | + )} |
| 128 | + <ShiningButton onClick={onProceed} className="w-full mt-4"> |
| 129 | + Continue |
| 130 | + </ShiningButton> |
| 131 | + </div> |
| 132 | + ) |
| 133 | +} |
| 134 | + |
| 135 | +export default ModelSelection |
0 commit comments