chatgpt_log.go 857 B

1234567891011121314151617181920212223242526272829303132333435
  1. package model
  2. import (
  3. "database/sql/driver"
  4. "encoding/json"
  5. "fmt"
  6. "errors"
  7. "github.com/sashabaranov/go-openai"
  8. )
  9. type ChatGPTCompletionMessages []openai.ChatCompletionMessage
  10. // Scan value into Jsonb, implements sql.Scanner interface
  11. func (j *ChatGPTCompletionMessages) Scan(value interface{}) error {
  12. bytes, ok := value.([]byte)
  13. if !ok {
  14. return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", value))
  15. }
  16. result := make([]openai.ChatCompletionMessage, 0)
  17. err := json.Unmarshal(bytes, &result)
  18. *j = result
  19. return err
  20. }
  21. // Value return json value, implement driver.Valuer interface
  22. func (j *ChatGPTCompletionMessages) Value() (driver.Value, error) {
  23. return json.Marshal(*j)
  24. }
  25. type ChatGPTLog struct {
  26. Name string `json:"name"`
  27. Content ChatGPTCompletionMessages `json:"content" gorm:"serializer:json"`
  28. }