Skip to content

Commit

Permalink
feat: adjust examples
Browse files Browse the repository at this point in the history
  • Loading branch information
BytePender committed Jan 26, 2025
1 parent 4c7f72f commit a953636
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func main() {

log.Printf("===== call File Loader in Chain =====")
// 创建 callback handler
handler := &callbacksHelper.LoaderCallbackHandler{
handlerHelper := &callbacksHelper.LoaderCallbackHandler{
OnStart: func(ctx context.Context, info *callbacks.RunInfo, input *document.LoaderCallbackInput) context.Context {
log.Printf("start loading docs...: %s\n", input.Source.URI)
return ctx
Expand All @@ -70,8 +70,8 @@ func main() {
}

// 使用 callback handler
helper := callbacksHelper.NewHandlerHelper().
Loader(handler).
handler := callbacksHelper.NewHandlerHelper().
Loader(handlerHelper).
Handler()

chain := compose.NewChain[document.Source, []*schema.Document]()
Expand All @@ -84,7 +84,7 @@ func main() {

outDocs, err := run.Invoke(ctx, document.Source{
URI: filePath,
}, compose.WithCallbacks(helper))
}, compose.WithCallbacks(handler))
if err != nil {
log.Fatalf("run.Invoke failed, err=%v", err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2025 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package main

import (
Expand All @@ -20,7 +36,7 @@ func main() {
transformer, err := markdown.NewHeaderSplitter(ctx, &markdown.HeaderConfig{
// 配置参数
Headers: map[string]string{
"##": "",
"##": "headerNameOfLevel2",
},
})
if err != nil {
Expand All @@ -46,7 +62,7 @@ func main() {
log.Printf("===== call Header Splitter in chain =====")

// 创建 callback handler
handler := &callbacksHelper.TransformerCallbackHandler{
handlerHelper := &callbacksHelper.TransformerCallbackHandler{
OnStart: func(ctx context.Context, info *callbacks.RunInfo, input *document.TransformerCallbackInput) context.Context {
log.Printf("input access, len: %v, content: %s\n", len(input.Input), input.Input[0].Content)
return ctx
Expand All @@ -59,8 +75,8 @@ func main() {
}

// 使用 callback handler
helper := callbacksHelper.NewHandlerHelper().
Transformer(handler).
handler := callbacksHelper.NewHandlerHelper().
Transformer(handlerHelper).
Handler()

chain := compose.NewChain[[]*schema.Document, []*schema.Document]()
Expand All @@ -72,7 +88,7 @@ func main() {
log.Fatalf("chain.Compile failed, err=%v", err)
}

outDocs, err := run.Invoke(ctx, []*schema.Document{markdownDoc}, compose.WithCallbacks(helper))
outDocs, err := run.Invoke(ctx, []*schema.Document{markdownDoc}, compose.WithCallbacks(handler))
if err != nil {
log.Fatalf("run.Invoke failed, err=%v", err)
}
Expand Down
16 changes: 15 additions & 1 deletion components/document/transformer/splitter/markdown/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,21 @@ import (
)

type HeaderConfig struct {
// Headers specify the headers to be identified and their names in document metadata. Headers can only consist of '#'.
// Headers specify the headers to be identified and their names in document metadata.
// Headers can only consist of '#'.
// e.g.
// headers = map[string]string{ "##": "headerNameOfLevel2" }
// then the document split from original documents:
// originDoc := &schema.Document{
// Content: "hell\n##Title 2\n hello world",
// }
// splitDoc := &schema.Document{
// Content: "## Title 2\n hello world",
// Metadata: map[string]any{
// // other fields
// "headerNameOfLevel2": "Title 2",
// },
// }
Headers map[string]string
// TrimHeaders specify if results contain header lines.
TrimHeaders bool
Expand Down
8 changes: 5 additions & 3 deletions components/embedding/openai/examples/embedding/embedding.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func main() {

log.Printf("===== call Embedder in Chain =====")

handler := &callbacksHelper.EmbeddingCallbackHandler{
handlerHelper := &callbacksHelper.EmbeddingCallbackHandler{
OnStart: func(ctx context.Context, runInfo *callbacks.RunInfo, input *embedding.CallbackInput) context.Context {
log.Printf("input access, len: %v, content: %s\n", len(input.Texts), input.Texts)
return ctx
Expand All @@ -71,7 +71,9 @@ func main() {
},
}

callbackHandler := callbacksHelper.NewHandlerHelper().Embedding(handler).Handler()
handler := callbacksHelper.NewHandlerHelper().
Embedding(handlerHelper).
Handler()

chain := compose.NewChain[[]string, [][]float64]()
chain.AppendEmbedding(embedder)
Expand All @@ -83,7 +85,7 @@ func main() {
}

vectors, err = runnable.Invoke(ctx, []string{"hello", "how are you"},
compose.WithCallbacks(callbackHandler))
compose.WithCallbacks(handler))
if err != nil {
panic(fmt.Errorf("runnable.Invoke failed, err=%v", err))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func main() {
log.Printf("===== call Indexer in chain =====")

// 创建 callback handler
handler := &callbacksHelper.IndexerCallbackHandler{
handlerHelper := &callbacksHelper.IndexerCallbackHandler{
OnStart: func(ctx context.Context, info *callbacks.RunInfo, input *indexer.CallbackInput) context.Context {
log.Printf("input access, len: %v, content: %s\n", len(input.Docs), input.Docs[0].Content)
return ctx
Expand All @@ -112,8 +112,8 @@ func main() {
}

// 使用 callback handler
helper := callbacksHelper.NewHandlerHelper().
Indexer(handler).
handler := callbacksHelper.NewHandlerHelper().
Indexer(handlerHelper).
Handler()

chain := compose.NewChain[[]*schema.Document, []string]()
Expand All @@ -125,7 +125,7 @@ func main() {
log.Fatalf("chain.Compile failed, err=%v", err)
}

outIDs, err := run.Invoke(ctx, docs, compose.WithCallbacks(helper))
outIDs, err := run.Invoke(ctx, docs, compose.WithCallbacks(handler))
if err != nil {
log.Fatalf("run.Invoke failed, err=%v", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func main() {
log.Printf("===== call Indexer in chain =====")

// 创建 callback handler
handler := &callbacksHelper.RetrieverCallbackHandler{
handlerHelper := &callbacksHelper.RetrieverCallbackHandler{
OnStart: func(ctx context.Context, info *callbacks.RunInfo, input *retriever.CallbackInput) context.Context {
log.Printf("input access, content: %s\n", input.Query)
return ctx
Expand All @@ -110,8 +110,8 @@ func main() {
}

// 使用 callback handler
helper := callbacksHelper.NewHandlerHelper().
Retriever(handler).
handler := callbacksHelper.NewHandlerHelper().
Retriever(handlerHelper).
Handler()

chain := compose.NewChain[string, []*schema.Document]()
Expand All @@ -123,7 +123,7 @@ func main() {
log.Fatalf("chain.Compile failed, err=%v", err)
}

outDocs, err := run.Invoke(ctx, query, compose.WithCallbacks(helper))
outDocs, err := run.Invoke(ctx, query, compose.WithCallbacks(handler))
if err != nil {
log.Fatalf("run.Invoke failed, err=%v", err)
}
Expand Down

0 comments on commit a953636

Please sign in to comment.