-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchat.go
65 lines (57 loc) · 4.77 KB
/
chat.go
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
package v1
import (
"context"
"encoding/json"
"fmt"
)
const (
ChatCompletion = "https://api.openai.com/v1/chat/completions"
)
type CreateChatCompletionRequest struct {
Model string `json:"model"` // ID of the model to use. Currently, only gpt-3.5-turbo and gpt-3.5-turbo-0301 are supported.
Messages []ChatMessage `json:"messages"` // The messages to generate chat completions for, in the chat format(https://platform.openai.com/docs/guides/chat/introduction).
MaxTokens int `json:"max_tokens,omitempty"` // 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).
Temperature float32 `json:"temperature,omitempty"` // 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.
TopP int `json:"top_p,omitempty"` // An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. We generally recommend altering this or temperature but not both.
N int `json:"n,omitempty"` // How many completions to generate for each prompt. Note: Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for max_tokens and stop.
Stream bool `json:"stream,omitempty"` // 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.
Stop string `json:"stop,omitempty"` // Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence.
PresencePenalty float32 `json:"presence_penalty,omitempty"` // presence_penalty number Optional Defaults to 0 Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.
FrequencyPenalty float32 `json:"frequency_penalty,omitempty"` // Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.
LogitBias map[string]interface{} `json:"logit_bias,omitempty"` // Modify the likelihood of specified tokens appearing in the completion. Accepts a json object that maps tokens (specified by their token ID in the GPT tokenizer) to an associated bias value from -100 to 100. You can use this tokenizer tool (which works for both GPT-2 and GPT-3) to convert text to token IDs. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token. As an example, you can pass {"50256": -100} to prevent the <|endoftext|> token from being generated.
User string `json:"user,omitempty"` // A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse
}
type ChatMessage struct {
Role string `json:"role"`
Content string `json:"content"`
}
type CreateChatCompletionResponse struct {
Id string `json:"id"`
Object string `json:"object"`
Created int `json:"created"`
Choices []Choice `json:"choices"`
Usage Usage `json:"usage"`
}
type Choice struct {
Index int `json:"index"`
Message ChatMessage `json:"message"`
FinishReason string `json:"finish_reason"`
}
type Usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}
func (chat *ChatGpt) CreateChatCompletion(ctx context.Context, req CreateChatCompletionRequest) (response CreateChatCompletionResponse, err error) {
resp, err := chat.Post(ctx, ChatCompletion, req)
if err != nil {
fmt.Println(err)
return
}
err = json.Unmarshal(resp, &response)
if err != nil {
fmt.Println(err)
return
}
return
}