Skip to content

Commit

Permalink
Merge pull request #778 from nkwangleiGIT/main
Browse files Browse the repository at this point in the history
fix documentloader issue and add calculator tool
  • Loading branch information
nkwangleiGIT committed Feb 29, 2024
2 parents cda8f47 + 1a479ec commit e1f908c
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 16 deletions.
3 changes: 3 additions & 0 deletions api/app-node/agent/v1alpha1/agent_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ type AgentConfig struct {

// Options defines the options to be used by agent
type Options struct {
// Whether to show tool action in the streaming output
// +kubebuilder:default=false
ShowToolAction bool `json:"showToolAction,omitempty"`
// +kubebuilder:validation:Minimum=1
// +kubebuilder:validation:Maximum=10
// +kubebuilder:default=5
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/arcadia.kubeagi.k8s.com.cn_agents.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ spec:
maximum: 10
minimum: 1
type: integer
showToolAction:
default: false
description: Whether to show tool action in the streaming output
type: boolean
type: object
type:
default: zeroShot
Expand Down
26 changes: 26 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,32 @@ rules:
- get
- patch
- update
- apiGroups:
- arcadia.kubeagi.k8s.com.cn
resources:
- documentloaders
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- arcadia.kubeagi.k8s.com.cn
resources:
- documentloaders/finalizers
verbs:
- update
- apiGroups:
- arcadia.kubeagi.k8s.com.cn
resources:
- documentloaders/status
verbs:
- get
- patch
- update
- apiGroups:
- arcadia.kubeagi.k8s.com.cn
resources:
Expand Down
3 changes: 3 additions & 0 deletions controllers/base/application_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ type ApplicationReconciler struct {
//+kubebuilder:rbac:groups=arcadia.kubeagi.k8s.com.cn,resources=agents,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=arcadia.kubeagi.k8s.com.cn,resources=agents/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=arcadia.kubeagi.k8s.com.cn,resources=agents/finalizers,verbs=update
//+kubebuilder:rbac:groups=arcadia.kubeagi.k8s.com.cn,resources=documentloaders,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=arcadia.kubeagi.k8s.com.cn,resources=documentloaders/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=arcadia.kubeagi.k8s.com.cn,resources=documentloaders/finalizers,verbs=update

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
Expand Down
2 changes: 1 addition & 1 deletion deploy/charts/arcadia/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v2
name: arcadia
description: A Helm chart(Also a KubeBB Component) for KubeAGI Arcadia
type: application
version: 0.3.1
version: 0.3.2
appVersion: "0.2.0"

keywords:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ spec:
maximum: 10
minimum: 1
type: integer
showToolAction:
default: false
description: Whether to show tool action in the streaming output
type: boolean
type: object
type:
default: zeroShot
Expand Down
26 changes: 26 additions & 0 deletions deploy/charts/arcadia/templates/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,32 @@ rules:
- get
- patch
- update
- apiGroups:
- arcadia.kubeagi.k8s.com.cn
resources:
- documentloaders
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- arcadia.kubeagi.k8s.com.cn
resources:
- documentloaders/finalizers
verbs:
- update
- apiGroups:
- arcadia.kubeagi.k8s.com.cn
resources:
- documentloaders/status
verbs:
- get
- patch
- update
- apiGroups:
- arcadia.kubeagi.k8s.com.cn
resources:
Expand Down
8 changes: 4 additions & 4 deletions deploy/charts/arcadia/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ global:
# @param resources Resources to be used
controller:
loglevel: 3
image: kubeagi/arcadia:v0.2.0
image: kubeagi/arcadia:latest
imagePullPolicy: IfNotPresent
resources:
limits:
Expand All @@ -27,7 +27,7 @@ controller:
apiserver:
bingKey: c30e4d7f3ec24c31a489f883616844b5
loglevel: 3
image: kubeagi/arcadia:v0.2.0
image: kubeagi/arcadia:latest
enableplayground: false
port: 8081
ingress:
Expand All @@ -45,7 +45,7 @@ apiserver:
portal:
enabled: true
kubebbEnabled: true
image: kubeagi/ops-console:v0.2.0
image: kubeagi/ops-console:latest
port: 80
ingress:
path: kubeagi-portal-public
Expand Down Expand Up @@ -108,7 +108,7 @@ minio:
# Related project: https://github.com/kubeagi/arcadia/tree/main/data-processing
dataprocess:
enabled: true
image: kubeagi/data-processing:v0.2.0
image: kubeagi/data-processing:latest
port: 28888
config:
llm:
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (

agentv1alpha1 "github.com/kubeagi/arcadia/api/app-node/agent/v1alpha1"
apichain "github.com/kubeagi/arcadia/api/app-node/chain/v1alpha1"
documentloaderv1alpha1 "github.com/kubeagi/arcadia/api/app-node/documentloader/v1alpha1"
apiprompt "github.com/kubeagi/arcadia/api/app-node/prompt/v1alpha1"
apiretriever "github.com/kubeagi/arcadia/api/app-node/retriever/v1alpha1"
arcadiav1alpha1 "github.com/kubeagi/arcadia/api/base/v1alpha1"
Expand Down Expand Up @@ -73,6 +74,7 @@ func init() {
utilruntime.Must(batchv1.AddToScheme(scheme))
utilruntime.Must(agentv1alpha1.AddToScheme(scheme))
utilruntime.Must(rbacv1.AddToScheme(scheme))
utilruntime.Must(documentloaderv1alpha1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}

Expand Down
20 changes: 12 additions & 8 deletions pkg/appruntime/agent/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ func (p *Executor) Run(ctx context.Context, cli client.Client, args map[string]a
continue
}
allowedTools = append(allowedTools, tool)
case "calculator":
tool := tools.Calculator{}
allowedTools = append(allowedTools, tool)
case "scraper":
// prepare options from toolSpec
options := make([]scraper.Options, 0)
Expand Down Expand Up @@ -109,16 +112,20 @@ func (p *Executor) Run(ctx context.Context, cli client.Client, args map[string]a
}
allowedTools = append(allowedTools, tool)
default:
return nil, fmt.Errorf("no tool found with name: %s", toolSpec.Name)
// Just continue if the tool does not exist
klog.Errorln("no tool found with name: %s", toolSpec.Name)
}
}

// Initialize executor using langchaingo
executorOptions := func(o *agents.CreationOptions) {
agents.WithMaxIterations(instance.Spec.Options.MaxIterations)(o)
if needStream, ok := args["_need_stream"].(bool); ok && needStream {
streamHandler := StreamHandler{callbacks.SimpleHandler{}, args}
agents.WithCallbacksHandler(streamHandler)(o)
// Only show tool action in the streaming output if configured
if instance.Spec.Options.ShowToolAction {
if needStream, ok := args["_need_stream"].(bool); ok && needStream {
streamHandler := StreamHandler{callbacks.SimpleHandler{}, args}
agents.WithCallbacksHandler(streamHandler)(o)
}
}
}
executor, err := agents.Initialize(llm, allowedTools, agents.ZeroShotReactDescription, executorOptions)
Expand All @@ -132,9 +139,6 @@ func (p *Executor) Run(ctx context.Context, cli client.Client, args map[string]a
return args, fmt.Errorf("error when call agent: %w", err)
}
klog.FromContext(ctx).V(5).Info("use agent, blocking out:", response["output"])
if err == nil {
args["_answer"] = response["output"]
return args, nil
}
args["_answer"] = response["output"]
return args, nil
}
2 changes: 1 addition & 1 deletion pkg/appruntime/documentloader/documentloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func NewDocumentLoader(baseNode base.BaseNode) *DocumentLoader {
func (dl *DocumentLoader) Init(ctx context.Context, cli client.Client, _ map[string]any) error {
instance := &v1alpha1.DocumentLoader{}
if err := cli.Get(ctx, types.NamespacedName{Namespace: dl.RefNamespace(), Name: dl.Ref.Name}, instance); err != nil {
return fmt.Errorf("can't find the knowledgebase in cluster: %w", err)
return fmt.Errorf("can't find the documentloader in cluster: %w", err)
}
dl.Instance = instance
return nil
Expand Down
11 changes: 10 additions & 1 deletion pkg/appruntime/prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"strings"
"time"

"github.com/tmc/langchaingo/prompts"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -69,7 +70,15 @@ func (p *Prompt) Run(ctx context.Context, cli client.Client, args map[string]any
}
ps = append(ps, prompts.NewHumanMessagePromptTemplate(instance.Spec.UserMessage, []string{"question"}))
}
template := prompts.NewChatPromptTemplate(ps)
template := prompts.ChatPromptTemplate{
Messages: ps,
// Add the date function to the prompt, and it'll be called when the prompt template is rendered
PartialVariables: map[string]any{
"date": func() string {
return fmt.Sprintf("%s %s", time.Now().Format("2006-01-02"), time.Now().Weekday())
},
},
}
// todo format
p.ChatPromptTemplate = template
args["prompt"] = p
Expand Down
2 changes: 1 addition & 1 deletion pypi/data-processing/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ duckdb==0.9.2
DBUtils==3.0.3
pyyaml==6.0.1

opencc==1.1.2
opencc==0.2
opencc-python-reimplemented==0.1.7
selectolax==0.3.17
openai==1.3.7
Expand Down

0 comments on commit e1f908c

Please sign in to comment.