Skip to content

Commit

Permalink
ollama runs
Browse files Browse the repository at this point in the history
  • Loading branch information
BitHighlander committed Aug 1, 2024
1 parent df42f09 commit fed6930
Show file tree
Hide file tree
Showing 23 changed files with 1,683 additions and 65 deletions.
425 changes: 424 additions & 1 deletion .pnp.cjs

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions executables/version.txt
Git LFS file not shown
2 changes: 1 addition & 1 deletion packages/keepkey-desktop-app/.env
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ REACT_APP_UNCHAINED_THORCHAIN_WS_URL=wss://api.thorchain.shapeshift.com
REACT_APP_THORCHAIN_NODE_URL=https://daemon.thorchain.shapeshift.com
REACT_APP_ETHEREUM_NODE_URL=https://daemon.ethereum.shapeshift.com
REACT_APP_ETHEREUM_INFURA_URL=https://mainnet.infura.io/v3/527a7b1368e9464fbdc9d0f0efac1c0e
REACT_APP_ETHEREUM_INFURA_URL2=https://goerli.infura.io/v3/fb05c87983c4431baafd4600fd33de7e
REACT_APP_ETHEREUM_INFURA_URL2=https://mainnet.infura.io/v3/527a7b1368e9464fbdc9d0f0efac1c0e
REACT_APP_ETHEREUM_INFURA_URL3=https://avalanche-mainnet.infura.io/v3/fb05c87983c4431baafd4600fd33de7e
REACT_APP_MIDGARD_URL=https://indexer.thorchain.shapeshift.com/v2
REACT_APP_WALLET_CONNECT_PROJECT_ID=14d36ca1bc76a70273d44d384e8475ae
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@
"IDR": "Indonesian Rupiah"
},
"tabs": {
"ollama": "Ollama",
"general": "General",
"app": "App",
"keepkey": "KeepKey"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { Divider, HStack, Stack } from '@chakra-ui/layout';
import { Avatar, Button, Icon, IconButton, Switch, Text } from '@chakra-ui/react';
import { useModal } from 'hooks/useModal/useModal';
import { useEffect, useState } from 'react';
import { FaTrash } from 'react-icons/fa';
import { HiRefresh } from 'react-icons/hi';
import { IoFileTray } from 'react-icons/io5';
import { TbRefreshAlert } from 'react-icons/tb';
import { MdPlayArrow, MdStop, MdViewList } from 'react-icons/md';

import { SettingsListItem } from './SettingsListItem';

interface OllamaSettingsProps {
shouldAutoUpdate: boolean;
shouldMinimizeToTray: boolean;
allowPreRelease: boolean;
autoScanQr: boolean;
}

export const OllamaSettings: FC = () => {
const { settings, onboardingSteps } = useModal();
const [appSettings, setAppSettings] = useState<OllamaSettingsProps>({
shouldAutoUpdate: true,
shouldMinimizeToTray: true,
allowPreRelease: false,
autoScanQr: false,
});

const [prevAppSettings, setPrevAppSettings] = useState<OllamaSettingsProps>(appSettings);

const handleViewModels = () => {
console.log('Viewing models...');
// Mock action: Display a message or simulate viewing models
};

const handleStartOllama = () => {
console.log('Starting Ollama...');
// Mock action: Display a message or simulate starting Ollama
};

const handleStopOllama = () => {
console.log('Stopping Ollama...');
// Mock action: Display a message or simulate stopping Ollama
};

useEffect(() => {
(async () => {
if (
prevAppSettings &&
appSettings.shouldAutoUpdate === prevAppSettings.shouldAutoUpdate &&
appSettings.shouldMinimizeToTray === prevAppSettings.shouldMinimizeToTray &&
appSettings.allowPreRelease === prevAppSettings.allowPreRelease &&
appSettings.autoScanQr === prevAppSettings.autoScanQr
)
return;
setPrevAppSettings(appSettings);
// Save settings
console.log('APP SETTINGS SAVED', appSettings);
})().catch(e => console.error(e));
}, [appSettings, prevAppSettings]);

return (
<Stack width='full' p={0}>
<Divider my={1} />
<SettingsListItem
label={'View Models'}
onClick={handleViewModels}
icon={<Icon as={MdViewList} color='gray.500' />}
>
<Button variant={'ghost'} onClick={handleViewModels}>
View Models
</Button>
</SettingsListItem>
<Divider my={1} />
<SettingsListItem
label={'Start Ollama'}
onClick={handleStartOllama}
icon={<Icon as={MdPlayArrow} color='gray.500' />}
>
<Button variant={'ghost'} onClick={handleStartOllama}>
Start
</Button>
</SettingsListItem>
<Divider my={1} />
<SettingsListItem
label={'Stop Ollama'}
onClick={handleStopOllama}
icon={<Icon as={MdStop} color='gray.500' />}
>
<Button variant={'ghost'} onClick={handleStopOllama}>
Stop
</Button>
</SettingsListItem>
</Stack>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type { RouteComponentProps } from 'react-router-dom'

import { AppSettings } from './AppSettings'
import { GeneralSettings } from './GeneralSettings'
import { OllamaSettings } from './OllamaSettings'

export type SettingsListProps = {
appHistory: RouteComponentProps['history']
Expand Down Expand Up @@ -65,6 +66,9 @@ export const SettingsList = ({ appHistory, ...routeProps }: SettingsListProps) =
<Tab>
<Text translation='modals.settings.tabs.keepkey' />
</Tab>
<Tab>
<Text translation='modals.settings.tabs.ollama' />
</Tab>
</TabList>
<TabPanels>
<TabPanel>
Expand All @@ -83,6 +87,11 @@ export const SettingsList = ({ appHistory, ...routeProps }: SettingsListProps) =
/>
</Menu>
</TabPanel>
<TabPanel>
<Menu>
<MorpheosSettings></MorpheosSettings>
</Menu>
</TabPanel>
</TabPanels>
</Tabs>
</ModalBody>
Expand Down
12 changes: 9 additions & 3 deletions packages/keepkey-desktop/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "keepkey-desktop",
"version": "3.0.26",
"version": "3.1.0",
"author": {
"name": "KeepKey",
"email": "[email protected]"
Expand Down Expand Up @@ -30,7 +30,8 @@
"generateUpdatesFilesForAllChannels": true,
"publish": "github",
"files": [
"build/**/*"
"build/**/*",
"executables/*"
],
"protocols": [
{
Expand Down Expand Up @@ -103,6 +104,7 @@
"bs58check": "^3.0.1",
"bufferutil": "^4.0.7",
"builder-util": "^25.0.0-alpha.6",
"check-disk-space": "^3.4.0",
"comlink": "^4.3.1",
"comlink-electron-endpoint": "^1.0.3",
"common-utils": "workspace:*",
Expand All @@ -111,6 +113,7 @@
"dotenv": "^16.0.3",
"electron-is-dev": "^2.0.0",
"electron-log": "^4.4.8",
"electron-store": "^10.0.0",
"electron-unhandled": "^4.0.1",
"electron-updater": "^6.1.8",
"esbuild": "^0.15.14",
Expand All @@ -130,19 +133,22 @@
"nedb-promises": "^6.2.1",
"node-fetch": "^3.3.0",
"node-hid": "^2.1.2",
"ollama": "^0.5.6",
"promise.any": "^2.0.5",
"protobufjs": "^7.2.6",
"secp256k1": "^4.0.3",
"semver": "^7.3.8",
"source-map-support": "^0.5.21",
"sudo-prompt": "^9.2.1",
"swagger-ui-dist": "^4.15.5",
"swagger-ui-express": "^4.6.0",
"tiny-secp256k1": "^1.1.6",
"usb": "^2.5.2",
"utf-8-validate": "^5.0.10",
"uuid": "^9.0.0",
"wait-promise": "^0.4.1",
"web3": "^4.0.1"
"web3": "^4.0.1",
"winston": "^3.13.1"
},
"devDependencies": {
"@keepkey/common-esbuild-bits": "workspace:*",
Expand Down
15 changes: 15 additions & 0 deletions packages/keepkey-desktop/scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const appSource = path.join(
const assetsSource = path.join(workspacePath, 'assets')
const swaggerUiDistSource = pnpapi.resolveToUnqualified('swagger-ui-dist', workspacePath)!
const firmwareSource = path.join(rootPath, 'firmware')
const executablesSource = path.join(rootPath, 'executables');
const executablesPath = path.join(buildPath, 'executables');

const apiPath = path.join(buildPath, 'api')
const appPath = path.join(buildPath, 'app')
Expand Down Expand Up @@ -70,6 +72,18 @@ const copySwaggerUiDist = async () => {
recursive: true,
})
}
//copyExecutables
const copyExecutables = async () => {
if (fs.existsSync(executablesSource)) {
await fs.promises.cp(executablesSource, executablesPath, {
dereference: true,
recursive: true,
});
console.log('Executables copied successfully.');
} else {
console.warn('Executables source directory does not exist.');
}
};

const copyFirmware = async () => {
const releases = JSON.parse(
Expand Down Expand Up @@ -224,6 +238,7 @@ export const build = async () => {
copyAssetsDir(),
copySwaggerUiDist(),
copyFirmware(),
copyExecutables(),
esbuild.then(async x => {
if (isDev) {
await fs.promises.writeFile(
Expand Down
14 changes: 14 additions & 0 deletions packages/keepkey-desktop/src/ai/ai-models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const models = {
mistral: {
tags: ['text', '7b', '7b-instruct', '7b-text'],
},
llama2: {
tags: ['chat', '7b', 'text'],
},
orcaMini: {
tags: ['latest', '3b'],
},
nousHermes: {
tags: ['latest', '7b'],
},
};
21 changes: 21 additions & 0 deletions packages/keepkey-desktop/src/ai/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export enum IpcChannel {
AppInit = 'app:init',
GetModelsPath = 'app:getfolder',
SetFolderPath = 'app:setfolder',
Close = 'app:close',
Minimize = 'app:minimize',
}

export enum OllamaChannel {
OllamaInit = 'ollama:init',
OllamaStatusUpdate = 'ollama:status',
OllamaGetAllModels = 'ollama:getallmodels',
OllamaQuestion = 'ollama:question',
OllamaAnswer = 'ollama:answer',
OllamaGetModel = 'ollama:getmodel',
}

export enum IpcMainChannel {
Error = 'main:error',
CommandOuput = 'command:output',
}
81 changes: 81 additions & 0 deletions packages/keepkey-desktop/src/ai/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { dialog } from 'electron';

import { IpcChannel } from './events';
import {
loadOllama,
stopOllama,
getAllLocalModels,
askOllama,
getOrPullModel,
} from './ollama';
import { OllamaQuestion } from './types';
import { saveModelPathToStorage, getModelPathFromStorage } from './storage';

export const initOllama = async () => {
try {
const ollamaLoaded = await loadOllama();

return ollamaLoaded;
} catch (err) {
handleError(err);

return false;
}
};

export const stopOllamaServe = async () => {
await stopOllama();
};

export const getAllModels = async () => {
try {
const models = await getAllLocalModels();

return models;
// event.reply(OllamaChannel.OllamaGetAllModels, models);
} catch (err) {
handleError(err);
}
};

export const getModel = async ( model: string) => {
try {
const response = await getOrPullModel(model);

return response;
} catch (err) {
handleError(err);
}
};

export const askOlama = async ( { model, query }: OllamaQuestion) => {
try {
const response = await askOllama(model, query);

return response;
} catch (err) {
handleError(err);
}
};

export const getModelsFolderPath = async () => {
return getModelPathFromStorage();
};

export const setModelFolderPath = async () => {
const result = await dialog.showOpenDialog({
properties: ['openDirectory', 'createDirectory'],
});

if (result.filePaths) {
saveModelPathToStorage(result.filePaths[0]);
}

return true;
};

const handleError = (err: Error) => {
console.error(err);

// log with winston here
};
29 changes: 29 additions & 0 deletions packages/keepkey-desktop/src/ai/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import winston from 'winston';
import path from 'path';
import { isDev } from '../main';
import { app } from 'electron';

const logFilePath = isDev ? '.' : app.getPath('userData');

export const logger = winston.createLogger({
format: winston.format.simple(),
transports: [
new winston.transports.Console(),
new winston.transports.File({
filename: path.join(logFilePath, 'error.log'),
maxFiles: 1,
level: 'error',
}),
new winston.transports.File({
filename: path.join(logFilePath, 'app.log'),
maxFiles: 1,
level: 'info',
}),
],
exceptionHandlers: [
new winston.transports.File({
filename: path.join(logFilePath, 'exceptions.log'),
}),
],
exitOnError: false,
});
Loading

0 comments on commit fed6930

Please sign in to comment.