Skip to content

Commit

Permalink
ollama bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
BitHighlander committed Aug 11, 2024
1 parent 96e6044 commit 05ee943
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 34 deletions.
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.2",
"version": "3.1.3",
"author": {
"name": "KeepKey",
"email": "[email protected]"
Expand Down
242 changes: 209 additions & 33 deletions packages/keepkey-sdk-server/src/controllers/ollama.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
Response,
Route,
Security,
Tags
Tags,
Path
} from '@tsoa/runtime';

import axios from 'axios';
Expand All @@ -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')
Expand All @@ -48,25 +36,14 @@ export class OllamaController extends ApiController {
@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);
Expand All @@ -83,25 +60,224 @@ export class OllamaController extends ApiController {
@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');
}
}

/**
* @summary Proxy POST /api/pull call
*/
@Post('api/pull')
@OperationId('ollama-pull-request')
public async ollamaPull(
@Request() req: ExpressRequest,
@Body() body: any
): Promise<any> {
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<any> {
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<any> {
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<any> {
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<any> {
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<any> {
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<any> {
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<any> {
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<any> {
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');
}
}
}

0 comments on commit 05ee943

Please sign in to comment.