Skip to content

update ChatCompletionResponse.Created filed ,support return Created type is string #1003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion chat.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package openai

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
)

// Chat message role defined by the OpenAI API.
Expand Down Expand Up @@ -385,11 +388,65 @@ type ChatCompletionChoice struct {
ContentFilterResults ContentFilterResults `json:"content_filter_results,omitempty"`
}

type FlexibleTime struct {
TimeNumber int64
TimeStr string
}

func (ft *FlexibleTime) UnmarshalJSON(data []byte) error {
if isNullOrEmpty(data) {
return nil
}
var timestamp int64
if err := json.Unmarshal(data, &timestamp); err == nil {
ft.TimeNumber = timestamp
ft.TimeStr = fmt.Sprintf("%d", timestamp)
return nil
}
var timeStr string
if err := json.Unmarshal(data, &timeStr); err != nil {
return fmt.Errorf("created type need number or string")
}

return ft.parseWithLayouts([]string{
"2006/01/02 15:04:05",
time.RFC3339,
"2006-01-02 15:04:05",
})
}

func isNullOrEmpty(data []byte) bool {
if bytes.Equal(data, []byte("null")) {
return true
}

if len(data) == 0 {
return true
}

if len(data) == 2 && data[0] == '"' && data[1] == '"' {
return true
}

return false
}

func (ft *FlexibleTime) parseWithLayouts(layouts []string) error {
for _, layout := range layouts {
parsedTime, err := time.Parse(layout, ft.TimeStr)
if err == nil {
ft.TimeNumber = parsedTime.Unix()
return nil
}
}
return fmt.Errorf("time string cannot be parsed: %s", ft.TimeStr)
}

// ChatCompletionResponse represents a response structure for chat completion API.
type ChatCompletionResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Created FlexibleTime `json:"created,omitempty"`
Model string `json:"model"`
Choices []ChatCompletionChoice `json:"choices"`
Usage Usage `json:"usage"`
Expand Down
18 changes: 12 additions & 6 deletions chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -771,9 +771,12 @@ func handleChatCompletionEndpoint(w http.ResponseWriter, r *http.Request) {
return
}
res := openai.ChatCompletionResponse{
ID: strconv.Itoa(int(time.Now().Unix())),
Object: "test-object",
Created: time.Now().Unix(),
ID: strconv.Itoa(int(time.Now().Unix())),
Object: "test-object",
Created: openai.FlexibleTime{
TimeNumber: time.Now().Unix(),
TimeStr: time.Now().String(),
},
// would be nice to validate Model during testing, but
// this may not be possible with how much upkeep
// would be required / wouldn't make much sense
Expand Down Expand Up @@ -853,9 +856,12 @@ func handleDeepseekR1ChatCompletionEndpoint(w http.ResponseWriter, r *http.Reque
return
}
res := openai.ChatCompletionResponse{
ID: strconv.Itoa(int(time.Now().Unix())),
Object: "test-object",
Created: time.Now().Unix(),
ID: strconv.Itoa(int(time.Now().Unix())),
Object: "test-object",
Created: openai.FlexibleTime{
TimeNumber: time.Now().Unix(),
TimeStr: time.Now().String(),
},
// would be nice to validate Model during testing, but
// this may not be possible with how much upkeep
// would be required / wouldn't make much sense
Expand Down
Loading