Skip to content

Commit

Permalink
langchain[minor]: Add imports migration script (langchain-ai#4103)
Browse files Browse the repository at this point in the history
* langchain[minor]: Add imports migration script

* abstract

* finish updating docs

* chore: lint files

* pure

* const enum

* update docs title

* error handling

* chore: lint files

* update for new entrypoints location

* chore: lint files

* cr

* chore: lint files

* add to peer deps too
  • Loading branch information
bracesproul authored Feb 7, 2024
1 parent 8c676db commit d4c5dd5
Show file tree
Hide file tree
Showing 9 changed files with 571 additions and 0 deletions.
51 changes: 51 additions & 0 deletions docs/core_docs/docs/guides/migrating.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: Migrating to 0.1
hide_table_of_contents: true
---

# Migration guide: 0.0 -> 0.1

If you're still using the pre `0.1` version of LangChain, but want to upgrade to the latest version, we've created a script that can handle almost every aspect of the migration for you.

At a high level, the changes from `0.0` to `0.1` are new packages and import path updates.
We've done our best to keep all core code functionality the same, so migrating can be as painless as possible.

In simple terms, this script will scan your TypeScript codebase for any imports from `langchain/*`, and if it finds imports which have been moved in `0.1`, it'll automatically update the import paths for you.

The new packages it checks for are:

- `@langchain/core`
- `@langchain/community`
- `@langchain/openai`
- `@langchain/cohere`
- `@langchain/pinecone`

Some of these integration packages (not `core` or `community`) do have breaking changes. If you'd like to opt out of updating to those modules, you may pass in the `skipCheck` arg with a list of modules you'd like to ignore.

For example, `@langchain/cohere` bumps to the new Cohere SDK version. If you do not wish to upgrade, it will instead update your `cohere` imports to the `@langchain/community` package which still contains the previous version of the Cohere SDK.

The example below demonstrates how to run the migration script, checking all new packages.

### Setup

Install the new packages.

import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";
import CodeBlock from "@theme/CodeBlock";
import MigrationExample from "@examples/guides/migrating.ts";

<IntegrationInstallTooltip></IntegrationInstallTooltip>

```bash npm2yarn
npm install @langchain/core @langchain/community @langchain/openai @langchain/cohere @langchain/pinecone
```

Then, install the required peer dependencies:

```bash npm2yarn
npm install ts-morph glob
```

Then, run the migration code as seen below.

<CodeBlock language="typescript">{MigrationExample}</CodeBlock>
2 changes: 2 additions & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"exa-js": "^1.0.12",
"faiss-node": "^0.5.1",
"firebase-admin": "^11.9.0",
"glob": "^10.3.10",
"graphql": "^16.6.0",
"ioredis": "^5.3.2",
"js-yaml": "^4.1.0",
Expand All @@ -73,6 +74,7 @@
"prisma": "^4.11.0",
"redis": "^4.6.13",
"sqlite3": "^5.1.4",
"ts-morph": "^21.0.1",
"typeorm": "^0.3.12",
"typesense": "^1.5.3",
"uuid": "^9.0.0",
Expand Down
8 changes: 8 additions & 0 deletions examples/src/guides/migrating.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { updateEntrypointsFrom0_0_xTo0_1_x } from "langchain/util/migrations/0_1";

updateEntrypointsFrom0_0_xTo0_1_x({
localLangChainPath: "/Users/my-profile/langchainjs",
codePath: "/Users/my-profile/langchainjs-project",
})
.then(() => console.log("done"))
.catch(console.error);
4 changes: 4 additions & 0 deletions langchain/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,10 @@ util/time.cjs
util/time.js
util/time.d.ts
util/time.d.cts
util/migrations/0_1.cjs
util/migrations/0_1.js
util/migrations/0_1.d.ts
util/migrations/0_1.d.cts
experimental/autogpt.cjs
experimental/autogpt.js
experimental/autogpt.d.ts
Expand Down
2 changes: 2 additions & 0 deletions langchain/langchain.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ export const config = {
"util/document": "util/document",
"util/math": "util/math",
"util/time": "util/time",
"util/migrations/0_1": "util/migrations/0_0-0_1-migrate-imports",
// experimental
"experimental/autogpt": "experimental/autogpt/index",
"experimental/openai_assistant": "experimental/openai_assistant/index",
Expand Down Expand Up @@ -720,6 +721,7 @@ export const config = {
"experimental/prompts/handlebars",
"experimental/tools/pyinterpreter",
"util/convex",
"util/migrations/0_1",
],
extraImportMapEntries: [
{
Expand Down
23 changes: 23 additions & 0 deletions langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,10 @@
"util/time.js",
"util/time.d.ts",
"util/time.d.cts",
"util/migrations/0_1.cjs",
"util/migrations/0_1.js",
"util/migrations/0_1.d.ts",
"util/migrations/0_1.d.cts",
"experimental/autogpt.cjs",
"experimental/autogpt.js",
"experimental/autogpt.d.ts",
Expand Down Expand Up @@ -1255,6 +1259,7 @@
"eslint-plugin-no-instanceof": "^1.0.1",
"eslint-plugin-prettier": "^4.2.1",
"fast-xml-parser": "^4.2.7",
"glob": "^10.3.10",
"google-auth-library": "^8.9.0",
"handlebars": "^4.7.8",
"html-to-text": "^9.0.5",
Expand All @@ -1281,6 +1286,7 @@
"sonix-speech-recognition": "^2.1.1",
"srt-parser-2": "^1.2.3",
"ts-jest": "^29.1.0",
"ts-morph": "^21.0.1",
"typeorm": "^0.3.12",
"typescript": "~5.1.6",
"vectordb": "^0.1.4",
Expand Down Expand Up @@ -1314,6 +1320,7 @@
"d3-dsv": "^2.0.0",
"epub2": "^3.0.1",
"fast-xml-parser": "^4.2.7",
"glob": "^10.3.10",
"google-auth-library": "^8.9.0",
"handlebars": "^4.7.8",
"html-to-text": "^9.0.5",
Expand All @@ -1333,6 +1340,7 @@
"redis": "^4.6.4",
"sonix-speech-recognition": "^2.1.1",
"srt-parser-2": "^1.2.3",
"ts-morph": "^21.0.1",
"typeorm": "^0.3.12",
"vectordb": "^0.1.4",
"weaviate-ts-client": "^1.4.0",
Expand Down Expand Up @@ -1417,6 +1425,9 @@
"fast-xml-parser": {
"optional": true
},
"glob": {
"optional": true
},
"google-auth-library": {
"optional": true
},
Expand Down Expand Up @@ -1474,6 +1485,9 @@
"srt-parser-2": {
"optional": true
},
"ts-morph": {
"optional": true
},
"typeorm": {
"optional": true
},
Expand Down Expand Up @@ -3946,6 +3960,15 @@
"import": "./util/time.js",
"require": "./util/time.cjs"
},
"./util/migrations/0_1": {
"types": {
"import": "./util/migrations/0_1.d.ts",
"require": "./util/migrations/0_1.d.cts",
"default": "./util/migrations/0_1.d.ts"
},
"import": "./util/migrations/0_1.js",
"require": "./util/migrations/0_1.cjs"
},
"./experimental/autogpt": {
"types": {
"import": "./experimental/autogpt.d.ts",
Expand Down
1 change: 1 addition & 0 deletions langchain/src/load/import_constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export const optionalImportEntrypoints: string[] = [
"langchain/graphs/neo4j_graph",
"langchain/hub",
"langchain/util/convex",
"langchain/util/migrations/0_1",
"langchain/experimental/multimodal_embeddings/googlevertexai",
"langchain/experimental/chat_models/anthropic_functions",
"langchain/experimental/llms/bittensor",
Expand Down
Loading

0 comments on commit d4c5dd5

Please sign in to comment.