python

[GPT][INVESTORGPT] OpenAI Functions Agent

으누아빠 2024. 5. 20. 19:58
반응형

 

OPENAI_FUNCTIONS

이경우 데이터의 형태를 지정하지 않으면 오류를 발생함

InvalidRequestError: Invalid 'functions[0].name': string does not match pattern. Expected a string that matches the pattern '^[a-zA-Z0-9_-]+$'.

 

from typing import Type
from langchain.chat_models import ChatOpenAI
from langchain.tools import BaseTool
from pydantic import BaseModel,Field
from langchain.agents import initialize_agent, AgentType

llm = ChatOpenAI(
    temperature=0.1,
)

class CalculatorToolArgsSchemea(BaseModel):
    a:float = Field(description="The First number")
    b:float = Field(description="The second number")

class CalculatorTool(BaseTool):
    name = "SumsCalculator"
    description = """
        Use this to perform sums of two numbers.
        The first and second arguments should be numbers.
        Only receives two arguments.
    """
    args_schema: Type[CalculatorToolArgsSchemea] = CalculatorToolArgsSchemea

    def _run(self, a, b):
        return a + b

agent = initialize_agent(
    llm=llm,
    verbose=True,
    agent=AgentType.OPENAI_FUNCTIONS,
    handle_parsing_erros=True,
    tools=[
        CalculatorTool(),
    ],
)

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"

agent.invoke(prompt)

 

pydantic

데이터 유효성 검사는 데이터가 각 속성에 대해 정의한 일련의 규칙, 스키마 또는 제약 조건을 준수하도록 하는 프로세스
데이터 유효성 검사를 통과하면 코드가 예상했던 정확한 방식으로 데이터를 수집하고 반환

데이터 유효성 검사는 잘못된 사용자 입력과 같은 문제로 인해 발생하는 예기치 않은 오류를 방지
그런 의미에서 sanitization process라고도 함

 

Field
모델 스키마 또는 복잡한 유효성 검사를 위해 필드에 대한 추가 정보를 제공하는 데 사용

 

 

'python' 카테고리의 다른 글

[GPT][INVESTORGPT] Search Tool  (0) 2024.05.20
[GPT][INVESTORGPT] Zero-shot ReAct Agent  (0) 2024.05.20
[GPT][INVESTORGPT] Agent  (0) 2024.05.20
[GPT][MEETINGGPT] 최종 예제  (0) 2024.05.14
[GPT][MEETINGGPT] Refine Chain  (0) 2024.05.14