포스트

InjectedToolArg 사용법

여기에 입력하면 된다.


Hiding arguments from the model

  • 모델이 민감한 정보를 생성하거나 조작하지 않도록 하기 위해서 모델이 생성하지 못하도록 argument 를 숨겨주는 역할을 한다.
  • 혹은, 고정값을 주입하기 위해서 사용한다. (Runtime Injection)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from typing import List

from langchain_core.tools import InjectedToolArg, tool
from typing_extensions import Annotated

user_to_pets = {}

@tool(parse_docstring=True) # 함수의 docstring 을 읽음
def update_favorite_pets(
    pets: List[str], user_id: Annotated[str, InjectedToolArg]
) -> None:
    """Add the list of favorite pets.
    Args:
        pets: List of favorite pets to set.
        user_id: User's ID
    """
    user_to_pets[user_id] = pets

@tool(parse_docstring=True)
def delete_favorite_pets(user_id: Annotated[str, InjectedToolArg]) -> None:
    """Delete the list of favorite pets.
    Args:
        user_id: User's ID.
    """
    if user_id in user_to_pets:
        del user_to_pets[user_id]

@tool(parse_docstring=True)
def list_favorite_pets(user_id: Annotated[str, InjectedToolArg]) -> None:
    """List favorite pets if any.
    Args:
        user_id: User's ID.
    """
    return user_to_pets.get(user_id, [])

get_input_schema()

  • 도구의 전체 입력 스키마를 반환한다.
  • 즉, 함수가 필요로 하는 모든 입력 매개변수(인자)들이 정의되어 있다.
  • 이는 직접 호출할때 (invoke()) 필요한 입력을 보여준다.
1
update_favorite_pets.get_input_schema().model_json_schema()
1
{'description': 'Add the list of favorite pets.', 'properties': {'pets': {'description': 'List of favorite pets to set.', 'items': {'type': 'string'}, 'title': 'Pets', 'type': 'array'}, 'user_id': {'description': "User's ID", 'title': 'User Id', 'type': 'string'}}, 'required': ['pets', 'user_id'], 'title': 'update_favorite_pets', 'type': 'object'}
  • iunput schemas 를 살펴보면 user_id 가 존재한다.

tool_call_schema

  • 모델이 도구를 호출할 떄 사용할 스키마를 정의한다.
  • LLM 이 자동으로 도구를 사용할 때, 어떤 매개변수를 생성해야 하는지를 결정한다.
1
update_favorite_pets.tool_call_schema.model_json_schema()
1
{'description': 'Add the list of favorite pets.', 'properties': {'pets': {'description': 'List of favorite pets to set.', 'items': {'type': 'string'}, 'title': 'Pets', 'type': 'array'}}, 'required': ['pets'], 'title': 'update_favorite_pets', 'type': 'object'}
  • user_id 가 존재하지 않는다.

따라서 invoke 를 사용하여 Tool 을 사용할 경우 user_id 가 반드시 필요하다.

1
2
3
4
5
user_id = "123"
update_favorite_pets.invoke({"pets": ["lizard", "dog"], "user_id": user_id})

print(user_to_pets)
print(list_favorite_pets.invoke({"user_id": user_id}))
1
2
{'123': ['lizard', 'dog']}
['lizard', 'dog']

LLM 에 적용

1
2
3
4
5
6
7
8
tools = [
    update_favorite_pets,
    delete_favorite_pets,
    list_favorite_pets,
]
llm_with_tools = llm.bind_tools(tools)
ai_msg = llm_with_tools.invoke("my favorite animals are cats and parrots")
ai_msg.tool_calls
1
[{'name': 'update_favorite_pets', 'args': {'pets': ['cats', 'parrots']}, 'id': 'call_c1gd70ab', 'type': 'tool_call'}]
  • LLM 이 Tool_calling 을 통해 schema 를 return 할때는 user_id 가 없는 것을 확인할 수 있다.

코드

코드 주소

Google AdSense — Post Ad
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.

Comments powered by Disqus.