feat: update according to openai doc

This commit is contained in:
Yiyang Kang 2025-02-02 15:28:16 +09:00
parent 2ff8372439
commit 4ab359053a
4 changed files with 30 additions and 18 deletions

View File

@ -193,8 +193,8 @@ func handleAssistantConversation(c tele.Context, thread []*tele.Message) error {
logger.Warnw("failed to cache message", "error", err) logger.Warnw("failed to cache message", "error", err)
} }
nBytes := 0 // Used to estimated number of tokens. For now we treat 3 bytes as 1 token. nBytes := 0 // Used to estimated number of tokens. For now we treat 3 bytes as 1 token.
nBytesMax := (4096 - 512) * 3 // Leave some space for the response nBytesMax := 16384 * 3 // Leave some space for the response
sysMsg := prompts.Assistant() sysMsg := prompts.Assistant()
chatReqMsgs := []openai.ChatMessage{ chatReqMsgs := []openai.ChatMessage{
@ -237,7 +237,6 @@ func handleAssistantConversation(c tele.Context, thread []*tele.Message) error {
Model: openai.ModelGpt4O, Model: openai.ModelGpt4O,
Messages: chatReqMsgs, Messages: chatReqMsgs,
Temperature: lo.ToPtr(0.42), Temperature: lo.ToPtr(0.42),
MaxTokens: 2048,
User: assistantHashUserId(lastMsg.Sender.ID), User: assistantHashUserId(lastMsg.Sender.ID),
} }

View File

@ -6,28 +6,40 @@ type ChatRole string
const ( const (
ChatRoleSystem ChatRole = "system" ChatRoleSystem ChatRole = "system"
ChatRoleDeveloper ChatRole = "developer" // replaces `system` role for o1 and newer models
ChatRoleTool ChatRole = "tool"
ChatRoleAssistant ChatRole = "assistant" ChatRoleAssistant ChatRole = "assistant"
ChatRoleUser ChatRole = "user" ChatRoleUser ChatRole = "user"
) )
type ReasoningEffort string
const (
ReasoningEffortLow ReasoningEffort = "low"
ReasoningEffortMedium ReasoningEffort = "medium"
ReasoningEffortHigh ReasoningEffort = "high"
)
type ChatMessage struct { type ChatMessage struct {
Role ChatRole `json:"role"` Role ChatRole `json:"role"`
Content string `json:"content"` Content string `json:"content"`
} }
type ChatRequest struct { type ChatRequest struct {
Model string `json:"model"` Model string `json:"model"`
Messages []ChatMessage `json:"messages"` Messages []ChatMessage `json:"messages"`
Temperature *float64 `json:"temperature,omitempty"` // What sampling temperature to use, between 0 and 2. Temperature *float64 `json:"temperature,omitempty"` // What sampling temperature to use, between 0 and 2.
TopP *float64 `json:"top_p,omitempty"` // Nucleus sampling. Specify this or temperature but not both. TopP *float64 `json:"top_p,omitempty"` // Nucleus sampling. Specify this or temperature but not both.
N int `json:"n,omitempty"` // How many chat completion choices to generate for each input message. N int `json:"n,omitempty"` // How many chat completion choices to generate for each input message.
Stream bool `json:"stream,omitempty"` // If set, partial message deltas will be sent as data-only server-sent events as they become available. Stream bool `json:"stream,omitempty"` // If set, partial message deltas will be sent as data-only server-sent events as they become available.
Stop []string `json:"stop,omitempty"` // Up to 4 sequences where the API will stop generating further tokens. Stop []string `json:"stop,omitempty"` // Up to 4 sequences where the API will stop generating further tokens.
MaxTokens int `json:"max_tokens,omitempty"` MaxTokens int `json:"max_tokens,omitempty"` // Deprecated: in favor of `max_completion_tokens`
PresencePenalty *float64 `json:"presence_penalty,omitempty"` // Number between -2.0 and 2.0. MaxCompletionTokens int `json:"max_completion_tokens,omitempty"` // Including visible output tokens and reasoning tokens.
FrequencyPenalty *float64 `json:"frequency_penalty,omitempty"` // Number between -2.0 and 2.0. PresencePenalty *float64 `json:"presence_penalty,omitempty"` // Number between -2.0 and 2.0.
LogitBias map[string]float64 `json:"logit_bias,omitempty"` // Modify the likelihood of specified tokens appearing in the completion. FrequencyPenalty *float64 `json:"frequency_penalty,omitempty"` // Number between -2.0 and 2.0.
User string `json:"user,omitempty"` // A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. LogitBias map[string]float64 `json:"logit_bias,omitempty"` // Modify the likelihood of specified tokens appearing in the completion.
User string `json:"user,omitempty"` // A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
ReasoningEffort ReasoningEffort `json:"reasoning_effort,omitempty"` // Constrains effort on reasoning for reasoning models.
} }
type ChatResponseChoice struct { type ChatResponseChoice struct {

View File

@ -20,7 +20,7 @@ func NewClient(apiKey string) *Client {
cli := resty.New(). cli := resty.New().
SetTransport(&http.Transport{ SetTransport(&http.Transport{
Proxy: http.ProxyFromEnvironment, Proxy: http.ProxyFromEnvironment,
ResponseHeaderTimeout: 10 * time.Second, ResponseHeaderTimeout: 90 * time.Second,
}). }).
SetBaseURL("https://api.openai.com"). SetBaseURL("https://api.openai.com").
SetHeader("Authorization", "Bearer "+apiKey). SetHeader("Authorization", "Bearer "+apiKey).

View File

@ -2,6 +2,7 @@ package openai
const ( const (
ModelGpt4O = "gpt-4o" // Safe default ModelGpt4O = "gpt-4o" // Safe default
ModelO1Preview = "o1-preview" // Expensive ModelO1Preview = "o1-preview" // Expensive reasoning model
ModelO1Mini = "o1-mini" ModelO1Mini = "o1-mini" // Cheaper reasoning model
ModelO3Mini = "o3-mini" // Cheaper yet powerful reasoning model
) )