SDK Gọi hàm AI Edge (SDK FC) là một thư viện cho phép nhà phát triển sử dụng lệnh gọi hàm bằng LLM trên thiết bị. Lệnh gọi hàm cho phép bạn kết nối các mô hình với các công cụ và API bên ngoài, cho phép các mô hình gọi các hàm cụ thể bằng các tham số cần thiết để thực thi các hành động trong thực tế.
Thay vì chỉ tạo văn bản, LLM sử dụng SDK FC có thể tạo một lệnh gọi có cấu trúc đến một hàm thực thi một hành động, chẳng hạn như tìm kiếm thông tin mới nhất, đặt chuông báo hoặc đặt chỗ.
SDK AI Edge FC có sẵn cho Android và có thể chạy hoàn toàn trên thiết bị bằng API suy luận LLM. Bắt đầu sử dụng SDK bằng cách làm theo hướng dẫn của Android. Hướng dẫn này sẽ hướng dẫn bạn cách triển khai cơ bản một ứng dụng mẫu bằng cách sử dụng lệnh gọi hàm.
Quy trình gọi hàm
Để thiết lập LLM trên thiết bị có chức năng gọi hàm, bạn cần thực hiện các bước chính sau:
Xác định phần khai báo hàm: Bạn phải xác định cấu trúc và tham số của các hàm mà LLM có thể gọi trong mã ứng dụng.
Việc này bao gồm việc chỉ định tên hàm, tham số và loại.
Định dạng câu lệnh và kết quả: Văn bản đầu vào và đầu ra có thể chứa ngôn ngữ tự nhiên và lệnh gọi hàm. Trình định dạng kiểm soát cách chuyển đổi cấu trúc dữ liệu sang và từ chuỗi, cho phép LLM định dạng thông tin một cách thích hợp.
Phân tích cú pháp đầu ra: Trình phân tích cú pháp phát hiện xem phản hồi được tạo có chứa lệnh gọi hàm hay không và phân tích cú pháp lệnh gọi đó thành một loại dữ liệu có cấu trúc để ứng dụng có thể thực thi lệnh gọi hàm.
Kiểm tra phản hồi: Nếu trình phân tích cú pháp phát hiện lệnh gọi hàm, thì ứng dụng sẽ gọi hàm đó bằng các tham số và loại dữ liệu có cấu trúc thích hợp. Nếu không, hàm này sẽ trả về văn bản bằng ngôn ngữ tự nhiên.
Các thành phần chính
SDK FC chứa các thành phần chính sau:
Phần phụ trợ suy luận: Giao diện để chạy suy luận trên mô hình AI tạo sinh. SDK FC sử dụng API suy luận LLM để thực thi suy luận trên các mô hình LiteRT (TFLite). API này sử dụng giao diện InferenceBackend.
Trình định dạng câu lệnh: Giao diện để định dạng các yêu cầu và phản hồi đến và từ mô hình AI tạo sinh. SDK FC cung cấp một trình định dạng chuyển đổi nội dung khai báo hàm thành định dạng dành riêng cho mô hình mà LLM yêu cầu và chèn các nội dung đó vào lời nhắc của hệ thống. Trình định dạng cũng xử lý các mã thông báo dành riêng cho mô hình để cho biết lượt người dùng và lượt mô hình. API này sử dụng giao diện ModelFormatter.
Trình phân tích cú pháp đầu ra: SDK FC cung cấp một trình phân tích cú pháp phát hiện xem đầu ra của mô hình có thể hiện lệnh gọi hàm hay không và phân tích cú pháp đầu ra đó thành một cấu trúc dữ liệu để ứng dụng sử dụng. API này sử dụng giao diện ModelFormatter.
Giải mã có điều kiện: Giao diện để tạo và quản lý các điều kiện ràng buộc nhằm đảm bảo rằng đầu ra được tạo tuân thủ các quy tắc hoặc điều kiện cụ thể.
Đối với các mô hình được hỗ trợ, SDK FC sẽ định cấu hình phần phụ trợ suy luận để sử dụng tính năng giải mã có điều kiện, đảm bảo rằng mô hình chỉ xuất ra tên hàm và tham số hợp lệ. API này sử dụng giao diện ConstraintProvider.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-05-26 UTC."],[],[],null,["# AI Edge Function Calling guide\n\n| **Attention:** The AI Edge Function Calling SDK is under active development.\n\nThe AI Edge Function Calling SDK (FC SDK) is a library that enables developers\nto use function calling with on-device LLMs. Function calling lets you connect\nmodels to external tools and APIs, enabling models to call specific functions\nwith the necessary parameters to execute real-world actions.\n\nRather than just generating text, an LLM using the FC SDK can generate a\nstructured call to a function that executes an action, such as searching for\nup-to-date information, setting alarms, or making reservations.\n\nThe AI Edge FC SDK is available for Android and can be run completely on-device\nwith the LLM Inference API. Start using the SDK by following the [Android\nguide](./android), which walks you through a basic implementation of a sample\napplication using function calling.\n\nFunction calling pipeline\n-------------------------\n\nSetting up an on-device LLM with function calling capabilities requires the\nfollowing key steps:\n\n1. **Define function declarations**: The structure and parameters of the functions that the LLM can call must be defined in your application code. This includes specifying function names, parameters, and types.\n2. **Format prompts and outputs**: Input and output text can contain natural language and function calls. A formatter controls how data structures are converted to and from strings, enabling the LLM to appropriately format information.\n3. **Parse outputs**: A parser detects if the generated response contains a function call and parses it into a structured data type so that the application can execute the function call.\n4. **Examine responses**: If the parser detects a function call, the application calls the function with the appropriate parameters and structured data type. Otherwise, it returns natural language text.\n\nKey components\n--------------\n\nThe FC SDK contains to following key components:\n\n- **Inference Backend** : An interface for running inference on a generative AI model. The FC SDK uses the LLM Inference API to execute inference on LiteRT (TFLite) models. The API uses the [InferenceBackend](https://github.com/google-ai-edge/ai-edge-apis/blob/main/local_agents/function_calling/java/com/google/ai/edge/localagents/fc/InferenceBackend.java) interface.\n- **Prompt Formatter** : An interface for formatting requests and responses to and from the Generative AI model. The FC SDK provides a formatter that converts function declarations into the model-specific format required by the LLM and inserts them into the system prompt. The formatter also handles model-specific tokens to indicate user and model turns. The API uses the [ModelFormatter](https://github.com/google-ai-edge/ai-edge-apis/blob/main/local_agents/function_calling/java/com/google/ai/edge/localagents/fc/ModelFormatter.java) interface.\n- **Output Parser** : The FC SDK provides a parser that detects if the model's output represents a function call and parses it into a data structure for use by the application. The API uses the [ModelFormatter](https://github.com/google-ai-edge/ai-edge-apis/blob/main/local_agents/function_calling/java/com/google/ai/edge/localagents/fc/ModelFormatter.java) interface.\n- **Constrained Decoding** : An interface for creating and managing constraints to ensure that the generated output adheres to specific rules or conditions. For supported models, the FC SDK will configure the inference backend to use constrained decoding, which ensures that the model only outputs valid function names and parameters. The API uses the [ConstraintProvider](https://github.com/google-ai-edge/ai-edge-apis/blob/main/local_agents/function_calling/java/com/google/ai/edge/localagents/fc/ConstraintProvider.java) interface."]]