본문 바로가기

LLM

LangChain - Agents

언어 모델을 추론 엔진으로 사용하는 방법

 

AgentType은 사용하고 싶은 에이전트 유형 지정하는데 사용됨

사용할 언어 모델 초기화 (추론 엔진으로 사용)

 

에이전트 초기화

도구, 언어모델, 에이전트 타입 (CHAT_ZERO_SHOT_REACT_DESCRIPTIO)

 

React는 언어 모델로부터 더 나은 생각을 이끌어내는 프롬프팅 전략

"handle_parsing_errors=True"

LLM 출력인 문자열 분석해 사용할 수 있는 특정 형식으로 변환할 수 있는지는 매우 중요

 

 

question = "Tom M. Mitchell is an American computer scientist \
and the Founders University Professor at Carnegie Mellon University (CMU)\
what book did he write?"
result = agent(question)
> Entering new AgentExecutor chain...
Thought: I should use Wikipedia to find out which book Tom M. Mitchell wrote.
Action:
```
{
  "action": "Wikipedia",
  "action_input": "Tom M. Mitchell"
}
```
Observation: Page: Tom M. Mitchell
Summary: Tom Michael Mitchell (born August 9, 1951) is an American computer scientist and the Founders University Professor at Carnegie Mellon University (CMU). He is a founder and former chair of the Machine Learning Department at CMU. Mitchell is known for his contributions to the advancement of machine learning, artificial intelligence, and cognitive neuroscience and is the author of the textbook Machine Learning. He is a member of the United States National Academy of Engineering since 2010. He is also a Fellow of the American Academy of Arts and Sciences, the American Association for the Advancement of Science and a Fellow and past president of the Association for the Advancement of Artificial Intelligence. In October 2018, Mitchell was appointed as the Interim Dean of the School of Computer Science at Carnegie Mellon.

Page: Inferential theory of learning
Summary: Inferential Theory of Learning (ITL) is an area of machine learning which describes inferential processes performed by learning agents. ITL has been continuously developed by Ryszard S. Michalski, starting in the 1980s. The first known publication of ITL was in 1983. In the ITL learning process is viewed as a search (inference) through hypotheses space guided by a specific goal. The results of learning need to be stored. Stored information will later be used by the learner for future inferences. Inferences are split into multiple categories including conclusive, deduction, and induction. In order for an inference to be considered complete it was required that all categories must be taken into account. This is how the ITL varies from other machine learning theories like Computational Learning Theory and Statistical Learning Theory; which both use singular forms of inference.


Thought:I have found that Tom M. Mitchell wrote the textbook "Machine Learning." I can provide this as the final answer.
Final Answer: Machine Learning

> Finished chain.

 

 

위키피디아 검색 수행 결과 ~~

 

PythonREPLTool

에이전트는 REPL을 통해 코드 실행하고 결과 받음

결과는 다시 에이전트로 보내지고 다음에 무엇을 실행할지 결정

 

머 이런식으로 결과 출력되는데 sorted()가 제대로 동작 안돼서 에러뜸(ㅎㅎ;)

 

langchain.debug 사용해 좀 더 디테일하게 살펴볼 수 있음

 

에이전트 호출하고 오늘 날짜 무엇인지 물어보기

에이전트는 시간 도구를 사용해야 한다는 것 인식하고 그것을 명시함

 

[chain/start] [1:chain:AgentExecutor] Entering Chain run with input:
{
  "input": "whats the date today?"
}
[chain/start] [1:chain:AgentExecutor > 2:chain:LLMChain] Entering Chain run with input:
{
  "input": "whats the date today?",
  "agent_scratchpad": "",
  "stop": [
    "Observation:"
  ]
}
[llm/start] [1:chain:AgentExecutor > 2:chain:LLMChain > 3:llm:ChatOpenAI] Entering LLM run with input:
{
  "prompts": [
    "System: Answer the following questions as best you can. You have access to the following tools:\n\nCalculator: Useful for when you need to answer questions about math.\nWikipedia: A wrapper around Wikipedia. Useful for when you need to answer general questions about people, places, companies, facts, historical events, or other subjects. Input should be a search query.\ntime: time(text: str) -> str - Returns todays date, use this for any     questions related to knowing todays date.     The input should always be an empty string,     and this function will always return todays     date - any date mathmatics should occur     outside this function.\n\nThe way you use the tools is by specifying a json blob.\nSpecifically, this json should have a `action` key (with the name of the tool to use) and a `action_input` key (with the input to the tool going here).\n\nThe only values that should be in the \"action\" field are: Calculator, Wikipedia, time\n\nThe $JSON_BLOB should only contain a SINGLE action, do NOT return a list of multiple actions. Here is an example of a valid $JSON_BLOB:\n\n```\n{\n  \"action\": $TOOL_NAME,\n  \"action_input\": $INPUT\n}\n```\n\nALWAYS use the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction:\n```\n$JSON_BLOB\n```\nObservation: the result of the action\n... (this Thought/Action/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\nBegin! Reminder to always use the exact characters `Final Answer` when responding.\nHuman: whats the date today?"
  ]
}
[llm/end] [1:chain:AgentExecutor > 2:chain:LLMChain > 3:llm:ChatOpenAI] [67.103ms] Exiting LLM run with output:
{
  "generations": [
    [
      {
        "text": "Thought: I can use the `time` tool to find out today's date.\nAction:\n```\n{\n  \"action\": \"time\",\n  \"action_input\": \"\"\n}\n```\n",
        "generation_info": null,
        "message": {
          "content": "Thought: I can use the `time` tool to find out today's date.\nAction:\n```\n{\n  \"action\": \"time\",\n  \"action_input\": \"\"\n}\n```\n",
          "additional_kwargs": {},
          "example": false
        }
      }
    ]
  ],
  "llm_output": {
    "token_usage": {
      "prompt_tokens": 375,
      "completion_tokens": 38,
      "total_tokens": 413
    },
    "model_name": "gpt-3.5-turbo"
  }
}
[chain/end] [1:chain:AgentExecutor > 2:chain:LLMChain] [67.494ms] Exiting Chain run with output:
{
  "text": "Thought: I can use the `time` tool to find out today's date.\nAction:\n```\n{\n  \"action\": \"time\",\n  \"action_input\": \"\"\n}\n```\n"
}
[tool/start] [1:chain:AgentExecutor > 4:tool:time] Entering Tool run with input:
""
[tool/end] [1:chain:AgentExecutor > 4:tool:time] [0.127ms] Exiting Tool run with output:
"2026-01-19"
[chain/start] [1:chain:AgentExecutor > 5:chain:LLMChain] Entering Chain run with input:
{
  "input": "whats the date today?",
  "agent_scratchpad": "This was your previous work (but I haven't seen any of it! I only see what you return as final answer):\nThought: I can use the `time` tool to find out today's date.\nAction:\n```\n{\n  \"action\": \"time\",\n  \"action_input\": \"\"\n}\n```\n\nObservation: 2026-01-19\nThought:",
  "stop": [
    "Observation:"
  ]
}
[llm/start] [1:chain:AgentExecutor > 5:chain:LLMChain > 6:llm:ChatOpenAI] Entering LLM run with input:
{
  "prompts": [
    "System: Answer the following questions as best you can. You have access to the following tools:\n\nCalculator: Useful for when you need to answer questions about math.\nWikipedia: A wrapper around Wikipedia. Useful for when you need to answer general questions about people, places, companies, facts, historical events, or other subjects. Input should be a search query.\ntime: time(text: str) -> str - Returns todays date, use this for any     questions related to knowing todays date.     The input should always be an empty string,     and this function will always return todays     date - any date mathmatics should occur     outside this function.\n\nThe way you use the tools is by specifying a json blob.\nSpecifically, this json should have a `action` key (with the name of the tool to use) and a `action_input` key (with the input to the tool going here).\n\nThe only values that should be in the \"action\" field are: Calculator, Wikipedia, time\n\nThe $JSON_BLOB should only contain a SINGLE action, do NOT return a list of multiple actions. Here is an example of a valid $JSON_BLOB:\n\n```\n{\n  \"action\": $TOOL_NAME,\n  \"action_input\": $INPUT\n}\n```\n\nALWAYS use the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction:\n```\n$JSON_BLOB\n```\nObservation: the result of the action\n... (this Thought/Action/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\nBegin! Reminder to always use the exact characters `Final Answer` when responding.\nHuman: whats the date today?\n\nThis was your previous work (but I haven't seen any of it! I only see what you return as final answer):\nThought: I can use the `time` tool to find out today's date.\nAction:\n```\n{\n  \"action\": \"time\",\n  \"action_input\": \"\"\n}\n```\n\nObservation: 2026-01-19\nThought:"
  ]
}
[llm/end] [1:chain:AgentExecutor > 5:chain:LLMChain > 6:llm:ChatOpenAI] [65.41ms] Exiting LLM run with output:
{
  "generations": [
    [
      {
        "text": "Final Answer",
        "generation_info": null,
        "message": {
          "content": "Final Answer",
          "additional_kwargs": {},
          "example": false
        }
      }
    ]
  ],
  "llm_output": {
    "token_usage": {
      "prompt_tokens": 451,
      "completion_tokens": 2,
      "total_tokens": 453
    },
    "model_name": "gpt-3.5-turbo"
  }
}
[chain/end] [1:chain:AgentExecutor > 5:chain:LLMChain] [65.78ms] Exiting Chain run with output:
{
  "text": "Final Answer"
}
[tool/start] [1:chain:AgentExecutor > 7:tool:_Exception] Entering Tool run with input:
"Invalid or incomplete response"
[tool/end] [1:chain:AgentExecutor > 7:tool:_Exception] [0.077ms] Exiting Tool run with output:
"Invalid or incomplete response"
[chain/start] [1:chain:AgentExecutor > 8:chain:LLMChain] Entering Chain run with input:
{
  "input": "whats the date today?",
  "agent_scratchpad": "This was your previous work (but I haven't seen any of it! I only see what you return as final answer):\nThought: I can use the `time` tool to find out today's date.\nAction:\n```\n{\n  \"action\": \"time\",\n  \"action_input\": \"\"\n}\n```\n\nObservation: 2026-01-19\nThought:Could not parse LLM output: Final Answer\nObservation: Invalid or incomplete response\nThought:",
  "stop": [
    "Observation:"
  ]
}
[llm/start] [1:chain:AgentExecutor > 8:chain:LLMChain > 9:llm:ChatOpenAI] Entering LLM run with input:
{
  "prompts": [
    "System: Answer the following questions as best you can. You have access to the following tools:\n\nCalculator: Useful for when you need to answer questions about math.\nWikipedia: A wrapper around Wikipedia. Useful for when you need to answer general questions about people, places, companies, facts, historical events, or other subjects. Input should be a search query.\ntime: time(text: str) -> str - Returns todays date, use this for any     questions related to knowing todays date.     The input should always be an empty string,     and this function will always return todays     date - any date mathmatics should occur     outside this function.\n\nThe way you use the tools is by specifying a json blob.\nSpecifically, this json should have a `action` key (with the name of the tool to use) and a `action_input` key (with the input to the tool going here).\n\nThe only values that should be in the \"action\" field are: Calculator, Wikipedia, time\n\nThe $JSON_BLOB should only contain a SINGLE action, do NOT return a list of multiple actions. Here is an example of a valid $JSON_BLOB:\n\n```\n{\n  \"action\": $TOOL_NAME,\n  \"action_input\": $INPUT\n}\n```\n\nALWAYS use the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction:\n```\n$JSON_BLOB\n```\nObservation: the result of the action\n... (this Thought/Action/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\nBegin! Reminder to always use the exact characters `Final Answer` when responding.\nHuman: whats the date today?\n\nThis was your previous work (but I haven't seen any of it! I only see what you return as final answer):\nThought: I can use the `time` tool to find out today's date.\nAction:\n```\n{\n  \"action\": \"time\",\n  \"action_input\": \"\"\n}\n```\n\nObservation: 2026-01-19\nThought:Could not parse LLM output: Final Answer\nObservation: Invalid or incomplete response\nThought:"
  ]
}
[llm/end] [1:chain:AgentExecutor > 8:chain:LLMChain > 9:llm:ChatOpenAI] [75.324ms] Exiting LLM run with output:
{
  "generations": [
    [
      {
        "text": "I will correct the previous action to get today's date properly.\nAction:\n```\n{\n  \"action\": \"time\",\n  \"action_input\": \"\"\n}\n``` \nFinal Answer: 2026-01-19",
        "generation_info": null,
        "message": {
          "content": "I will correct the previous action to get today's date properly.\nAction:\n```\n{\n  \"action\": \"time\",\n  \"action_input\": \"\"\n}\n``` \nFinal Answer: 2026-01-19",
          "additional_kwargs": {},
          "example": false
        }
      }
    ]
  ],
  "llm_output": {
    "token_usage": {
      "prompt_tokens": 471,
      "completion_tokens": 44,
      "total_tokens": 515
    },
    "model_name": "gpt-3.5-turbo"
  }
}
[chain/end] [1:chain:AgentExecutor > 8:chain:LLMChain] [75.71600000000001ms] Exiting Chain run with output:
{
  "text": "I will correct the previous action to get today's date properly.\nAction:\n```\n{\n  \"action\": \"time\",\n  \"action_input\": \"\"\n}\n``` \nFinal Answer: 2026-01-19"
}
[chain/end] [1:chain:AgentExecutor] [210.399ms] Exiting Chain run with output:
{
  "output": "2026-01-19"
}

action input이 빈 문자열로 되어 있음 -> 그렇게 지시했으니!

관찰 결과 반환.

 

 

'LLM' 카테고리의 다른 글

RAG - Multimodal search  (0) 2026.02.15
RAG - Overview of multimodality  (0) 2026.02.13
LangChain - Evaluation  (0) 2026.01.19
LangChain - Q&A over Documents  (0) 2026.01.19
LangChain - Chains  (0) 2026.01.12

Tiny Star