Anthropic と Databricks Unity Catalog ツールの統合
Databricks Unity Catalog を使用して、SQL 関数と Python 関数を Anthropic SDK LLM 呼び出しのツールとして統合します。この統合により、Unity Catalog のガバナンスと Anthropic モデルが組み合わされ、強力なジェネレーション AI アプリが作成されます。
必要条件
- Databricks Runtime 15.0 以降を使用します。
Unity Catalog ツールと Anthropic の統合
ノートブックまたは Python スクリプトで次のコードを実行して Unity Catalog ツールを作成し、Anthropic モデルを呼び出すときに使用します。
-
Anthropic用のDatabricks Unity Catalog統合パッケージをインストールします。
Python%pip install unitycatalog-anthropic[databricks]
dbutils.library.restartPython() -
Unity Catalog 関数クライアントのインスタンスを作成します。
Pythonfrom unitycatalog.ai.core.base import get_uc_function_client
client = get_uc_function_client() -
Python で記述された Unity Catalog 関数を作成します。
PythonCATALOG = "your_catalog"
SCHEMA = "your_schema"
func_name = f"{CATALOG}.{SCHEMA}.weather_function"
def weather_function(location: str) -> str:
"""
Fetches the current weather from a given location in degrees Celsius.
Args:
location (str): The location to fetch the current weather from.
Returns:
str: The current temperature for the location provided in Celsius.
"""
return f"The current temperature for {location} is 24.5 celsius"
client.create_python_function(
func=weather_function,
catalog=CATALOG,
schema=SCHEMA,
replace=True
) -
Unity Catalog 関数のインスタンスをツールキットとして作成します。
Pythonfrom unitycatalog.ai.anthropic.toolkit import UCFunctionToolkit
# Create an instance of the toolkit
toolkit = UCFunctionToolkit(function_names=[func_name], client=client) -
Anthropicでツールコールを使用します。
Pythonimport anthropic
# Initialize the Anthropic client with your API key
anthropic_client = anthropic.Anthropic(api_key="YOUR_ANTHROPIC_API_KEY")
# User's question
question = [{"role": "user", "content": "What's the weather in New York City?"}]
# Make the initial call to Anthropic
response = anthropic_client.messages.create(
model="claude-3-5-sonnet-20240620", # Specify the model
max_tokens=1024, # Use 'max_tokens' instead of 'max_tokens_to_sample'
tools=toolkit.tools,
messages=question # Provide the conversation history
)
# Print the response content
print(response) -
ツール応答を作成します。Claude モデルからの応答には、ツールを呼び出す必要がある場合に、ツール要求メタデータ ブロックが含まれます。
from unitycatalog.ai.anthropic.utils import generate_tool_call_messages
# Call the UC function and construct the required formatted response
tool_messages = generate_tool_call_messages(
response=response,
client=client,
conversation_history=question
)
# Continue the conversation with Anthropic
tool_response = anthropic_client.messages.create(
model="claude-3-5-sonnet-20240620",
max_tokens=1024,
tools=toolkit.tools,
messages=tool_messages,
)
print(tool_response)
unitycatalog.ai-anthropic
パッケージには、Unity Catalog 関数の呼び出しの解析と処理を簡略化するメッセージ ハンドラー ユーティリティが含まれています。このユーティリティは、次の処理を行います。
- ツール呼び出しの要件を検出します。
- クエリからツール呼び出し情報を抽出します。
- Unity Catalog 関数の呼び出しを実行します。
- Unity Catalog 関数からの応答を解析します。
- 次のメッセージ形式を作成して、Claude との会話を続けます。
会話履歴全体は、generate_tool_call_messages
API の conversation_history
引数で指定する必要があります。Claude モデルでは、会話 (元のユーザー入力の質問) と、その後のすべての LLM で生成された応答とマルチターン ツール呼び出し結果の初期化が必要です。