이 노트북은 LangChain과 LangGraph agent API 모두에서 UC function을 LangChain tool로 사용하는 방법을 보여줍니다.UC에서 SQL 또는 Python function을 생성하는 방법을 알아보려면 Databricks 문서(AWS|Azure|GCP)를 참조하세요. LLM이 function을 올바르게 호출하는 데 중요한 function 및 parameter 주석을 생략하지 마세요.이 예제 노트북에서는 임의의 코드를 실행하는 간단한 Python function을 생성하고 이를 LangChain tool로 사용합니다:
Copy
CREATE FUNCTION main.tools.python_exec ( code STRING COMMENT 'Python code to execute. Remember to print the final result to stdout.')RETURNS STRINGLANGUAGE PYTHONCOMMENT 'Executes Python code and returns its stdout.'AS $$ import sys from io import StringIO stdout = StringIO() sys.stdout = stdout exec(code) return stdout.getvalue()$$
이는 Databricks SQL warehouse 내의 안전하고 격리된 환경에서 실행됩니다.
Note: you may need to restart the kernel to use updated packages.
Copy
from databricks_langchain import ChatDatabricksllm = ChatDatabricks(endpoint="databricks-meta-llama-3-70b-instruct")
Copy
from databricks_langchain.uc_ai import ( DatabricksFunctionClient, UCFunctionToolkit, set_uc_function_client,)client = DatabricksFunctionClient()set_uc_function_client(client)tools = UCFunctionToolkit( # Include functions as tools using their qualified names. # You can use "{catalog_name}.{schema_name}.*" to get all functions in a schema. function_names=["main.tools.python_exec"]).tools
(선택사항) function 실행 응답을 받기 위한 재시도 시간을 늘리려면 환경 변수 UC_TOOL_CLIENT_EXECUTION_TIMEOUT을 설정하세요. 기본 재시도 시간 값은 120초입니다.
from langchain.agents import create_agentagent = create_agent( llm, tools, system_prompt="You are a helpful assistant. Make sure to use tool for information.",)agent.invoke({"messages": [{"role": "user", "content": "36939 * 8922.4"}]})
> Entering new AgentExecutor chain...Invoking: `main__tools__python_exec` with `{'code': 'print(36939 * 8922.4)'}`{"format": "SCALAR", "value": "329584533.59999996\n", "truncated": false}The result of the multiplication is:329584533.59999996> Finished chain.
Copy
{'input': '36939 * 8922.4', 'output': 'The result of the multiplication is:\n\n329584533.59999996'}