Eat Study Love

먹고 공부하고 사랑하라

Data Science/Research

Llama3 Prompt Engineering(7)

eatplaylove 2025. 4. 30. 17:55

https://eglife.tistory.com/347

 

Llama3 Prompt Engineering(6)

https://eglife.tistory.com/340 PostgreSQL, LLM 연결(5)PostgreSQL Openai or Llama 연결 Settinghttps://eglife.tistory.com/338 PostgreSQL, LLM 연결(4)1.https://github.com/cloudquery/pg_gpt2.https://github.com/prabin-acharya/pg_gpt3.https://github.com/Je

eglife.tistory.com

뭐, 일단 PostgreSQL extension(Postgres에서 사용가능한 customized function이라고 생각하면 된다.)으로 Llama를 불러와서 SQL2NL , NL2SQL은 해봤다. 근데 이후에 뭐 어쩌란 말인가..

 

그냥 Python으로도 PostgreSQL 랑 LLM 둘다 같이 연결해서 위 작업은 수행할 수 있다. GPT끼고 코딩하면 LLM API좀 넣고 PostgreSQL 연결 module만 좀 덯으면 나름 손 쉽게 코드를 짤 수 있다.

 

그럼 이 시점에서 알아봐야할 것은 무엇인가?

1. 복잡한 DB에서 복잡한 SQL에 대해 NL변환 정확도가 좋은가?
--> 근데 이게 PostgreSQL랑 관련이 있나? PostgreSQL은 이 Task에서 하는 게 아무것도 없다. 그냥 공간만 제공해줄 뿐이고 나머지는 Llama의 몫이다.

2. 위 1번 질문의 연장선, 그럼 더 뭐를 해야하나? Small Model 즉, Llama3 8b Model의 Fine-tunimg을 실행해보자.
--> Finetuning용 data-set은 뭘 사용? 그 전에 Local Llama 환경에서 파인튜닝 어떻게 진행?

 

일단 예제 샘플 5개 정도로 기본 Llama의 변환 성능을 보자. In PostgreSQL

사실 이게 PostgreSQL라서 일전에 KCC때 실험과 결과가 다를 거 같지가 않다.. 그니까 사실 이게 실험하는게 뭔 의미가 있겠냐만 그냥 그래도 혹시 PostgreSQL은 다를 수 있으니 함 돌려나보자.

https://eglife.tistory.com/346

 

SQL2NL Prompt Engineering - KCC(4) Last

https://eglife.tistory.com/345 SQL2NL Prompt Engineering - KCC(3)https://eglife.tistory.com/342 SQL2NL Prompt Engineering - KCC(2)https://eglife.tistory.com/341 SQL2NL Prompt Engineering - KCC(1)SQL2NL Module의 정확도를 향상시키는 법에대한

eglife.tistory.com

SQL Gold NL 변환된 NL in PostgreSQL
1. SELECT "Free Meal Count (K-12)"::numeric / NULLIF("Enrollment (K-12)",0)
FROM frpm
WHERE "County Name" = 'Alameda'
ORDER BY 1 DESC
LIMIT 1;
What is the highest eligible free rate for K-12 students in the schools in Alameda County? The school with the highest FRPM count of students eligible for Free and Reduced-Priced Meals in K-12 is located at the given street address.
2. SELECT "Free Meal Count (Ages 5-17)"::numeric
       / NULLIF("Enrollment (Ages 5-17)", 0)     AS free_meal_ratio
FROM   frpm
WHERE  "Educational Option Type" = 'Continuation School'
  AND  "Free Meal Count (Ages 5-17)" IS NOT NULL
  AND  "Enrollment (Ages 5-17)"      IS NOT NULL
ORDER  BY free_meal_ratio ASC
LIMIT  3;
Please list the lowest three eligible free rates for students aged 5-17 in continuation schools. Get the names of the top 3 schools in Orange County with the highest FRPM rate for K-12 students, listed in descending order.
3.SELECT s.zip
FROM   frpm    AS f
JOIN   schools AS s ON f.cdscode = s.cdscode
WHERE  f."District Name"        = 'Fresno County Office of Education'
  AND  f."Charter School (Y/N)" = 1;

What is the unabbreviated mailing street address of the school with the highest FRPM count for K-12 students? List of schools with high enrollment in the 2020 academic year, ordered by highest to lowest student count.
4. SELECT s.mailstreet
FROM   frpm    AS f
JOIN   schools AS s ON f.cdscode = s.cdscode
ORDER  BY f."FRPM Count (K-12)" DESC
LIMIT  1;
Please list the phone numbers of the direct charter-funded schools that are opened after 2000/1/1. The street address of the school with the highest FRPM count among K-12 students is displayed.
5. SELECT s.phone
FROM   frpm    AS f
JOIN   schools AS s ON f.cdscode = s.cdscode
WHERE  f."Charter Funding Type" = 'Directly funded'
  AND  f."Charter School (Y/N)" = 1
  AND  s.opendate > DATE '2000-01-01';

How many schools with an average score in Math greater than 400 in the SAT test are exclusively virtual? Here is the summarized explanation:  Schools in San Diego with FRPM Rates sorted in descending order.


참고로 지금 PSQL 안에서 SQL문을 JSON 형태로 받았다가 Parsing하고 있는데, 이 때 SQL문에 문자열이 있는 경우 따옴표"를 \로 escape해주지 않으면 ERROR가 발생한다.

 

그리고 작은따옴표 표현에도 주의!

SQL 문자열 안에서 '는 두 번 반복 ('')하기

PostgreSQL에서는 문자열 안에서 '을 표현하려면 '' (작은 따옴표 두 번) 을 써야 합니다.

 

일단,, SQL2NL 변환 결과는 개판이다. 난이도 빡센 SQL, DB에서 SQL2NL 정확도 기대하는 건 언감생심인듯

 

이제 할게 없다. PostgreSQL에 Llama 연결했는데.. 그래서 뭐? 뭐 어쩌라는 거냐라는 생각이 스스로도 든다.

 

결국 Llama SQL<->NL 파인튜닝에 집중을 한 번 해봐야겠다.

 

Local 환경에서 Llama 소형모델(Llama3 8b) 파인튜닝 때려보고, 내가 가는 길이 맞는지 추가 미팅을 진행해보자.

 

내 기준으론 지금 논문소제가 떠오르지가 않는다 ㅋㅋ ㅠㅠ