次の方法で共有


Anthropic と Databricks Unity カタログ ツールの統合

Databricks Unity カタログを使用して、Anthropic SDK LLM 呼び出しで SQL 関数と Python 関数をツールとして統合します。 この統合により、Unity Catalog のガバナンスと Anthropic モデルが組み合わせ、強力な Gen AI アプリが作成されます。

必要条件

  • Databricks Runtime 15.0 以降を使用します。

Unity カタログ ツールと Anthropic の統合

ノートブックまたは Python スクリプトで次のコードを実行して Unity カタログ ツールを作成し、Anthropic モデルの呼び出し中に使用します。

  1. Anthropic 用の Databricks Unity カタログ統合パッケージをインストールします。

    %pip install unitycatalog-anthropic[databricks]
    dbutils.library.restartPython()
    
  2. Unity カタログ関数クライアントのインスタンスを作成します。

    from unitycatalog.ai.core.base import get_uc_function_client
    
    client = get_uc_function_client()
    
  3. Python で記述された Unity カタログ関数を作成します。

    CATALOG = "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
    )
    
  4. ツールキットとして Unity Catalog 関数のインスタンスを作成します。

    from unitycatalog.ai.anthropic.toolkit import UCFunctionToolkit
    
    # Create an instance of the toolkit
    toolkit = UCFunctionToolkit(function_names=[func_name], client=client)
    
  5. Anthropic でツール呼び出しを使用します。

    import 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)
    
  6. ツールの応答を作成します。 ツールを呼び出す必要がある場合、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 関数の呼び出しの解析と処理を簡略化するためのメッセージ ハンドラー ユーティリティが含まれています。 このユーティリティは、次の処理を行います。

  1. ツール呼び出しの要件を検出します。
  2. クエリからツールの呼び出し情報を抽出します。
  3. Unity カタログ関数の呼び出しを実行します。
  4. Unity Catalog 関数からの応答を解析します。
  5. 次のメッセージ形式を作成して、Claude との会話を続けます。

会話履歴全体は、conversation_history API のgenerate_tool_call_messages引数で指定する必要があります。 クロード モデルでは、会話 (元のユーザー入力質問) の初期化と、後続のすべての LLM 生成応答とマルチターン ツール呼び出し結果が必要です。