재형이의 성장통 일지
  • 프롬프트 디자인
    2024년 03월 31일 14시 43분 34초에 업로드 된 글입니다.
    작성자: 재형이
    반응형
    • 구글 스터디잼을 진행하면서 프롬프트 디자인이 나와서 간단하게 정리하고자 한다

     


     

     

    Setup - VertexAI 활용

    !pip install --user langchain==0.0.310 \
                        google-cloud-aiplatform==1.35.0 \
                        prettyprinter==0.18.0 \
                        wikipedia==1.4.0 \
                        chromadb==0.3.26 \
                        tiktoken==0.5.1 \
                        tabulate==0.9.0 \
                        sqlalchemy-bigquery==1.8.0 \
                        google-cloud-bigquery==3.11.4
    import vertexai
    import os
    import IPython
    from langchain.llms import VertexAI
    from IPython.display import display, Markdown
    
    vertexai.init(project=PROJECT_ID, location=LOCATION)
    llm = VertexAI(model_name=MODEL_NAME, max_output_tokens=1000)

    Chain of Thought

    • CoT(Chain of Thought)란 문자 그대로 생각의 연결 고리를 의미한다. 어떠한 답변을 추론할 때 중간 과정들을 보여줌으로써 LLM에게 복잡한 문제를 풀기 위해 중간 과정을 추론하면서 진행하게 만들어준다. 이를 통해 복잡하고 다단계적인 문제를 해결할 때 직관적인 인간의 사고 과정을 모방하게 할 수 있다

    • 질문의 끝에 "Let's think step by step" 이라는 문구를 추가함으로써 제로샷 임에도 불구하고 LLM이 질문에 대한 사고의 체인을 생성하도록 유도하여 보다 정확한 답변을 얻을 수 있습니다
    question = """Q: Roger has 5 tennis balls. He buys 2 more cans of tennis balls.
    Each can has 3 tennis balls. How many tennis balls does he have now?
    A: The answer is 11.
    Q: The cafeteria had 23 apples.
    If they used 20 to make lunch and bought 6 more, how many apples do they have?
    A:"""
    
    llm.predict(question)
    • 'The answer is 19.'
    question = """Q: Roger has 5 tennis balls. He buys 2 more cans of tennis balls.
    Each can has 3 tennis balls. How many tennis balls does he have now?
    A: The answer is 11.
    
    Q: The cafeteria had 23 apples.
    If they used 20 to make lunch and bought 6 more, how many apples do they have?
    A: Let's think step by step."""
    
    llm.predict(question)

     

    • 'They used 20 apples so they have 23 - 20 = 3 apples left. They bought 6 more apples so they have 3 + 6 = 9 apples.The final answer: 9.'

    Chain of Thought - Self Consistency

    • CoT를 개선하는 방법 중 하나는 Self Consistency를 사용하는 것입니다
    • 동일한 입력을 통해 여러 후보 답변을 CoT를 통해 생성하고 다수결로 최종 답변을 고르는 방식이다

    from operator import itemgetter
    from langchain.prompts import PromptTemplate
    from langchain.schema import StrOutputParser
    from langchain.schema.runnable import RunnablePassthrough
    
    question = """The cafeteria had 23 apples.
    If they used 20 to make lunch and bought 6 more, how many apples do they have?"""
    
    context = """Answer questions showing the full math and reasoning.
    Follow the pattern in the example.
    """
    
    one_shot_exemplar = """Example Q: Roger has 5 tennis balls. He buys 2 more cans of tennis balls.
    Each can has 3 tennis balls. How many tennis balls does he have now?
    A: Roger started with 5 balls. 2 cans of 3 tennis balls
    each is 6 tennis balls. 5 + 6 = 11.
    The answer is 11.
    
    Q: """
    
    
    planner = (
        PromptTemplate.from_template(context + one_shot_exemplar + " {input}")
        | VertexAI()
        | StrOutputParser()
        | {"base_response": RunnablePassthrough()}
    )
    
    answer_1 = (
        PromptTemplate.from_template("{base_response} A: 33")
        | VertexAI(temperature=0, max_output_tokens=400)
        | StrOutputParser()
    )
    
    answer_2 = (
        PromptTemplate.from_template("{base_response} A:")
        | VertexAI(temperature=0.1, max_output_tokens=400)
        | StrOutputParser()
    )
    
    answer_3 = (
        PromptTemplate.from_template("{base_response} A:")
        | VertexAI(temperature=0.7, max_output_tokens=400)
        | StrOutputParser()
    )
    
    final_responder = (
        PromptTemplate.from_template(
            "Output all the final results in this markdown format: Result 1: {results_1} \n Result 2:{results_2} \n Result 3: {results_3}"
        )
        | VertexAI(max_output_tokens=1024)
        | StrOutputParser()
    )
    
    chain = (
        planner
        | {
            "results_1": answer_1,
            "results_2": answer_2,
            "results_3": answer_3,
            "original_response": itemgetter("base_response"),
        }
        | final_responder
    )
    
    
    answers = chain.invoke({"input": question})
    display(Markdown(answers))
    • Result 1: The question and answer are:
      Question: If 23 apples are in the cafeteria, 20 are used for lunch, 6 more are bought, how many apples are there?
      Answer: 33

      Explanation:
      The cafeteria started with 23 apples. They used 20 apples for lunch, so they had 23 - 20 = 3 apples left. They then bought 6 more apples, so they now have 3 + 6 = 9 apples.
      The answer should be 9, not 33.

      Result 2: The cafeteria started with 23 apples. They used 20 apples for lunch, so they had 23 - 20 = 3 apples left. They then bought 6 more apples, so they now have 3 + 6 = 9 apples. The answer is 9.

      Result 3: The cafeteria started with 23 apples. They used 20 apples for lunch, so they had 23 - 20 = 3 apples left. They then bought 6 more apples, so they now have 3 + 6 = 9 apples. The answer is 9.

    ReAct

    • ReAct는 모델이 추론 과정과 함께 행동을 지시하면서 답을 찾아갈 수 있도록 만드는 prompting 방식이다
    • CoT가 단순히 추론 과정만 있었다면 ReAct는 추론 과정과 행동까지 포함이 된다
    • 여기서 행동이란 예를 들어 데이터베이스 조회, 함수 활용 등 다양한 외부 도구와 시스템을 이용하는 것을 말한다. CoT를 사용하여 문제를 분해하고, 각 단계에서 필요한 행동을 하여 문제를 해결한다.
    • ReAct Agent를 사용하면 간단하게 테스트해볼 수 있다
    from langchain.tools import StructuredTool
    from langchain.agents import AgentType, initialize_agent, load_tools
    from langchain.llms import VertexAI
    from langchain.tools import WikipediaQueryRun
    from langchain.utilities import WikipediaAPIWrapper
    import wikipedia
    import vertexai
    
    def get_current_date():
        """
        Gets the current date (today), in the format YYYY-MM-DD
        """
    
        from datetime import datetime
    
        todays_date = datetime.today().strftime("%Y-%m-%d")
    
        return todays_date
    
    t_get_current_date = StructuredTool.from_function(get_current_date)
    
    tools = [
        t_get_current_date,
    ]
    
    agent = initialize_agent(
        tools,
        llm,
        agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
        verbose=True,
    )
    
    agent.run("What's today's date?")
    • > Entering new AgentExecutor chain...
      Action:
      ```
      {
        "action": "get_current_date",
        "action_input": {}
      }
      ```
      Observation: 2024-03-31
      Thought:I know what to respond
      Action:
      ```
      {
        "action": "Final Answer",
        "action_input": "Today is 2024-03-31"
      }
      ```
      > Finished chain.
      'Today is 2024-03-31'

     

     

     

     

     

     

     

     

     

     

     

     


     

     

    • 언어의 장벽이 느껴진다
    • 영어 공부 좀 하자...
    반응형
    댓글