Skip to content

Commit

Permalink
Fix lint, add to universal chat model
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 committed Jan 2, 2025
1 parent 54c30d5 commit 3601a00
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 50 deletions.
5 changes: 5 additions & 0 deletions langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@
"@jest/globals": "^29.5.0",
"@langchain/anthropic": "*",
"@langchain/aws": "*",
"@langchain/cerebras": "*",
"@langchain/cohere": "*",
"@langchain/core": "workspace:*",
"@langchain/google-genai": "*",
Expand Down Expand Up @@ -460,6 +461,7 @@
"peerDependencies": {
"@langchain/anthropic": "*",
"@langchain/aws": "*",
"@langchain/cerebras": "*",
"@langchain/cohere": "*",
"@langchain/core": ">=0.2.21 <0.4.0",
"@langchain/google-genai": "*",
Expand All @@ -480,6 +482,9 @@
"@langchain/aws": {
"optional": true
},
"@langchain/cerebras": {
"optional": true
},
"@langchain/cohere": {
"optional": true
},
Expand Down
5 changes: 5 additions & 0 deletions langchain/src/chat_models/universal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ async function _initChatModelHelper(
const { ChatGroq } = await import("@langchain/groq");
return new ChatGroq({ model, ...passedParams });
}
case "cerebras": {
const { ChatCerebras } = await import("@langchain/cerebras");
return new ChatCerebras({ model, ...passedParams });
}
case "bedrock": {
const { ChatBedrockConverse } = await import("@langchain/aws");
return new ChatBedrockConverse({ model, ...passedParams });
Expand Down Expand Up @@ -598,6 +602,7 @@ export async function initChatModel<
* - mistralai (@langchain/mistralai)
* - groq (@langchain/groq)
* - ollama (@langchain/ollama)
* - cerebras (@langchain/cerebras)
* @param {string[] | "any"} [fields.configurableFields] - Which model parameters are configurable:
* - undefined: No configurable fields.
* - "any": All fields are configurable. (See Security Note in description)
Expand Down
4 changes: 4 additions & 0 deletions libs/langchain-cerebras/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
index.cjs
index.js
index.d.ts
index.d.cts
node_modules
dist
.yarn
4 changes: 2 additions & 2 deletions libs/langchain-cerebras/LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License

Copyright (c) 2023 LangChain
Copyright (c) 2025 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
Expand All @@ -18,4 +18,4 @@ 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.
THE SOFTWARE.
76 changes: 76 additions & 0 deletions libs/langchain-cerebras/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# @langchain/cerebras

This package contains the LangChain.js integrations for Cerebras via the `@cerebras/cerebras_cloud_sdk` package.

## Installation

```bash npm2yarn
npm install @langchain/cerebras @langchain/core
```

## Chat models

This package adds support for Cerebras chat model inference.

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

```bash
export CEREBRAS_API_KEY=
```

```typescript
import { ChatCerebras } from "@langchain/cerebras";
import { HumanMessage } from "@langchain/core/messages";

const model = new ChatCerebras({
apiKey: process.env.CEREBRAS_API_KEY, // Default value.
});

const message = new HumanMessage("What color is the sky?");

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

## Development

To develop the `@langchain/cerebras` package, you'll need to follow these instructions:

### Install dependencies

```bash
yarn install
```

### Build the package

```bash
yarn build
```

Or from the repo root:

```bash
yarn build --filter=@langchain/cerebras
```

### Run tests

Test files should live within a `tests/` file in the `src/` folder. Unit tests should end in `.test.ts` and integration tests should
end in `.int.test.ts`:

```bash
$ yarn test
$ yarn test:int
```

### Lint & Format

Run the linter & formatter to ensure your code is up to standard:

```bash
yarn lint && yarn format
```

### Adding new entrypoints

If you add a new file to be exported, either import & re-export from `src/index.ts`, or add it to the `entrypoints` field in the `config` variable located inside `langchain.config.js` and run `yarn build` to generate the new entrypoint.
19 changes: 14 additions & 5 deletions libs/langchain-cerebras/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "langchain-cerebras",
"name": "@langchain/cerebras",
"version": "0.0.0",
"description": "Sample integration for LangChain.js",
"type": "module",
Expand All @@ -12,9 +12,9 @@
"type": "git",
"url": "[email protected]:langchain-ai/langchainjs.git"
},
"homepage": "https://github.com/langchain-ai/langchainjs/tree/main/libs/langchain-INTEGRATION_NAME/",
"homepage": "https://github.com/langchain-ai/langchainjs/tree/main/libs/langchain-cerebras/",
"scripts": {
"build": "yarn turbo:command build:internal --filter=@langchain/INTEGRATION_NAME",
"build": "yarn turbo:command build:internal --filter=@langchain/cerebras",
"build:internal": "yarn lc_build --create-entrypoints --pre --tree-shaking",
"lint:eslint": "NODE_OPTIONS=--max-old-space-size=4096 eslint --cache --ext .ts,.js src/",
"lint:dpdm": "dpdm --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts",
Expand All @@ -33,6 +33,7 @@
"license": "MIT",
"dependencies": {
"@cerebras/cerebras_cloud_sdk": "^1.15.0",
"uuid": "^10.0.0",
"zod": "^3.22.4",
"zod-to-json-schema": "^3.22.3"
},
Expand All @@ -41,10 +42,13 @@
},
"devDependencies": {
"@jest/globals": "^29.5.0",
"@langchain/core": "workspace:*",
"@langchain/scripts": ">=0.1.0 <0.2.0",
"@langchain/standard-tests": "0.0.0",
"@swc/core": "^1.3.90",
"@swc/jest": "^0.2.29",
"@tsconfig/recommended": "^1.0.3",
"@types/uuid": "^10",
"@typescript-eslint/eslint-plugin": "^6.12.0",
"@typescript-eslint/parser": "^6.12.0",
"dotenv": "^16.3.1",
Expand All @@ -68,7 +72,11 @@
},
"exports": {
".": {
"types": "./index.d.ts",
"types": {
"import": "./index.d.ts",
"require": "./index.d.cts",
"default": "./index.d.ts"
},
"import": "./index.js",
"require": "./index.cjs"
},
Expand All @@ -78,6 +86,7 @@
"dist/",
"index.cjs",
"index.js",
"index.d.ts"
"index.d.ts",
"index.d.cts"
]
}
24 changes: 16 additions & 8 deletions libs/langchain-cerebras/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ import {
} from "@langchain/core/language_models/chat_models";
import { getEnvironmentVariable } from "@langchain/core/utils/env";
import { ChatGenerationChunk, ChatResult } from "@langchain/core/outputs";
import {
convertToCerebrasMessageParams,
formatToCerebrasToolChoice,
} from "./utils.js";
import {
Runnable,
RunnableLambda,
Expand All @@ -35,9 +31,14 @@ import {
import { convertToOpenAITool } from "@langchain/core/utils/function_calling";
import { concat } from "@langchain/core/utils/stream";
import { isZodSchema } from "@langchain/core/utils/types";
import zodToJsonSchema from "zod-to-json-schema";
import { zodToJsonSchema } from "zod-to-json-schema";
import { z } from "zod";

import {
convertToCerebrasMessageParams,
formatToCerebrasToolChoice,
} from "./utils.js";

/**
* Input to chat model class.
*/
Expand Down Expand Up @@ -238,28 +239,35 @@ export class ChatCerebras
});
for await (const chunk of stream) {
const { choices, system_fingerprint, model, id, ...rest } = chunk;
// TODO: Remove casts when underlying types are fixed
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const choice = (choices as any)[0];
// TODO: Remove cast when underlying types are fixed
const content = choice?.delta?.content ?? "";
const usage: UsageMetadata = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
input_tokens: (rest.usage as any)?.prompt_tokens,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
output_tokens: (rest.usage as any)?.completion_tokens,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
total_tokens: (rest.usage as any)?.total_tokens,
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const generationInfo: Record<string, any> = {};
if (choice.finish_reason != null) {
generationInfo.finish_reason = choice.finish_reason;
// Only include system fingerprint and related in the last chunk for now
// to avoid concatenation issues
generationInfo.id = id;
generationInfo.system_fingerprint = chunk.system_fingerprint;
generationInfo.model = chunk.model;
generationInfo.system_fingerprint = system_fingerprint;
generationInfo.model = model;
}
yield new ChatGenerationChunk({
text: content,
message: new AIMessageChunk({
content,
tool_call_chunks: choice?.delta.tool_calls?.map(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(toolCallChunk: any) => ({
id: toolCallChunk.id,
name: toolCallChunk.function?.name,
Expand Down
78 changes: 43 additions & 35 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11744,6 +11744,44 @@ __metadata:
languageName: unknown
linkType: soft

"@langchain/cerebras@*, @langchain/cerebras@workspace:libs/langchain-cerebras":
version: 0.0.0-use.local
resolution: "@langchain/cerebras@workspace:libs/langchain-cerebras"
dependencies:
"@cerebras/cerebras_cloud_sdk": ^1.15.0
"@jest/globals": ^29.5.0
"@langchain/core": "workspace:*"
"@langchain/scripts": ">=0.1.0 <0.2.0"
"@langchain/standard-tests": 0.0.0
"@swc/core": ^1.3.90
"@swc/jest": ^0.2.29
"@tsconfig/recommended": ^1.0.3
"@types/uuid": ^10
"@typescript-eslint/eslint-plugin": ^6.12.0
"@typescript-eslint/parser": ^6.12.0
dotenv: ^16.3.1
dpdm: ^3.12.0
eslint: ^8.33.0
eslint-config-airbnb-base: ^15.0.0
eslint-config-prettier: ^8.6.0
eslint-plugin-import: ^2.27.5
eslint-plugin-no-instanceof: ^1.0.1
eslint-plugin-prettier: ^4.2.1
jest: ^29.5.0
jest-environment-node: ^29.6.4
prettier: ^2.8.3
release-it: ^15.10.1
rollup: ^4.5.2
ts-jest: ^29.1.0
typescript: <5.2.0
uuid: ^10.0.0
zod: ^3.22.4
zod-to-json-schema: ^3.22.3
peerDependencies:
"@langchain/core": ">=0.3.0 <0.4.0"
languageName: unknown
linkType: soft

"@langchain/cloudflare@workspace:*, @langchain/cloudflare@workspace:libs/langchain-cloudflare":
version: 0.0.0-use.local
resolution: "@langchain/cloudflare@workspace:libs/langchain-cloudflare"
Expand Down Expand Up @@ -20134,7 +20172,7 @@ __metadata:
languageName: node
linkType: hard

"@types/uuid@npm:^10.0.0":
"@types/uuid@npm:^10, @types/uuid@npm:^10.0.0":
version: 10.0.0
resolution: "@types/uuid@npm:10.0.0"
checksum: e3958f8b0fe551c86c14431f5940c3470127293280830684154b91dc7eb3514aeb79fe3216968833cf79d4d1c67f580f054b5be2cd562bebf4f728913e73e944
Expand Down Expand Up @@ -33372,40 +33410,6 @@ __metadata:
languageName: node
linkType: hard

"langchain-cerebras@workspace:libs/langchain-cerebras":
version: 0.0.0-use.local
resolution: "langchain-cerebras@workspace:libs/langchain-cerebras"
dependencies:
"@cerebras/cerebras_cloud_sdk": ^1.15.0
"@jest/globals": ^29.5.0
"@langchain/scripts": ">=0.1.0 <0.2.0"
"@swc/core": ^1.3.90
"@swc/jest": ^0.2.29
"@tsconfig/recommended": ^1.0.3
"@typescript-eslint/eslint-plugin": ^6.12.0
"@typescript-eslint/parser": ^6.12.0
dotenv: ^16.3.1
dpdm: ^3.12.0
eslint: ^8.33.0
eslint-config-airbnb-base: ^15.0.0
eslint-config-prettier: ^8.6.0
eslint-plugin-import: ^2.27.5
eslint-plugin-no-instanceof: ^1.0.1
eslint-plugin-prettier: ^4.2.1
jest: ^29.5.0
jest-environment-node: ^29.6.4
prettier: ^2.8.3
release-it: ^15.10.1
rollup: ^4.5.2
ts-jest: ^29.1.0
typescript: <5.2.0
zod: ^3.22.4
zod-to-json-schema: ^3.22.3
peerDependencies:
"@langchain/core": ">=0.3.0 <0.4.0"
languageName: unknown
linkType: soft

"langchain@>=0.2.3 <0.3.0 || >=0.3.4 <0.4.0, langchain@workspace:*, langchain@workspace:langchain":
version: 0.0.0-use.local
resolution: "langchain@workspace:langchain"
Expand All @@ -33414,6 +33418,7 @@ __metadata:
"@jest/globals": ^29.5.0
"@langchain/anthropic": "*"
"@langchain/aws": "*"
"@langchain/cerebras": "*"
"@langchain/cohere": "*"
"@langchain/core": "workspace:*"
"@langchain/google-genai": "*"
Expand Down Expand Up @@ -33472,6 +33477,7 @@ __metadata:
peerDependencies:
"@langchain/anthropic": "*"
"@langchain/aws": "*"
"@langchain/cerebras": "*"
"@langchain/cohere": "*"
"@langchain/core": ">=0.2.21 <0.4.0"
"@langchain/google-genai": "*"
Expand All @@ -33489,6 +33495,8 @@ __metadata:
optional: true
"@langchain/aws":
optional: true
"@langchain/cerebras":
optional: true
"@langchain/cohere":
optional: true
"@langchain/google-genai":
Expand Down

0 comments on commit 3601a00

Please sign in to comment.