|
| 1 | +import { Inject, Injectable } from '@nestjs/common'; |
| 2 | +import axios from 'axios'; |
| 3 | +import { ConfigService } from '@nestjs/config'; |
| 4 | +import { AiResponseDto } from './dto/ai.dto'; |
| 5 | +import { Logger } from 'winston'; |
| 6 | +import { preview } from '../common/util'; |
| 7 | + |
| 8 | +@Injectable() |
| 9 | +export class AiService { |
| 10 | + private readonly headers = { |
| 11 | + 'Content-Type': 'application/json', |
| 12 | + Accept: 'application/json', |
| 13 | + }; |
| 14 | + private readonly instance = axios.create({ |
| 15 | + baseURL: |
| 16 | + 'https://clovastudio.stream.ntruss.com/testapp/v1/chat-completions/HCX-002', |
| 17 | + timeout: 50000, |
| 18 | + headers: this.headers, |
| 19 | + }); |
| 20 | + |
| 21 | + constructor( |
| 22 | + private configService: ConfigService, |
| 23 | + @Inject('winston') private readonly logger: Logger, |
| 24 | + ) { |
| 25 | + this.instance.interceptors.request.use((config) => { |
| 26 | + config.headers['X-NCP-CLOVASTUDIO-API-KEY'] = this.configService.get( |
| 27 | + 'X_NCP_CLOVASTUDIO_API_KEY', |
| 28 | + ); |
| 29 | + config.headers['X-NCP-APIGW-API-KEY'] = this.configService.get( |
| 30 | + 'X_NCP_APIGW_API_KEY', |
| 31 | + ); |
| 32 | + config.headers['X-NCP-CLOVASTUDIO-REQUEST-ID'] = this.configService.get( |
| 33 | + 'X_NCP_CLOVASTUDIO_REQUEST_ID', |
| 34 | + ); |
| 35 | + return config; |
| 36 | + }); |
| 37 | + } |
| 38 | + async getApiResponse(message: string): Promise<AiResponseDto> { |
| 39 | + const response = await this.instance.post('/', { |
| 40 | + messages: [ |
| 41 | + { |
| 42 | + role: 'system', |
| 43 | + content: |
| 44 | + '- Git ์ ๋ฌธ๊ฐ์
๋๋ค.\\n- Git์ ๋ํ ์ง๋ฌธ๋ง ๋๋ตํฉ๋๋ค.\\n- Git ์ฌ์ฉ์ด ๋ฏ์ ์ฌ๋๋ค์๊ฒ ์ง๋ฌธ์ ๋ฐ์ต๋๋ค.\\n- Git ์ค์น๋ ์ด๋ฏธ ๋ง์ณค์ต๋๋ค.\\n- ์ค๋ช
์ ์ดํดํ๊ธฐ ์ฝ๊ฒ ๋ช
๋ฃํ๊ณ ๊ฐ๋จํ๊ฒ ๋ช
๋ น์ด ์์ฃผ๋ก ๋๋ตํฉ๋๋ค.\\n- ์ง๋ฌธํ ๊ฒ๋ง ๋๋ตํฉ๋๋ค.\\n- Git ๋ช
๋ น์ด๋ก๋ง ํด๋ต์ ์ ์ํฉ๋๋ค.\\n- ์๋ฅผ ๋ค์ด ์ค๋ช
ํ์ง ์๋๋ค.', |
| 45 | + }, |
| 46 | + { |
| 47 | + role: 'user', |
| 48 | + content: message, |
| 49 | + }, |
| 50 | + ], |
| 51 | + topP: 0.8, |
| 52 | + topK: 0, |
| 53 | + maxTokens: 512, |
| 54 | + temperature: 0.3, |
| 55 | + repeatPenalty: 5.0, |
| 56 | + stopBefore: [], |
| 57 | + includeAiFilters: true, |
| 58 | + }); |
| 59 | + |
| 60 | + this.logger.log( |
| 61 | + 'info', |
| 62 | + `AI response: ${preview(response.data.result.message.content)}`, |
| 63 | + ); |
| 64 | + |
| 65 | + return { message: response.data.result.message.content }; |
| 66 | + } |
| 67 | +} |
0 commit comments