base.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. package compapi
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "net/http"
  7. "reflect"
  8. "sync"
  9. "wechat-api/internal/types"
  10. "wechat-api/internal/utils/contextkey"
  11. "github.com/invopop/jsonschema"
  12. "github.com/openai/openai-go"
  13. "github.com/openai/openai-go/option"
  14. "github.com/openai/openai-go/packages/ssestream"
  15. "github.com/zeromicro/go-zero/rest/httpx"
  16. )
  17. type ClientConfig struct {
  18. ApiKey string
  19. ApiBase string
  20. }
  21. type ClientOption func(*ClientConfig)
  22. func WithApiKey(ApiKey string) ClientOption {
  23. return func(cfg *ClientConfig) {
  24. cfg.ApiKey = ApiKey
  25. }
  26. }
  27. func WithApiBase(ApiBase string) ClientOption {
  28. return func(cfg *ClientConfig) {
  29. cfg.ApiBase = ApiBase
  30. }
  31. }
  32. type clientActionFace interface {
  33. DoRequest(req *types.CompApiReq) (*types.CompOpenApiResp, error)
  34. DoRequestStream(req *types.CompApiReq) (*types.CompOpenApiResp, error)
  35. BuildRequest(req *types.CompApiReq) error
  36. CallbackPrepare(params any) ([]byte, error)
  37. }
  38. type Client struct {
  39. OAC *openai.Client
  40. Config ClientConfig
  41. ctx context.Context
  42. }
  43. func NewClient(ctx context.Context, opts ...ClientOption) *Client {
  44. client := Client{}
  45. for _, opt := range opts {
  46. opt(&client.Config)
  47. }
  48. client.NewOAC() //初始化openai client
  49. client.ctx = ctx
  50. return &client
  51. }
  52. // 以下新增加client类型工厂自注册相关函数
  53. // --- Registry Func Type---
  54. type ClientBuilderFunc func(c *Client) (clientActionFace, error)
  55. var (
  56. clientRegistry = make(map[string]ClientBuilderFunc)
  57. defaultClientType string
  58. defaultClientBuilder ClientBuilderFunc
  59. registryMutex sync.RWMutex // Protects both registry and default builder
  60. )
  61. // RegisterClient remains the same
  62. func RegisterClient(clientType string, builder ClientBuilderFunc) error {
  63. registryMutex.Lock()
  64. defer registryMutex.Unlock()
  65. if _, exists := clientRegistry[clientType]; exists {
  66. return fmt.Errorf("client type '%s' already registered", clientType)
  67. }
  68. clientRegistry[clientType] = builder
  69. return nil
  70. }
  71. // RegisterDefaultClient registers a builder function as the default fallback.
  72. // Typically called from the init() function of the default client implementation.
  73. func RegisterDefaultClient(clientType string, builder ClientBuilderFunc) error {
  74. registryMutex.Lock()
  75. defer registryMutex.Unlock()
  76. if defaultClientBuilder != nil {
  77. // Prevent multiple defaults or decide on override behavior
  78. return fmt.Errorf("default client type '%s' already registered, cannot register '%s' as default", defaultClientType, clientType)
  79. }
  80. defaultClientBuilder = builder
  81. defaultClientType = clientType // Store the name
  82. return nil
  83. }
  84. // GetClientBuilder remains the same
  85. func GetClientBuilder(clientType string) (ClientBuilderFunc, bool) {
  86. registryMutex.RLock()
  87. defer registryMutex.RUnlock()
  88. builder, exists := clientRegistry[clientType]
  89. return builder, exists
  90. }
  91. // GetDefaultClientBuilder retrieves the registered default builder.
  92. func GetDefaultClientBuilder() (ClientBuilderFunc, bool) {
  93. registryMutex.RLock()
  94. defer registryMutex.RUnlock()
  95. exists := defaultClientBuilder != nil
  96. return defaultClientBuilder, exists
  97. }
  98. // 根据client生成不同实现了clientActionFace接口的client
  99. func (me *Client) GetClientActFace(clientType string) (clientActionFace, error) {
  100. // 1. Try to find the specific client type
  101. builder, exists := GetClientBuilder(clientType)
  102. if exists {
  103. return builder(me)
  104. }
  105. // 2. If not found, try to get the default client builder
  106. defaultBuilder, defaultExists := GetDefaultClientBuilder()
  107. if defaultExists {
  108. return defaultBuilder(me)
  109. }
  110. // 3. If neither specific nor default exists, return an error
  111. return nil, fmt.Errorf("unsupported client type '%s' and no default client registered", clientType)
  112. }
  113. func (me *Client) NewOAC() {
  114. opts := []option.RequestOption{}
  115. if len(me.Config.ApiKey) > 0 {
  116. opts = append(opts, option.WithAPIKey(me.Config.ApiKey))
  117. }
  118. if len(me.Config.ApiBase) > 0 {
  119. opts = append(opts, option.WithBaseURL(me.Config.ApiBase))
  120. }
  121. oac := openai.NewClient(opts...)
  122. me.OAC = &oac
  123. }
  124. func (me *Client) Callback(clientType string, callbackUrl string, params any) (map[string]any, error) {
  125. actFace, err := me.GetClientActFace(clientType)
  126. if err != nil {
  127. return nil, err
  128. }
  129. //临时测试
  130. //humanSeeActFaceMake(actFace, clientType, "Callback")
  131. var newParams []byte
  132. if newParams, err = actFace.CallbackPrepare(params); err != nil {
  133. return nil, err
  134. }
  135. //Post(ctx context.Context, path string, params interface{}, res interface{}, opts ...option.RequestOption)
  136. resp := map[string]any{}
  137. err = me.OAC.Post(me.ctx, callbackUrl, newParams, &resp)
  138. if err != nil {
  139. fmt.Printf("Callback Post(%s) By Params:'%s' error\n", callbackUrl, string(newParams))
  140. return nil, err
  141. }
  142. return resp, nil
  143. }
  144. func (me *Client) Chat(chatInfo *types.CompApiReq) (*types.CompOpenApiResp, error) {
  145. var (
  146. err error
  147. actFace clientActionFace
  148. apiResp *types.CompOpenApiResp
  149. )
  150. actFace, err = me.GetClientActFace(chatInfo.EventType)
  151. if err != nil {
  152. return nil, err
  153. }
  154. //临时测试
  155. //humanSeeActFaceMake(actFace, chatInfo.EventType, "Chat")
  156. err = actFace.BuildRequest(chatInfo)
  157. if err != nil {
  158. return nil, err
  159. }
  160. if chatInfo.Stream {
  161. apiResp, err = actFace.DoRequestStream(chatInfo)
  162. } else {
  163. apiResp, err = actFace.DoRequest(chatInfo)
  164. }
  165. return apiResp, err
  166. }
  167. func GenerateSchema[T any]() any {
  168. // Structured Outputs uses a subset of JSON schema
  169. // These flags are necessary to comply with the subset
  170. reflector := jsonschema.Reflector{
  171. AllowAdditionalProperties: false,
  172. DoNotReference: true,
  173. }
  174. var v T
  175. schema := reflector.Reflect(v)
  176. return schema
  177. }
  178. func GenerateSchemaFromValue(value any) (any, error) {
  179. if value == nil {
  180. // 处理 nil 值的情况,根据你的需求决定是返回错误还是空 schema
  181. return nil, fmt.Errorf("cannot generate schema from a nil value")
  182. }
  183. reflector := jsonschema.Reflector{
  184. AllowAdditionalProperties: false, // 根据你的需求设置
  185. DoNotReference: true, // 根据你的需求设置
  186. }
  187. // 直接对传入的值进行反射
  188. schema := reflector.Reflect(value)
  189. return schema, nil
  190. }
  191. // 新函数:通过 reflect.Type 生成 Schema
  192. func GenerateSchemaByType(t reflect.Type) any {
  193. reflector := jsonschema.Reflector{
  194. AllowAdditionalProperties: false,
  195. DoNotReference: true,
  196. }
  197. schema := reflector.ReflectFromType(t)
  198. return schema
  199. }
  200. func getHttpResponseTools(ctx context.Context) (*http.ResponseWriter, *http.Flusher, error) {
  201. hw, ok := contextkey.HttpResponseWriterKey.GetValue(ctx) //context取出http.ResponseWriter
  202. if !ok {
  203. return nil, nil, errors.New("content get http writer err")
  204. }
  205. flusher, ok := (hw).(http.Flusher)
  206. if !ok {
  207. return nil, nil, errors.New("streaming unsupported")
  208. }
  209. return &hw, &flusher, nil
  210. }
  211. func streamOut(ctx context.Context, res *http.Response) {
  212. var ehw http.ResponseWriter
  213. hw, flusher, err := getHttpResponseTools(ctx)
  214. if err != nil {
  215. http.Error(ehw, "Streaming unsupported!", http.StatusInternalServerError)
  216. }
  217. //获取API返回结果流
  218. chatStream := ssestream.NewStream[ApiRespStreamChunk](ApiRespStreamDecoder(res), err)
  219. defer chatStream.Close()
  220. //设置流式输出头 http1.1
  221. (*hw).Header().Set("Content-Type", "text/event-stream;charset=utf-8")
  222. (*hw).Header().Set("Connection", "keep-alive")
  223. (*hw).Header().Set("Cache-Control", "no-cache")
  224. for chatStream.Next() {
  225. chunk := chatStream.Current()
  226. fmt.Fprintf((*hw), "data:%s\n\n", chunk.Data.RAW)
  227. (*flusher).Flush()
  228. //time.Sleep(1 * time.Millisecond)
  229. }
  230. fmt.Fprintf((*hw), "data:%s\n\n", "[DONE]")
  231. (*flusher).Flush()
  232. httpx.Ok((*hw))
  233. }
  234. func humanSeeActFaceMake(actFace clientActionFace, clientType string, funcName string) {
  235. clientName := ""
  236. switch actFace.(type) {
  237. case *MismatchClient:
  238. clientName = "MismatchClient"
  239. case *IntentClient:
  240. clientName = "IntentClient"
  241. case *FastgptClient:
  242. clientName = "FastgptClient"
  243. default:
  244. clientName = "maybe StdClient"
  245. }
  246. fmt.Printf("%s.%s() for EventType:'%s'\n", clientName, funcName, clientType)
  247. }