-
Notifications
You must be signed in to change notification settings - Fork 44
feat: Complete Ollama AI integration with health checks and documentation #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,11 @@ import { Request, Response } from 'express' | |
| import { SendResponse } from '../../../../utils/SendResponse.utils' | ||
| import OllamaService from './Ollama.service' | ||
| import OllamaUtils from './Ollama.utils' | ||
| import axios from 'axios' | ||
| import { env } from '../../../../constant/env.constant' | ||
|
|
||
| class OllamaController { | ||
| async ChartWithOllama(req: Request, res: Response) { | ||
| async ChatWithOllama(req: Request, res: Response) { | ||
| try { | ||
| const { prompt, model } = req.body | ||
|
|
||
|
|
@@ -20,6 +22,85 @@ class OllamaController { | |
| SendResponse.error(res, 'Failed to generate AI response', 500, err) | ||
| } | ||
| } | ||
|
|
||
| async checkOllamaStatus(req: Request, res: Response) { | ||
| try { | ||
| const response = await axios.get(`${env.OLLAMA_HOST}/api/tags`) | ||
|
|
||
| const models = response.data.models || [] | ||
|
|
||
| SendResponse.success( | ||
| res, | ||
| 'Ollama is running and accessible', | ||
| { | ||
| status: 'online', | ||
| host: env.OLLAMA_HOST, | ||
| models: models.map((m: { name: string; size: number; modified_at: string }) => ({ | ||
| name: m.name, | ||
| size: m.size, | ||
| modified: m.modified_at, | ||
| })), | ||
| totalModels: models.length, | ||
| }, | ||
| 200 | ||
| ) | ||
| } catch (error: any) { | ||
| if (error.code === 'ECONNREFUSED' || error.code === 'ECONNRESET') { | ||
| SendResponse.error( | ||
| res, | ||
| 'Ollama server is not running. Please start it using: ollama serve', | ||
| 503, | ||
| { host: env.OLLAMA_HOST } | ||
| ) | ||
| } else { | ||
| SendResponse.error(res, 'Failed to connect to Ollama', 500, error) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async listModels(req: Request, res: Response) { | ||
| try { | ||
| const models = await OllamaUtils.listAvailableModels() | ||
|
|
||
| SendResponse.success(res, 'Models retrieved successfully', { models, count: models.length }, 200) | ||
| } catch (error: any) { | ||
| if (error.code === 'ECONNREFUSED' || error.code === 'ECONNRESET') { | ||
| SendResponse.error( | ||
| res, | ||
| 'Ollama server is not running. Please start it using: ollama serve', | ||
| 503, | ||
| { host: env.OLLAMA_HOST } | ||
| ) | ||
| } else { | ||
| SendResponse.error(res, 'Failed to list models', 500, error) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async testModel(req: Request, res: Response) { | ||
| try { | ||
| const { model } = req.params | ||
|
|
||
| // Test with a simple prompt | ||
| const testPrompt = 'Say hello in one sentence' | ||
|
|
||
| const response = await OllamaService.generateText(testPrompt, model) | ||
|
Comment on lines
+80
to
+87
|
||
|
|
||
| SendResponse.success( | ||
| res, | ||
| `Model '${model}' is working correctly`, | ||
| { | ||
| model, | ||
| testPrompt, | ||
| response, | ||
| latency: '< 1s', // Could be measured accurately | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| }, | ||
|
Comment on lines
+96
to
+97
|
||
| 200 | ||
| ) | ||
| } catch (error: any) { | ||
| SendResponse.error(res, `Model '${req.params.model}' test failed`, 500, error) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export default new OllamaController() | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -253,7 +253,7 @@ Ensure you have the following installed: | |||||
| | **Node.js** | 18.x or higher | [nodejs.org](https://nodejs.org/) | | ||||||
| | **npm** | 9.x or higher | Included with Node.js | | ||||||
| | **Git** | Latest | [git-scm.com](https://git-scm.com/) | | ||||||
| | **Ollama** (optional) | Latest | [ollama.ai](https://ollama.ai/) | | ||||||
| | **Ollama** (optional) | Latest | [ollama.ai](https://ollama.ai/) - [Setup Guide](docs/OLLAMA_SETUP.md) | | ||||||
|
||||||
| | **Ollama** (optional) | Latest | [ollama.ai](https://ollama.ai/) - [Setup Guide](docs/OLLAMA_SETUP.md) | | |
| | **Ollama** (optional) | Latest | [ollama.com](https://ollama.com/) - [Setup Guide](docs/OLLAMA_SETUP.md) | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Catching errors as
anyis not type-safe in modern TypeScript. More importantly, sending the entireerrorobject in the response can leak sensitive information like stack traces or system paths in a production environment. It's recommended to type the error asunknown, perform type checks (e.g., usingaxios.isAxiosError), log the full error on the server for debugging, and send a more generic, safe error message to the client.