본문 바로가기

LangChain&LangGraph

Meta prompt 생성 Agent

 

# 목적

LLM 등에서 사용되는 프롬프트를 대화형 모델을 통해 생성해주는 Agent로  "프롬프트를 만드는 프롬프트" 라고 불린다.

프롬프트 안에서 one-shot, few-shot 등을 통해 모델 품질, 구조,최적화 등에 영향을 미친다.

프롬프트를 작성하기위하여 사용자의 요구사항을 충분히 수집하고 넘어 가는 것이 중요하다.

충분히 수집하고 난 후에 다음단계로 진행하여 원하는 양식의 프롬프트를 LLM 이 생성하도록 한다.

 

 

# 구성도



요구사항이 충족될 때까지 gathering_infomation에서 사용자에게 추가정보를 요청 한다.

 

# 핵심

템플릿에서 4가지 타입을 제시하여 정보를 차례대로 수집하라고 제시 

( objective, variables, constraints, requirements )

def get_prompt_require_infomation()->ChatPromptTemplate:
    """
        사용자 요구사항 수집을 위한 시스템 메시지 템플릿
    """
    template = """
    Your job is to gather complete and clear information from a user about the prompt template they want to create.

    You must explicitly ask for each of the following, one by one, if not provided:
    - The objective of the prompt
    - The list of variables to include in the prompt template
    - Any constraints about what the output must NOT do
    - Any requirements the output MUST satisfy

    If you cannot clearly identify any of these, politely ask the user to clarify or provide more details.
    Do NOT guess or assume missing information.

    Only when all info is collected clearly, call the relevant tool and next step.


    [IMPORTANT]
    Prompt generation must be done exclusively in the "prompt_generate node".
    Your conversation must be in Korean.
    The prompt you generate must be in English.
    return ChatPromptTemplate(
    [
        ("system",template),
        ("placeholder", "{placeholder}"),
    ]
    """

 

 

LLM 에서 도우 바인딩을 통해 각 변수에 고객에게 수집된 정보를 매핑하기위한 스키마

class Instructions(BaseModel):
    """Instructions on how to prompt the LLM."""
   
    objective: str =Field(description='What the objective of the prompt is') # 프롬프트의 목표
   
    variables: List[str] =Field(description='What variables will be passed into the prompt template') # 프롬프트 템플릿에 전달될 변수 목록
   
    constraints: List[str] = Field(description='Any constraints for what the output should NOT do')# 출력에서 피해야 할 제약 조건 목록

    requirements: List[str] = Field(description='Any requirements that the output MUST adhere to')# 출력이 반드시 따라야 할 요구 사항 목록

 

llm을 통해 지속적 정보를 요청하며, 정보 수집 완료될 경우 응답 값이 tool_calls로 변경됨

여기서 AI 모델에 따라 tool_calls 호출을 안하는경우도 발생함 ( prompt_generate 단계로 넘어가지않고 현재 단계에서 프롬프트를 생성하는 이슈 발생)

특히 GPT-4o 급부터는 매우 잘하지만 그 아래 급에서는 기존 프롬프트에 `정보 수집 완료시 반드시 다음단계 넘어가야함, 프롬프트 생성은 prompt_generate node에서 수행' 하라고 명시하고나니 이슈 해결

def gathering_infomation(state : State):
   
    question = state['messages']
    prompt = get_prompt_require_infomation()
    llm = get_gemini()
    llm_with_tools =  llm.bind_tools([Instructions])
    chain = prompt | llm_with_tools
    result = chain.invoke({"placeholder":question})
    return State({'messages':result})

 

prompt_generate 단계에서는 gathering_information 단계에서 수집된 정보& 변수와 구글에서 제공하는 메타 프롬프트 활용하여 새로운 프롬프트를 제시해줌

 

# 앞으로

 

LLM 사용이나 개발시 항상 프롬프트의 역할에대해서 자주 듣었지만

모델에 따라서 프롬프트를 이해할수 있는 능력또한 차이가 있다는 것을 알게 되었습니다.

모델의 성능이 낮을수록 더욱 자세한 프롬프트를 명시적으로 작성하여 가이드라인을 주는게 중요함

 

또한

고객의 단답형등으로 충분하지 못한 정보를 제공 할 경우 gathering_information 단계를 통해 충분한 정보를 수집하는 방법을 활용하는것이 좋을것 같음