Skip to content

Commit

Permalink
baidu-qianfan[minor]: Baidu Qianfan ChatCompletion upgrade, remove mo…
Browse files Browse the repository at this point in the history
…del validation (#6293)

* feat: Baidu Qianfan ChatCompletion upgrade

* feat: Baidu Qianfan ChatCompletion upgrade

* Apply suggestions from code review

* Format

* Merge branch 'langchain-ai:main' into main

---------

Co-authored-by: dailin01 <[email protected]>
Co-authored-by: Brace Sproul <[email protected]>
Co-authored-by: jacoblee93 <[email protected]>
  • Loading branch information
4 people authored Aug 7, 2024
1 parent 7696d4f commit 3ebc243
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 98 deletions.
4 changes: 3 additions & 1 deletion docs/core_docs/docs/integrations/chat/baidu_qianfan.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ import IntegrationInstallTooltip from "@mdx_components/integration_install_toolt
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`,
Available models: `ERNIE-Bot`,`ERNIE-Lite-8K`,`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`

Abandoned models: `ERNIE-Bot-turbo`

## Usage

import ChatBaiduQianfanExample from "@examples/models/chat/chat_baidu_qianfan.ts";
Expand Down
2 changes: 2 additions & 0 deletions docs/core_docs/docs/integrations/chat/baidu_wenxin.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Available models: `ERNIE-Bot`,`ERNIE-Bot-turbo`,`ERNIE-Bot-4`,`ERNIE-Speed-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`

Abandoned models: `ERNIE-Bot-turbo`

import Wenxin from "@examples/models/chat/integration_baiduwenxin.ts";

<CodeBlock language="typescript">{Wenxin}</CodeBlock>
Expand Down
2 changes: 1 addition & 1 deletion examples/src/models/chat/chat_baidu_qianfan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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",
model: "ERNIE-Lite-8K",
});

const message = new HumanMessage("北京天气");
Expand Down
2 changes: 1 addition & 1 deletion examples/src/models/chat/chat_stream_baidu_qianfan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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",
model: "ERNIE-Lite-8K",
streaming: true,
});

Expand Down
2 changes: 1 addition & 1 deletion libs/langchain-baidu-qianfan/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { ChatBaiduQianfan } from "@langchain/baidu-qianfan";
import { HumanMessage } from "@langchain/core/messages";

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

Expand Down
2 changes: 1 addition & 1 deletion libs/langchain-baidu-qianfan/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"author": "LangChain",
"license": "MIT",
"dependencies": {
"@baiducloud/qianfan": "0.1.0",
"@baiducloud/qianfan": "^0.1.6",
"@langchain/core": ">0.1.56 <0.3.0",
"@langchain/openai": "~0.1.0",
"zod": "^3.22.4",
Expand Down
12 changes: 5 additions & 7 deletions libs/langchain-baidu-qianfan/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import { getEnvironmentVariable } from "@langchain/core/utils/env";
import { convertEventStreamToIterableReadableDataStream } from "@langchain/core/utils/event_source_parse";
import { ChatCompletion } from "@baiducloud/qianfan";

import { isValidModel } from "./utils.js";

/**
* Type representing the role of a message in the Qianfan chat model.
*/
Expand Down Expand Up @@ -72,12 +70,12 @@ interface ChatCompletionResponse {
*/
declare interface BaiduQianfanChatInput {
/**
* Model name to use. Available options are: ERNIE-Bot, ERNIE-Bot-turbo, ERNIE-Bot-4
* Model name to use. Available options are: ERNIE-Bot, ERNIE-Lite-8K, ERNIE-Bot-4
* Alias for `model`
* @default "ERNIE-Bot-turbo"
*/
modelName: string;
/** Model name to use. Available options are: ERNIE-Bot, ERNIE-Bot-turbo, ERNIE-Bot-4
/** Model name to use. Available options are: ERNIE-Bot, ERNIE-Lite-8K, ERNIE-Bot-4
* @default "ERNIE-Bot-turbo"
*/
model: string;
Expand Down Expand Up @@ -244,8 +242,8 @@ export class ChatBaiduQianfan
this.modelName = fields?.model ?? fields?.modelName ?? this.model;
this.model = this.modelName;

if (!isValidModel(this.model)) {
throw new Error(`Invalid model name: ${this.model}`);
if (!this.model) {
throw new Error(`Please provide modelName`);
}

this.qianfanAK = fields?.qianfanAK ?? getEnvironmentVariable("QIANFAN_AK");
Expand Down Expand Up @@ -416,7 +414,7 @@ export class ChatBaiduQianfan
if (!stream) {
return response;
} else {
let streamResponse = {} as {
let streamResponse = { result: "" } as {
id: string;
object: string;
created: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import { ChatBaiduQianfan } from "../chat_models.js";

test("invoke", async () => {
const chat = new ChatBaiduQianfan({
model: "ERNIE-Bot-turbo",
model: "ERNIE-Lite-8K",
});
const message = new HumanMessage("北京天气");
const res = await chat.invoke([message]);
// console.log({ res });
// console.log(res.content);
expect(res.content.length).toBeGreaterThan(10);
});

test("invokeWithStream", async () => {
const chat = new ChatBaiduQianfan({
model: "ERNIE-Bot-turbo",
model: "ERNIE-Lite-8K",
streaming: true,
});
const message = new HumanMessage("等额本金和等额本息有什么区别?");
Expand Down
73 changes: 0 additions & 73 deletions libs/langchain-baidu-qianfan/src/utils.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,17 @@ const testConfigs: TestConfig[] = [
},
shouldThrow: true,
},
{ modelName: "ERNIE-Bot-turbo", config: {} },
{ modelName: "ERNIE-Lite-8K", config: {} },
{
modelName: "ERNIE-Bot-turbo",
modelName: "ERNIE-Lite-8K",
config: {
description: "in streaming mode",
streaming: true,
},
message: "您好,请讲个长笑话",
},
{
modelName: "ERNIE-Bot-turbo",
modelName: "ERNIE-Lite-8K",
config: {
description: "with system message",
},
Expand Down
86 changes: 79 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8298,9 +8298,9 @@ __metadata:
languageName: node
linkType: hard

"@baiducloud/qianfan@npm:0.1.0":
version: 0.1.0
resolution: "@baiducloud/qianfan@npm:0.1.0"
"@baiducloud/qianfan@npm:^0.1.6":
version: 0.1.6
resolution: "@baiducloud/qianfan@npm:0.1.6"
dependencies:
"@babel/preset-react": ^7.24.6
"@rollup/plugin-inject": ^5.0.5
Expand All @@ -8310,11 +8310,12 @@ __metadata:
bottleneck: ^2.19.5
crypto-js: ^4.2.0
dotenv: ^16.4.5
express: ^4.19.2
node-fetch: 2.7.0
rollup: ^3.29.4
typescript: ^5.3.3
web-streams-polyfill: ^4.0.0
checksum: 05a607846a8e4d2e86b7ad88c798eb9d8db15cbd8bdafd2606aee522c7fda2b1aa48c30b7cbca92ac17fd2228c8f3d786f6f0b420c5dc4ee05fd8dfa5a720c5e
checksum: 63e21d10f971303cb44bf4490086c5abf4603b97ef295efd399d56efaa34aaf90e00f6bbd5d07be3036318320efc71575db3d376b95468f9fef445b26e3fb21a
languageName: node
linkType: hard

Expand Down Expand Up @@ -10965,7 +10966,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@langchain/baidu-qianfan@workspace:libs/langchain-baidu-qianfan"
dependencies:
"@baiducloud/qianfan": 0.1.0
"@baiducloud/qianfan": ^0.1.6
"@jest/globals": ^29.5.0
"@langchain/core": ">0.1.56 <0.3.0"
"@langchain/openai": ~0.1.0
Expand Down Expand Up @@ -21145,6 +21146,26 @@ __metadata:
languageName: node
linkType: hard

"body-parser@npm:1.20.2":
version: 1.20.2
resolution: "body-parser@npm:1.20.2"
dependencies:
bytes: 3.1.2
content-type: ~1.0.5
debug: 2.6.9
depd: 2.0.0
destroy: 1.2.0
http-errors: 2.0.0
iconv-lite: 0.4.24
on-finished: 2.4.1
qs: 6.11.0
raw-body: 2.5.2
type-is: ~1.6.18
unpipe: 1.0.0
checksum: 14d37ec638ab5c93f6099ecaed7f28f890d222c650c69306872e00b9efa081ff6c596cd9afb9930656aae4d6c4e1c17537bea12bb73c87a217cb3cfea8896737
languageName: node
linkType: hard

"bonjour-service@npm:^1.0.11":
version: 1.1.1
resolution: "bonjour-service@npm:1.1.1"
Expand Down Expand Up @@ -22676,7 +22697,7 @@ __metadata:
languageName: node
linkType: hard

"content-type@npm:^1.0.5, content-type@npm:~1.0.4":
"content-type@npm:^1.0.5, content-type@npm:~1.0.4, content-type@npm:~1.0.5":
version: 1.0.5
resolution: "content-type@npm:1.0.5"
checksum: 566271e0a251642254cde0f845f9dd4f9856e52d988f4eb0d0dcffbb7a1f8ec98de7a5215fc628f3bce30fe2fb6fd2bc064b562d721658c59b544e2d34ea2766
Expand Down Expand Up @@ -22738,7 +22759,7 @@ __metadata:
languageName: node
linkType: hard

"cookie@npm:^0.6.0":
"cookie@npm:0.6.0, cookie@npm:^0.6.0":
version: 0.6.0
resolution: "cookie@npm:0.6.0"
checksum: f56a7d32a07db5458e79c726b77e3c2eff655c36792f2b6c58d351fb5f61531e5b1ab7f46987150136e366c65213cbe31729e02a3eaed630c3bf7334635fb410
Expand Down Expand Up @@ -26375,6 +26396,45 @@ __metadata:
languageName: node
linkType: hard

"express@npm:^4.19.2":
version: 4.19.2
resolution: "express@npm:4.19.2"
dependencies:
accepts: ~1.3.8
array-flatten: 1.1.1
body-parser: 1.20.2
content-disposition: 0.5.4
content-type: ~1.0.4
cookie: 0.6.0
cookie-signature: 1.0.6
debug: 2.6.9
depd: 2.0.0
encodeurl: ~1.0.2
escape-html: ~1.0.3
etag: ~1.8.1
finalhandler: 1.2.0
fresh: 0.5.2
http-errors: 2.0.0
merge-descriptors: 1.0.1
methods: ~1.1.2
on-finished: 2.4.1
parseurl: ~1.3.3
path-to-regexp: 0.1.7
proxy-addr: ~2.0.7
qs: 6.11.0
range-parser: ~1.2.1
safe-buffer: 5.2.1
send: 0.18.0
serve-static: 1.15.0
setprototypeof: 1.2.0
statuses: 2.0.1
type-is: ~1.6.18
utils-merge: 1.0.1
vary: ~1.1.2
checksum: 212dbd6c2c222a96a61bc927639c95970a53b06257080bb9e2838adb3bffdb966856551fdad1ab5dd654a217c35db94f987d0aa88d48fb04d306340f5f34dca5
languageName: node
linkType: hard

"ext@npm:^1.1.2":
version: 1.7.0
resolution: "ext@npm:1.7.0"
Expand Down Expand Up @@ -36226,6 +36286,18 @@ __metadata:
languageName: node
linkType: hard

"raw-body@npm:2.5.2":
version: 2.5.2
resolution: "raw-body@npm:2.5.2"
dependencies:
bytes: 3.1.2
http-errors: 2.0.0
iconv-lite: 0.4.24
unpipe: 1.0.0
checksum: ba1583c8d8a48e8fbb7a873fdbb2df66ea4ff83775421bfe21ee120140949ab048200668c47d9ae3880012f6e217052690628cf679ddfbd82c9fc9358d574676
languageName: node
linkType: hard

"rc@npm:1.2.8, rc@npm:^1.0.1, rc@npm:^1.1.6, rc@npm:^1.2.7, rc@npm:^1.2.8":
version: 1.2.8
resolution: "rc@npm:1.2.8"
Expand Down

0 comments on commit 3ebc243

Please sign in to comment.