콘텐츠로 이동

동적쿼리 생성 이해하기

실행 준비

!pip install openai
!pip install gradio

Gradio 템플릿

import gradio as gr
from openai import OpenAI
import json

client = OpenAI()

# 응답 생성 함수
def generate_response(prompt_text: str):

    completion = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {"role": "user", "content": prompt_text}
        ]
    )

    response = completion.choices[0].message;
    return response.content


# Gradio Blocks 인터페이스 설정
def chatgpt_interface():
    with gr.Blocks() as demo:
        gr.Markdown("### My ChatGPT")

        with gr.Row():
            with gr.Column():
                # 사용자 입력 텍스트 박스
                input_text = gr.Textbox(label="문의 사항을 입력해 주세요:")

                # ChatGPT의 응답 출력 텍스트 박스
                output_text = gr.Textbox(label="AI 응답 결과", interactive=False)

                # 버튼 클릭 시, ChatGPT 응답 생성
                input_text.submit(generate_response, inputs=input_text, outputs=output_text)

        demo.launch()

# ChatGPT 인터페이스 실행
chatgpt_interface()

동적 쿼리 생성을 위한 프롬프트 엔지니어링

import gradio as gr
from openai import OpenAI
import json

client = OpenAI()

# 응답 생성 함수
def generate_response(prompt_text: str):

    completion = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {"role": "system", "content": "요청사항을 분석한 후 다음의 상품정보 테이블에서 데이터를 조회하는 SQLite 기반의 SQL 명령어를 작성해줘."},
            {"role": "system", "content": "상품정보 테이블의 구성은 다음과 같아: create table products(prod_id integer primary key, prod_type text, prod_name text, prod_price integer, prod_count integer, desc text, ref text)"},
            {"role": "system", "content": "prod_name은 제품의 이름, prod_price는 제품의 가격, prod_count는 제품의 판매 가능한 재고 숫자를 의미해."},
            {"role": "user", "content": prompt_text}
        ]
    )

    response = completion.choices[0].message;
    return response.content


# Gradio Blocks 인터페이스 설정
def chatgpt_interface():
    with gr.Blocks() as demo:
        gr.Markdown("### My ChatGPT")

        with gr.Row():
            with gr.Column():
                # 사용자 입력 텍스트 박스
                input_text = gr.Textbox(label="문의 사항을 입력해 주세요:")

                # ChatGPT의 응답 출력 텍스트 박스
                output_text = gr.Textbox(label="AI 응답 결과", interactive=False)

                # 버튼 클릭 시, ChatGPT 응답 생성
                input_text.submit(generate_response, inputs=input_text, outputs=output_text)

        demo.launch()

# ChatGPT 인터페이스 실행
chatgpt_interface()