python

[GPT][INVESTORGPT] Agent

으누아빠 2024. 5. 20. 14:17
반응형


> Entering new AgentExecutor chain...
Thought: I can use the Sum Calculator tool to find the total cost of all the given amounts. Action: ``` { "action": "Sum Calculator", "action_input": {"a": 355.39, "b": 924.87} } ``` Observation: 1280.26 Thought:``` { "action": "Sum Calculator", "action_input": {"a": 1280.26, "b": 721.2} } ``` Observation: 2001.46 Thought:``` { "action": "Sum Calculator", "action_input": {"a": 2001.46, "b": 1940.29} } ``` Observation: 3941.75 Thought:``` { "action": "Sum Calculator", "action_input": {"a": 3941.75, "b": 573.63} } ``` Observation: 4515.38 Thought:``` { "action": "Sum Calculator", "action_input": {"a": 4515.38, "b": 65.72} } ``` Observation: 4581.1 Thought:``` { "action": "Sum Calculator", "action_input": {"a": 4581.1, "b": 35.00} } ``` Observation: 4616.1 Thought:``` { "action": "Sum Calculator", "action_input": {"a": 4616.1, "b": 552.00} } ``` Observation: 5168.1 Thought:``` { "action": "Sum Calculator", "action_input": {"a": 5168.1, "b": 76.16} } ``` Observation: 5244.26 Thought:``` { "action": "Sum Calculator", "action_input": {"a": 5244.26, "b": 29.12} }

 

{'input': 'Tell me Total Cost of $355.39 + $924.87 + $721.2 + $1940.29 + $573.63 + $65.72 + $35.00 + $552.00 + $76.16 + $29.12', 'output': 'I have calculated the total cost of all the given amounts. The final answer is $5273.38.'}

 

 

from langchain.chat_models import ChatOpenAI
from langchain.tools import StructuredTool
from langchain.agents import initialize_agent, AgentType

llm = ChatOpenAI(
    temperature=0.1,
)

def plus(a, b):
    return a + b

agent = initialize_agent(
    llm=llm,
    verbose=True,
    agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
    tools=[
        StructuredTool.from_function(
            func=plus,
            name="Sum Calculator",
            description="Use this to perform sums of two numbers. This tool take two arguments, both should be numbers. till finish.",
        )
    ],
)

prompt = "Tell me Total Cost of $355.39 + $924.87 + $721.2 + $1940.29 + $573.63 + $65.72 + $35.00 + $552.00 + $76.16 + $29.12"

#llm.invoke(prompt)
agent.invoke(prompt)

 

llm 은 연산을 목적으로 만들어진것이 아니기 때문에 잘못된 정보를 호출하게됨. 이를 막기 위해서 agent를 이용하여

정상적인 답이 나오도록 수정할수 있음

 

agent

에이전트의 핵심 아이디어는 언어 모델을 사용하여 수행할 일련의 작업을 선택

체인에서는 일련의 작업이 코드로 하드코딩됨

에이전트에서 언어 모델은 수행할 작업과 순서를 결정하는 추론 엔진으로 사용됩니다.

즉 agent는 체인이 사용할 도구를 선택해서 지원할 수 있도록하며 LLM을 사용하여 원하는 작업을 수행하는 절차를 정의 하며, 이때 어떠한 도구를 사용하여 목적을 달성할 것인지를 스스로 판단하여 사용
사용자는 구현된 tool이 어떠한 동작을 하는지 정확하게 description 해주기만 하면 알아서 가져다가 쓰도록 되어 있음


StructuredTool
에이전트가 수행할 수 있는 작업을 지정해주는  기능을 함


func - 툴로 등록할 함수 이름을 지정, 
name - 툴의 이름,
description - 툴에 대한 설명을 추가  


AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION
이 에이전트는 여러 입력이 있는 도구를 호출

'python' 카테고리의 다른 글

[GPT][INVESTORGPT] OpenAI Functions Agent  (0) 2024.05.20
[GPT][INVESTORGPT] Zero-shot ReAct Agent  (0) 2024.05.20
[GPT][MEETINGGPT] 최종 예제  (0) 2024.05.14
[GPT][MEETINGGPT] Refine Chain  (0) 2024.05.14
[GPT][MEETINGGPT] Whisper Transcript  (0) 2024.05.14