Skip to content

Commit 178c426

Browse files
committed
docs: add comprehensive AI plugin development guide
- 添加完整的AI插件开发文档 - 包含接口规范、实现示例和最佳实践 - 提供Go语言实现示例代码 - 说明主程序调用方式(HTTP/gRPC/内部调用) - 包含测试方法和常见问题解答
1 parent 0cb08b0 commit 178c426

File tree

1 file changed

+317
-0
lines changed

1 file changed

+317
-0
lines changed

AI_PLUGIN_DEVELOPMENT.md

Lines changed: 317 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,317 @@
1+
# API Testing AI插件开发指南
2+
3+
## 概述
4+
5+
API Testing项目支持AI插件扩展,AI插件与其他插件使用相同的扩展系统架构。本文档说明如何开发一个符合标准的AI插件。
6+
7+
## 架构说明
8+
9+
AI插件使用API Testing统一的插件系统架构:
10+
- 通过Unix Socket与主程序通信
11+
- 使用标准的`testing.Loader.Query(map[string]string)`接口
12+
- 通过`categories: ["ai"]`标识为AI插件
13+
- 采用JSON消息协议进行通信
14+
15+
## 插件配置
16+
17+
### 插件描述文件 (extension.yaml)
18+
19+
```yaml
20+
items:
21+
- name: your-ai-plugin
22+
categories:
23+
- ai # 必须包含"ai"类别
24+
dependencies:
25+
- name: your-ai-plugin
26+
link: https://github.com/yourorg/your-ai-plugin
27+
```
28+
29+
## 接口规范
30+
31+
AI插件必须实现以下两个方法:
32+
33+
### 1. ai.generate - 生成内容
34+
35+
**请求参数** (通过`Query(map[string]string)`传入):
36+
```go
37+
map[string]string{
38+
"method": "ai.generate", // 必需:方法名
39+
"model": "gpt-4", // 必需:模型标识符
40+
"prompt": "生成测试用例...", // 必需:提示词或指令
41+
"config": `{"temperature": 0.7}` // 可选:JSON配置字符串
42+
}
43+
```
44+
45+
**响应格式** (通过`DataResult.Pairs`返回):
46+
```go
47+
map[string]string{
48+
"content": "生成的内容...", // 必需:生成的内容
49+
"meta": `{"tokens": 100}`, // 可选:JSON元数据
50+
"success": "true" // 必需:是否成功
51+
}
52+
```
53+
54+
### 2. ai.capabilities - 获取插件能力
55+
56+
**请求参数**:
57+
```go
58+
map[string]string{
59+
"method": "ai.capabilities" // 必需:方法名
60+
}
61+
```
62+
63+
**响应格式**:
64+
```go
65+
map[string]string{
66+
"capabilities": `{ // 推荐:完整能力描述JSON
67+
"models": ["gpt-4", "gpt-3.5"],
68+
"features": ["text-generation", "code-generation"],
69+
"maxTokens": 4096,
70+
"supportedLanguages": ["zh", "en"]
71+
}`,
72+
"models": `["gpt-4", "gpt-3.5"]`, // 备选:支持的模型列表
73+
"features": `["text-generation"]`, // 备选:支持的功能列表
74+
"description": "AI代码生成插件", // 备选:插件描述
75+
"version": "1.0.0", // 备选:插件版本
76+
"success": "true" // 必需:是否成功
77+
}
78+
```
79+
80+
### 错误处理
81+
82+
当发生错误时,返回格式:
83+
```go
84+
map[string]string{
85+
"error": "具体的错误信息",
86+
"success": "false"
87+
}
88+
```
89+
90+
## 实现示例
91+
92+
### Go语言实现示例
93+
94+
```go
95+
package main
96+
97+
import (
98+
"encoding/json"
99+
"fmt"
100+
"github.com/linuxsuren/api-testing/pkg/testing"
101+
)
102+
103+
type AIPlugin struct {
104+
// 你的AI客户端
105+
client AIClient
106+
}
107+
108+
// 实现testing.Loader接口的Query方法
109+
func (p *AIPlugin) Query(query map[string]string) (result testing.DataResult, err error) {
110+
result = testing.DataResult{
111+
Pairs: make(map[string]string),
112+
}
113+
114+
method := query["method"]
115+
116+
switch method {
117+
case "ai.generate":
118+
// 处理生成请求
119+
model := query["model"]
120+
prompt := query["prompt"]
121+
configJSON := query["config"]
122+
123+
// 解析配置
124+
var config map[string]interface{}
125+
if configJSON != "" {
126+
json.Unmarshal([]byte(configJSON), &config)
127+
}
128+
129+
// 调用AI服务
130+
content, meta, err := p.client.Generate(model, prompt, config)
131+
if err != nil {
132+
result.Pairs["error"] = err.Error()
133+
result.Pairs["success"] = "false"
134+
return result, nil
135+
}
136+
137+
// 返回成功结果
138+
result.Pairs["content"] = content
139+
if meta != nil {
140+
metaJSON, _ := json.Marshal(meta)
141+
result.Pairs["meta"] = string(metaJSON)
142+
}
143+
result.Pairs["success"] = "true"
144+
145+
case "ai.capabilities":
146+
// 返回插件能力
147+
capabilities := map[string]interface{}{
148+
"models": []string{"gpt-4", "gpt-3.5-turbo"},
149+
"features": []string{"text-generation", "code-generation"},
150+
"maxTokens": 4096,
151+
"supportedLanguages": []string{"zh", "en"},
152+
}
153+
154+
capJSON, _ := json.Marshal(capabilities)
155+
result.Pairs["capabilities"] = string(capJSON)
156+
result.Pairs["success"] = "true"
157+
158+
default:
159+
result.Pairs["error"] = fmt.Sprintf("不支持的方法: %s", method)
160+
result.Pairs["success"] = "false"
161+
}
162+
163+
return result, nil
164+
}
165+
166+
// 其他必需的Loader接口方法
167+
func (p *AIPlugin) HasMore() bool { return false }
168+
func (p *AIPlugin) Load() ([]byte, error) { return nil, nil }
169+
func (p *AIPlugin) Reset() {}
170+
func (p *AIPlugin) Put([]byte) error { return nil }
171+
```
172+
173+
## 主程序调用方式
174+
175+
主程序通过以下方式调用AI插件:
176+
177+
### 1. HTTP API调用
178+
179+
```bash
180+
# 获取AI插件能力
181+
GET /api/v1/ai/capabilities/{plugin_name}
182+
183+
# 生成内容
184+
POST /api/v1/ai/generate
185+
{
186+
"plugin_name": "your-ai-plugin",
187+
"model": "gpt-4",
188+
"prompt": "生成一个用户登录的测试用例",
189+
"config": "{\"temperature\": 0.7, \"max_tokens\": 1000}"
190+
}
191+
```
192+
193+
### 2. gRPC调用
194+
195+
```protobuf
196+
// 调用AI生成
197+
rpc CallAI(AIRequest) returns (AIResponse);
198+
199+
// 获取AI能力
200+
rpc GetAICapabilities(AICapabilitiesRequest) returns (AICapabilitiesResponse);
201+
```
202+
203+
### 3. 内部代码调用
204+
205+
```go
206+
// 获取AI插件
207+
stores, err := server.GetStores(ctx, &SimpleQuery{Kind: "ai"})
208+
209+
// 调用AI插件
210+
loader, err := server.getLoaderByStoreName("your-ai-plugin")
211+
result, err := loader.Query(map[string]string{
212+
"method": "ai.generate",
213+
"model": "gpt-4",
214+
"prompt": "生成测试用例",
215+
"config": `{"temperature": 0.7}`,
216+
})
217+
content := result.Pairs["content"]
218+
```
219+
220+
## 配置参数说明
221+
222+
### config字段(JSON格式)
223+
224+
常用配置参数示例:
225+
```json
226+
{
227+
"temperature": 0.7, // 生成随机性 (0-1)
228+
"max_tokens": 1000, // 最大生成长度
229+
"top_p": 0.9, // 核采样参数
230+
"frequency_penalty": 0, // 频率惩罚
231+
"presence_penalty": 0, // 存在惩罚
232+
"stop": ["\n\n"], // 停止序列
233+
"system": "你是一个测试专家" // 系统提示词
234+
}
235+
```
236+
237+
## 最佳实践
238+
239+
1. **错误处理**:始终返回清晰的错误信息,帮助调试
240+
2. **配置验证**:验证必需参数,提供合理的默认值
241+
3. **异步处理**:对于长时间运行的任务,考虑异步实现
242+
4. **日志记录**:记录关键操作和错误,便于问题定位
243+
5. **资源管理**:正确管理AI客户端连接和资源释放
244+
6. **版本兼容**:在capabilities中声明版本,保证向后兼容
245+
246+
## 测试插件
247+
248+
### 1. 单元测试
249+
250+
```go
251+
func TestAIPlugin_Generate(t *testing.T) {
252+
plugin := &AIPlugin{}
253+
result, err := plugin.Query(map[string]string{
254+
"method": "ai.generate",
255+
"model": "gpt-4",
256+
"prompt": "测试提示词",
257+
})
258+
259+
assert.NoError(t, err)
260+
assert.Equal(t, "true", result.Pairs["success"])
261+
assert.NotEmpty(t, result.Pairs["content"])
262+
}
263+
```
264+
265+
### 2. 集成测试
266+
267+
使用curl测试HTTP接口:
268+
```bash
269+
# 测试能力获取
270+
curl -X GET "http://localhost:8080/api/v1/ai/capabilities/your-ai-plugin"
271+
272+
# 测试内容生成
273+
curl -X POST "http://localhost:8080/api/v1/ai/generate" \
274+
-H "Content-Type: application/json" \
275+
-d '{
276+
"plugin_name": "your-ai-plugin",
277+
"model": "gpt-4",
278+
"prompt": "Hello"
279+
}'
280+
```
281+
282+
## 常见问题
283+
284+
### Q1: 如何处理流式响应?
285+
A: 当前接口设计为同步调用,如需流式响应,可以:
286+
1. 在meta中返回任务ID
287+
2. 提供额外的查询接口获取流式结果
288+
3. 或等待后续版本支持
289+
290+
### Q2: 如何支持多模态输入?
291+
A: 可以在prompt中使用特殊格式,如:
292+
```json
293+
{
294+
"prompt": "[IMAGE:base64_data] 描述这张图片",
295+
"config": "{\"input_type\": \"multimodal\"}"
296+
}
297+
```
298+
299+
### Q3: 如何处理上下文对话?
300+
A: 在config中传递会话ID和历史消息:
301+
```json
302+
{
303+
"config": "{\"session_id\": \"xxx\", \"history\": [...]}"
304+
}
305+
```
306+
307+
## 参考资源
308+
309+
- [API Testing扩展开发文档](https://github.com/LinuxSuRen/api-testing/docs)
310+
- [示例AI插件实现](https://github.com/LinuxSuRen/atest-ext-ai-example)
311+
- [主项目源码](https://github.com/LinuxSuRen/api-testing)
312+
313+
## 联系方式
314+
315+
如有问题,请通过以下方式联系:
316+
- GitHub Issues: https://github.com/LinuxSuRen/api-testing/issues
317+
- 邮件列表: [email protected]

0 commit comments

Comments
 (0)