chatApi.go 1020 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package model
  2. import (
  3. "aiChat/internal/consts"
  4. "context"
  5. "github.com/gogf/gf/v2/frame/g"
  6. "github.com/gogf/gf/v2/net/gclient"
  7. "github.com/gogf/gf/v2/util/gconv"
  8. )
  9. type GPTReq struct {
  10. *BaseQuestion
  11. Identity string `json:"identity"`
  12. }
  13. type GPTRes struct {
  14. Status int `json:"status"`
  15. Response string `json:"response"`
  16. History [][]string `json:"history"`
  17. Time string `json:"time"`
  18. }
  19. var (
  20. ChatGpt = &cChatGpt{}
  21. )
  22. type cChatGpt struct {
  23. }
  24. func (c *cChatGpt) Do(ctx context.Context, qReq *QuestionReq) (res *GPTRes, err error) {
  25. gReq := GPTReq{
  26. BaseQuestion: qReq.BaseQuestion,
  27. Identity: g.Config().MustGet(ctx, "chat.api.identity", "剑鱼chat").String(),
  28. }
  29. //g.Dump(gReq)
  30. var gRes *gclient.Response
  31. gRes, err = g.Client().Header(consts.RequestJsonHeader).Post(ctx, g.Config().MustGet(ctx, "chat.api.addr", "").String(), gReq)
  32. if err != nil {
  33. return nil, err
  34. }
  35. res = &GPTRes{}
  36. err = gconv.Struct(gRes.ReadAll(), res)
  37. if err != nil {
  38. return nil, err
  39. }
  40. return
  41. }