-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
baidu-qianfan[major]: Baidu Qianfan ChatCompletion (#5879)
* 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
1 parent
d0bc53a
commit e5644bc
Showing
33 changed files
with
2,036 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 2 additions & 8 deletions
10
docs/core_docs/docs/integrations/text_embedding/baidu_qianfan.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] | ||
} | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] | ||
} | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: {} | ||
} | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
} | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Oops, something went wrong.