|
| 1 | +--- |
| 2 | +title: Assistants <span style="font-weight:bold; margin-left:10px; color:red;">Beta</span> |
| 3 | +--- |
| 4 | + |
| 5 | +!!! Note |
| 6 | + |
| 7 | + 调用前请先构建客户端,构建代码如下: |
| 8 | + |
| 9 | + ```java |
| 10 | + OpenAiClient client = OpenAiClient.builder() |
| 11 | + .apiHost("https://api.openai.com") |
| 12 | + .apiKey(System.getProperty("openai.token")) |
| 13 | + .build(); |
| 14 | + ``` |
| 15 | + |
| 16 | + `System.getProperty("openai.token")` 是访问 API 授权的关键。 |
| 17 | + |
| 18 | +### 创建助手 |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +```java |
| 23 | +AssistantsEntity entity = AssistantsEntity.builder() |
| 24 | + .name("Math Tutor") |
| 25 | + .model(CompletionModel.GPT_35_TURBO) |
| 26 | + .instructions("You are a personal math tutor. When asked a question, write and run Python code to answer the question.") |
| 27 | + .build(); |
| 28 | +client.createAssistants(entity); |
| 29 | +``` |
| 30 | + |
| 31 | +返回: |
| 32 | + |
| 33 | +```json |
| 34 | +{ |
| 35 | + "id": "asst_abc123", |
| 36 | + "object": "assistant", |
| 37 | + "created_at": 1698984975, |
| 38 | + "name": "Math Tutor", |
| 39 | + "description": null, |
| 40 | + "model": "gpt-4", |
| 41 | + "instructions": "You are a personal math tutor. When asked a question, write and run Python code to answer the question.", |
| 42 | + "tools": [ |
| 43 | + { |
| 44 | + "type": "code_interpreter" |
| 45 | + } |
| 46 | + ], |
| 47 | + "file_ids": [], |
| 48 | + "metadata": {} |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +### 创建助手文件 |
| 53 | + |
| 54 | +--- |
| 55 | + |
| 56 | +```java |
| 57 | +client.createAssistantsFile("file-jNuKdx61rNQ0FUhuPFpMNmGZ","asst_xv9N9dNXstuV8OVLElLqgV7U") |
| 58 | +``` |
| 59 | + |
| 60 | +返回: |
| 61 | + |
| 62 | +```json |
| 63 | +{ |
| 64 | + "id": "file-abc123", |
| 65 | + "object": "assistant.file", |
| 66 | + "created_at": 1699055364, |
| 67 | + "assistant_id": "asst_abc123" |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +### 列出助手 |
| 72 | + |
| 73 | +--- |
| 74 | + |
| 75 | +```java |
| 76 | +client.listAssistants(null); |
| 77 | + |
| 78 | +// With query params |
| 79 | +QueryEntity configure = QueryEntity.builder() |
| 80 | + .limit(2) |
| 81 | + .build(); |
| 82 | +client.assistants(configure); |
| 83 | +``` |
| 84 | + |
| 85 | +返回: |
| 86 | + |
| 87 | +```json |
| 88 | +{ |
| 89 | + "object": "list", |
| 90 | + "data": [ |
| 91 | + { |
| 92 | + "id": "asst_abc123", |
| 93 | + "object": "assistant", |
| 94 | + "created_at": 1698982736, |
| 95 | + "name": "Coding Tutor", |
| 96 | + "description": null, |
| 97 | + "model": "gpt-4", |
| 98 | + "instructions": "You are a helpful assistant designed to make me better at coding!", |
| 99 | + "tools": [], |
| 100 | + "file_ids": [], |
| 101 | + "metadata": {} |
| 102 | + } |
| 103 | + ], |
| 104 | + "first_id": "asst_abc123", |
| 105 | + "last_id": "asst_abc789", |
| 106 | + "has_more": false |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +### 列出助手文件 |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +```java |
| 115 | +client.assistantsFiles("asst_xv9N9dNXstuV8OVLElLqgV7U")); |
| 116 | +``` |
| 117 | + |
| 118 | +返回: |
| 119 | + |
| 120 | +```json |
| 121 | +{ |
| 122 | + "object": "list", |
| 123 | + "data": [ |
| 124 | + { |
| 125 | + "id": "file-abc123", |
| 126 | + "object": "assistant.file", |
| 127 | + "created_at": 1699060412, |
| 128 | + "assistant_id": "asst_abc123" |
| 129 | + } |
| 130 | + ], |
| 131 | + "first_id": "file-abc123", |
| 132 | + "last_id": "file-abc456", |
| 133 | + "has_more": false |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +### 检索助手 |
| 138 | + |
| 139 | +--- |
| 140 | + |
| 141 | +```java |
| 142 | +client.retrieveAssistant("asst_xv9N9dNXstuV8OVLElLqgV7U"); |
| 143 | +``` |
| 144 | + |
| 145 | +Returns: |
| 146 | + |
| 147 | +```json |
| 148 | +{ |
| 149 | + "id": "asst_abc123", |
| 150 | + "object": "assistant", |
| 151 | + "created_at": 1698984975, |
| 152 | + "name": "Math Tutor", |
| 153 | + "description": null, |
| 154 | + "model": "gpt-4", |
| 155 | + "instructions": "You are a personal math tutor. When asked a question, write and run Python code to answer the question.", |
| 156 | + "tools": [ |
| 157 | + { |
| 158 | + "type": "code_interpreter" |
| 159 | + } |
| 160 | + ], |
| 161 | + "file_ids": [], |
| 162 | + "metadata": {} |
| 163 | +} |
| 164 | +``` |
| 165 | + |
| 166 | +### 检索助手文件 |
| 167 | + |
| 168 | +--- |
| 169 | + |
| 170 | +```java |
| 171 | +client.retrieveAssistantFile("asst_xv9N9dNXstuV8OVLElLqgV7U","file-jNuKdx61rNQ0FUhuPFpMNmGZ"); |
| 172 | +``` |
| 173 | + |
| 174 | +返回: |
| 175 | + |
| 176 | +```json |
| 177 | +{ |
| 178 | + "id": "file-abc123", |
| 179 | + "object": "assistant.file", |
| 180 | + "created_at": 1699055364, |
| 181 | + "assistant_id": "asst_abc123" |
| 182 | +} |
| 183 | +``` |
| 184 | + |
| 185 | +### 修改助手 |
| 186 | + |
| 187 | +--- |
| 188 | + |
| 189 | +```java |
| 190 | +AssistantsEntity entity = AssistantsEntity.builder() |
| 191 | + .name("Math Tutor 1") |
| 192 | + .model(CompletionModel.GPT_35_TURBO) |
| 193 | + .instructions("You are a personal math tutor. When asked a question, write and run Python code to answer the question.") |
| 194 | + .build(); |
| 195 | +client.updateAssistant("asst_xv9N9dNXstuV8OVLElLqgV7U",entity); |
| 196 | +``` |
| 197 | + |
| 198 | +返回: |
| 199 | + |
| 200 | +```json |
| 201 | +{ |
| 202 | + "id": "asst_abc123", |
| 203 | + "object": "assistant", |
| 204 | + "created_at": 1698984975, |
| 205 | + "name": "Math Tutor 1", |
| 206 | + "description": null, |
| 207 | + "model": "gpt-4", |
| 208 | + "instructions": "You are a personal math tutor. When asked a question, write and run Python code to answer the question.", |
| 209 | + "tools": [ |
| 210 | + { |
| 211 | + "type": "code_interpreter" |
| 212 | + } |
| 213 | + ], |
| 214 | + "file_ids": [], |
| 215 | + "metadata": {} |
| 216 | +} |
| 217 | +``` |
| 218 | + |
| 219 | +### 删除助手 |
| 220 | + |
| 221 | +--- |
| 222 | + |
| 223 | +```java |
| 224 | +client.deleteAssistant("asst_xv9N9dNXstuV8OVLElLqgV7U"); |
| 225 | +``` |
| 226 | + |
| 227 | +返回: |
| 228 | + |
| 229 | +```json |
| 230 | +{ |
| 231 | + "id": "asst_abc123", |
| 232 | + "object": "assistant.deleted", |
| 233 | + "deleted": true |
| 234 | +} |
| 235 | +``` |
| 236 | + |
| 237 | +### 删除助手文件 |
| 238 | + |
| 239 | +--- |
| 240 | + |
| 241 | +```java |
| 242 | +client.deleteAssistantFile("asst_xv9N9dNXstuV8OVLElLqgV7U","file-jNuKdx61rNQ0FUhuPFpMNmGZ"); |
| 243 | +``` |
| 244 | + |
| 245 | +返回: |
| 246 | + |
| 247 | +```json |
| 248 | +{ |
| 249 | + "id": "file-abc123", |
| 250 | + "object": "assistant.file.deleted", |
| 251 | + "deleted": true |
| 252 | +} |
| 253 | +``` |
0 commit comments