-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #361 from mergestat/codex-experiment
feat: initial `codex` experiment - natural language to SQL
- Loading branch information
Showing
5 changed files
with
109 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
-- The following is SQLite SQL. | ||
-- Table valued function commits, columns = [hash, message, author_name, author_email, author_when, committer_name, committer_email, committer_when, parents] | ||
-- Table valued function refs, columns = [name, type, remotate, full_name, hash, target] | ||
-- Table valued function stats, columns = [file_path, additions, deletions] | ||
-- Table valued function files, columns = [path, executable, contents] | ||
-- Table valued function blame, columns = [line_no, commit_hash] | ||
|
||
-- list all commits | ||
SELECT hash, message, author_name, author_email, author_when, committer_name, committer_email, committer_when, parents FROM commits; | ||
|
||
-- specify an alternative repo on disk | ||
SELECT hash, message, author_name, author_email, author_when, committer_name, committer_email, committer_when, parents FROM commits('/some/path/to/repo'); | ||
|
||
-- clone a remote repo and use it | ||
SELECT hash, message, author_name, author_email, author_when, committer_name, committer_email, committer_when, parents FROM commits('https://github.com/mergestat/mergestat-lite'); | ||
|
||
-- use the default repo, but provide an alternate branch/ref | ||
-- list available refs and branches with `SELECT * FROM refs('https://github.com/mergestat/mergestat-lite')` | ||
SELECT hash, message, author_name, author_email, author_when, committer_name, committer_email, committer_when, parents FROM commits('', 'some-ref'); | ||
|
||
-- list only commits that were authored in the last 30 days | ||
SELECT hash, message, author_name, author_email, author_when, committer_name, committer_email, committer_when, parents FROM commits WHERE author_when > datetime('now', '-30 days'); | ||
|
||
-- list the file change stats of just the HEAD commit | ||
SELECT file_path, additions, deletions FROM stats; | ||
|
||
-- list the file change stats of a specific commit with hash 'COMMIT_HASH' | ||
SELECT file_path, additions, deletions FROM stats('', 'COMMIT_HASH'); | ||
|
||
-- list the file change stats for every commit in the commit history from the HEAD. We apply an implicit lateral join to get the stats for every commits. | ||
-- this means that for every commit, we look up the stats for that commit. | ||
SELECT commits.*, stats.* FROM commits, stats('', commits.hash); | ||
|
||
-- list the file change stats for every commit in the current history but filter for commits that modified some/file.ext | ||
SELECT commits.*, stats.* FROM commits, stats('', commits.hash) WHERE file_path = 'some/file.ext'; | ||
|
||
-- list the file change stats for every commit in the current history but filter for commits that modified some/file.ext in the last year | ||
SELECT commits.*, stats.* FROM commits, stats('', commits.hash) WHERE file_path = 'some/file.ext' AND author_when > datetime('now', '-1 year'); | ||
|
||
-- |
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 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
_ "embed" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/PullRequestInc/go-gpt3" | ||
) | ||
|
||
//go:embed codex-prompt-context.sql | ||
var promptPrefix string | ||
|
||
// codexToSQL generates SQL from a natural language prompt | ||
func codexToSQL(ctx context.Context, prompt string) (string, error) { | ||
apiKey := os.Getenv("OPENAI_API_KEY") | ||
if apiKey == "" { | ||
return "", fmt.Errorf("missing OPENAI_API_KEY environment variable") | ||
} | ||
|
||
client := gpt3.NewClient(apiKey) | ||
var temp float32 = 0 | ||
var topP float32 = 1 | ||
var maxTokens = 512 | ||
res, err := client.CompletionWithEngine(ctx, "code-davinci-002", gpt3.CompletionRequest{ | ||
Prompt: []string{promptPrefix + prompt + "SELECT"}, | ||
Temperature: &temp, | ||
TopP: &topP, | ||
FrequencyPenalty: 1, | ||
Stop: []string{";"}, | ||
MaxTokens: &maxTokens, | ||
}) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
for _, choice := range res.Choices { | ||
return "SELECT" + choice.Text, nil | ||
} | ||
|
||
return "", 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
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