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

Add more docs for other envs #705

Merged
merged 2 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ services:
- ./langchain:/langchain
- ./scripts:/scripts
command: bash /scripts/docker-ci-entrypoint.sh
test-exports-vite:
image: node:18
working_dir: /app
volumes:
- ./test-exports-vite:/package
- ./langchain:/langchain
- ./scripts:/scripts
command: bash /scripts/docker-ci-entrypoint.sh
success:
image: alpine:3.14
command: echo "Success"
Expand All @@ -54,3 +62,5 @@ services:
condition: service_completed_successfully
test-exports-vercel:
condition: service_completed_successfully
test-exports-vite:
condition: service_completed_successfully
56 changes: 48 additions & 8 deletions docs/docs/getting-started/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { OpenAI } from "langchain/llms/openai";

If you are using TypeScript in an ESM project we suggest updating your `tsconfig.json` to include the following:

```json
```json title="tsconfig.json"
{
"compilerOptions": {
...
Expand Down Expand Up @@ -74,7 +74,7 @@ import { OpenAI } from "langchain/llms/openai";

If you want to use LangChain in frontend `pages`, you need to add the following to your `next.config.js` to enable support for WebAssembly modules (which is required by the tokenizer library `@dqbd/tiktoken`):

```js
```js title="next.config.js"
const nextConfig = {
webpack(config) {
config.experiments = {
Expand All @@ -87,6 +87,46 @@ const nextConfig = {
};
```

### Deno / Supabase Edge Functions

LangChain can be used in Deno / Supabase Edge Functions. You can import it using the following syntax:

```typescript
import { OpenAI } from "https://esm.sh/langchain/llms/openai";
```

We recommend looking at our [Supabase Template](https://github.com/langchain-ai/langchain-template-supabase) for an example of how to use LangChain in Supabase Edge Functions.

### Browser

LangChain can be used in the browser. In our CI we test bundling LangChain with Webpack and Vite, but other bundlers should work too. You can import it using the following syntax:

```typescript
import { OpenAI } from "langchain/llms/openai";
```

#### Create React App

If you're using `create-react-app` by default it doesn't support WebAssembly modules, so the tokenizer library `@dqbd/tiktoken` will not work in the browser. You can follow the instructions [here](https://github.com/dqbd/tiktoken/tree/main/js#create-react-app) to enable support for WebAssembly modules.

#### Vite

If you're using Vite, you need to add the following to your `vite.config.js` to enable support for WebAssembly modules (which is required by the tokenizer library `@dqbd/tiktoken`):

```bash npm2yarn
npm install -D vite-plugin-wasm vite-plugin-top-level-await
```

```js title="vite.config.js"
import wasm from "vite-plugin-wasm";
import topLevelAwait from "vite-plugin-top-level-await";
import { defineConfig } from "vite";

export default defineConfig({
plugins: [wasm(), topLevelAwait()],
});
```

## Updating from <0.0.52

If you are updating from a version of LangChain prior to 0.0.52, you will need to update your imports to use the new path structure.
Expand All @@ -105,12 +145,12 @@ import { OpenAI } from "langchain/llms/openai";

This applies to all imports from the following 6 modules, which have been split into submodules for each integration. The combined modules are deprecated, do not work outside of Node.js, and will be removed in a future version.

- If you were importing from `langchain/llms`, see [LLMs](../modules/models/llms/integrations) for updated import paths.
- If you were importing from `langchain/chat_models`, see [Chat Models](../modules/models/chat/integrations) for updated import paths.
- If you were importing from `langchain/embeddings`, see [Embeddings](../modules/models/embeddings/integrations) for updated import paths.
- If you were importing from `langchain/vectorstores`, see [Vector Stores](../modules/indexes/vector_stores/integrations/) for updated import paths.
- If you were importing from `langchain/document_loaders`, see [Document Loaders](../modules/indexes/document_loaders/examples/) for updated import paths.
- If you were importing from `langchain/retrievers`, see [Retrievers](../modules/indexes/retrievers/) for updated import paths.
- If you were using `langchain/llms`, see [LLMs](../modules/models/llms/integrations) for updated import paths.
- If you were using `langchain/chat_models`, see [Chat Models](../modules/models/chat/integrations) for updated import paths.
- If you were using `langchain/embeddings`, see [Embeddings](../modules/models/embeddings/integrations) for updated import paths.
- If you were using `langchain/vectorstores`, see [Vector Stores](../modules/indexes/vector_stores/integrations/) for updated import paths.
- If you were using `langchain/document_loaders`, see [Document Loaders](../modules/indexes/document_loaders/examples/) for updated import paths.
- If you were using `langchain/retrievers`, see [Retrievers](../modules/indexes/retrievers/) for updated import paths.

Other modules are not affected by this change, and you can continue to import them from the same path.

Expand Down
6 changes: 3 additions & 3 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
"format:check": "prettier --check \"**/*.{js,jsx,ts,tsx,md,mdx}\""
},
"dependencies": {
"@docusaurus/core": "2.3.1",
"@docusaurus/preset-classic": "2.3.1",
"@docusaurus/remark-plugin-npm2yarn": "^2.3.1",
"@docusaurus/core": "2.4.0",
"@docusaurus/preset-classic": "2.4.0",
"@docusaurus/remark-plugin-npm2yarn": "^2.4.0",
"@mdx-js/react": "^1.6.22",
"@mendable/search": "^0.0.79",
"clsx": "^1.2.1",
Expand Down
2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "langchain-examples",
"name": "examples",
"version": "0.0.0",
"private": true,
"description": "Langchain examples",
Expand Down
1 change: 1 addition & 0 deletions langchain/scripts/create-entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ const testExports = [
["test-exports-cf", (p) => `export * from "langchain/${p}";`],
["test-exports-cra", (p) => `export * from "langchain/${p}";`],
["test-exports-vercel", (p) => `export * from "langchain/${p}";`],
["test-exports-vite", (p) => `export * from "langchain/${p}";`],
];

const updateJsonFile = (relativePath, updateFunction) => {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"test-exports-cjs",
"test-exports-cf",
"test-exports-cra",
"test-exports-vercel"
"test-exports-vercel",
"test-exports-vite"
],
"repository": {
"type": "git",
Expand All @@ -32,7 +33,7 @@
"test:int": "turbo run test:integration",
"test:exports:docker": "docker compose up",
"publish": "bash scripts/release-branch.sh && turbo run build lint test test:integration && yarn run test:exports:docker && yarn workspace langchain run release && echo '🔗 Open https://github.com/hwchase17/langchainjs/compare/release?expand=1 and merge the release PR'",
"example": "turbo run start --filter langchain-examples --",
"example": "turbo run start --filter examples --",
"prepare": "husky install",
"precommit": "turbo run precommit",
"docs": "yarn workspace docs start"
Expand Down
2 changes: 1 addition & 1 deletion test-exports-cf/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Learn more at https://developers.cloudflare.com/workers/
*/

// import all entrypoints
// import all entrypoints to test, do not do this in your own app
import "./entrypoints.js";

// Import a few things we'll use to test the exports
Expand Down
2 changes: 1 addition & 1 deletion test-exports-cra/src/App.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-unused-vars */

// import all entrypoints
// import all entrypoints to test, do not do this in your own app
import "./entrypoints.js";

// Import a few things we'll use to test the exports
Expand Down
2 changes: 1 addition & 1 deletion test-exports-vercel/src/pages/api/hello-edge.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction

// import all entrypoints
// import all entrypoints to test, do not do this in your own app
import "../../entrypoints.js";

// Import a few things we'll use to test the exports
Expand Down
2 changes: 1 addition & 1 deletion test-exports-vercel/src/pages/api/hello-serverless.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction

// import all entrypoints
// import all entrypoints to test, do not do this in your own app
import "../../entrypoints.js";

// Import a few things we'll use to test the exports
Expand Down
2 changes: 1 addition & 1 deletion test-exports-vercel/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// import all entrypoints
// import all entrypoints to test, do not do this in your own app
import "../entrypoints.js";

import Head from "next/head";
Expand Down
24 changes: 24 additions & 0 deletions test-exports-vite/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
13 changes: 13 additions & 0 deletions test-exports-vite/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + TS</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
21 changes: 21 additions & 0 deletions test-exports-vite/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "test-exports-vite",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
"test": "tsc"
},
"dependencies": {
"langchain": "workspace:*"
},
"devDependencies": {
"typescript": "^4.9.3",
"vite": "^4.2.0",
"vite-plugin-top-level-await": "^1.3.0",
"vite-plugin-wasm": "^3.2.2"
}
}
1 change: 1 addition & 0 deletions test-exports-vite/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions test-exports-vite/src/chain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Import a few things we'll use to test the exports
import { LLMChain } from "langchain/chains";
import { ChatOpenAI } from "langchain/chat_models/openai";
import {
ChatPromptTemplate,
HumanMessagePromptTemplate,
} from "langchain/prompts";
import { CallbackManager } from "langchain/callbacks";

export function setupChain(element: HTMLButtonElement) {
const runChain = async () => {
const llm = new ChatOpenAI({
// Don't do this in your app, it would leak your API key
openAIApiKey: import.meta.env.VITE_OPENAI_API_KEY,
streaming: true,
callbackManager: CallbackManager.fromHandlers({
handleLLMNewToken: async (token) =>
console.log("handleLLMNewToken", token),
}),
});

// Test count tokens
const n = await llm.getNumTokens("Hello");
console.log("getNumTokens", n);

// Test a chain + prompt + model
const chain = new LLMChain({
llm,
prompt: ChatPromptTemplate.fromPromptMessages([
HumanMessagePromptTemplate.fromTemplate("{input}"),
]),
});
const res = await chain.run("hello");

console.log("runChain", res);
};
element.addEventListener("click", runChain);
}
25 changes: 25 additions & 0 deletions test-exports-vite/src/entrypoints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export * from "langchain/agents";
export * from "langchain/base_language";
export * from "langchain/tools";
export * from "langchain/chains";
export * from "langchain/embeddings/base";
export * from "langchain/embeddings/fake";
export * from "langchain/embeddings/openai";
export * from "langchain/llms/base";
export * from "langchain/llms/openai";
export * from "langchain/prompts";
export * from "langchain/vectorstores/base";
export * from "langchain/vectorstores/prisma";
export * from "langchain/text_splitter";
export * from "langchain/memory";
export * from "langchain/document";
export * from "langchain/docstore";
export * from "langchain/document_loaders/base";
export * from "langchain/chat_models/base";
export * from "langchain/chat_models/openai";
export * from "langchain/chat_models/anthropic";
export * from "langchain/schema";
export * from "langchain/callbacks";
export * from "langchain/output_parsers";
export * from "langchain/retrievers/remote";
export * from "langchain/cache";
27 changes: 27 additions & 0 deletions test-exports-vite/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// import all entrypoints to test, do not do this in your own app
import "./entrypoints.js";

import "./style.css";
import typescriptLogo from "./typescript.svg";
import viteLogo from "/vite.svg";
import { setupChain } from "./chain";

document.querySelector<HTMLDivElement>("#app")!.innerHTML = `
<div>
<a href="https://vitejs.dev" target="_blank">
<img src="${viteLogo}" class="logo" alt="Vite logo" />
</a>
<a href="https://www.typescriptlang.org/" target="_blank">
<img src="${typescriptLogo}" class="logo vanilla" alt="TypeScript logo" />
</a>
<h1>Vite + TypeScript</h1>
<div class="card">
<button id="chain" type="button">click to run chain</button>
</div>
<p class="read-the-docs">
Click on the Vite and TypeScript logos to learn more
</p>
</div>
`;

setupChain(document.querySelector<HTMLButtonElement>("#chain")!);
Loading