Skip to content

Commit

Permalink
Fix: add local model host for docker deployment (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
saimanoj authored Jul 26, 2024
1 parent d93cf7c commit cd75d91
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ COHERE_API_KEY=
OPENAI_API_KEY=

LOCAL_MODEL=llama3

OLLAMA_HOST=http://localhost:11434



Expand Down
13 changes: 9 additions & 4 deletions apps/server/src/modules/ai-requests/ai-requests.services.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable, Logger } from '@nestjs/common';
import { PrismaService } from 'nestjs-prisma';
import ollama from 'ollama';
import { Ollama } from 'ollama';
import OpenAI from 'openai';
import { ChatCompletionMessageParam } from 'openai/resources';

Expand All @@ -10,13 +10,15 @@ import { requestInputBody } from './ai-requests.interface';
export default class AIRequestsService {
private readonly logger: Logger = new Logger('RequestsService');
private readonly openaiClient;
private readonly ollama;
constructor(private prisma: PrismaService) {
if (process.env['OPENAI_API_KEY']) {
this.openaiClient = new OpenAI({
apiKey: process.env['OPENAI_API_KEY'],
});
} else {
ollama.pull({ model: process.env['LOCAL_MODEL'] });
this.ollama = new Ollama({ host: process.env['OLLAMA_HOST'] });
this.ollama.pull({ model: process.env['LOCAL_MODEL'] });
}
}

Expand All @@ -25,6 +27,10 @@ export default class AIRequestsService {
const messages = reqBody.messages;
let model = reqBody.llmModel;
this.logger.log(`Received request with model: ${model}`);
if (!process.env['OPENAI_API_KEY']) {
this.logger.log('OPENAI_API_KEY not found, using local model');
model = process.env['LOCAL_MODEL'];
}
switch (model) {
case 'gpt-3.5-turbo':
case 'gpt-4-turbo':
Expand All @@ -39,9 +45,8 @@ export default class AIRequestsService {
break;
default:
// Send request to ollama as fallback
model = process.env.LOCAL_MODEL;
this.logger.log(`Sending request to ollama with model: ${model}`);
const response = await ollama.chat({
const response = await this.ollama.chat({
model,
messages,
});
Expand Down
11 changes: 6 additions & 5 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"COHERE_API_KEY",
"OPENAI_API_KEY",
"LOCAL_MODEL",
"OLLAMA_HOST",

"TRIGGER_DB",
"TRIGGER_DATABASE_URL",
Expand All @@ -75,18 +76,18 @@
"ui": "tui",
"tasks": {
"start-prod": {
"dependsOn": [ "db:generate" ],
"dependsOn": ["db:generate"],
"cache": false
},
"dev": {
"dependsOn": [ "db:generate" ],
"dependsOn": ["db:generate"],
"cache": false
},
"build": {
"dependsOn": [ "db:generate" ],
"outputs": [ "dist/**", ".next/**" ]
"dependsOn": ["db:generate"],
"outputs": ["dist/**", ".next/**"]
},
"lint": { },
"lint": {},
"create-resources": {
"interactive": true,
"cache": false
Expand Down

0 comments on commit cd75d91

Please sign in to comment.