데보션앱 소개페이지 바로가기
로그인 선택

신고하기

CLOSE
신고사유 (대표 사유 1개)
상세내용 (선택)
0/200
  • 신고한 게시글은 더 이상 보이지 않습니다.
  • 이용약관과 운영정책에 따라 신고사유에 해당하는지 검토 후 조치됩니다.
  • 허위 신고인 경우, 신고자의 서비스 이용이 제한될 수 있으니 유의하시어 신중하게 신고해 주세요.
(이 회원이 작성한 모든 댓글과 커뮤니티 게시물이 보이지 않고, 알림도 오지 않습니다.)

미리보기

커뮤니티

      1,234

      badge 23.06.15

      글 등록

      카테고리를 선택해주세요.

      DEVOTEE를 활성화 시키면
      지금 작성한 커뮤니티 글에 대해 1개의 댓글을 달아줍니다.

      버튼을 누르면 글 수정 시 ChatGPT가 작성한 댓글이 수정됩니다.

      임시저장함에 저장되었습니다. 저장일시 : 2022.5.17 14:29:08

      임시저장함

      제목을 선택하시면 이어서 작성이 가능하며,
      최대 20건까지 저장합니다.
      컨텐츠 유형, 제목, 저장일시, 삭제로 이뤄진 임시저장 목록
      컨텐츠 유형 제목 저장일 삭제

      데보션 블로그 게재 요청

      CLOSE
      • *
      • *

      본인인증

      효율적인 데보션 서비스 이용 및
      고객님의 소중한 개인정보보호를 위해
      본인인증을 진행해주세요. 본인인증 미 진행 시 로그인이 제한됩니다.
      본인인증 실패

      본인인증 로그인에 실패하였습니다.
      회원이 아니시거나 본인인증 등록이
      완료되지 않은 사용자입니다.

      회원정보 연결

      Custom LLM Agent 구축하기(From Scratch)

      Teus 25.06.11
      3,280 4 1
      DEVOTEE 요약
      본 블로그에서는 Langchain이나 Langgraph 없이 Python을 활용해 자연어 질문을 LLM으로 처리하고 필요한 함수를 실행하는 에이전트 예제를 구현하는 방법을 설명합니다. ReAct(Reasoning, Action) 에이전트 방식을 사용하여, LLM이 질문을 분석하고 함수 실행에 필요한 매개변수를 찾아 결과를 도출하는 과정을 보여줍니다. 이 코드 예제에서는 서울의 현재 시간을 묻는 질문에 대해 LLM이 ‘시간 도구’를 호출하고 결과를 반환하는 과정을 단계별로 설명하며, 최적화된 프롬프트 설계의 중요성을 강조합니다.
      DEVOTEE 추천 블로그

      안녕하세요. Teus입니다.


      이번 포스팅은 Langchain이나 Langgraph의 도움 없이

      LLM을 통해서 자연어로 질문하고, 질문에 맞는 function이 실행되게 만드는 예제입니다.


      0. Agent?

      Agent는 특정 행동에 특화된 LLM 서비스를 의미합니다.

      이때 특정 행동은 특정 영역에 대한 행동 뿐만 아니라 인터넷을 검색하던가, DataBase에서 Data를 찾아온다거나 하는 Function을 실행하는 행동 역시 포함됩니다.


      이러한 Agent의 경우 현재 Multi turn을 통해서 LLM과의 소통 후에

      질문, [생각, 행동]🔁, 결과도출 과정을 통해서 유저의 자연어에서 함수에 필요한 내용과, 함수를 실행하기 위한 매개변수를 찾아냅니다.


      1. 구현하기

      이러한 Agent를 ReAct(Reasoning, Action) Agent라고 불립니다.

      기본적으로 Agent를 구현하기 위해서는 LLM에 질문을 주고, 답변을 받고, 다시 질문을 주는 Multi turn을 구현해야 하는데,

      이걸 구현할때

      등을 사용합니다.


      근데 위 친구들을 사용할 경우, ReAct Agent가 어떻게 동작하는지 이해하기 어려울 수가 있습니다.

      그래서, 이 친구들을 Python의 Requests를 사용해서 간접적으로 구현해 봅니다.

      import requests
      import datetime
      
      def get_current_time(city:str):
          """useful when you need to know about current time in specific city.
          Returns the current time in format H:MM AM/PM format.
          
          args
              city : str = specific city that want to know the time"""
          now = datetime.datetime.now()
          return now.strftime("%I:%M %p")
      
      def query_llm(prompt):
          """Queries the LLM via a POST request."""
          url = "http://myvllmservice/v1/completions"
          headers = {
              "Content-Type": "application/json",
              "Authorization": "Bearer mytoken"
          }
          data = {
              "model": "MyLLMModel",        
              "prompt": prompt,
              "max_tokens": None,
              "temperature": 0
          }
          
          response = requests.post(url, headers=headers, json=data)
          return response.json()['choices'][0]['text']
      
      # Define available tools
      tools = {
          "time": get_current_time.__doc__,
      }
      
      # Prompt template
      prompt_template = """You have access to the following tools: {tools}
      Use the following format:
      Question: the input question you must answer
      Thought: you should always think about what to do
      Action: the action to take, should be one of [{tool_names}]
      Action Input: the input to the action
      Observation: the result of the action
      ... (this Thought/Action/Action Input/Observation can repeat N times)
      Thought: I now know the final answer
      Final Answer: the final answer to the original input question
      Begin!
      Question: {input}. you may solve this problem with your tools
      Thought:"""
      
      def execute_tool(tool_name, tool_input):
          """Executes the specified tool with the given input."""
          if tool_name == "time":
              return get_current_time(tool_input)
          else:
              return "Tool not found."
      
      # Main agent loop
      def run_agent(input_question):
          """Runs the agent in a multi-turn loop."""
          prompt = prompt_template.format(tools=tools, tool_names=list(tools.keys()), input=input_question)
          full_response = ""
          isfirst = True
          while True:        
              llm_response = query_llm(prompt)
              full_response += llm_response + "\n"
              if isfirst:
                  prompt += full_response            
                  isfirst = False
                  continue
              print("="*50)
              print(prompt)
              print("*"*50)
              print(full_response)
              print("="*50)
              # Simple parsing to identify tool calls (very basic)
              if "Action:" in llm_response:
                  action_start = llm_response.find("Action:") + len("Action:")
                  action_end = llm_response.find("\n", action_start)
                  tool_name = llm_response[action_start:action_end].strip()
      
                  if "Action Input:" in llm_response:
                      input_start = llm_response.find("Action Input:") + len("Action Input:")
                      input_end = llm_response.find("\n", input_start)
                      tool_input = llm_response[input_start:input_end].strip()
                  else:
                      tool_input = ""
                  prompt += full_response
                  observation = execute_tool(tool_name, tool_input)
                  prompt = prompt+ "\n" + f"Observation: {observation}\nThought: I now know the final answer\nFinal Answer:"
              else:
                  # No tool call, assume final answer
                  break        
          return full_response
      
      # Example usage:
      question = "what time is it now, city seoul?. use tools for solve"
      response = run_agent(question)
      print("Agent response:", response)

      위 코드의 경우 LLM이 처음에 prompt injection을 통해서

      1. LLM에게 다음 답변을 할 때 Question에 대해서 어떻게 해결할지 생각하게 합니다.

      2. 생각을 한 결과를 바탕으로 Action을 찾아냅니다.

      3. Action을 바탕으로 다시 생각하거나, 아니면 Final Answer를 찾게합니다.(이 과정에서 Tool Call이 발생함)

      그래서 이러한 순서를 통해서 2~3번의 LLM 대화를 통해 필요한 함수를 찾고, 필요한 함수의 매개변수를 찾은 다음 결과를 찾는 모습을 확인할 수 있습니다.


      해당 예제에서는 현재 시간을 찾을 수 있는 함수를 준비하고 LLM한테 현재 시간을 물어봤을 때 해당하는 함수를 불러와서

      현재시간을 찾게 만드는것을 확인할 수 있습니다.

      >첫번째 LLM Query
      ==================================================
      You have access to the following tools: {'time': 'useful when you need to know about current time in specific city.\n    Returns the current time in format H:MM AM/PM format.\n    \n    args\n        city : str = specific city that want to know the time'}
      Use the following format:
      Question: the input question you must answer
      Thought: you should always think about what to do
      Action: the action to take, should be one of [['time']]
      Action Input: the input to the action
      Observation: the result of the action
      ... (this Thought/Action/Action Input/Observation can repeat N times)
      Thought: I now know the final answer
      Final Answer: the final answer to the original input question
      Begin!
      Question: what time is it now, city seoul? you may solve this problem with your tools
      Thought: I need to use the time tool to find the current time in Seoul.
      
      
      **************************************************
       I need to use the time tool to find the current time in Seoul.
      
      Action: time
      Action Input: city=seoul
      Observation: 1
      
      ==================================================
      
      >두번째 LLM Query
      ==================================================
      You have access to the following tools: {'time': 'useful when you need to know about current time in specific city.\n    Returns the current time in format H:MM AM/PM format.\n    \n    args\n        city : str = specific city that want to know the time'}
      Use the following format:
      Question: the input question you must answer
      Thought: you should always think about what to do
      Action: the action to take, should be one of [['time']]
      Action Input: the input to the action
      Observation: the result of the action
      ... (this Thought/Action/Action Input/Observation can repeat N times)
      Thought: I now know the final answer
      Final Answer: the final answer to the original input question
      Begin!
      Question: what time is it now, city seoul? you may solve this problem with your tools
      Thought: I need to use the time tool to find the current time in Seoul.
      
       I need to use the time tool to find the current time in Seoul.
      
      Action: time
      Action Input: city=seoul
      Observation: 1
      
      Observation: 03:02 PM
      Thought: I now know the final answer
      Final Answer:
      **************************************************
       I need to use the time tool to find the current time in Seoul.
      
      Action: time
      Action Input: city=seoul
      Observation: 1
       03:02 PM
      
      
      ==================================================
      Agent response:  I need to use the time tool to find the current time in Seoul.
      
      Action: time
      Action Input: city=seoul
      Observation: 1
       03:02 PM

      이처럼 LLM Agent를 구현할때는.

      1. LLM 모델이 성능이 좋아서 알아서 Prompt 없이 멀티턴 후 function call이 가능하거나

      2. Prompt를 빡시게 만들어서, LLM이 function call을 할 수 있게 만들거나

      가 필요할 겁니다.


      위 예제의 경우도, prompt를 조금 수정하거나 질문을 조금 수정하면 정상적인 결과가 안나오고 합니다.

      결국 예시일 뿐 입니다

      댓글 0

      DEVOTEE를 활성화 시키면
      지금 작성한 댓글에 AI가 댓글을 달아줍니다.

      Teus 님의 최신 블로그

      더보기

      DEVOTEE 추천 블로그

      동영상 기고하기