Skip to content

Commit a01b5d5

Browse files
committed
feat(agentic): define AgenticModel component interface
1 parent 893138b commit a01b5d5

File tree

12 files changed

+781
-4
lines changed

12 files changed

+781
-4
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright 2025 CloudWeGo Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package agentic
18+
19+
import (
20+
"github.com/cloudwego/eino/callbacks"
21+
"github.com/cloudwego/eino/schema"
22+
)
23+
24+
// TokenUsageMeta is the token usage for the model.
25+
type TokenUsageMeta struct {
26+
InputTokens int64 `json:"input_tokens"`
27+
InputTokensDetails InputTokensUsageDetails `json:"input_tokens_details"`
28+
OutputTokens int64 `json:"output_tokens"`
29+
OutputTokensDetails OutputTokensUsageDetails `json:"output_tokens_details"`
30+
TotalTokens int64 `json:"total_tokens"`
31+
}
32+
33+
type InputTokensUsageDetails struct {
34+
CachedTokens int64 `json:"cached_tokens"`
35+
}
36+
37+
type OutputTokensUsageDetails struct {
38+
ReasoningTokens int64 `json:"reasoning_tokens"`
39+
}
40+
41+
// Config is the config for the model.
42+
type Config struct {
43+
// Model is the model name.
44+
Model string
45+
// Temperature is the temperature, which controls the randomness of the model.
46+
Temperature float32
47+
// TopP is the top p, which controls the diversity of the model.
48+
TopP float32
49+
}
50+
51+
// CallbackInput is the input for the model callback.
52+
type CallbackInput struct {
53+
// Responses is the responses to be sent to the model.
54+
Responses []*schema.AgenticMessage
55+
// Tools is the tools to be used in the model.
56+
Tools []*schema.ToolInfo
57+
// Config is the config for the model.
58+
Config *Config
59+
// Extra is the extra information for the callback.
60+
Extra map[string]any
61+
}
62+
63+
// CallbackOutput is the output for the model callback.
64+
type CallbackOutput struct {
65+
// Response is the response generated by the model.
66+
Response *schema.AgenticMessage
67+
// Config is the config for the model.
68+
Config *Config
69+
// Usage is the token usage of this request.
70+
Usage *TokenUsageMeta
71+
// Extra is the extra information for the callback.
72+
Extra map[string]any
73+
}
74+
75+
// ConvCallbackInput converts the callback input to the model callback input.
76+
func ConvCallbackInput(src callbacks.CallbackInput) *CallbackInput {
77+
switch t := src.(type) {
78+
case *CallbackInput: // when callback is triggered within component implementation, the input is usually already a typed *model.CallbackInput
79+
return t
80+
case []*schema.AgenticMessage: // when callback is injected by graph node, not the component implementation itself, the input is the input of Chat Model interface, which is []*schema.AgenticMessage
81+
return &CallbackInput{
82+
Responses: t,
83+
}
84+
default:
85+
return nil
86+
}
87+
}
88+
89+
// ConvCallbackOutput converts the callback output to the model callback output.
90+
func ConvCallbackOutput(src callbacks.CallbackOutput) *CallbackOutput {
91+
switch t := src.(type) {
92+
case *CallbackOutput: // when callback is triggered within component implementation, the output is usually already a typed *model.CallbackOutput
93+
return t
94+
case *schema.AgenticMessage: // when callback is injected by graph node, not the component implementation itself, the output is the output of Chat Model interface, which is *schema.AgenticMessage
95+
return &CallbackOutput{
96+
Response: t,
97+
}
98+
default:
99+
return nil
100+
}
101+
}

components/agentic/interface.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2025 CloudWeGo Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package agentic
18+
19+
import (
20+
"context"
21+
22+
"github.com/cloudwego/eino/schema"
23+
)
24+
25+
type AgenticModel interface {
26+
Generate(ctx context.Context, input []*schema.AgenticMessage, opts ...Option) (*schema.AgenticMessage, error)
27+
Stream(ctx context.Context, input []*schema.AgenticMessage, opts ...Option) (*schema.StreamReader[*schema.AgenticMessage], error)
28+
WithTools(tools []*schema.ToolInfo) error
29+
}

components/agentic/option.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2025 CloudWeGo Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package agentic
18+
19+
// Options is the common options for the model.
20+
type Options struct {
21+
}
22+
23+
// Option is the call option for ChatModel component.
24+
type Option struct {
25+
apply func(opts *Options)
26+
27+
implSpecificOptFn any
28+
}
29+
30+
// WrapImplSpecificOptFn is the option to wrap the implementation specific option function.
31+
func WrapImplSpecificOptFn[T any](optFn func(*T)) Option {
32+
return Option{
33+
implSpecificOptFn: optFn,
34+
}
35+
}
36+
37+
// GetCommonOptions extract model Options from Option list, optionally providing a base Options with default values.
38+
func GetCommonOptions(base *Options, opts ...Option) *Options {
39+
if base == nil {
40+
base = &Options{}
41+
}
42+
43+
for i := range opts {
44+
opt := opts[i]
45+
if opt.apply != nil {
46+
opt.apply(base)
47+
}
48+
}
49+
50+
return base
51+
}
52+
53+
// GetImplSpecificOptions extract the implementation specific options from Option list, optionally providing a base options with default values.
54+
// e.g.
55+
//
56+
// myOption := &MyOption{
57+
// Field1: "default_value",
58+
// }
59+
//
60+
// myOption := model.GetImplSpecificOptions(myOption, opts...)
61+
func GetImplSpecificOptions[T any](base *T, opts ...Option) *T {
62+
if base == nil {
63+
base = new(T)
64+
}
65+
66+
for i := range opts {
67+
opt := opts[i]
68+
if opt.implSpecificOptFn != nil {
69+
optFn, ok := opt.implSpecificOptFn.(func(*T))
70+
if ok {
71+
optFn(base)
72+
}
73+
}
74+
}
75+
76+
return base
77+
}

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ require (
3939
github.com/yargevad/filepathx v1.0.0 // indirect
4040
golang.org/x/arch v0.11.0 // indirect
4141
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 // indirect
42-
golang.org/x/sys v0.26.0 // indirect
42+
golang.org/x/sys v0.29.0 // indirect
43+
golang.org/x/term v0.28.0 // indirect
4344
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
4445
gopkg.in/yaml.v3 v3.0.1 // indirect
4546
)

go.sum

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,10 @@ golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJ
115115
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
116116
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
117117
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
118-
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
119-
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
120-
golang.org/x/term v0.10.0 h1:3R7pNqamzBraeqj/Tj8qt1aQ2HpmlC+Cx/qL/7hn4/c=
118+
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
119+
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
120+
golang.org/x/term v0.28.0 h1:/Ts8HFuMR2E6IP/jlo7QVLZHggjKQbhu/7H0LJFr3Gg=
121+
golang.org/x/term v0.28.0/go.mod h1:Sw/lC2IAUZ92udQNf3WodGtn4k/XoLyZoh8v/8uiwek=
121122
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
122123
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
123124
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=

0 commit comments

Comments
 (0)