Skip to content

Commit

Permalink
feat: remove eino utils (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
meguminnnnnnnnn authored Feb 21, 2025
1 parent dff213a commit e8ef7aa
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 18 deletions.
19 changes: 17 additions & 2 deletions components/model/ark/chatmodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/cloudwego/eino/callbacks"
fmodel "github.com/cloudwego/eino/components/model"
"github.com/cloudwego/eino/schema"
"github.com/cloudwego/eino/utils/safe"
)

var (
Expand Down Expand Up @@ -285,7 +284,7 @@ func (cm *ChatModel) Stream(ctx context.Context, in []*schema.Message, opts ...f
defer func() {
panicErr := recover()
if panicErr != nil {
_ = sw.Send(nil, safe.NewPanicErr(panicErr, debug.Stack()))
_ = sw.Send(nil, newPanicErr(panicErr, debug.Stack()))
}

sw.Close()
Expand Down Expand Up @@ -650,3 +649,19 @@ func closeArkStreamReader(r *autils.ChatCompletionStreamReader) error {
func ptrOf[T any](v T) *T {
return &v
}

type panicErr struct {
info any
stack []byte
}

func (p *panicErr) Error() string {
return fmt.Sprintf("panic error: %v, \nstack: %s", p.info, string(p.stack))
}

func newPanicErr(info any, stack []byte) error {
return &panicErr{
info: info,
stack: stack,
}
}
4 changes: 4 additions & 0 deletions components/model/ark/chatmodel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,7 @@ func TestBindTools(t *testing.T) {

})
}
func TestPanicErr(t *testing.T) {
err := newPanicErr("info", []byte("stack"))
assert.Equal(t, "panic error: info, \nstack: stack", err.Error())
}
19 changes: 17 additions & 2 deletions components/model/claude/claude.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/cloudwego/eino/callbacks"
"github.com/cloudwego/eino/components/model"
"github.com/cloudwego/eino/schema"
"github.com/cloudwego/eino/utils/safe"
)

// NewChatModel creates a new Claude chat model instance
Expand Down Expand Up @@ -165,7 +164,7 @@ func (c *ChatModel) Stream(ctx context.Context, input []*schema.Message, opts ..
panicErr := recover()

if panicErr != nil {
_ = sw.Send(nil, safe.NewPanicErr(panicErr, debug.Stack()))
_ = sw.Send(nil, newPanicErr(panicErr, debug.Stack()))
}
stream.Close()
sw.Close()
Expand Down Expand Up @@ -586,3 +585,19 @@ func isMessageEmpty(message *schema.Message) bool {
}
return false
}

type panicErr struct {
info any
stack []byte
}

func (p *panicErr) Error() string {
return fmt.Sprintf("panic error: %v, \nstack: %s", p.info, string(p.stack))
}

func newPanicErr(info any, stack []byte) error {
return &panicErr{
info: info,
stack: stack,
}
}
5 changes: 5 additions & 0 deletions components/model/claude/claude_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,8 @@ func TestConvStreamEvent(t *testing.T) {
assert.Equal(t, message.ToolCalls[0].Function.Arguments, "")
})
}

func TestPanicErr(t *testing.T) {
err := newPanicErr("info", []byte("stack"))
assert.Equal(t, "panic error: info, \nstack: stack", err.Error())
}
19 changes: 17 additions & 2 deletions components/model/deepseek/deepseek.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/cloudwego/eino/callbacks"
"github.com/cloudwego/eino/components/model"
"github.com/cloudwego/eino/schema"
"github.com/cloudwego/eino/utils/safe"
"github.com/cohesion-org/deepseek-go"
)

Expand Down Expand Up @@ -204,7 +203,7 @@ func (cm *ChatModel) Stream(ctx context.Context, in []*schema.Message, opts ...m
_ = stream.Close()

if panicErr != nil {
_ = sw.Send(nil, safe.NewPanicErr(panicErr, debug.Stack()))
_ = sw.Send(nil, newPanicErr(panicErr, debug.Stack()))
}

sw.Close()
Expand Down Expand Up @@ -546,3 +545,19 @@ func toCallbackUsage(usage *schema.TokenUsage) *model.TokenUsage {
TotalTokens: usage.TotalTokens,
}
}

type panicErr struct {
info any
stack []byte
}

func (p *panicErr) Error() string {
return fmt.Sprintf("panic error: %v, \nstack: %s", p.info, string(p.stack))
}

func newPanicErr(info any, stack []byte) error {
return &panicErr{
info: info,
stack: stack,
}
}
5 changes: 5 additions & 0 deletions components/model/deepseek/deepseek_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,8 @@ func (m *mockStream) Recv() (*deepseek.StreamChatCompletionResponse, error) {
func (m *mockStream) Close() error {
return nil
}

func TestPanicErr(t *testing.T) {
err := newPanicErr("info", []byte("stack"))
assert.Equal(t, "panic error: info, \nstack: stack", err.Error())
}
19 changes: 17 additions & 2 deletions components/model/gemini/gemini.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/cloudwego/eino/callbacks"
"github.com/cloudwego/eino/components/model"
"github.com/cloudwego/eino/schema"
"github.com/cloudwego/eino/utils/safe"
"github.com/getkin/kin-openapi/openapi3"
"github.com/google/generative-ai-go/genai"
"google.golang.org/api/iterator"
Expand Down Expand Up @@ -202,7 +201,7 @@ func (c *ChatModel) Stream(ctx context.Context, input []*schema.Message, opts ..
panicErr := recover()

if panicErr != nil {
_ = sw.Send(nil, safe.NewPanicErr(panicErr, debug.Stack()))
_ = sw.Send(nil, newPanicErr(panicErr, debug.Stack()))
}
sw.Close()
}()
Expand Down Expand Up @@ -649,3 +648,19 @@ func toGeminiRole(role schema.RoleType) string {
}
return roleUser
}

type panicErr struct {
info any
stack []byte
}

func (p *panicErr) Error() string {
return fmt.Sprintf("panic error: %v, \nstack: %s", p.info, string(p.stack))
}

func newPanicErr(info any, stack []byte) error {
return &panicErr{
info: info,
stack: stack,
}
}
5 changes: 5 additions & 0 deletions components/model/gemini/gemini_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,8 @@ func TestGemini(t *testing.T) {
assert.Equal(t, "I see a beautiful sunset image", resp.Content)
})
}

func TestPanicErr(t *testing.T) {
err := newPanicErr("info", []byte("stack"))
assert.Equal(t, "panic error: info, \nstack: stack", err.Error())
}
19 changes: 17 additions & 2 deletions components/model/ollama/chatmodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/cloudwego/eino/callbacks"
"github.com/cloudwego/eino/components/model"
"github.com/cloudwego/eino/schema"
"github.com/cloudwego/eino/utils/safe"
)

var CallbackMetricsExtraKey = "ollama_metrics"
Expand Down Expand Up @@ -145,7 +144,7 @@ func (cm *ChatModel) Stream(ctx context.Context, input []*schema.Message, opts .
panicErr := recover()

if panicErr != nil {
_ = sw.Send(nil, safe.NewPanicErr(panicErr, debug.Stack()))
_ = sw.Send(nil, newPanicErr(panicErr, debug.Stack()))
}

sw.Close()
Expand Down Expand Up @@ -418,3 +417,19 @@ func toOllamaTools(einoTools []*schema.ToolInfo) ([]api.Tool, error) {
}
return ollamaTools, nil
}

type panicErr struct {
info any
stack []byte
}

func (p *panicErr) Error() string {
return fmt.Sprintf("panic error: %v, \nstack: %s", p.info, string(p.stack))
}

func newPanicErr(info any, stack []byte) error {
return &panicErr{
info: info,
stack: stack,
}
}
5 changes: 5 additions & 0 deletions components/model/ollama/chatmodel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,8 @@ func TestBindTools(t *testing.T) {

})
}

func TestPanicErr(t *testing.T) {
err := newPanicErr("info", []byte("stack"))
assert.Equal(t, "panic error: info, \nstack: stack", err.Error())
}
19 changes: 17 additions & 2 deletions components/model/qianfan/chatmodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/cloudwego/eino/callbacks"
"github.com/cloudwego/eino/components/model"
"github.com/cloudwego/eino/schema"
"github.com/cloudwego/eino/utils/safe"
)

// GetQianfanSingletonConfig qianfan config is singleton, you should set ak+sk / bear_token before init chat model
Expand Down Expand Up @@ -152,7 +151,7 @@ func (c *ChatModel) Stream(ctx context.Context, input []*schema.Message, opts ..
go func() {
defer func() {
if pe := recover(); pe != nil {
_ = sw.Send(nil, safe.NewPanicErr(pe, debug.Stack()))
_ = sw.Send(nil, newPanicErr(pe, debug.Stack()))
}

r.Close()
Expand Down Expand Up @@ -498,3 +497,19 @@ func (c *ChatModel) GetType() string {
func (c *ChatModel) IsCallbacksEnabled() bool {
return true
}

type panicErr struct {
info any
stack []byte
}

func (p *panicErr) Error() string {
return fmt.Sprintf("panic error: %v, \nstack: %s", p.info, string(p.stack))
}

func newPanicErr(info any, stack []byte) error {
return &panicErr{
info: info,
stack: stack,
}
}
5 changes: 5 additions & 0 deletions components/model/qianfan/chatmodel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/baidubce/bce-qianfan-sdk/go/qianfan"
. "github.com/bytedance/mockey"
"github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"

fmodel "github.com/cloudwego/eino/components/model"
"github.com/cloudwego/eino/schema"
Expand Down Expand Up @@ -325,3 +326,7 @@ func TestBindTools(t *testing.T) {

})
}
func TestPanicErr(t *testing.T) {
err := newPanicErr("info", []byte("stack"))
assert.Equal(t, "panic error: info, \nstack: stack", err.Error())
}
10 changes: 6 additions & 4 deletions devops/internal/utils/generic/type_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ package generic
import (
"reflect"
"strings"

"github.com/cloudwego/eino/utils/generic"
)

func GetJsonName(field reflect.StructField) string {
Expand All @@ -44,15 +42,19 @@ func IsMapType[K, V any](t reflect.Type) bool {
if t.Kind() != reflect.Map {
return false
}
if t.Key().Kind() != generic.TypeOf[K]().Kind() {
if t.Key().Kind() != typeOf[K]().Kind() {
return false
}
if t.Elem().Kind() != generic.TypeOf[V]().Kind() {
if t.Elem().Kind() != typeOf[V]().Kind() {
return false
}
return true
}

func typeOf[T any]() reflect.Type {
return reflect.TypeOf((*T)(nil)).Elem()
}

var comfortableKind = map[reflect.Kind]bool{
reflect.String: true,
reflect.Bool: true,
Expand Down
19 changes: 17 additions & 2 deletions libs/acl/openai/chat_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/cloudwego/eino/callbacks"
"github.com/cloudwego/eino/components/model"
"github.com/cloudwego/eino/schema"
"github.com/cloudwego/eino/utils/safe"
"github.com/getkin/kin-openapi/openapi3"
)

Expand Down Expand Up @@ -519,7 +518,7 @@ func (cm *Client) Stream(ctx context.Context, in []*schema.Message,
_ = stream.Close()

if panicErr != nil {
_ = sw.Send(nil, safe.NewPanicErr(panicErr, debug.Stack()))
_ = sw.Send(nil, newPanicErr(panicErr, debug.Stack()))
}

sw.Close()
Expand Down Expand Up @@ -716,3 +715,19 @@ func (cm *Client) BindForcedTools(tools []*schema.ToolInfo) error {

return nil
}

type panicErr struct {
info any
stack []byte
}

func (p *panicErr) Error() string {
return fmt.Sprintf("panic error: %v, \nstack: %s", p.info, string(p.stack))
}

func newPanicErr(info any, stack []byte) error {
return &panicErr{
info: info,
stack: stack,
}
}
5 changes: 5 additions & 0 deletions libs/acl/openai/chat_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,8 @@ func randStr() string {
}
return string(b)
}

func TestPanicErr(t *testing.T) {
err := newPanicErr("info", []byte("stack"))
assert.Equal(t, "panic error: info, \nstack: stack", err.Error())
}

0 comments on commit e8ef7aa

Please sign in to comment.