diff --git a/content/en/docs/eino/ecosystem_integration/embedding/embedding_qianfan.md b/content/en/docs/eino/ecosystem_integration/embedding/embedding_qianfan.md index 0d0a0c8556..39a71341af 100644 --- a/content/en/docs/eino/ecosystem_integration/embedding/embedding_qianfan.md +++ b/content/en/docs/eino/ecosystem_integration/embedding/embedding_qianfan.md @@ -7,4 +7,107 @@ title: Embedding - Qianfan weight: 0 --- -You can use Qianfan (Baidu) platform embedding service. + +## **Basic Introduction** + +Embedding models primarily provide the functionality of converting text into vector representations, thereby facilitating users' subsequent operations based on vector representations, such as vector similarity retrieval (Retriever). Many domestic and international vendors provide related models, and Baidu's Qianfan Large Model Platform offers relevant product options. + +`qianfan Embedding` is an implementation of the Embedding interface in the [Eino library](https://github.com/cloudwego/eino), where qianfan is the **abbreviation** for Baidu Qianfan Large Model. Therefore, this module: + +```go +"github.com/cloudwego/eino-ext/components/embedding/qianfan" +``` + +provides the relevant encapsulation to facilitate users' calls to **qianfan provider's** related embedding models. + +## **Usage Method** + +1. First, import the qianfan provider's related embedding module from the eino-ext library, as follows: + +```go +"github.com/cloudwego/eino-ext/components/embedding/qianfan" +``` + +2. Then, users can configure based on their AK and SK applied for on the Baidu Qianfan official website and the Embedding model they need to call. + +3. Finally, users can quickly integrate and use the related models. + +#### Core Configuration 1: Regarding AK and SK + +Although the example script below directly reads the related variables set in the user's environment variables. + +If users want to explicitly specify the related AK and SK, they can also configure them using the following method: + +```go +os.Setenv("QIANFAN_ACCESS_KEY", "your Baidu Qianfan AK") +os.Setenv("QIANFAN_SECRET_KEY", "your Baidu Qianfan SK") +``` + +#### Core Configuration 2: Name of the Embedding Model to Call + +```go +emb, err := qianfan.NewEmbedder(ctx, &qianfan.EmbeddingConfig{ + Model: "Embedding-V1", // Specify the name of the Embedding model you're calling here +}) +``` + +### **Complete Usage Example** + +```go +package main + +import ( + "context" + "encoding/json" + "fmt" + "log" + "os" + + "github.com/cloudwego/eino-ext/components/embedding/qianfan" +) + +func main() { + ctx := context.Background() + + qcfg := qianfan.GetQianfanSingletonConfig() + qcfg.AccessKey = os.Getenv("QIANFAN_ACCESS_KEY") + qcfg.SecretKey = os.Getenv("QIANFAN_SECRET_KEY") + + emb, err := qianfan.NewEmbedder(ctx, &qianfan.EmbeddingConfig{ + Model: "Embedding-V1", + }) + if err != nil { + log.Fatalf("NewEmbedder of qianfan embedding failed, err=%v", err) + } + + v, err := emb.EmbedStrings(ctx, []string{"hello world", "bye world"}) + if err != nil { + log.Fatalf("EmbedStrings of qianfan embedding failed, err=%v", err) + } + + b, _ := json.Marshal(v) + fmt.Println(string(b)) + // The example text is expected to generate vector representations similar to the following: + // [ + // [ + // 0.08621871471405029, + // -0.0012516016140580177, + // -0.09416878968477249, + // 0.11720088124275208, + // ... + // ], + // [ + // 0.09814976155757904, + // 0.10714524984359741, + // 0.06678730994462967, + // 0.08447521179914474, + // ... + // ] + // ] +} +``` + +## **Related Documentation** + +- Embedding Provider Official Website: [Baidu Qianfan](https://cloud.baidu.com/product-s/qianfan_home) +- Provider Official API Integration Documentation: [Baidu Qianfan Official API Documentation](https://cloud.baidu.com/doc/qianfan/index.html) diff --git a/content/zh/docs/eino/ecosystem_integration/embedding/embedding_qianfan.md b/content/zh/docs/eino/ecosystem_integration/embedding/embedding_qianfan.md index 166acdce77..2ee839af45 100644 --- a/content/zh/docs/eino/ecosystem_integration/embedding/embedding_qianfan.md +++ b/content/zh/docs/eino/ecosystem_integration/embedding/embedding_qianfan.md @@ -7,4 +7,112 @@ title: Embedding - Qianfan weight: 0 --- -你可以使用千帆平台提供的 embedding 服务 (百度)。 + + + +## **基本介绍** + +Embedding模型主要提供的功能是将文本转换为向量表示,进而方便用户基于向量表示进行下一步的操作,比如向量相似度召回(Retriever),在国内外提供相关模型的厂商有很多,百度的千帆大模型平台就提供了相关产品选择。 + + +`qianfan Embedding` 是[Eino库](https://github.com/cloudwego/eino)中Embedding 接口的一个实现,其中qianfan是百度千帆大模型的**简称**,因而本模块: + +```go +"github.com/cloudwego/eino-ext/components/embedding/qianfan" +``` +提供了相关的封装,方便用户进行**qianfan供应商**的相关embedding模型调用。 + + + + +## **使用方式** + +1、首先引用eino-ext库中,qianfan供应商的相关embedding模块,具体如下: + +```go +"github.com/cloudwego/eino-ext/components/embedding/qianfan" +``` + +2、然后,用户可以基于个人在百度千帆官网申请的ak、sk和自己需要调用的Embedding模型进行相关配置。 + +3、最后,用户即可快速接入相关模型的使用 + +#### 核心配置1、关于ak和sk + +在后续的示例脚本中虽然是直接读取的用户环境变量中设置的相关变量。 +但如果用户想要显示的指定相关ak和sk,也可以使用如下方式进行配置: + +```go +os.Setenv("QIANFAN_ACCESS_KEY", "你的百度千帆ak") +os.Setenv("QIANFAN_SECRET_KEY", "你的百度千帆sk") +``` + +#### 核心配置2、调用的Embedding模型名称 + +```go +emb, err := qianfan.NewEmbedder(ctx, &qianfan.EmbeddingConfig{ + Model: "Embedding-V1", // 在此处指定你调用的Embedding模型名称 +}) +``` + + + +### **完整使用示例** + +```go +package main + +import ( + "context" + "encoding/json" + "fmt" + "log" + "os" + + "github.com/cloudwego/eino-ext/components/embedding/qianfan" +) + +func main() { + ctx := context.Background() + qcfg := qianfan.GetQianfanSingletonConfig() + qcfg.AccessKey = os.Getenv("QIANFAN_ACCESS_KEY") + qcfg.SecretKey = os.Getenv("QIANFAN_SECRET_KEY") + + emb, err := qianfan.NewEmbedder(ctx, &qianfan.EmbeddingConfig{ + Model: "Embedding-V1", + }) + if err != nil { + log.Fatalf("NewEmbedder of qianfan embedding failed, err=%v", err) + } + + v, err := emb.EmbedStrings(ctx, []string{"hello world", "bye world"}) + if err != nil { + log.Fatalf("EmbedStrings of qianfan embedding failed, err=%v", err) + } + + b, _ := json.Marshal(v) + fmt.Println(string(b)) + // 示例文字预计生成的向量表示类似如下: + // [ + // [ + // 0.08621871471405029, + // -0.0012516016140580177, + // -0.09416878968477249, + // 0.11720088124275208, + // ... + // ], + // [ + // 0.09814976155757904, + // 0.10714524984359741, + // 0.06678730994462967, + // 0.08447521179914474, + // ... + // ] + //] +} +``` + +## **相关文档** + +- Embedding供应商官网:[百度千帆(qianfan)](https://cloud.baidu.com/product-s/qianfan_home) +- 供应商官方API接入文档:[百度千帆(qianfan)官方API文档](https://cloud.baidu.com/doc/qianfan/index.html)