Skip to content

Commit 38fff59

Browse files
committed
update json req and res
1 parent a29e87d commit 38fff59

File tree

3 files changed

+111
-46
lines changed

3 files changed

+111
-46
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
}

json.go

+94-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,43 +88,44 @@ 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"`
7495
Voice string `json:"voice"`
7596
ResponseFormat string `json:"response_format"`
7697
Speed float64 `json:"speed"`
7798
}
99+
100+
// CompletionRequest represents a request for text completions (non-chat).
101+
type CompletionRequest struct {
102+
Model string `json:"model"`
103+
Prompt string `json:"prompt"`
104+
MaxTokens *int `json:"max_tokens,omitempty"`
105+
Temperature *float64 `json:"temperature,omitempty"`
106+
TopP *float64 `json:"top_p,omitempty"`
107+
N *int `json:"n,omitempty"`
108+
Stop []string `json:"stop,omitempty"`
109+
PresencePenalty *float64 `json:"presence_penalty,omitempty"`
110+
FrequencyPenalty *float64 `json:"frequency_penalty,omitempty"`
111+
User string `json:"user,omitempty"`
112+
}
113+
114+
// CompletionResponse represents the response structure for text completions (non-chat).
115+
type CompletionResponse struct {
116+
ID string `json:"id"`
117+
Object string `json:"object"`
118+
Created int `json:"created"`
119+
Model string `json:"model"`
120+
Choices []struct {
121+
Text string `json:"text"`
122+
Index int `json:"index"`
123+
Logprobs interface{} `json:"logprobs"`
124+
FinishReason string `json:"finish_reason"`
125+
} `json:"choices"`
126+
Usage struct {
127+
PromptTokens int `json:"prompt_tokens"`
128+
CompletionTokens int `json:"completion_tokens"`
129+
TotalTokens int `json:"total_tokens"`
130+
} `json:"usage"`
131+
}

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)