From 05ee94385bfa5874f8f65105e81c8e3348bf105a Mon Sep 17 00:00:00 2001 From: bithighlander Date: Sat, 10 Aug 2024 22:31:03 -0600 Subject: [PATCH] ollama bridge --- packages/keepkey-desktop/package.json | 2 +- .../src/controllers/ollama.ts | 242 +++++++++++++++--- 2 files changed, 210 insertions(+), 34 deletions(-) diff --git a/packages/keepkey-desktop/package.json b/packages/keepkey-desktop/package.json index 7f3c4f5bf..3f33c6e0e 100644 --- a/packages/keepkey-desktop/package.json +++ b/packages/keepkey-desktop/package.json @@ -1,6 +1,6 @@ { "name": "keepkey-desktop", - "version": "3.1.2", + "version": "3.1.3", "author": { "name": "KeepKey", "email": "support@keepkey.com" diff --git a/packages/keepkey-sdk-server/src/controllers/ollama.ts b/packages/keepkey-sdk-server/src/controllers/ollama.ts index 56c15723d..a1f5cee3b 100644 --- a/packages/keepkey-sdk-server/src/controllers/ollama.ts +++ b/packages/keepkey-sdk-server/src/controllers/ollama.ts @@ -8,7 +8,8 @@ import { Response, Route, Security, - Tags + Tags, + Path } from '@tsoa/runtime'; import axios from 'axios'; @@ -18,19 +19,6 @@ import { Request as ExpressRequest } from 'express'; const DEFAULT_OLLAMA_URL = 'http://127.0.0.1:11434'; -// interface OllamaRequestBody { -// model: string; -// messages: Array<{ -// role: 'system' | 'user' | 'assistant'; -// content: string; -// }>; -// tools: Array<{ -// type: 'function'; -// function: any; -// }>; -// stream: boolean; -// } - @Route('/ollama') @Tags('Ollama') @Security('apiKey') @@ -48,25 +36,14 @@ export class OllamaController extends ApiController { @Body() body: any ): Promise { try { - // Extract the wildcard part from the request URL const path = req.originalUrl.replace('/ollama/', ''); - - // Construct the target URL const targetUrl = `${DEFAULT_OLLAMA_URL}/${path}`; - - // Log the request body console.log('body: ', body); - - // Make the request to the Ollama server const response = await axios.post(targetUrl, body, { headers: { 'Content-Type': 'application/json', - // Forward any additional headers if necessary - // ...req.headers, } }); - - // Return the response from Ollama to the client return response.data; } catch (error) { console.error('Error processing Ollama POST request:', error); @@ -83,25 +60,224 @@ export class OllamaController extends ApiController { @Request() req: ExpressRequest ): Promise { try { - // Extract the wildcard part from the request URL const path = req.originalUrl.replace('/ollama/', ''); - - // Construct the target URL const targetUrl = `${DEFAULT_OLLAMA_URL}/${path}`; - - // Make the GET request to the Ollama server const response = await axios.get(targetUrl, { headers: { // Forward any additional headers if necessary - // ...req.headers, } }); - - // Return the response from Ollama to the client return response.data; } catch (error) { console.error('Error processing Ollama GET request:', error); throw new Error('Error processing Ollama GET request'); } } + + /** + * @summary Proxy POST /api/pull call + */ + @Post('api/pull') + @OperationId('ollama-pull-request') + public async ollamaPull( + @Request() req: ExpressRequest, + @Body() body: any + ): Promise { + try { + const targetUrl = `${DEFAULT_OLLAMA_URL}/api/pull`; + const response = await axios.post(targetUrl, body, { + headers: { + 'Authorization': `Bearer ${req.headers.authorization}`, + } + }); + return response.data; + } catch (error) { + console.error('Error processing Ollama pull request:', error); + throw new Error('Error processing Ollama pull request'); + } + } + + /** + * @summary Proxy POST /api/copy call + */ + @Post('api/copy') + @OperationId('ollama-copy-request') + public async ollamaCopy( + @Request() req: ExpressRequest, + @Body() body: any + ): Promise { + try { + const targetUrl = `${DEFAULT_OLLAMA_URL}/api/copy`; + const response = await axios.post(targetUrl, body, { + headers: { + 'Authorization': `Bearer ${req.headers.authorization}`, + } + }); + return response.data; + } catch (error) { + console.error('Error processing Ollama copy request:', error); + throw new Error('Error processing Ollama copy request'); + } + } + + /** + * @summary Proxy DELETE /api/delete call + */ + @Post('api/delete') + @OperationId('ollama-delete-request') + public async ollamaDelete( + @Request() req: ExpressRequest, + @Body() body: any + ): Promise { + try { + const targetUrl = `${DEFAULT_OLLAMA_URL}/api/delete`; + const response = await axios.delete(targetUrl, { + headers: { + 'Authorization': `Bearer ${req.headers.authorization}`, + }, + data: body, + }); + console.log('response: ', response.data); + return response.data; + } catch (error) { + console.error('Error processing Ollama delete request:', error); + throw new Error('Error processing Ollama delete request'); + } + } + + /** + * @summary Proxy GET /api/tags call + */ + @Get('api/tags') + @OperationId('ollama-tags-request') + public async ollamaTags( + @Request() req: ExpressRequest + ): Promise { + try { + const targetUrl = `${DEFAULT_OLLAMA_URL}/api/tags`; + const response = await axios.get(targetUrl, { + headers: { + 'Authorization': `Bearer ${req.headers.authorization}`, + } + }); + return response.data; + } catch (error) { + console.error('Error processing Ollama tags request:', error); + throw new Error('Error processing Ollama tags request'); + } + } + + /** + * @summary Proxy GET /api/blobs/:digest call + */ + @Get('api/blobs/:digest') + @OperationId('ollama-blobs-request') + public async ollamaBlobs( + @Path() digest: string, + @Request() req: ExpressRequest + ): Promise { + try { + const targetUrl = `${DEFAULT_OLLAMA_URL}/api/blobs/${digest}`; + const response = await axios.head(targetUrl, { + headers: { + 'Authorization': `Bearer ${req.headers.authorization}`, + } + }); + return response.data; + } catch (error) { + console.error('Error processing Ollama blobs request:', error); + throw new Error('Error processing Ollama blobs request'); + } + } + + /** + * @summary Proxy POST /api/show call + */ + @Post('api/show') + @OperationId('ollama-show-request') + public async ollamaShow( + @Request() req: ExpressRequest, + @Body() body: any + ): Promise { + try { + const targetUrl = `${DEFAULT_OLLAMA_URL}/api/show`; + const response = await axios.post(targetUrl, body, { + headers: { + 'Authorization': `Bearer ${req.headers.authorization}`, + } + }); + return response.data; + } catch (error) { + console.error('Error processing Ollama show request:', error); + throw new Error('Error processing Ollama show request'); + } + } + + /** + * @summary Proxy POST /api/embed call + */ + @Post('api/embed') + @OperationId('ollama-embed-request') + public async ollamaEmbed( + @Request() req: ExpressRequest, + @Body() body: any + ): Promise { + try { + const targetUrl = `${DEFAULT_OLLAMA_URL}/api/embed`; + const response = await axios.post(targetUrl, body, { + headers: { + 'Authorization': `Bearer ${req.headers.authorization}`, + } + }); + return response.data; + } catch (error) { + console.error('Error processing Ollama embed request:', error); + throw new Error('Error processing Ollama embed request'); + } + } + + /** + * @summary Proxy POST /api/embeddings call + */ + @Post('api/embeddings') + @OperationId('ollama-embeddings-request') + public async ollamaEmbeddings( + @Request() req: ExpressRequest, + @Body() body: any + ): Promise { + try { + const targetUrl = `${DEFAULT_OLLAMA_URL}/api/embeddings`; + const response = await axios.post(targetUrl, body, { + headers: { + 'Authorization': `Bearer ${req.headers.authorization}`, + } + }); + return response.data; + } catch (error) { + console.error('Error processing Ollama embeddings request:', error); + throw new Error('Error processing Ollama embeddings request'); + } + } + + /** + * @summary Proxy GET /api/ps call + */ + @Get('api/ps') + @OperationId('ollama-ps-request') + public async ollamaPs( + @Request() req: ExpressRequest + ): Promise { + try { + const targetUrl = `${DEFAULT_OLLAMA_URL}/api/ps`; + const response = await axios.get(targetUrl, { + headers: { + 'Authorization': `Bearer ${req.headers.authorization}`, + } + }); + return response.data; + } catch (error) { + console.error('Error processing Ollama ps request:', error); + throw new Error('Error processing Ollama ps request'); + } + } }