Skip to content

Commit

Permalink
chore: init app runtime with chatserver's client which is also the sy…
Browse files Browse the repository at this point in the history
…stem client

Signed-off-by: bjwswang <[email protected]>
  • Loading branch information
bjwswang committed Apr 9, 2024
1 parent 532011d commit 73b2f48
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 32 deletions.
2 changes: 1 addition & 1 deletion apiserver/pkg/chat/chat_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
42 changes: 16 additions & 26 deletions apiserver/pkg/chat/chat_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion apiserver/service/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (cs *ChatService) ChatFile() gin.HandlerFunc {
return
}
req.AppNamespace = NamespaceInHeader(c)
app, _, err := cs.server.GetApp(c.Request.Context(), req.APPName, req.AppNamespace)
app, err := cs.server.GetApp(c.Request.Context(), req.APPName, req.AppNamespace)
if err != nil {
klog.FromContext(c.Request.Context()).Error(err, "conversationFileHandler: error get app")
c.JSON(http.StatusBadRequest, chat.ErrorResp{Err: err.Error()})
Expand Down
15 changes: 11 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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
}

0 comments on commit 73b2f48

Please sign in to comment.