-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support natural language as a search method (#556)
## What type of PR is this? /kind feature ## What this PR does / why we need it: Support natural language as a search method for kubernetes resources. ## Which issue(s) this PR fixes: Fixes #452 --------- Co-authored-by: ruquanzhao <[email protected]>
- Loading branch information
1 parent
bc2b358
commit 0ddb27a
Showing
12 changed files
with
226 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright The Karpor Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ai | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/KusionStack/karpor/pkg/infra/ai" | ||
) | ||
|
||
// ConvertTextToSQL converts natural language text to an SQL query | ||
func (a *AIManager) ConvertTextToSQL(query string) (string, error) { | ||
servicePrompt := ai.ServicePromptMap[ai.Text2sqlType] | ||
prompt := fmt.Sprintf(servicePrompt, query) | ||
res, err := a.client.Generate(context.Background(), prompt) | ||
if err != nil { | ||
return "", err | ||
} | ||
return ExtractSelectSQL(res), nil | ||
} | ||
|
||
// FixSQL fix the error SQL | ||
func (a *AIManager) FixSQL(sql string, query string, error string) (string, error) { | ||
servicePrompt := ai.ServicePromptMap[ai.SqlFixType] | ||
prompt := fmt.Sprintf(servicePrompt, query, sql, error) | ||
res, err := a.client.Generate(context.Background(), prompt) | ||
if err != nil { | ||
return "", err | ||
} | ||
return ExtractSelectSQL(res), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright The Karpor Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ai | ||
|
||
import "regexp" | ||
|
||
// ExtractSelectSQL extracts SQL statements that start with "SELECT * FROM" | ||
func ExtractSelectSQL(sql string) string { | ||
res := regexp.MustCompile(`(?i)SELECT \* FROM [^;]+`) | ||
match := res.FindString(sql) | ||
return match | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright The Karpor Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ai | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
// TestExtractSelectSQL tests the correctness of the ExtractSelectSQL function. | ||
func TestExtractSelectSQL(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
sql string | ||
expected string | ||
}{ | ||
{ | ||
name: "NormalCase", | ||
sql: "Q: 所有kind=namespace " + | ||
"Schema_links: [kind, namespace] " + | ||
"SQL: select * from resources where kind='namespace';", | ||
expected: "select * from resources where kind='namespace'", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
actual := ExtractSelectSQL(tc.sql) | ||
require.Equal(t, tc.expected, actual) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ import ( | |
|
||
const ( | ||
Equals = "=" | ||
NLPatternType = "nl" | ||
DSLPatternType = "dsl" | ||
SQLPatternType = "sql" | ||
) | ||
|