다음을 통해 공유


Databricks Unity 카탈로그 도구와 Anthropic 통합

Databricks Unity 카탈로그를 사용하여 SQL 및 Python 함수를 Anthropic SDK LLM 호출의 도구로 통합합니다. 이 통합은 Unity 카탈로그의 거버넌스와 Anthropic 모델을 결합하여 강력한 세대 AI 앱을 만듭니다.

요구 사항

  • Databricks Runtime 15.0 이상을 사용합니다.

Anthropic과 Unity 카탈로그 도구 통합

Notebook 또는 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 카탈로그 함수의 인스턴스를 도구 키트로 만듭니다.

    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 카탈로그 함수에 대한 호출의 구문 분석 및 처리를 간소화하는 메시지 처리기 유틸리티가 포함되어 있습니다. 유틸리티는 다음을 수행합니다.

  1. 도구 호출 요구 사항을 검색합니다.
  2. 쿼리에서 도구 호출 정보를 추출합니다.
  3. Unity 카탈로그 함수에 대한 호출을 수행합니다.
  4. Unity 카탈로그 함수의 응답을 구문 분석합니다.
  5. 다음 메시지 형식을 작성하여 Claude와의 대화를 계속합니다.

비고

API에 대한 인수 conversation_historygenerate_tool_call_messages 전체 대화 기록을 제공해야 합니다. Claude 모델에는 대화의 초기화(원래 사용자 입력 질문) 및 모든 후속 LLM 생성 응답 및 다중 턴 도구 호출 결과가 필요합니다.