-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchat_completion.go
More file actions
95 lines (79 loc) · 3.33 KB
/
chat_completion.go
File metadata and controls
95 lines (79 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package openaiclient
import "fmt"
// CreateChatCompletionRequest ...
type CreateChatCompletionRequest struct {
// Model is ID of the model to use.
// You can use the List models API to see all of your available models,
// or see our Model overview for descriptions of them.
// model is required.
Model string `json:"model"`
// messages is the messages to generate chat completions for, in the chat format.
Messages []CreateChatCompletionMessage `json:"messages"`
// MaxTokens is the maximum number of tokens to generate in the completion.
// The token count of your prompt plus max_tokens cannot exceed the model's context length.
// Most models have a context length of 2048 tokens (except for the newest models, which support 4096).
MaxTokens int `json:"max_tokens"`
// Temperature means What sampling temperature to use, between 0 and 2.
// Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
// We generally recommend altering this or top_p but not both.
Temperature float64 `json:"temperature"`
// Stream whether to stream back partial progress.
// If set, tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message.
Stream bool `json:"stream"`
// Stop is up to 4 sequences where the API will stop generating further tokens.
// The returned text will not contain the stop sequence.
Stop string `json:"stop"`
// Suffix string `json:"suffix"`
// TopP int `json:"top_p"`
// N int `json:"n"`
// Logprobs bool `json:"logprobs"`
// User is a unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
User string `json:"user"`
}
// CreateChatCompletionResponse ...
type CreateChatCompletionResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Model string `json:"model"`
Choices []struct {
Message CreateChatCompletionMessage `json:"message"`
Index int `json:"index"`
Logprobs int `json:"logprobs"`
FinishReason string `json:"finish_reason"`
} `json:"choices"`
Usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
} `json:"usage"`
}
// CreateChatCompletionMessage ...
type CreateChatCompletionMessage struct {
// Role is the mssage role, available: system | user | assistant
Role string `json:"role"`
Content string `json:"content"`
}
// reference: https://platform.openai.com/docs/api-reference/completions/create
func (c *client) CreateChatCompletion(cfg *CreateChatCompletionRequest) (*CreateChatCompletionResponse, error) {
if cfg.Temperature == 0 {
cfg.Temperature = 0.8
}
var apiPath string
switch c.cfg.APIType {
case APITypeOpenAI:
// /chat/completions
apiPath = fmt.Sprintf("/%s", ResourceChatCompletion)
case APITypeAzure:
// openai/deployments/{deployment_id}/completions
apiPath = fmt.Sprintf("/openai/deployments/%s/%s", c.cfg.AzureDeployment, ResourceChatCompletion)
}
resp, err := c.post(apiPath, cfg)
if err != nil {
return nil, err
}
var response CreateChatCompletionResponse
if err := resp.UnmarshalJSON(&response); err != nil {
return nil, err
}
return &response, nil
}