Skip to content

Commit

Permalink
feat: Baidu Qianfan ChatCompletion upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
dailin01 committed Jul 31, 2024
1 parent 39229f9 commit b0dc620
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 106 deletions.
2 changes: 1 addition & 1 deletion docs/core_docs/docs/integrations/chat/baidu_qianfan.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ 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`

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
20 changes: 7 additions & 13 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,13 +70,13 @@ 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"
* @default "ERNIE-Lite-8K"
*/
modelName: string;
/** Model name to use. Available options are: ERNIE-Bot, ERNIE-Bot-turbo, ERNIE-Bot-4
* @default "ERNIE-Bot-turbo"
/** Model name to use. Available options are: ERNIE-Bot, ERNIE-Lite-8K, ERNIE-Bot-4
* @default "ERNIE-Lite-8K"
*/
model: string;

Expand Down Expand Up @@ -217,9 +215,9 @@ export class ChatBaiduQianfan

userId?: string;

modelName = "ERNIE-Bot-turbo";
modelName = "ERNIE-Lite-8K";

model = "ERNIE-Bot-turbo";
model = "ERNIE-Lite-8K";

temperature?: number | undefined;

Expand All @@ -244,10 +242,6 @@ 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}`);
}

this.qianfanAK = fields?.qianfanAK ?? getEnvironmentVariable("QIANFAN_AK");

this.qianfanSK = fields?.qianfanSK ?? getEnvironmentVariable("QIANFAN_SK");
Expand Down Expand Up @@ -416,7 +410,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,21 +5,21 @@ 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("等额本金和等额本息有什么区别?");
const res = await chat.invoke([message]);
// console.log({ res });
console.log({ res });
expect(res.content.length).toBeGreaterThan(10);
});
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
76 changes: 68 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8313,9 +8313,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 @@ -8325,11 +8325,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 @@ -10973,7 +10974,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 @@ -21221,6 +21222,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 @@ -22726,7 +22747,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 @@ -22788,7 +22809,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 @@ -26469,6 +26490,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 @@ -36630,7 +36690,7 @@ __metadata:
languageName: node
linkType: hard

"raw-body@npm:^2.2.0":
"raw-body@npm:2.5.2, raw-body@npm:^2.2.0":
version: 2.5.2
resolution: "raw-body@npm:2.5.2"
dependencies:
Expand Down

0 comments on commit b0dc620

Please sign in to comment.