chatApi.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. "github.com/gogf/gf/v2/util/grand"
  9. )
  10. type GPTReq struct {
  11. *BaseQuestion
  12. Identity string `json:"identity"`
  13. }
  14. type GPTRes struct {
  15. Status int `json:"status"`
  16. Response string `json:"response"`
  17. History [][]string `json:"history"`
  18. Time string `json:"time"`
  19. }
  20. var (
  21. ChatGpt = &cChatGpt{}
  22. )
  23. type cChatGpt struct {
  24. }
  25. var (
  26. tmp = []string{
  27. "smart_潜在客户_smart",
  28. }
  29. )
  30. func (c *cChatGpt) Do(ctx context.Context, qReq *QuestionReq) (res *GPTRes, err error) {
  31. //模拟请求
  32. return &GPTRes{
  33. Status: 0,
  34. Response: tmp[grand.Intn(len(tmp)-1)],
  35. History: nil,
  36. }, nil
  37. //
  38. gReq := GPTReq{
  39. BaseQuestion: qReq.BaseQuestion,
  40. Identity: g.Config().MustGet(ctx, "chat.api.identity", "剑鱼chat").String(),
  41. }
  42. //g.Dump(gReq)
  43. var gRes *gclient.Response
  44. gRes, err = g.Client().Header(consts.RequestJsonHeader).Post(ctx, g.Config().MustGet(ctx, "chat.api.addr", "").String(), gReq)
  45. if err != nil {
  46. return nil, err
  47. }
  48. res = &GPTRes{}
  49. err = gconv.Struct(gRes.ReadAll(), res)
  50. if err != nil {
  51. return nil, err
  52. }
  53. return
  54. }