dspy.Tool

dspy.Tool

dspy.Tool은 LLM의 도구 호출(tool calling / function calling)을 위한 도구를 간단히 만들 수 있게 해 주는 클래스입니다. 현재는 함수만 지원합니다.

출처: 문서

본문

dspy.Tool(
    func: Callable,
    name: str | None = None,
    desc: str | None = None,
    args: dict[str, Any] | None = None,
    arg_types: dict[str, Any] | None = None,
    arg_desc: dict[str, str] | None = None,
)
  • Bases: Type

Tool 클래스입니다. LLM의 도구 호출(함수 호출)을 위한 도구 생성을 단순화합니다. 현재는 함수만 지원합니다.

name, desc, args, arg_types를 직접 지정하거나 함수로부터 자동 추론하게 할 수 있습니다. 사용자가 지정한 값에 대해서는 자동 추론이 수행되지 않습니다.

소스 코드는 dspy/adapters/types/tool.py에 있습니다.

Methods

__call__(**kwargs)

인자를 검증·파싱한 뒤 함수를 호출합니다. 결과가 코루틴(async)이면 settings.allow_tool_async_sync_conversion이 켜져 있을 때 비동기를 동기로 변환해 실행하고, 꺼져 있으면 acall을 쓰라는 ValueError를 발생시킵니다.

acall(**kwargs)

비동기 호출 경로입니다. 함수가 코루틴을 반환하면 await하고, 아니면(동기 함수) 그대로 반환합니다.

from_langchain(tool: BaseTool) -> Tool (classmethod)

LangChain 도구에서 DSPy 도구를 만듭니다.

import asyncio
import dspy
from langchain.tools import tool as lc_tool

@lc_tool
def add(x: int, y: int):
    "Add two numbers together."
    return x + y

dspy_tool = dspy.Tool.from_langchain(add)

from_mcp_tool(session, tool, *, result_mode='text') -> Tool (classmethod)

MCP 도구와 호환되는 MCP 클라이언트에서 DSPy 도구를 만듭니다. session은 비동기 call_tool 메서드를 가진 MCP 클라이언트/세션입니다. result_mode가 "text"면 기존의 text/non-text 변환을 유지하고, "structured"면 MCP 구조화 콘텐츠가 존재할 때 그대로 반환합니다.

format_as_litellm_function_call()

LiteLLM 함수 호출 형식으로 변환합니다. {"type": "function", "function": {"name", "description", "parameters"}} 딕셔너리를 반환하며, default가 없는 인자가 required 목록이 됩니다.

description() -> str (classmethod)

커스텀 타입의 설명을 반환합니다.

표준 상속 메서드

adapt_to_native_lm_feature(...), extract_custom_type_from_annotation(annotation), format(), serialize_model(), parse_lm_response(...), parse_stream_chunk(...), is_streamable() 등은 모든 커스텀 타입이 공유하는 base_type.py의 표준 메서드입니다.

더 알아보기 (Learn more)