@@ -56,18 +56,27 @@ func main() {
56
56
}
57
57
}
58
58
59
+ type OpenAIResponse struct {
60
+ Choices []struct {
61
+ Text string `json:"text"`
62
+ } `json:"choices"`
63
+ }
64
+
59
65
func generateCommitMessage (prompt , modelEngine , openaiAPIKey string ) (string , error ) {
60
66
client := resty .New ()
61
67
62
68
resp , err := client .R ().
63
- SetHeader ("Authorization" , fmt .Sprintf ("Bearer %s" , openaiAPIKey )).
69
+ SetHeaders (map [string ]string {
70
+ "Content-Type" : "application/json" ,
71
+ "Authorization" : fmt .Sprintf ("Bearer %s" , openaiAPIKey ),
72
+ }).
64
73
SetBody (map [string ]interface {}{
65
74
"model" : modelEngine ,
66
- "prompt" : prompt ,
75
+ "prompt" : prompt ,
67
76
"max_tokens" : 50 , // Adjust the max tokens and other parameters as needed
68
77
"temperature" : 0.5 , // Adjust the temperature value as needed
69
78
}).
70
- SetResult (map [ string ] interface {} {}).
79
+ SetResult (& OpenAIResponse {}).
71
80
Post ("https://api.openai.com/v1/completions" )
72
81
73
82
if err != nil {
@@ -78,27 +87,16 @@ func generateCommitMessage(prompt, modelEngine, openaiAPIKey string) (string, er
78
87
return "" , fmt .Errorf ("API request failed with status code: %d" , resp .StatusCode ())
79
88
}
80
89
81
- result , ok := resp .Result ().(map [ string ] interface {} )
90
+ openAIResp , ok := resp .Result ().(* OpenAIResponse )
82
91
if ! ok {
83
92
return "" , fmt .Errorf ("Unable to access the desired fields from resp.Result()" )
84
93
}
85
94
86
- choices , ok := result ["choices" ].([]interface {})
87
- if ! ok || len (choices ) == 0 {
88
- return "" , fmt .Errorf ("Unable to access the choices field or choices is empty" )
89
- }
90
-
91
- choice , ok := choices [0 ].(map [string ]interface {})
92
- if ! ok {
93
- return "" , fmt .Errorf ("Unable to access the choice element" )
94
- }
95
-
96
- text , ok := choice ["text" ].(string )
97
- if ! ok {
98
- return "" , fmt .Errorf ("Unable to access the text field" )
95
+ if len (openAIResp .Choices ) == 0 {
96
+ return "" , fmt .Errorf ("No choices returned in the response" )
99
97
}
100
98
101
- return text , nil
99
+ return openAIResp . Choices [ 0 ]. Text , nil
102
100
}
103
101
104
102
func commitChanges (commitMessage string ) {
0 commit comments