Files
aurak/server/src/api/api.service.ts
T

51 lines
1.4 KiB
TypeScript

import { Injectable, Logger } from '@nestjs/common';
import { ChatOpenAI } from '@langchain/openai';
import { ModelConfig } from '../types';
import { I18nService } from '../i18n/i18n.service';
@Injectable()
export class ApiService {
private readonly logger = new Logger(ApiService.name);
constructor(private i18nService: I18nService) {}
// Simple health check method
healthCheck() {
return { status: 'ok', message: 'API is healthy' };
}
async getChatCompletion(
prompt: string,
modelConfig: ModelConfig,
): Promise<string> {
// API key is optional - allows local models
try {
const llm = this.createLLM(modelConfig);
const response = await llm.invoke(prompt);
return response.content.toString();
} catch (error) {
this.logger.error('LangChain call failed:', error);
if (error.message?.includes('401')) {
throw new Error(this.i18nService.getMessage('invalidApiKey'));
}
throw new Error(
this.i18nService.formatMessage('apiCallFailed', {
message: error.message,
}),
);
}
}
private createLLM(modelConfig: ModelConfig): ChatOpenAI {
return new ChatOpenAI({
temperature: 0.7,
apiKey: modelConfig.apiKey,
modelName: modelConfig.modelId,
configuration: {
baseURL: modelConfig.baseUrl || 'http://localhost:11434/v1',
},
});
}
}