From 76d57e0965c9385b71b8ca971945be7ab8f8c04b Mon Sep 17 00:00:00 2001 From: bjwswang Date: Tue, 9 Apr 2024 09:09:38 +0000 Subject: [PATCH] chore: init app runtime with chatserver's client which is also the system client Signed-off-by: bjwswang --- apiserver/pkg/chat/chat_docs.go | 2 +- apiserver/pkg/chat/chat_server.go | 42 ++++++++++++------------------- pkg/config/config.go | 15 ++++++++--- 3 files changed, 28 insertions(+), 31 deletions(-) diff --git a/apiserver/pkg/chat/chat_docs.go b/apiserver/pkg/chat/chat_docs.go index f8dc88c23..7882d549e 100644 --- a/apiserver/pkg/chat/chat_docs.go +++ b/apiserver/pkg/chat/chat_docs.go @@ -200,7 +200,7 @@ func (cs *ChatServer) BuildConversationKnowledgeBase(ctx context.Context, req Co } // app as ownerreference - app, _, err := cs.GetApp(ctx, req.APPName, req.AppNamespace) + app, err := cs.GetApp(ctx, req.APPName, req.AppNamespace) if err != nil { return err } diff --git a/apiserver/pkg/chat/chat_server.go b/apiserver/pkg/chat/chat_server.go index 8725abb81..59ef26fa2 100644 --- a/apiserver/pkg/chat/chat_server.go +++ b/apiserver/pkg/chat/chat_server.go @@ -107,7 +107,7 @@ func (cs *ChatServer) Storage() storage.Storage { } func (cs *ChatServer) AppRun(ctx context.Context, req ChatReqBody, respStream chan string, messageID string, timeout *float64) (*ChatRespBody, error) { - app, c, err := cs.GetApp(ctx, req.APPName, req.AppNamespace) + app, err := cs.GetApp(ctx, req.APPName, req.AppNamespace) if err != nil { return nil, err } @@ -153,12 +153,13 @@ func (cs *ChatServer) AppRun(ctx context.Context, req ChatReqBody, respStream ch Query: req.Query, Answer: "", }) - appRun, err := appruntime.NewAppOrGetFromCache(ctx, c, app) + // since authenticattion already passed by http handler,we should use chatserver's client which is also the system client to new/ini appruntime + appRun, err := appruntime.NewAppOrGetFromCache(ctx, cs.cli, app) if err != nil { return nil, err } klog.FromContext(ctx).Info("begin to run application", "appName", req.APPName, "appNamespace", req.AppNamespace) - out, err := appRun.Run(ctx, c, respStream, appruntime.Input{Question: req.Query, Files: req.Files, NeedStream: req.ResponseMode.IsStreaming(), History: history, ConversationID: req.ConversationID}) + out, err := appRun.Run(ctx, cs.cli, respStream, appruntime.Input{Question: req.Query, Files: req.Files, NeedStream: req.ResponseMode.IsStreaming(), History: history, ConversationID: req.ConversationID}) if err != nil { return nil, err } @@ -239,7 +240,7 @@ func (cs *ChatServer) GetMessageReferences(ctx context.Context, req MessageReqBo // ListPromptStarters PromptStarter are examples for users to help them get up and running with the application quickly. We use same name with chatgpt func (cs *ChatServer) ListPromptStarters(ctx context.Context, req APPMetadata, limit int) (promptStarters []string, err error) { - app, c, err := cs.GetApp(ctx, req.APPName, req.AppNamespace) + app, err := cs.GetApp(ctx, req.APPName, req.AppNamespace) if err != nil { return nil, err } @@ -253,19 +254,19 @@ func (cs *ChatServer) ListPromptStarters(ctx context.Context, req APPMetadata, l switch baseNode.Kind() { case "llmchain": ch := appruntimechain.NewLLMChain(baseNode) - if err := ch.Init(ctx, c, nil); err != nil { + if err := ch.Init(ctx, cs.cli, nil); err != nil { klog.Infof("init llmchain err:%s, will use empty chain config", err) } chainOptions = appruntimechain.GetChainOptions(ch.Instance.Spec.CommonChainConfig) case "retrievalqachain": ch := appruntimechain.NewRetrievalQAChain(baseNode) - if err := ch.Init(ctx, c, nil); err != nil { + if err := ch.Init(ctx, cs.cli, nil); err != nil { klog.Infof("init retrievalqachain err:%s, will use empty chain config", err) } chainOptions = appruntimechain.GetChainOptions(ch.Instance.Spec.CommonChainConfig) case "apichain": ch := appruntimechain.NewAPIChain(baseNode) - if err := ch.Init(ctx, c, nil); err != nil { + if err := ch.Init(ctx, cs.cli, nil); err != nil { klog.Infof("init apichain err:%s, will use empty chain config", err) } chainOptions = appruntimechain.GetChainOptions(ch.Instance.Spec.CommonChainConfig) @@ -276,14 +277,14 @@ func (cs *ChatServer) ListPromptStarters(ctx context.Context, req APPMetadata, l switch baseNode.Kind() { case "llm": l := llm.NewLLM(baseNode) - if err := l.Init(ctx, c, nil); err != nil { + if err := l.Init(ctx, cs.cli, nil); err != nil { klog.Infof("init llm err:%s, abort", err) return nil, err } model = l.Model case "knowledgebase": k := knowledgebase.NewKnowledgebase(baseNode) - if err := k.Init(ctx, c, nil); err != nil { + if err := k.Init(ctx, cs.cli, nil); err != nil { klog.Infof("init knowledgebase err:%s, abort", err) return nil, err } @@ -295,7 +296,7 @@ func (cs *ChatServer) ListPromptStarters(ctx context.Context, req APPMetadata, l content := bytes.Buffer{} // if there is a knowledgebase, use it to generate prompt starter if kb != nil { - outArg, finish, err := retriever.GenerateKnowledgebaseRetriever(ctx, c, kb.Name, kb.Namespace, apiretriever.CommonRetrieverConfig{NumDocuments: limit * 2}, map[string]any{"question": "开始"}) + outArg, finish, err := retriever.GenerateKnowledgebaseRetriever(ctx, cs.cli, kb.Name, kb.Namespace, apiretriever.CommonRetrieverConfig{NumDocuments: limit * 2}, map[string]any{"question": "开始"}) if err != nil { return nil, err } @@ -391,26 +392,15 @@ Requires language consistent with the information, no restating of my words, que --- The question you asked is:` -func (cs *ChatServer) GetApp(ctx context.Context, appName, appNamespace string) (*v1alpha1.Application, runtimeclient.Client, error) { - token := auth.ForOIDCToken(ctx) - var c runtimeclient.Client - var err error - if !cs.isGpts { - c, err = client.GetClient(token) - } else { - c = cs.cli - } - if err != nil { - return nil, nil, fmt.Errorf("failed to get a client: %w", err) - } +func (cs *ChatServer) GetApp(ctx context.Context, appName, appNamespace string) (*v1alpha1.Application, error) { app := &v1alpha1.Application{} - if err := c.Get(ctx, types.NamespacedName{Namespace: appNamespace, Name: appName}, app); err != nil { - return nil, c, fmt.Errorf("failed to get application: %w", err) + if err := cs.cli.Get(ctx, types.NamespacedName{Namespace: appNamespace, Name: appName}, app); err != nil { + return nil, fmt.Errorf("failed to get application: %w", err) } if !app.Status.IsReady() { - return nil, c, fmt.Errorf("application not ready: %s", app.Status.GetCondition(v1alpha1.TypeReady).Message) + return nil, fmt.Errorf("application not ready: %s", app.Status.GetCondition(v1alpha1.TypeReady).Message) } - return app, c, nil + return app, nil } // todo Reuse the flow without having to rebuild req same, not finish, Flow doesn't start with/contain nodes that depend on incomingInput.question diff --git a/pkg/config/config.go b/pkg/config/config.go index 3121d4334..2a1b26f12 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -50,6 +50,17 @@ var ( ) var systemCli client.Client +func InitSystemClient(cli client.Client) { + systemCli = cli +} + +func GetSystemClient() (client.Client, error) { + if systemCli == nil { + return nil, ErrSystemCliNotFound + } + return systemCli, nil +} + func getDatasource(ctx context.Context, ref arcadiav1alpha1.TypedObjectReference, c client.Client) (ds *arcadiav1alpha1.Datasource, err error) { name := ref.Name namespace := ref.GetNamespace(utils.GetCurrentNamespace()) @@ -206,7 +217,3 @@ func GetSystemDatasourceOSS(ctx context.Context) (*datasource.OSS, error) { } return datasource.NewOSS(ctx, systemCli, endpoint) } - -func InitSystemClient(cli client.Client) { - systemCli = cli -}