Skip to content

Commit

Permalink
ollama bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
BitHighlander committed Aug 10, 2024
1 parent 531e21b commit 96e6044
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 1 deletion.
31 changes: 31 additions & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/keepkey-desktop/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "keepkey-desktop",
"version": "3.1.1",
"version": "3.1.2",
"author": {
"name": "KeepKey",
"email": "[email protected]"
Expand Down
1 change: 1 addition & 0 deletions packages/keepkey-sdk-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@keepkey/device-protocol": "^7.10.2",
"@pioneer-platform/pioneer-client": "^9.0.10",
"@tsoa/runtime": "^4.1.3",
"axios": "^1.7.3",
"common-utils": "workspace:*",
"express": "^4.18.2",
"lodash": "^4.17.21",
Expand Down
2 changes: 2 additions & 0 deletions packages/keepkey-sdk-server/src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Readable } from 'stream'

import { ApiController } from '../auth'

export * from './ollama'

export * from './addresses'
export * from './auth'
export * from './bnb'
Expand Down
107 changes: 107 additions & 0 deletions packages/keepkey-sdk-server/src/controllers/ollama.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import {
Body,
Middlewares,
OperationId,
Post,
Get,
Request,
Response,
Route,
Security,
Tags
} from '@tsoa/runtime';

import axios from 'axios';
import { ApiController } from '../auth';
import { extra } from '../middlewares';
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')
@Middlewares(extra)
@Response(400, 'Bad request')
@Response(500, 'Error processing request')
export class OllamaController extends ApiController {
/**
* @summary Proxy POST Ollama calls
*/
@Post('api/*')
@OperationId('ollama-post-request')
public async ollamaPost(
@Request() req: ExpressRequest,
@Body() body: any
): Promise<any> {
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);
throw new Error('Error processing Ollama POST request');
}
}

/**
* @summary Proxy GET Ollama calls
*/
@Get('api/*')
@OperationId('ollama-get-request')
public async ollamaGet(
@Request() req: ExpressRequest
): Promise<any> {
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');
}
}
}
1 change: 1 addition & 0 deletions packages/keepkey-sdk-server/src/types/ollama/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type OllamaRquest = {}
22 changes: 22 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10970,6 +10970,17 @@ __metadata:
languageName: node
linkType: hard

"axios@npm:^1.7.3":
version: 1.7.3
resolution: "axios@npm:1.7.3"
dependencies:
follow-redirects: "npm:^1.15.6"
form-data: "npm:^4.0.0"
proxy-from-env: "npm:^1.1.0"
checksum: 10/7f92af205705a8fb4a9d35666b663729507657f252a1d39d83582590119941872d49078017cf992e32f47aa3b7317f5439f77be772a173dac2ae0fedd38f43ae
languageName: node
linkType: hard

"axobject-query@npm:^2.2.0":
version: 2.2.0
resolution: "axobject-query@npm:2.2.0"
Expand Down Expand Up @@ -16433,6 +16444,16 @@ __metadata:
languageName: node
linkType: hard

"follow-redirects@npm:^1.15.6":
version: 1.15.6
resolution: "follow-redirects@npm:1.15.6"
peerDependenciesMeta:
debug:
optional: true
checksum: 10/70c7612c4cab18e546e36b991bbf8009a1a41cf85354afe04b113d1117569abf760269409cb3eb842d9f7b03d62826687086b081c566ea7b1e6613cf29030bf7
languageName: node
linkType: hard

"for-each@npm:^0.3.3":
version: 0.3.3
resolution: "for-each@npm:0.3.3"
Expand Down Expand Up @@ -19710,6 +19731,7 @@ __metadata:
"@types/node": "npm:^18"
"@typescript-eslint/eslint-plugin": "npm:^5.45.0"
"@typescript-eslint/parser": "npm:^5.45.0"
axios: "npm:^1.7.3"
common-utils: "workspace:*"
eslint: "npm:^8.28.0"
eslint-config-prettier: "npm:^8.3.0"
Expand Down

0 comments on commit 96e6044

Please sign in to comment.