chat_completions_logic.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package chat
  2. import (
  3. "context"
  4. "errors"
  5. "wechat-api/ent"
  6. "wechat-api/internal/svc"
  7. "wechat-api/internal/types"
  8. "wechat-api/internal/utils/compapi"
  9. "wechat-api/internal/utils/contextkey"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type ChatCompletionsLogic struct {
  13. logx.Logger
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. }
  17. func NewChatCompletionsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatCompletionsLogic {
  18. return &ChatCompletionsLogic{
  19. Logger: logx.WithContext(ctx),
  20. ctx: ctx,
  21. svcCtx: svcCtx}
  22. }
  23. func (l *ChatCompletionsLogic) ChatCompletions(req *types.CompApiReq) (resp *types.CompOpenApiResp, err error) {
  24. // todo: add your logic here and delete this line
  25. /*
  26. 1.鉴权获得token
  27. 2.必要参数检测及转换
  28. 3. 根据event_type选择不同处理路由
  29. */
  30. var (
  31. apiKeyObj *ent.ApiKey
  32. ok bool
  33. )
  34. apiKeyObj, ok = contextkey.AuthTokenInfoKey.GetValue(l.ctx)
  35. if !ok {
  36. return nil, errors.New("content get token err")
  37. }
  38. /*
  39. fmt.Println("=========================================")
  40. fmt.Printf("In ChatCompletion Get Token Info:\nKey:'%s'\n", apiKeyObj.Key)
  41. fmt.Printf("Title:'%s'\n", apiKeyObj.Title)
  42. fmt.Printf("OpenaiBase:'%s'\n", apiKeyObj.OpenaiBase)
  43. fmt.Printf("OpenaiKey:'%s'\n", apiKeyObj.OpenaiKey)
  44. fmt.Println("=========================================")
  45. */
  46. if len(apiKeyObj.OpenaiBase) == 0 || len(apiKeyObj.OpenaiKey) == 0 {
  47. return nil, errors.New("not auth info")
  48. }
  49. return l.workForFastgpt(req, apiKeyObj.OpenaiKey, apiKeyObj.OpenaiBase)
  50. }
  51. func (l *ChatCompletionsLogic) workForFastgpt(req *types.CompApiReq, apiKey string, apiBase string) (resp *types.CompOpenApiResp, err error) {
  52. //apiKey := "fastgpt-d2uehCb2T40h9chNGjf4bpFrVKmMkCFPbrjfVLZ6DAL2zzqzOFJWP"
  53. return compapi.NewFastgptChatCompletions(l.ctx, apiKey, apiBase, req)
  54. }