forked from hangshuo652/aurak
feat: implement QuestionBank CRUD with pagination and template query
- Add pagination support to findAll (page, limit query params) - Add findByTemplateId method to service - Add GET /by-template/:templateId endpoint to controller - Service already includes CRUD for QuestionBank and QuestionBankItem
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ChatOpenAI } from '@langchain/openai';
|
||||
import { ModelConfig } from '../types';
|
||||
import { I18nService } from '../i18n/i18n.service';
|
||||
|
||||
@Injectable()
|
||||
export class ApiService {
|
||||
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) {
|
||||
console.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',
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user