Skip to content

Commit

Permalink
baidu-qianfan[major]: Baidu Qianfan ChatCompletion (#5879)
Browse files Browse the repository at this point in the history
* feat: Baidu Qianfan ChatCompletion

* feat: Baidu Qianfan ChatCompletion Doc

* feat: Baidu Qianfan ChatCompletion Doc

* feat: Baidu Qianfan ChatCompletion Doc

* feat: Baidu Qianfan ChatCompletion

* feat: Baidu Qianfan ChatCompletion

* feat: Baidu Qianfan ChatCompletion

* Update docs, format, lint, rename

* Format

* Fix lint

---------

Co-authored-by: dailin01 <[email protected]>
Co-authored-by: jacoblee93 <[email protected]>
  • Loading branch information
3 people authored Jul 3, 2024
1 parent d0bc53a commit e5644bc
Show file tree
Hide file tree
Showing 33 changed files with 2,036 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/unit-tests-integrations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
needs: get-changed-files
runs-on: ubuntu-latest
env:
PACKAGES: "anthropic,azure-openai,cloudflare,cohere,core,community,exa,google-common,google-gauth,google-genai,google-vertexai,google-vertexai-web,google-webauth,groq,mistralai,mongo,nomic,openai,pinecone,qdrant,redis,textsplitters,weaviate,yandex"
PACKAGES: "anthropic,azure-openai,cloudflare,cohere,core,community,exa,google-common,google-gauth,google-genai,google-vertexai,google-vertexai-web,google-webauth,groq,mistralai,mongo,nomic,openai,pinecone,qdrant,redis,textsplitters,weaviate,yandex,baidu-qianfan"
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
Expand Down Expand Up @@ -95,4 +95,4 @@ jobs:
- name: Install dependencies
run: yarn install --immutable
- name: Test '@langchain/${{ matrix.package }}' package
run: yarn test:unit:ci --filter=@langchain/${{ matrix.package }}
run: yarn test:unit:ci --filter=@langchain/${{ matrix.package }}
37 changes: 37 additions & 0 deletions docs/core_docs/docs/integrations/chat/baidu_qianfan.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
sidebar_label: Baidu Qianfan
---

import CodeBlock from "@theme/CodeBlock";

# ChatBaiduQianfan

## Setup

You'll first need to install the [`@langchain/baidu-qianfan`](https://www.npmjs.com/package/@langchain/baidu-qianfan) package:

import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";

<IntegrationInstallTooltip></IntegrationInstallTooltip>

```bash npm2yarn
npm install @langchain/baidu-qianfan
```

Available models: `ERNIE-Bot`,`ERNIE-Bot-turbo`,`ERNIE-Bot-4`,`ERNIE-Speed-8K`,`ERNIE-Speed-128K`,`ERNIE-4.0-8K`,
`ERNIE-4.0-8K-Preview`,`ERNIE-3.5-8K`,`ERNIE-3.5-8K-Preview`,`ERNIE-Lite-8K`,`ERNIE-Tiny-8K`,`ERNIE-Character-8K`,
`ERNIE Speed-AppBuilder`

## Usage

import ChatBaiduQianfanExample from "@examples/models/chat/chat_baidu_qianfan.ts";

<CodeBlock language="typescript">{ChatBaiduQianfanExample}</CodeBlock>

## Streaming

Qianfan's API also supports streaming token responses. The example below demonstrates how to use this feature.

import ChatBaiduQianfanStreamExample from "@examples/models/chat/chat_stream_baidu_qianfan.ts";

<CodeBlock language="typescript">{ChatBaiduQianfanStreamExample}</CodeBlock>
7 changes: 7 additions & 0 deletions docs/core_docs/docs/integrations/chat/baidu_wenxin.mdx
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
---
sidebar_label: Baidu Wenxin
sidebar_class_name: hidden
---

import CodeBlock from "@theme/CodeBlock";

# ChatBaiduWenxin

:::warning
This class has been deprecated.

Use the [`@langchain/baidu-qianfan`](/docs/integrations/chat/baidu_qianfan/) package instead.
:::

LangChain.js supports Baidu's ERNIE-bot family of models. Here's an example:

import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
---
sidebar_class_name: node-only
---

# Baidu Qianfan

The `BaiduQianfanEmbeddings` class uses the Baidu Qianfan API to generate embeddings for a given text.

## Setup

Official Website: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/alj562vvu

An API key is required to use this embedding model. You can get one by registering at https://cloud.baidu.com/doc/WENXINWORKSHOP/s/alj562vvu.

Please set the acquired API key as an environment variable named BAIDU_API_KEY, and set your secret key as an environment variable named BAIDU_SECRET_KEY.

Then, you'll need to install the [`@langchain/community`](https://www.npmjs.com/package/@langchain/community) package:
Then, you'll need to install the [`@langchain/baidu-qianfan`](https://www.npmjs.com/package/@langchain/baidu-qianfan) package:

import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";

<IntegrationInstallTooltip></IntegrationInstallTooltip>

```bash npm2yarn
npm install @langchain/community
npm install @langchain/baidu-qianfan
```

## Usage
Expand Down
1 change: 1 addition & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@langchain/aws": "workspace:*",
"@langchain/azure-dynamic-sessions": "workspace:^",
"@langchain/azure-openai": "workspace:*",
"@langchain/baidu-qianfan": "workspace:*",
"@langchain/cloudflare": "workspace:*",
"@langchain/cohere": "workspace:*",
"@langchain/community": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion examples/src/embeddings/baidu_qianfan.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaiduQianfanEmbeddings } from "@langchain/community/embeddings/baidu_qianfan";
import { BaiduQianfanEmbeddings } from "@langchain/baidu-qianfan";

const embeddings = new BaiduQianfanEmbeddings();
const res = await embeddings.embedQuery(
Expand Down
37 changes: 37 additions & 0 deletions examples/src/models/chat/chat_baidu_qianfan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ChatBaiduQianfan } from "@langchain/baidu-qianfan";
import { HumanMessage } from "@langchain/core/messages";

const chat = new ChatBaiduQianfan({
qianfanAccessKey: process.env.QIANFAN_ACCESS_KEY,
qianfanSecretKey: process.env.QIANFAN_SECRET_KEY,
model: "ERNIE-Bot-turbo",
});

const message = new HumanMessage("北京天气");
const res = await chat.invoke([message]);
console.log({ res });
/**
{
res: AIMessage {
lc_serializable: true,
lc_kwargs: {
content: '北京天气**多云,气温13~24°C**,微风,空气质量良,预报无持续降水^[2]^。\n' +
'\n' +
'近期天气情况来说,白天最高气温多在30度左右,而夜晚最低气温仅有几度,早晚较凉,需要做好保暖,昼夜温差较大。由于现在正处于雨水节气,此时天气阴沉、多变,时而下起冰雹,时而下起大雨,天色昏暗。冰雹时间不会持续太长,通常都是下冰雹一小段时间后就会停止,天气就会逐渐恢复晴好^[1]^。',
tool_calls: [],
invalid_tool_calls: [],
additional_kwargs: {},
response_metadata: {}
},
lc_namespace: [ 'langchain_core', 'messages' ],
content: '北京天气**多云,气温13~24°C**,微风,空气质量良,预报无持续降水^[2]^。\n' +
'\n' +
'近期天气情况来说,白天最高气温多在30度左右,而夜晚最低气温仅有几度,早晚较凉,需要做好保暖,昼夜温差较大。由于现在正处于雨水节气,此时天气阴沉、多变,时而下起冰雹,时而下起大雨,天色昏暗。冰雹时间不会持续太长,通常都是下冰雹一小段时间后就会停止,天气就会逐渐恢复晴好^[1]^。',
name: undefined,
additional_kwargs: {},
response_metadata: { tokenUsage: [Object] },
tool_calls: [],
invalid_tool_calls: []
}
}
*/
47 changes: 47 additions & 0 deletions examples/src/models/chat/chat_stream_baidu_qianfan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { ChatBaiduQianfan } from "@langchain/baidu-qianfan";
import { HumanMessage } from "@langchain/core/messages";

const chat = new ChatBaiduQianfan({
qianfanAccessKey: process.env.QIANFAN_ACCESS_KEY,
qianfanSecretKey: process.env.QIANFAN_SECRET_KEY,
model: "ERNIE-Bot-turbo",
streaming: true,
});

const message = new HumanMessage("等额本金和等额本息有什么区别?");
const res = await chat.invoke([message]);
console.log({ res });

/**
{
res: AIMessage {
lc_serializable: true,
lc_kwargs: {
content: 'undefined等额本金和等额本息是两种常见的贷款还款方式,它们之间的主要区别在于计息方式、每月还款额和利息支出等方面。\n' +
'\n' +
'1. 计息方式:等额本金是一种按月递减的计息方式,每月偿还相同数额的本金和剩余贷款在该月产生的利息。而等额本息则是每月偿还相同金额的利息,根据贷款金额和贷款期限计算月供,本金和利息在每月还款中占的比例逐月变化。\n' +
'2. 每月还款额:由于等额本息每月偿还的利息占每月还款总额的比例逐渐减少,导致每月还款额逐渐增加,而等额本金每月偿还的本金相同,因此每月还款额逐渐减少。\n' +
'3. 利息支出:在贷款期限相同的情况下,等额本金的利息支出相对较少,因为随着本金的减少,剩余贷款产生的利息也相应减少。而等额本息的利息支出则相对较高,因为每月偿还的利息逐渐减少,导致总利息支出相对较高。\n' +
'\n' +
'总之,等额本金和等额本息在贷款期限相同的情况下,等额本金由于利息支出相对较少,更适合于资金充裕、有提前还款打算的借款人;而等额本息每月还款额固定,更适合于每月收入较高的借款人。',
tool_calls: [],
invalid_tool_calls: [],
additional_kwargs: {},
response_metadata: {}
},
lc_namespace: [ 'langchain_core', 'messages' ],
content: 'undefined等额本金和等额本息是两种常见的贷款还款方式,它们之间的主要区别在于计息方式、每月还款额和利息支出等方面。\n' +
'\n' +
'1. 计息方式:等额本金是一种按月递减的计息方式,每月偿还相同数额的本金和剩余贷款在该月产生的利息。而等额本息则是每月偿还相同金额的利息,根据贷款金额和贷款期限计算月供,本金和利息在每月还款中占的比例逐月变化。\n' +
'2. 每月还款额:由于等额本息每月偿还的利息占每月还款总额的比例逐渐减少,导致每月还款额逐渐增加,而等额本金每月偿还的本金相同,因此每月还款额逐渐减少。\n' +
'3. 利息支出:在贷款期限相同的情况下,等额本金的利息支出相对较少,因为随着本金的减少,剩余贷款产生的利息也相应减少。而等额本息的利息支出则相对较高,因为每月偿还的利息逐渐减少,导致总利息支出相对较高。\n' +
'\n' +
'总之,等额本金和等额本息在贷款期限相同的情况下,等额本金由于利息支出相对较少,更适合于资金充裕、有提前还款打算的借款人;而等额本息每月还款额固定,更适合于每月收入较高的借款人。',
name: undefined,
additional_kwargs: {},
response_metadata: { tokenUsage: [Object] },
tool_calls: [],
invalid_tool_calls: []
}
}
*/
38 changes: 38 additions & 0 deletions examples/src/models/chat/integration_qianfan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { ChatBaiduWenxin } from "@langchain/community/chat_models/baiduwenxin";
import { HumanMessage } from "@langchain/core/messages";

// Default model is ERNIE-Bot-turbo
const ernieTurbo = new ChatBaiduWenxin({
baiduApiKey: "YOUR-API-KEY", // In Node.js defaults to process.env.BAIDU_API_KEY
baiduSecretKey: "YOUR-SECRET-KEY", // In Node.js defaults to process.env.BAIDU_SECRET_KEY
});

// Use ERNIE-Bot
const ernie = new ChatBaiduWenxin({
model: "ERNIE-Bot", // Available models are shown above
temperature: 1,
baiduApiKey: "YOUR-API-KEY", // In Node.js defaults to process.env.BAIDU_API_KEY
baiduSecretKey: "YOUR-SECRET-KEY", // In Node.js defaults to process.env.BAIDU_SECRET_KEY
});

const messages = [new HumanMessage("Hello")];

let res = await ernieTurbo.invoke(messages);
/*
AIChatMessage {
text: 'Hello! How may I assist you today?',
name: undefined,
additional_kwargs: {}
}
}
*/

res = await ernie.invoke(messages);
/*
AIChatMessage {
text: 'Hello! How may I assist you today?',
name: undefined,
additional_kwargs: {}
}
}
*/
75 changes: 75 additions & 0 deletions libs/langchain-baidu-qianfan/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
module.exports = {
extends: [
"airbnb-base",
"eslint:recommended",
"prettier",
"plugin:@typescript-eslint/recommended",
],
parserOptions: {
ecmaVersion: 12,
parser: "@typescript-eslint/parser",
project: "./tsconfig.json",
sourceType: "module",
},
plugins: ["@typescript-eslint", "no-instanceof"],
ignorePatterns: [
".eslintrc.cjs",
"scripts",
"node_modules",
"dist",
"dist-cjs",
"*.js",
"*.cjs",
"*.d.ts",
],
rules: {
"no-process-env": 2,
"no-instanceof/no-instanceof": 2,
"@typescript-eslint/explicit-module-boundary-types": 0,
"@typescript-eslint/no-empty-function": 0,
"@typescript-eslint/no-shadow": 0,
"@typescript-eslint/no-empty-interface": 0,
"@typescript-eslint/no-use-before-define": ["error", "nofunc"],
"@typescript-eslint/no-unused-vars": ["warn", { args: "none" }],
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/no-misused-promises": "error",
"arrow-body-style": 0,
camelcase: 0,
"class-methods-use-this": 0,
"import/extensions": [2, "ignorePackages"],
"import/no-extraneous-dependencies": [
"error",
{ devDependencies: ["**/*.test.ts"] },
],
"import/no-unresolved": 0,
"import/prefer-default-export": 0,
"keyword-spacing": "error",
"max-classes-per-file": 0,
"max-len": 0,
"no-await-in-loop": 0,
"no-bitwise": 0,
"no-console": 0,
"no-restricted-syntax": 0,
"no-shadow": 0,
"no-continue": 0,
"no-void": 0,
"no-underscore-dangle": 0,
"no-use-before-define": 0,
"no-useless-constructor": 0,
"no-return-await": 0,
"consistent-return": 0,
"no-else-return": 0,
"func-names": 0,
"no-lonely-if": 0,
"prefer-rest-params": 0,
"new-cap": ["error", { properties: false, capIsNew: false }],
},
overrides: [
{
files: ['**/*.test.ts'],
rules: {
'@typescript-eslint/no-unused-vars': 'off'
}
}
]
};
7 changes: 7 additions & 0 deletions libs/langchain-baidu-qianfan/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
index.cjs
index.js
index.d.ts
index.d.cts
node_modules
dist
.yarn
19 changes: 19 additions & 0 deletions libs/langchain-baidu-qianfan/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"$schema": "https://json.schemastore.org/prettierrc",
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": false,
"quoteProps": "as-needed",
"jsxSingleQuote": false,
"trailingComma": "es5",
"bracketSpacing": true,
"arrowParens": "always",
"requirePragma": false,
"insertPragma": false,
"proseWrap": "preserve",
"htmlWhitespaceSensitivity": "css",
"vueIndentScriptAndStyle": false,
"endOfLine": "lf"
}
12 changes: 12 additions & 0 deletions libs/langchain-baidu-qianfan/.release-it.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"github": {
"release": true,
"autoGenerate": true,
"tokenRef": "GITHUB_TOKEN_RELEASE"
},
"npm": {
"versionArgs": [
"--workspaces-update=false"
]
}
}
21 changes: 21 additions & 0 deletions libs/langchain-baidu-qianfan/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) 2023 LangChain

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Loading

0 comments on commit e5644bc

Please sign in to comment.