반응형

GPT 파인 튜닝
컬럼명을 자동완성 시키는데 AI를 활용해보자는 의견을 받았고,
import openai
openai.api_key = ""
# 1. 파일 업로드
file_response = openai.files.create(
file=open("./sample_data/training.jsonl", "rb"),
purpose="fine-tune"
)
file_id = file_response.id
# 2. 파인튜닝 작업 생성
job = openai.fine_tuning.jobs.create(
training_file=file_id,
model="gpt-3.5-turbo" # 또는 사용 가능한 최신 모델명
)
print("파인튜닝 작업 ID:", job.id)
print("파인튜닝 작업 ID:", job.id)
job_status.status == "succeeded"
response = openai.chat.completions.create(
model="ft:gpt-3.5-turbo-0125:personal::BlTSOEv8",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "FUNDKRCODE이 뭐야?"}
]
)
print(response.choices[0].message.content)
우선 GPT-API를 사용하여, 컬렴명이 메핑된 Json파일로 GPT를 파인튜닝해서 모델명을 저장했다.
package com.example.demo;
import org.springframework.web.bind.annotation.*;
@RestController
public class OpenAiController {
private final OpenAiService openAiService;
public OpenAiController(OpenAiService openAiService) {
this.openAiService = openAiService;
}
@GetMapping("/chat")
public String chat(@RequestParam String prompt) {
return openAiService.ask(prompt);
}
}
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class OpenAiService {
@Value("${openai.api.key}")
private String apiKey;
@Value("${openai.api.url}")
private String apiUrl;
@Value("${openai.model}")
private String model;
private final RestTemplate restTemplate = new RestTemplate();
public String ask(String userPrompt) {
String requestBody = String.format(
"{ \"model\": \"%s\", \"messages\": [{\"role\": \"user\", \"content\": \"%s\"}] }",
model, userPrompt.replace("\"", "\\\"")
);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setBearerAuth(apiKey);
HttpEntity<String> entity = new HttpEntity<>(requestBody, headers);
ResponseEntity<String> response = restTemplate.exchange(
apiUrl, HttpMethod.POST, entity, String.class
);
return response.getBody();
}
}
openai.api.key=
openai.api.url=
openai.model=
저장된 모델명으로 Spring을 돌려봤더니, 정상 작동되는 것을 볼 수 있다.
다음 목표는 Open-LLM을 통해서 instruction튜닝을 시켜 비슷한 효과를 만드는 것이다.
truble
json파일을 넣을때 ~이 뭐야? 를 빼고 단어만 넣었더니 튜닝에 실패하였다.
찾아보니 튜닝시킬때 어느정도 token이 입력되어야 튜닝이 진행된다는 것을 알게되었다.

반응형
'Trouble Shooting' 카테고리의 다른 글
태태개발일지 - JpA,Mybatis,다중DB 한번에 사용? (1) | 2024.10.28 |
---|---|
태태개발일지 - JPA N+1 (1) | 2024.10.25 |