본문 바로가기
소프트웨어 마에스트로/BackEnd(Django)

[백엔드] LLM 적용기 + python ruff 적용기

by alpakaka 2024. 8. 8.

LLM 적용기

LLM 을 적용해야한다.

더보기
{"role": "system",
"content": """너는 퍼스널 매니저야.
너가 하는 일은 이 사람이 할 이야기를 듣고 약 1시간 정도면 끝낼 수 있도록 작업을 나눠주는 식으로 진행할 거야.
아래는 너가 나눠줄 작업 형식이야.
{ id : 1, content: "3학년 2학기 운영체제 중간고사 준비", start_date="2024-09-01", end_date="2024-09-24"}
이런 형식으로 작성된 작업을 받았을 때 너는 이 작업을 어떻게 나눠줄 것인지를 알려주면 돼.
Output a JSON object structured like:
{id, content, start_date, end_date, order, is_completed, children : [
{content, date, todo(parent todo id)}, ... ,{content, date, todo(parent todo id)}]}
[조건]
- start_date 와 end_date 사이에 분포할 것
- start_date 가 null 인 경우 현재 날짜와 end_date 사이에 분포할 것
- end_date 가 없는 경우 start_date 부터 꾸준히 진행할 수 있도록 제시할 것
- 작업은 1시간 정도로 이루어지도록 제시할 것
- subtodo의 경우 예시와 동일한 구조로 제시할 것 {content: "예시", date : "예시"}
"""},
{"role": "user", "content": f"id: {todo.id}, content: {todo.content}, start_date: {todo.start_date}, end_date: {todo.end_date}"}

일단 위에 접은 글 처럼 프롬프팅을 해봤는데 계속 string 으로 나와서 해당 링크를 참고해서 json 파일로 변환해봤다.

https://platform.openai.com/docs/guides/structured-outputs/json-mode

 

여기서 확인하면

response_format={"type": "json_object"}

이런식으로 정하는게 가능하다고 적혀있다. 그래서 적용해보니 또 문제가 발생했다.

내가 원했던 건 저기서 애초에 json 파일로 온다고 생각했는데 살펴보니 string 으로 와서 계속 이런식으로 작동되었다.

"{\n "id": 69,\n "content": "광화문에서 친구 만나기",\n "start_date": "2024-08-07",\n "end_date": "2024-08-07",\n "category_id": 1,\n "order": "0|i000ac",\n "is_completed": false,\n "children": [\n {\n "content": "광화문 가는 길 확인하기",\n "date": "2024-08-07",\n "todo": 69,\n "order": null,\n "is_completed": false\n },\n {\n "content": "친구와 만날 장소 정하기",\n "date": "2024-08-07",\n "todo": 69,\n "order": null,\n "is_completed": false\n },\n {\n "content": "만날 시간 정하기",\n "date": "2024-08-07",\n "todo": 69,\n "order": null,\n "is_completed": false\n },\n {\n "content": "음료수 또는 간식 준비하기",\n "date": "2024-08-07",\n "todo": 69,\n "order": null,\n "is_completed": false\n }\n ]\n}"

이래서 들었던 고민은 일단 json 파일 형식은 맞긴한데.. 근데 또 뭔가 이상한데 라는 생각과 type 을 찍어보니 str 이라서 이대로 보내면 안되지 않나 싶은 생각이 들었다. str-> json 을 하기 위해 서치를 해봤다. 여러군데 둘러봤다.

https://ourcstory.tistory.com/106

 

[Python] json 모듈 사용하기 :: String을 Dict으로 변환

들어가며 HTTP 통신을 하면서 data를 주고 받을 경우에 json형태로 데이터를 주고 받을 때가 많습니다. 또한 python에서는 dict의 type을 자주 사용하기 때문에 String을 dict으로 변환할 줄 알아야 하는데

ourcstory.tistory.com

사실 가장 도움이 되었던 건 stack overflow 에 있었던 내용이었는데 홈페이지를 잃었다.

일단 여기에 있는 json.loads로 하니까 원래 원하던 대로 작성이 되서 완료했다.

근데 아직 test가 많이 부족해서 일단 오늘은 여기까지하고 내일 테스트를 해봐야할 것 같다.

 

 

Python ruff 적용기

일단 
https://velog.io/@ozoooooh/Python-Linter%EB%A1%9C-Ruff%EB%A5%BC-%EC%82%AC%EC%9A%A9%ED%95%B4%EB%B3%B4%EA%B8%B0feat.-pre-commit

 

Python Linter로 Ruff를 사용해보기(feat. pre-commit)

최신 Python Linter인 Ruff를 알아보자

velog.io

참고해봤다. 생각보다 간단해보여서 적용해 볼 예정이다

https://docs.astral.sh/ruff/

 

Ruff

Ruff Docs | Playground An extremely fast Python linter and code formatter, written in Rust. Linting the CPython codebase from scratch. ⚡️ 10-100x faster than existing linters (like Flake8) and formatters (like Black) 🐍 Installable via pip 🛠️ py

docs.astral.sh

사실 오후에 일정이 있어서.. 이건 내일해야 할 것 같다.