Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

baidu-qianfan[major]: Baidu Qianfan ChatCompletion #5879

Merged
merged 14 commits into from
Jul 3, 2024
Merged
2 changes: 1 addition & 1 deletion .github/workflows/unit-tests-integrations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
matrix:
os: [ubuntu-latest]
node-version: [18.x, 20.x]
package: [anthropic, azure-openai, cloudflare, cohere, 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]
package: [anthropic, azure-openai, cloudflare, cohere, 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, qianfan]
dl102306 marked this conversation as resolved.
Show resolved Hide resolved
# See Node.js release schedule at https://nodejs.org/en/about/releases/
# include:
# - os: windows-latest
Expand Down
39 changes: 39 additions & 0 deletions docs/core_docs/docs/integrations/chat/qianfan.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
sidebar_label: Baidu Qianfan
---

import CodeBlock from "@theme/CodeBlock";

# ChatBaiduQianfan

## Setup

Baidu Qianfan SDK(https://cloud.baidu.com/doc/WENXINWORKSHOP/s/wlmhm7vuo)

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

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

<IntegrationInstallTooltip></IntegrationInstallTooltip>

```bash npm2yarn
npm install @langchain/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_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_qianfan.ts";

<CodeBlock language="typescript">{ChatBaiduQianfanStreamExample}</CodeBlock>
1 change: 1 addition & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@langchain/openai": "workspace:*",
"@langchain/pinecone": "workspace:*",
"@langchain/qdrant": "workspace:*",
"@langchain/qianfan": "workspace:*",
"@langchain/redis": "workspace:*",
"@langchain/scripts": "~0.0.14",
"@langchain/textsplitters": "workspace:*",
Expand Down
39 changes: 39 additions & 0 deletions examples/src/models/chat/chat_qianfan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { ChatBaiduQianfan } from "@langchain/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_qianfan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { ChatBaiduQianfan } from "@langchain/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: {}
}
}
*/
74 changes: 74 additions & 0 deletions libs/langchain-qianfan/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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",
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-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-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-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-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.
42 changes: 42 additions & 0 deletions libs/langchain-qianfan/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# @langchain/qianfan

This package contains the LangChain.js integrations for Qianfan via the qianfan/sdk package.


## Installation

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

## Chat models

This package adds support for Qianfan chat model inference.

Set the necessary environment variable (or pass it in via the constructor):

```bash
export QIANFAN_AK=""
export QIANFAN_SK=""
export QIANFAN_ACCESS_KEY=""
export QIANFAN_SECRET_KEY=""
```

```typescript
import { ChatBaiduQianfan } from "@langchain/qianfan";
import { HumanMessage } from "@langchain/core/messages";

const chat = new ChatBaiduQianfan({
model: 'ERNIE-Bot-turbo'
});
const message = new HumanMessage("北京天气");

const res = await chat.invoke([message]);
```

```typescript
import { BaiduQianfanEmbeddings } from "@langchain/qianfan";

const embeddings = new BaiduQianfanEmbeddings();
const res = await embeddings.embedQuery("Introduce the city Beijing");
```
14 changes: 14 additions & 0 deletions libs/langchain-qianfan/example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 鉴权相关
## AK 与 SK 分别对应于千帆平台上 **应用** 的 API Key 与 Secret Key
## 主要用于模型相关 API 鉴权
## 获取位置:https://console.bce.baidu.com/qianfan/ais/console/applicationConsole/application
# QIANFAN_AK=""
# QIANFAN_SK=""

## Access Key 与 Secret Key 对应于 [百度智能云控制台-安全认证]
## 中的 Access Key 与 Secret Key
## 获取位置:https://console.bce.baidu.com/iam/#/iam/accesslist
## 主要用于非模型相关的、管控类的 API 鉴权
## 注意与 AK 与 SK 的使用范围进行区分,详细参见文档
# QIANFAN_ACCESS_KEY=""
# QIANFAN_SECRET_KEY=""
Loading