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

Conversation

dragonTalon
Copy link

ChatCompletionResponse.Created filed support compatibility between int64 and string formats. and created is null

Created             FlexibleTime           `json:"created,omitempty"`

type FlexibleTime struct {
	Time int64  //openai
	Raw  string // azure
}

func (ft *FlexibleTime) UnmarshalJSON(data []byte) error {
	var timestamp int64
	if err := json.Unmarshal(data, &timestamp); err == nil {
		ft.Time = timestamp
		ft.Raw = fmt.Sprintf("%d", timestamp)
		return nil
	}
	var timeStr string
	if err := json.Unmarshal(data, &timeStr); err == nil {
		ft.Raw = timeStr
		layouts := []string{
			"2006/01/02 15:04:05",
			time.RFC3339,
			"2006-01-02 15:04:05",
		}
		for _, layout := range layouts {
			if t, err := time.Parse(layout, timeStr); err == nil {
				ft.Time = t.Unix()
				return nil
			}
		}
		return fmt.Errorf("无法解析时间字符串: %s", timeStr)
	}

	return fmt.Errorf("created 字段必须是整数或字符串")
}

Issue: #1002

talonjian added 3 commits May 16, 2025 16:54
the creation process must support compatibility between int64 and string formats.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant