Skip to content

Commit 6d2d2e0

Browse files
author
Kevin Jayne
authored
Merge pull request #1 from seemywingz/development
Development
2 parents cce9aef + c611baf commit 6d2d2e0

File tree

5 files changed

+94
-58
lines changed

5 files changed

+94
-58
lines changed

chat.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@ package goai
22

33
func (goai Client) ChatCompletion(messages []Message) (ChatCompletionResponse, error) {
44
res := ChatCompletionResponse{}
5+
56
req := ChatCompletionRequest{
6-
N: 1,
7+
N: IntPtr(1), // Convert to pointer using helper function
78
Messages: messages,
89
User: goai.User,
9-
TopP: goai.TopP,
10+
TopP: Float64Ptr(goai.TopP), // Convert to pointer
1011
Model: goai.ChatModel,
11-
MaxTokens: goai.MaxTokens,
12-
Temperature: goai.Temperature,
13-
PresencePenalty: goai.PresencePenalty,
14-
FrequencyPenalty: goai.FrequencyPenalty,
12+
MaxTokens: IntPtr(goai.MaxTokens), // Convert to pointer
13+
Temperature: Float64Ptr(goai.Temperature), // Convert to pointer
14+
PresencePenalty: Float64Ptr(goai.PresencePenalty), // Convert to pointer
15+
FrequencyPenalty: Float64Ptr(goai.FrequencyPenalty), // Convert to pointer
1516
}
17+
1618
_, err := goai.PostJson(req, &res, goai.Endpoint+"chat/completions")
1719
return res, err
1820
}

http.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@ import (
1111
"strconv"
1212
)
1313

14-
func httpCatchErr(resp *http.Response, jsonString []byte) error {
14+
func httpCatchErr(resp *http.Response) ([]byte, error) {
15+
resBody, err := io.ReadAll(resp.Body)
16+
if err != nil {
17+
return nil, err
18+
}
1519
// Check for HTTP Response Errors
1620
if resp.StatusCode != 200 {
17-
return errors.New("API Error: " + strconv.Itoa(resp.StatusCode) + "\n" + string(jsonString))
21+
errJson := ErrorResponse{}
22+
err = json.Unmarshal(resBody, &errJson)
23+
return nil, errors.New("API Error: " + strconv.Itoa(resp.StatusCode) + "\n" + errJson.Error.Message)
1824
}
19-
return nil
25+
return resBody, nil
2026
}
2127

2228
func (goai Client) MakeRequest(request *http.Request, responseJson interface{}) ([]byte, error) {
@@ -27,14 +33,12 @@ func (goai Client) MakeRequest(request *http.Request, responseJson interface{})
2733
return nil, err
2834
}
2935

30-
// Read the JSON Response Body
31-
jsonString, err := io.ReadAll(resp.Body)
36+
// Check for HTTP Errors
37+
jsonString, err := httpCatchErr(resp)
3238
if err != nil {
3339
return nil, err
3440
}
3541

36-
// Check for HTTP Errors
37-
httpCatchErr(resp, jsonString)
3842
if goai.Verbose {
3943
b, err := io.ReadAll(resp.Body)
4044
if err != nil {

images.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,13 @@ func (goai Client) UploadImage(requestJson, responseJson interface{}, endpoint,
138138
}
139139

140140
// Read the JSON Response Body
141-
jsonString, err := io.ReadAll(resp.Body)
142-
if err != nil {
143-
return err
144-
}
141+
// jsonString, err := io.ReadAll(resp.Body)
142+
// if err != nil {
143+
// return err
144+
// }
145145

146146
// Check for API Errors
147-
err = httpCatchErr(resp, jsonString)
147+
jsonString, err := httpCatchErr(resp)
148148
if err != nil {
149149
return err
150150
}

json.go

+61-40
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,55 @@
11
package goai
22

3+
// ChatCompletionRequest represents the request structure for OpenAI's chat completion API.
4+
type ChatCompletionRequest struct {
5+
Model string `json:"model"`
6+
Messages []Message `json:"messages"`
7+
MaxTokens *int `json:"max_tokens,omitempty"`
8+
Temperature *float64 `json:"temperature,omitempty"`
9+
TopP *float64 `json:"top_p,omitempty"`
10+
N *int `json:"n,omitempty"`
11+
Stream bool `json:"stream"`
12+
PresencePenalty *float64 `json:"presence_penalty,omitempty"`
13+
FrequencyPenalty *float64 `json:"frequency_penalty,omitempty"`
14+
Stop []string `json:"stop,omitempty"`
15+
User string `json:"user,omitempty"`
16+
}
17+
18+
// ChatCompletionResponse represents the response structure from OpenAI's chat completion API.
19+
type ChatCompletionResponse struct {
20+
ID string `json:"id"`
21+
Object string `json:"object"`
22+
Created int `json:"created"`
23+
Model string `json:"model"`
24+
SystemFingerprint string `json:"system_fingerprint"`
25+
Choices []struct {
26+
Index int `json:"index"`
27+
Message struct {
28+
Role string `json:"role"`
29+
Content string `json:"content"`
30+
} `json:"message"`
31+
Logprobs interface{} `json:"logprobs"`
32+
FinishReason string `json:"finish_reason"`
33+
} `json:"choices"`
34+
Usage struct {
35+
PromptTokens int `json:"prompt_tokens"`
36+
CompletionTokens int `json:"completion_tokens"`
37+
TotalTokens int `json:"total_tokens"`
38+
CompletionTokensDetails struct {
39+
ReasoningTokens int `json:"reasoning_tokens"`
40+
AcceptedPredictionTokens int `json:"accepted_prediction_tokens"`
41+
RejectedPredictionTokens int `json:"rejected_prediction_tokens"`
42+
} `json:"completion_tokens_details"`
43+
} `json:"usage"`
44+
}
45+
46+
// Message represents a single message in the conversation history.
47+
type Message struct {
48+
Role string `json:"role"` // "system", "assistant", or "user"
49+
Content string `json:"content"` // The text content of the message
50+
}
51+
52+
// ImageRequest represents the request structure for OpenAI's image generation API.
353
type ImageRequest struct {
454
Prompt string `json:"prompt"`
555
N int `json:"n"`
@@ -9,23 +59,26 @@ type ImageRequest struct {
959
Model string `json:"model"`
1060
}
1161

62+
// ImageEditRequest represents the request structure for editing images using OpenAI's API.
1263
type ImageEditRequest struct {
13-
Prompt string `json:"prompt"`
14-
N int `json:"n"`
15-
Size string `json:"size"`
16-
ResponseFormat string `json:"response_format"`
17-
User string `json:"user"`
18-
Image string `json:"image"`
19-
Mask string `json:"mask"`
64+
Prompt string `json:"prompt"`
65+
N int `json:"n"`
66+
Size string `json:"size"`
67+
ResponseFormat string `json:"response_format"`
68+
User string `json:"user"`
69+
Image *string `json:"image,omitempty"`
70+
Mask *string `json:"mask,omitempty"`
2071
}
2172

73+
// ImageResponse represents the response structure for OpenAI's image generation/editing APIs.
2274
type ImageResponse struct {
2375
Created int64 `json:"created"`
2476
Data []struct {
2577
URL string `json:"url"`
2678
} `json:"data"`
2779
}
2880

81+
// ErrorResponse represents the structure of an error response from OpenAI's API.
2982
type ErrorResponse struct {
3083
Error struct {
3184
Message string `json:"message"`
@@ -35,39 +88,7 @@ type ErrorResponse struct {
3588
} `json:"error"`
3689
}
3790

38-
type ChatCompletionResponse struct {
39-
ID string `json:"id"`
40-
Object string `json:"object"`
41-
Created int `json:"created"`
42-
Choices []Choice `json:"choices"`
43-
Usage map[string]int `json:"usage"`
44-
}
45-
46-
type Choice struct {
47-
Index int `json:"index"`
48-
Message Message `json:"message"`
49-
FinishReason string `json:"finish_reason"`
50-
}
51-
52-
type Message struct {
53-
Role string `json:"role"`
54-
Content string `json:"content"`
55-
}
56-
57-
type ChatCompletionRequest struct {
58-
Model string `json:"model"`
59-
Messages []Message `json:"messages"`
60-
MaxTokens int `json:"max_tokens"`
61-
Temperature float64 `json:"temperature"`
62-
TopP float64 `json:"top_p"`
63-
N int `json:"n"`
64-
Stream bool `json:"stream"`
65-
PresencePenalty float64 `json:"presence_penalty"`
66-
FrequencyPenalty float64 `json:"frequency_penalty"`
67-
Stop []string `json:"stop"`
68-
User string `json:"user"`
69-
}
70-
91+
// TTSRequest represents the request structure for OpenAI's text-to-speech API.
7192
type TTSRequest struct {
7293
Model string `json:"model"`
7394
Input string `json:"input"`

utils.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package goai
2+
3+
func IntPtr(i int) *int {
4+
return &i
5+
}
6+
7+
func Float64Ptr(f float64) *float64 {
8+
return &f
9+
}

0 commit comments

Comments
 (0)