Skip to content

feat: Added AI extension support and SQL generation APIs #804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion console/atest-ui/src/views/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const ExtensionKindRedis = "atest-store-redis"
const ExtensionKindMongoDB = "atest-store-mongodb"
const ExtensionKindElasticsearch = "atest-store-elasticsearch"
const ExtensionKindOpengeMini = "atest-store-opengemini"
const ExtensionKindAI = "atest-ext-ai"

export const ExtensionKind = {
ExtensionKindGit,
Expand All @@ -68,7 +69,8 @@ export const ExtensionKind = {
ExtensionKindRedis,
ExtensionKindMongoDB,
ExtensionKindElasticsearch,
ExtensionKindOpengeMini
ExtensionKindOpengeMini,
ExtensionKindAI
}

const storeExtensions = [
Expand Down Expand Up @@ -170,6 +172,36 @@ const storeExtensions = [
name: ExtensionKindOpengeMini,
params: [],
link: 'https://github.com/LinuxSuRen/atest-ext-store-opengemini'
},
{
name: ExtensionKindAI,
params: [{
key: 'model_provider',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个变量该怎么用呢?下面的接口里没有体现到。

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

另外,接口的定义后续会重构。大体的思路是:

  1. 在单独的仓库里定义;
  2. 启动的时候,下载并解析相关文件
  3. 提供对应的接口,前端获取所有支持的插件

LinuxSuRen/atest-ext-data-swagger#1
#803

重构完成后再调整即可。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个变量该怎么用呢?下面的接口里没有体现到。

ExtensionKindAI 变量定义了AI扩展的配置参数:

  • model_provider : AI模型提供商(ollama, openai)
  • model_name : 具体模型名称(如llama3.2)
  • api_key : 外部提供商的认证密钥
  • base_url : AI服务端点URL
  • temperature : 控制响应随机性(0.0-1.0)
  • max_tokens : 最大响应长度

这些参数通过 GenerateContentRequest.parameters 字段传递给AI服务,用于配置AI模型的行为。在接口中作为 parameters 映射传递给AI扩展服务。

defaultValue: 'ollama',
enum: ['ollama', 'openai'],
description: 'AI model provider used in GenerateContentRequest.parameters for content generation'
}, {
key: 'model_name',
defaultValue: 'llama3.2',
description: 'AI model name passed to GenerateContentRequest.parameters["model_name"] for specific model selection'
}, {
key: 'api_key',
defaultValue: '',
description: 'API key for external providers, used in GenerateContentRequest.parameters["api_key"] for authentication'
}, {
key: 'base_url',
defaultValue: 'http://localhost:11434',
description: 'Base URL for AI service, used in GenerateContentRequest.parameters["base_url"] to specify service endpoint'
}, {
key: 'temperature',
defaultValue: '0.7',
description: 'Controls randomness in AI responses (0.0-1.0), passed via GenerateContentRequest.parameters["temperature"]'
}, {
key: 'max_tokens',
defaultValue: '2048',
description: 'Maximum tokens in AI response, used in GenerateContentRequest.parameters["max_tokens"] to limit output length'
}],
link: 'https://github.com/LinuxSuRen/atest-ext-ai'
}
] as Store[]

Expand Down
151 changes: 151 additions & 0 deletions extensions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,157 @@
| Agent | [collector](https://github.com/LinuxSuRen/atest-ext-collector) | |
| Secret | [Vault](https://github.com/LinuxSuRen/api-testing-vault-extension) | |
| Data | [Swagger](https://github.com/LinuxSuRen/atest-ext-data-swagger) | |
| AI | [ai-extension](https://github.com/LinuxSuRen/atest-ext-ai) | 50051 |

## AI Extension Interface

Check notice on line 20 in extensions/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

extensions/README.md#L20

Expected: [None]; Actual: ## AI Extension Interface

This extension provides AI-powered content generation capabilities through a unified gRPC interface.

Check notice on line 22 in extensions/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

extensions/README.md#L22

Expected: 80; Actual: 100

### Unified Interface Design

#### `GenerateContent`

Check notice on line 26 in extensions/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

extensions/README.md#L26

Expected: 1; Actual: 0; Below
The single, unified interface for all AI content generation tasks. This interface can handle various content types including SQL generation, test case writing, mock service creation, and more through a single endpoint.

Check notice on line 27 in extensions/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

extensions/README.md#L27

Expected: 80; Actual: 218

**Request Parameters:**
- `prompt`: Natural language description of what you want to generate

Check notice on line 30 in extensions/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

extensions/README.md#L30

Lists should be surrounded by blank lines
- `contentType`: Type of content to generate (e.g., "sql", "testcase", "mock")
- `context`: Additional context information as key-value pairs
- `parameters`: Additional parameters specific to the content type

**Response:**
- `success.content`: Generated content

Check notice on line 36 in extensions/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

extensions/README.md#L36

Lists should be surrounded by blank lines
- `success.explanation`: Explanation of the generated content
- `success.confidenceScore`: Confidence score (0.0 to 1.0)
- `success.metadata`: Additional metadata about the generation
- `error`: Error information if generation fails

### Content Types and Task Identification

The `contentType` parameter serves as the task identifier, allowing the system to:

1. **Apply appropriate prompting strategies** for different content types
2. **Use specialized processing logic** for each task type
3. **Provide task-specific validation and post-processing**
4. **Generate relevant metadata** for each content type

#### Supported Content Types:

Check notice on line 51 in extensions/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

extensions/README.md#L51

Punctuation: ':'

1. **SQL Generation** (`contentType: "sql"`)
- Generates SQL queries from natural language
- Supports multiple database types via parameters
- Context can include schema information
- Applies SQL-specific validation and dialect adaptation

2. **Test Case Generation** (`contentType: "testcase"`)
- Generates comprehensive test cases
- Context can include code specifications
- Uses test-specific prompting strategies

3. **Mock Service Generation** (`contentType: "mock"`)
- Generates mock services and API responses
- Context can include API specifications
- Applies service-specific formatting

4. **Generic Content** (any other `contentType`)
- Handles any other content generation requests
- Flexible prompt-based generation
- Extensible for future content types

### Architecture Benefits

**Single Interface Advantages:**
- **Consistency**: All content generation follows the same request/response pattern

Check notice on line 77 in extensions/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

extensions/README.md#L77

Lists should be surrounded by blank lines
- **Extensibility**: New content types can be added without API changes
- **Simplicity**: Clients only need to integrate with one interface
- **Flexibility**: Rich context and parameter support for all content types

**Task Differentiation:**
While using a single interface, the system internally routes requests to specialized handlers based on `contentType`, ensuring:

Check notice on line 83 in extensions/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

extensions/README.md#L83

Expected: 80; Actual: 127
- Appropriate AI model prompting for each task

Check notice on line 84 in extensions/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

extensions/README.md#L84

Lists should be surrounded by blank lines
- Task-specific validation and processing
- Relevant metadata and confidence scoring
- Optimized performance for different content types

### Configuration Parameters

The following parameters can be passed via `GenerateContentRequest.parameters`:

- `model_provider`: AI provider ("ollama", "openai") - used in GenerateContentRequest.parameters
- `model_name`: Specific model name (e.g., "llama3.2", "gpt-4") - passed via parameters["model_name"]
- `api_key`: Authentication key for external providers - used in parameters["api_key"]
- `base_url`: Service endpoint URL - specified in parameters["base_url"]
- `temperature`: Response randomness control (0.0-1.0) - set via parameters["temperature"]
- `max_tokens`: Maximum response length - controlled by parameters["max_tokens"]

### Usage Examples

```protobuf
// Generate SQL query
GenerateContentRequest {
prompt: "Show me all users from California"
contentType: "sql"
context: {
"schema": "CREATE TABLE users (id INT, name VARCHAR(100), state VARCHAR(50))"
}
parameters: {
"model_provider": "ollama"
"model_name": "llama3.2"
"database_type": "mysql"
}
}

// Generate test case
GenerateContentRequest {
prompt: "Create a test case for user registration API"
contentType: "testcase"
context: {
"api_spec": "POST /api/users {name, email, password}"
}
}

// Generate mock service
GenerateContentRequest {
prompt: "Create a mock REST API for user management"
contentType: "mock"
context: {
"endpoints": "GET /users, POST /users, PUT /users/{id}, DELETE /users/{id}"
"response_format": "JSON"
}
}
```

### Migration Notes

This version removes the legacy `GenerateSQLFromNaturalLanguage` interface in favor of the unified `GenerateContent` approach. All SQL generation should now use:

Check notice on line 139 in extensions/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

extensions/README.md#L139

Expected: 80; Actual: 161

```protobuf
GenerateContentRequest {
prompt: "your natural language query"
contentType: "sql"
parameters: {"database_type": "mysql|postgresql|sqlite"}
context: {"schemas": "table definitions"}
}
```

### Frequently Asked Questions

**Q1: Do I need different interfaces for different tasks (SQL generation, test case writing, mock service creation)?**

Check notice on line 152 in extensions/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

extensions/README.md#L152

Expected: 80; Actual: 118

A: No, you only need the single `GenerateContent` interface. The `contentType` parameter tells the system what kind of content you want to generate, and the system internally routes your request to the appropriate specialized handler. This design provides:

Check notice on line 154 in extensions/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

extensions/README.md#L154

Expected: 80; Actual: 256
- **Task identification**: The system knows exactly what you're trying to accomplish

Check notice on line 155 in extensions/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

extensions/README.md#L155

Lists should be surrounded by blank lines
- **Specialized processing**: Each content type gets optimized prompting and validation
- **Consistent API**: You always use the same interface regardless of the task
- **Future extensibility**: New content types can be added without changing the API

**Q2: Will removing the SQL-specific interface affect other files?**

A: The removal has been carefully implemented to ensure minimal impact:
- **Protobuf files**: All generated code (Go, Python, gRPC-gateway, OpenAPI) has been updated

Check notice on line 163 in extensions/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

extensions/README.md#L163

Expected: 80; Actual: 93

Check notice on line 163 in extensions/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

extensions/README.md#L163

Lists should be surrounded by blank lines
- **Implementation files**: The Python gRPC server has been updated to use the new unified interface

Check notice on line 164 in extensions/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

extensions/README.md#L164

Expected: 80; Actual: 100
- **Backward compatibility**: While the old interface is removed, the same functionality is available through `GenerateContent` with `contentType: "sql"`

Check notice on line 165 in extensions/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

extensions/README.md#L165

Expected: 80; Actual: 153
- **No breaking changes**: The core functionality remains the same, just accessed through a cleaner, unified interface

Check notice on line 166 in extensions/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

extensions/README.md#L166

Expected: 80; Actual: 118

The migration ensures that all SQL generation capabilities are preserved while providing a more maintainable and extensible architecture.

Check notice on line 168 in extensions/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

extensions/README.md#L168

Expected: 80; Actual: 137

## Contribute a new extension

Expand Down
Loading