chat_completions_logic.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. workToken := compapi.GetWorkTokenByID(req.EventType, req.WorkId)
  35. apiKeyObj, ok = contextkey.AuthTokenInfoKey.GetValue(l.ctx)
  36. if !ok {
  37. return nil, errors.New("content get token err")
  38. }
  39. /*
  40. fmt.Println("=========================================")
  41. fmt.Printf("In ChatCompletion Get Token Info:\nKey:'%s'\n", apiKeyObj.Key)
  42. fmt.Printf("Title:'%s'\n", apiKeyObj.Title)
  43. fmt.Printf("OpenaiBase:'%s'\n", apiKeyObj.OpenaiBase)
  44. fmt.Printf("OpenaiKey:'%s'\n", apiKeyObj.OpenaiKey)
  45. fmt.Printf("workToken:'%s' because %s/%s\n", workToken, req.EventType, req.WorkId)
  46. fmt.Println("=========================================")
  47. */
  48. if len(apiKeyObj.OpenaiBase) == 0 || len(workToken) == 0 {
  49. return nil, errors.New("not auth info")
  50. }
  51. return l.workForFastgpt(req, workToken, apiKeyObj.OpenaiBase)
  52. }
  53. func (l *ChatCompletionsLogic) workForFastgpt(req *types.CompApiReq, apiKey string, apiBase string) (resp *types.CompOpenApiResp, err error) {
  54. //apiKey := "fastgpt-d2uehCb2T40h9chNGjf4bpFrVKmMkCFPbrjfVLZ6DAL2zzqzOFJWP"
  55. return compapi.NewFastgptChatCompletions(l.ctx, apiKey, apiBase, req)
  56. }