이 노트북은 Tilores tool을 시작하는 방법을 다룹니다. 더 복잡한 예제는 고객 인사이트 챗봇 예제를 확인하세요.

개요

Integration 세부 정보

ClassPackageSerializableJS supportVersion
TiloresToolstilores-langchainPyPI - Version

설정

이 integration에는 다음 패키지가 필요합니다:
pip install --quiet -U tilores-langchain langchain
Note: you may need to restart the kernel to use updated packages.

자격 증명

Tilores에 액세스하려면 인스턴스를 생성하고 구성해야 합니다. Tilores를 먼저 테스트해보고 싶다면 읽기 전용 데모 자격 증명을 사용할 수 있습니다.
import os

os.environ["TILORES_API_URL"] = "<api-url>"
os.environ["TILORES_TOKEN_URL"] = "<token-url>"
os.environ["TILORES_CLIENT_ID"] = "<client-id>"
os.environ["TILORES_CLIENT_SECRET"] = "<client-secret>"

인스턴스화

다음은 Tilores tool의 인스턴스를 생성하는 방법입니다:
from tilores import TiloresAPI
from tilores_langchain import TiloresTools

tilores = TiloresAPI.from_environ()
tilores_tools = TiloresTools(tilores)
search_tool = tilores_tools.search_tool()
edge_tool = tilores_tools.edge_tool()

호출

tilores_search tool의 parameter는 Tilores 내에서 구성된 schema에 따라 달라집니다. 다음 예제는 생성된 데이터가 있는 데모 인스턴스의 schema를 사용합니다.

인자와 함께 직접 호출

다음 예제는 베를린에 있는 Sophie Müller라는 사람을 검색합니다. Tilores 데이터에는 이러한 사람이 여러 명 포함되어 있으며 알려진 이메일 주소와 전화번호를 반환합니다.
result = search_tool.invoke(
    {
        "searchParams": {
            "name": "Sophie Müller",
            "city": "Berlin",
        },
        "recordFieldsToQuery": {
            "email": True,
            "phone": True,
        },
    }
)
print("Number of entities:", len(result["data"]["search"]["entities"]))
for entity in result["data"]["search"]["entities"]:
    print("Number of records:", len(entity["records"]))
    print(
        "Email Addresses:",
        [record["email"] for record in entity["records"] if record.get("email")],
    )
    print(
        "Phone Numbers:",
        [record["phone"] for record in entity["records"] if record.get("phone")],
    )
Number of entities: 3
Number of records: 3
Email Addresses: ['[email protected]', '[email protected]']
Phone Numbers: ['30987654', '30987654', '30987654']
Number of records: 5
Email Addresses: ['[email protected]', '[email protected]', '[email protected]']
Phone Numbers: ['30135792', '30135792']
Number of records: 2
Email Addresses: ['[email protected]']
Phone Numbers: ['30123456', '30123456']
첫 번째 entity의 record들이 어떻게 관련되어 있는지 알고 싶다면 edge_tool을 사용할 수 있습니다. Tilores entity resolution 엔진이 이러한 record들 간의 관계를 자동으로 파악했다는 점에 유의하세요. 자세한 내용은 edge 문서를 참조하세요.
edge_result = edge_tool.invoke(
    {"entityID": result["data"]["search"]["entities"][0]["id"]}
)
edges = edge_result["data"]["entity"]["entity"]["edges"]
print("Number of edges:", len(edges))
print("Edges:", edges)
Number of edges: 7
Edges: ['e1f2g3h4-i5j6-k7l8-m9n0-o1p2q3r4s5t6:f2g3h4i5-j6k7-l8m9-n0o1-p2q3r4s5t6u7:L1', 'e1f2g3h4-i5j6-k7l8-m9n0-o1p2q3r4s5t6:g3h4i5j6-k7l8-m9n0-o1p2-q3r4s5t6u7v8:L4', 'e1f2g3h4-i5j6-k7l8-m9n0-o1p2q3r4s5t6:f2g3h4i5-j6k7-l8m9-n0o1-p2q3r4s5t6u7:L2', 'f2g3h4i5-j6k7-l8m9-n0o1-p2q3r4s5t6u7:g3h4i5j6-k7l8-m9n0-o1p2-q3r4s5t6u7v8:L1', 'f2g3h4i5-j6k7-l8m9-n0o1-p2q3r4s5t6u7:g3h4i5j6-k7l8-m9n0-o1p2-q3r4s5t6u7v8:L4', 'e1f2g3h4-i5j6-k7l8-m9n0-o1p2q3r4s5t6:g3h4i5j6-k7l8-m9n0-o1p2-q3r4s5t6u7v8:L1', 'e1f2g3h4-i5j6-k7l8-m9n0-o1p2q3r4s5t6:f2g3h4i5-j6k7-l8m9-n0o1-p2q3r4s5t6u7:L4']

ToolCall과 함께 호출

model이 생성한 ToolCall로 tool을 호출할 수도 있으며, 이 경우 ToolMessage가 반환됩니다:
# This is usually generated by a model, but we'll create a tool call directly for demo purposes.
model_generated_tool_call = {
    "args": {
        "searchParams": {
            "name": "Sophie Müller",
            "city": "Berlin",
        },
        "recordFieldsToQuery": {
            "email": True,
            "phone": True,
        },
    },
    "id": "1",
    "name": search_tool.name,
    "type": "tool_call",
}
search_tool.invoke(model_generated_tool_call)
ToolMessage(content='{"data": {"search": {"entities": [{"id": "9601cf3b-e85f-46ab-aaa8-ffb8b46f1c5b", "hits": {"c3d4e5f6-g7h8-i9j0-k1l2-m3n4o5p6q7r8": ["L1"]}, "records": [{"email": "", "phone": "30123456"}, {"email": "[email protected]", "phone": "30123456"}]}, {"id": "03da2e11-0aa2-4d17-8aaa-7b32c52decd9", "hits": {"e1f2g3h4-i5j6-k7l8-m9n0-o1p2q3r4s5t6": ["L1"], "g3h4i5j6-k7l8-m9n0-o1p2-q3r4s5t6u7v8": ["L1"]}, "records": [{"email": "[email protected]", "phone": "30987654"}, {"email": "", "phone": "30987654"}, {"email": "[email protected]", "phone": "30987654"}]}, {"id": "4d896fb5-0d08-4212-a043-b5deb0347106", "hits": {"j6k7l8m9-n0o1-p2q3-r4s5-t6u7v8w9x0y1": ["L1"], "l8m9n0o1-p2q3-r4s5-t6u7-v8w9x0y1z2a3": ["L1"], "m9n0o1p2-q3r4-s5t6-u7v8-w9x0y1z2a3b4": ["L1"], "n0o1p2q3-r4s5-t6u7-v8w9-x0y1z2a3b4c5": ["L1"]}, "records": [{"email": "[email protected]", "phone": ""}, {"email": "[email protected]", "phone": ""}, {"email": "", "phone": "30135792"}, {"email": "", "phone": ""}, {"email": "[email protected]", "phone": "30135792"}]}]}}}', name='tilores_search', tool_call_id='1')

체이닝

먼저 tool-calling model에 tool을 바인딩한 다음 호출하여 chain에서 tool을 사용할 수 있습니다:
# | output: false
# | echo: false

# !pip install -qU langchain langchain-openai
from langchain.chat_models import init_chat_model

model = init_chat_model(model="gpt-4o", model_provider="openai")
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnableConfig, chain

prompt = ChatPromptTemplate(
    [
        ("system", "You are a helpful assistant."),
        ("human", "{user_input}"),
        ("placeholder", "{messages}"),
    ]
)

# specifying tool_choice will force the model to call this tool.
model_with_tools = model.bind_tools([search_tool], tool_choice=search_tool.name)

model_chain = prompt | model_with_tools


@chain
def tool_chain(user_input: str, config: RunnableConfig):
    input_ = {"user_input": user_input}
    ai_msg = model_chain.invoke(input_, config=config)
    tool_msgs = search_tool.batch(ai_msg.tool_calls, config=config)
    return model_chain.invoke({**input_, "messages": [ai_msg, *tool_msgs]}, config=config)


tool_chain.invoke("Tell me the email addresses from Sophie Müller from Berlin.")

API reference

모든 Tilores 기능 및 구성에 대한 자세한 문서는 공식 문서를 참조하세요: docs.tilotech.io/tilores/
Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.
I