|
| 1 | +# `agent-schema` Example: |
| 2 | + |
| 3 | +This example demonstrates how to define and use JSON Schema for AI tool outputs in `polaris`. By using `UseJSONOutput` with tool definitions, you can ensure that AI inference outputs conform to your specified schema structure. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The `agent-schema` example showcases how to: |
| 8 | + |
| 9 | +- Define tools with structured JSON Schema responses |
| 10 | +- Use AI inference to generate outputs that match these schemas |
| 11 | +- Create a seamless integration between AI models and structured data |
| 12 | + |
| 13 | +This approach allows you to leverage AI capabilities while maintaining control over the output format, making it easier to process and use the results in your applications. |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +Before running this example, you need to set up the following environment variables for Google Cloud Platform authentication: |
| 18 | + |
| 19 | +```bash |
| 20 | +export GOOGLE_CLOUD_PROJECT=your_project_id |
| 21 | +export GOOGLE_CLOUD_LOCATION=your_gcp_project_location |
| 22 | +export GOOGLE_APPLICATION_CREDENTIALS=/path/to/credential.json |
| 23 | +``` |
| 24 | + |
| 25 | +These environment variables are required to authenticate with Google Cloud and access the Gemini AI model used in this example. |
| 26 | + |
| 27 | +## Components |
| 28 | + |
| 29 | +This example consists of three main components: |
| 30 | + |
| 31 | +### 1. Registry Server (`registry.go`) |
| 32 | + |
| 33 | +The registry server acts as a central hub for registering and managing tools. It binds to a local address and port, allowing clients and agents to connect. |
| 34 | + |
| 35 | +```go |
| 36 | +registry, err := polaris.CreateRegistry( |
| 37 | + polaris.WithBind("127.0.0.1", 4222), |
| 38 | +) |
| 39 | +``` |
| 40 | + |
| 41 | +### 2. Agent Implementation (`agents.go`) |
| 42 | + |
| 43 | +The agent implementation defines several tools with specific JSON schemas for their responses: |
| 44 | + |
| 45 | +- `getWeather`: Returns weather information for a specified city |
| 46 | +- `getFortune`: Provides a fortune-telling result |
| 47 | +- `getCurrentDate`: Returns the current date information |
| 48 | + |
| 49 | +Each tool uses `polaris.UseJSONOutput(myTool.Response)` to ensure that the AI model's output conforms to the defined schema. |
| 50 | + |
| 51 | +### 3. Client Application (`client.go`) |
| 52 | + |
| 53 | +The client application connects to the registry, creates a session with the AI model, and sends a prompt to get information about today's conditions, including fortune and weather. |
| 54 | + |
| 55 | +## Key Features |
| 56 | + |
| 57 | +### JSON Schema Definition for AI Outputs |
| 58 | + |
| 59 | +The core feature of this example is the ability to define JSON schemas for AI outputs using `UseJSONOutput`. This ensures that the AI model generates responses that match your expected structure. |
| 60 | + |
| 61 | +For example, in the `getWeather` tool: |
| 62 | + |
| 63 | +```go |
| 64 | +myTool, _ := conn.Tool(toolName) |
| 65 | +gen, err := polaris.GenerateJSON( |
| 66 | + ctx, |
| 67 | + polaris.UseModel("gemini-2.5-pro-exp-03-25"), |
| 68 | + polaris.UseSystemInstruction( |
| 69 | + polaris.AddTextSystemInstruction("Output must be in Japanese."), |
| 70 | + ), |
| 71 | + polaris.UseJSONOutput(myTool.Response), |
| 72 | + polaris.UseTemperature(0.5), |
| 73 | +) |
| 74 | +``` |
| 75 | + |
| 76 | +The `UseJSONOutput(myTool.Response)` parameter tells the AI model to generate output that conforms to the schema defined in `myTool.Response`. |
| 77 | + |
| 78 | +### Structured AI Responses |
| 79 | + |
| 80 | +By using JSON schemas, you can ensure that AI responses are structured and predictable, making them easier to process in your application logic. This approach bridges the gap between free-form AI outputs and structured data requirements. |
| 81 | + |
| 82 | +## Usage |
| 83 | + |
| 84 | +To run this example: |
| 85 | + |
| 86 | +1. Set up the required environment variables as described in the Prerequisites section. |
| 87 | + |
| 88 | +2. Start the registry server: |
| 89 | + ```bash |
| 90 | + go run registry.go |
| 91 | + ``` |
| 92 | + |
| 93 | +3. In a separate terminal, start the agent: |
| 94 | + ```bash |
| 95 | + go run agents.go |
| 96 | + ``` |
| 97 | + |
| 98 | +4. In another terminal, run the client: |
| 99 | + ```bash |
| 100 | + go run client.go |
| 101 | + ``` |
| 102 | + |
| 103 | +The client will send a prompt to the AI model, which will use the registered tools to generate structured responses about today's fortune and weather in Tokyo. |
| 104 | + |
| 105 | +## Code Explanation |
| 106 | + |
| 107 | +### Tool Definition with Response Schema |
| 108 | + |
| 109 | +Each tool is defined with a specific response schema that the AI model must follow: |
| 110 | + |
| 111 | +```go |
| 112 | +conn.RegisterTool(polaris.Tool{ |
| 113 | + Name: toolName, |
| 114 | + Description: "get weather by city", |
| 115 | + Parameters: polaris.Object{ |
| 116 | + Properties: polaris.Properties{ |
| 117 | + "cityName": polaris.String{ |
| 118 | + Description: "cityName", |
| 119 | + Default: "tokyo", |
| 120 | + Required: true, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + Response: polaris.Object{ |
| 125 | + Properties: polaris.Properties{ |
| 126 | + "temperature": polaris.Int{ |
| 127 | + Description: "estimated maximum temperatures", |
| 128 | + Required: true, |
| 129 | + }, |
| 130 | + "sky_condition": polaris.String{ |
| 131 | + Description: "sky condition", |
| 132 | + Required: true, |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + Handler: func(c *polaris.Ctx) error { |
| 137 | + // Handler implementation |
| 138 | + }, |
| 139 | +}) |
| 140 | +``` |
| 141 | + |
| 142 | +### AI Inference with Schema Enforcement |
| 143 | + |
| 144 | +The `GenerateJSON` function is used to create an AI inference function that enforces the output schema: |
| 145 | + |
| 146 | +```go |
| 147 | +gen, err := polaris.GenerateJSON( |
| 148 | + ctx, |
| 149 | + polaris.UseModel("gemini-2.5-pro-exp-03-25"), |
| 150 | + polaris.UseSystemInstruction( |
| 151 | + polaris.AddTextSystemInstruction("Output must be in Japanese."), |
| 152 | + ), |
| 153 | + polaris.UseJSONOutput(myTool.Response), |
| 154 | + polaris.UseTemperature(0.5), |
| 155 | +) |
| 156 | +``` |
| 157 | + |
| 158 | +This ensures that the AI model's output will match the structure defined in `myTool.Response`, allowing for seamless integration between AI capabilities and structured data requirements. |
| 159 | + |
| 160 | +## Benefits |
| 161 | + |
| 162 | +Using JSON schemas for AI outputs provides several benefits: |
| 163 | + |
| 164 | +1. **Predictability**: Ensures that AI outputs follow a consistent structure |
| 165 | +2. **Validation**: Automatically validates that outputs contain required fields |
| 166 | +3. **Integration**: Makes it easier to integrate AI capabilities into existing systems |
| 167 | +4. **Type Safety**: Provides type information for downstream processing |
| 168 | + |
| 169 | +This approach allows you to leverage the power of AI while maintaining the structure and predictability needed for production applications. |
0 commit comments