Skip to content

Commit

Permalink
feat: add eino examples
Browse files Browse the repository at this point in the history
  • Loading branch information
BytePender committed Jan 10, 2025
1 parent f4d7f9c commit 10ab1d5
Show file tree
Hide file tree
Showing 42 changed files with 8,259 additions and 30 deletions.
17 changes: 1 addition & 16 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,4 @@ jobs:
run: go test -race -covermode=atomic -coverprofile=coverage.out ./...

- name: Benchmark
run: go test -bench=. -benchmem -run=none ./...

compatibility-test:
strategy:
matrix:
go: [ "1.19", "1.20", "1.21", "1.22" ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}
cache: true # don't use cache for self-hosted runners
- name: Unit Test
run: go test -race -covermode=atomic ./...
run: go test -bench=. -benchmem -run=none ./...
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,39 @@
# .github
# Eino Examples

English | [中文](README.zh_CN.md)

## Overview

This repository contains examples and demonstrations for using the Eino framework. It provides practical examples to help developers better understand and utilize Eino's features.

## Repository Structure

- **components/**: Examples demonstrating the usage of various components from cloudwego/eino-ext
- Includes implementations and usage patterns for different component types
- Shows how to use and customize Eino's extension components

- **compose/**: Examples showcasing Eino's orchestration capabilities
- Demonstrates how to use Graph and Chain orchestration
- Provides patterns for combining different components
- Shows various orchestration scenarios and best practices

- **flow/**: Examples illustrating the usage of Eino's flow module
- Contains demonstrations of flow-based programming patterns
- Shows how to implement and manage data flows
- Includes examples of stream processing

- **quickstart/**: Quick start examples from the user documentation
- Basic examples to help new users get started
- Contains the same demos as shown in the official documentation

## Security

If you discover a potential security issue in this project, or think you may
have discovered a security issue, we ask that you notify Bytedance Security via
our [security center](https://security.bytedance.com/src) or [vulnerability reporting email]([email protected]).

Please do **not** create a public GitHub issue.

## License

This project is licensed under the [Apache-2.0 License](LICENSE.txt).
38 changes: 38 additions & 0 deletions README.zh_CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Eino Examples

[English](README.md) | 中文

## 概述

本仓库包含了 Eino 框架的示例和演示代码,提供了实用的示例来帮助开发者更好地理解和使用 Eino 的功能。

## 仓库结构

- **components/**: cloudwego/eino-ext 中各种组件的使用示例
- 包含不同类型组件的实现和使用方式
- 展示如何使用和自定义 Eino 的扩展组件

- **compose/**: Eino 编排能力的使用示例
- 展示如何使用 Graph 和 Chain 进行编排
- 提供不同组件组合的模式
- 展示各种编排场景和最佳实践

- **flow/**: Eino flow 模块的使用示例
- 包含基于流的编程模式演示
- 展示如何实现和管理数据流
- 包含流处理的示例

- **quickstart/**: 用户文档中的快速入门示例
- 帮助新用户快速上手的基础示例
- 包含与官方文档中相同的演示代码


## 安全

如果你在该项目中发现潜在的安全问题,或你认为可能发现了安全问题,请通过我们的[安全中心](https://security.bytedance.com/src)[漏洞报告邮箱]([email protected])通知字节跳动安全团队。

**不要**创建公开的 GitHub Issue。

## 开源许可证

本项目依据 [Apache-2.0 许可证](LICENSE.txt) 授权。
62 changes: 62 additions & 0 deletions components/prompt/chat_prompt/chat_prompt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2024 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 (
"context"

"github.com/cloudwego/eino/components/prompt"
"github.com/cloudwego/eino/schema"

"github.com/cloudwego/eino-examples/internal/logs"
)

func main() {

systemTpl := `你是情绪助手,你的任务是根据用户的输入,生成一段赞美的话,语句优美,韵律强。
用户姓名:{user_name}
用户年龄:{user_age}
用户性别:{user_gender}
用户喜好:{user_hobby}`

chatTpl := prompt.FromMessages(schema.FString,
schema.SystemMessage(systemTpl),
schema.MessagesPlaceholder("message_histories", true),
schema.UserMessage("{user_query}"),
)

msgList, err := chatTpl.Format(context.Background(), map[string]any{
"user_name": "张三",
"user_age": "18",
"user_gender": "男",
"user_hobby": "打篮球、打游戏",
"message_histories": []*schema.Message{ // => value of "messages_histories" will be rendered into chatTpl slot.
schema.UserMessage("我喜欢打羽毛球"),
schema.AssistantMessage("xxxxxxxx", nil),
},
"user_query": "请为我赋诗一首",
})
if err != nil {
logs.Errorf("Format failed, err=%v", err)
return
}

logs.Infof("Rendered Messages:")
for _, msg := range msgList {
logs.Infof("- %v", msg)
}
}
128 changes: 128 additions & 0 deletions components/retriever/multiquery/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright 2024 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 (
"context"
"os"
"strings"

"github.com/cloudwego/eino-ext/components/model/openai"
"github.com/cloudwego/eino-ext/components/retriever/volc_vikingdb"
"github.com/cloudwego/eino/components/model"
"github.com/cloudwego/eino/components/retriever"
"github.com/cloudwego/eino/flow/retriever/multiquery"

"github.com/cloudwego/eino-examples/internal/logs"
)

func main() {

openAIAPIKey := os.Getenv("OPENAI_API_KEY")
openAIBaseURL := os.Getenv("OPENAI_BASE_URL")
openAIModelName := os.Getenv("OPENAI_MODEL_NAME")

vikingDBHost := os.Getenv("VIKING_DB_HOST")
vikingDBRegion := os.Getenv("VIKING_DB_REGION")
vikingDBAK := os.Getenv("VIKING_DB_AK")
vikingDBSK := os.Getenv("VIKING_DB_SK")

ctx := context.Background()
vk, err := newVikingDBRetriever(ctx, vikingDBHost, vikingDBRegion, vikingDBAK, vikingDBSK)
if err != nil {
logs.Errorf("newVikingDBRetriever failed, err=%v", err)
return
}

llm, err := newChatModel(ctx, openAIBaseURL, openAIAPIKey, openAIModelName)
if err != nil {
logs.Errorf("newChatModel failed, err=%v", err)
return
}

// rewrite query by llm
mqr, err := multiquery.NewRetriever(ctx, &multiquery.Config{
RewriteLLM: llm,
RewriteTemplate: nil, // use default
QueryVar: "", // use default
LLMOutputParser: nil, // use default
MaxQueriesNum: 3,
OrigRetriever: vk,
FusionFunc: nil, // use default fusion, just deduplicate by doc id
})
if err != nil {
logs.Errorf("NewMultiQueryRetriever failed, err=%v", err)
return
}

resp, err := mqr.Retrieve(ctx, "tourist attraction")
if err != nil {
logs.Errorf("Multi-Query Retrieve failed, err=%v", err)
return
}

logs.Infof("Multi-Query Retrieve success, docs=%v", resp)

// rewrite query by custom method
mqr, err = multiquery.NewRetriever(ctx, &multiquery.Config{
RewriteHandler: func(ctx context.Context, query string) ([]string, error) {
return strings.Split(query, "\n"), nil
},
MaxQueriesNum: 3,
OrigRetriever: vk,
FusionFunc: nil, // use default fusion, just deduplicate by doc id
})
if err != nil {
logs.Errorf("NewMultiQueryRetriever failed, err=%v", err)
return
}

resp, err = mqr.Retrieve(ctx, "tourist attraction")
if err != nil {
logs.Errorf("Multi-Query Retrieve failed, err=%v", err)
return
}

logs.Infof("Multi-Query Retrieve success, docs=%v", resp)
}

func newChatModel(ctx context.Context, baseURL, apiKey, modelName string) (model.ChatModel, error) {

return openai.NewChatModel(ctx, &openai.ChatModelConfig{
BaseURL: baseURL,
APIKey: apiKey,
Model: modelName,
ByAzure: true,
APIVersion: "2024-06-01",
})
}

func newVikingDBRetriever(ctx context.Context, host, region, ak, sk string) (retriever.Retriever, error) {

baseTopK := 5
return volc_vikingdb.NewRetriever(ctx, &volc_vikingdb.RetrieverConfig{
Host: host,
Region: region,
AK: ak,
SK: sk,
EmbeddingConfig: volc_vikingdb.EmbeddingConfig{
UseBuiltin: true,
},
Index: "3", // index version, replace if needed
TopK: &baseTopK,
})
}
Loading

0 comments on commit 10ab1d5

Please sign in to comment.