-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
azure-dynamic-sessions[major]: Add new package #5788
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
eeab4a6
Add Azure Container Apps sessions REPL
anthonychu b33669b
Rename package
anthonychu 0c94c67
Update to API version 2024-02-02-preview
anthonychu c922c2b
Update to JSON output
anthonychu d500436
Add user agent
anthonychu 7aeeb53
Bump version to 0.1.0
anthonychu da503bd
Update (#1)
sinedied cddaff9
docs: add azure dynamic sessions documentation
sinedied daff1f4
Fix build and make it CJS compatible
sinedied bb9e010
Update error message
sinedied facd22c
Add sessions to platform docs
sinedied c1791ff
Formatting
sinedied 3fbac37
Add env var
sinedied ace8bcc
Add package to examples
sinedied 81b1a92
Fix agent example
sinedied c4bfd30
Add readme
sinedied 0abe8fa
Fix bad merge
sinedied a957a4b
Add missing package
sinedied 0127973
Fix formatting
sinedied 3cf47d9
Fix langchain/core dependency
sinedied 6d4cba3
Regenerate lockfile
sinedied fcd5451
Format, update example
jacoblee93 16e27bc
Merge branch 'main' of https://github.com/hwchase17/langchainjs into …
jacoblee93 258443f
refactor: fix comments
sinedied b68a258
Remove unneeded import
jacoblee93 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
docs/core_docs/docs/integrations/tools/azure_dynamic_sessions.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Azure Container Apps Dynamic Sessions | ||
|
||
> [Azure Container Apps dynamic sessions](https://learn.microsoft.com/azure/container-apps/sessions) provide fast access to secure sandboxed environments that are ideal for running code or applications that require strong isolation from other workloads. | ||
|
||
You can learn more about Azure Container Apps dynamic sessions and its code interpretation capabilities on [this page](https://learn.microsoft.com/azure/container-apps/sessions). If you don't have an Azure account, you can [create a free account](https://azure.microsoft.com/free/) to get started. | ||
|
||
## Setup | ||
|
||
You'll first need to install the [`@langchain/azure-dynamic-sessions`](https://www.npmjs.com/package/@langchain/azure-dynamic-sessions) package: | ||
|
||
import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx"; | ||
|
||
<IntegrationInstallTooltip></IntegrationInstallTooltip> | ||
|
||
```bash npm2yarn | ||
npm install @langchain/azure-dynamic-sessions | ||
``` | ||
|
||
You'll also need to have a code interpreter session pool instance running. You can deploy a version using [Azure CLI](https://learn.microsoft.com/cli/azure/install-azure-cli) following [this guide](https://learn.microsoft.com/azure/container-apps/sessions-code-interpreter). | ||
|
||
Once you have your instance running, you need to make sure you have properly set up the Azure Entra authentication for it. You can find the instructions on how to do that [here](https://learn.microsoft.com/azure/container-apps/sessions?tabs=azure-cli#authentication). | ||
|
||
After you've added the role for your identity, you need to retrieve the **session pool management endpoint**. You can find it in the Azure Portal, under the "Overview" section of your instance. Then you need to set the following environment variable: | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
import EnvVars from "@examples/tools/azure_dynamic_sessions/.env.example"; | ||
|
||
<CodeBlock language="text">{EnvVars}</CodeBlock> | ||
|
||
## Usage example | ||
|
||
Below is a simple example that creates a new Python code interpreter session, invoke the tool and prints the result. | ||
|
||
import Example from "@examples/tools/azure_dynamic_sessions/azure_dynamic_sessions.ts"; | ||
|
||
<CodeBlock language="typescript">{Example}</CodeBlock> | ||
|
||
Here is a complete example where we use an Azure OpenAI chat model to call the Python code interpreter session tool to execute the code and get the result: | ||
|
||
import AgentExample from "@examples/tools/azure_dynamic_sessions/azure_dynamic_sessions-agent.ts"; | ||
|
||
<CodeBlock language="typescript">{AgentExample}</CodeBlock> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
AZURE_CONTAINER_APP_SESSION_POOL_MANAGEMENT_ENDPOINT=<your_endpoint> |
41 changes: 41 additions & 0 deletions
41
examples/src/tools/azure_dynamic_sessions/azure_dynamic_sessions-agent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { ChatPromptTemplate } from "@langchain/core/prompts"; | ||
import { pull } from "langchain/hub"; | ||
import { AgentExecutor, createToolCallingAgent } from "langchain/agents"; | ||
import { SessionsPythonREPLTool } from "@langchain/azure-dynamic-sessions"; | ||
import { AzureChatOpenAI } from "@langchain/openai"; | ||
|
||
const tools = [ | ||
new SessionsPythonREPLTool({ | ||
poolManagementEndpoint: | ||
process.env.AZURE_CONTAINER_APP_SESSION_POOL_MANAGEMENT_ENDPOINT || "", | ||
}), | ||
]; | ||
|
||
// Note: you need a model deployment that supports function calling, | ||
// like `gpt-35-turbo` version `1106`. | ||
const llm = new AzureChatOpenAI({ | ||
temperature: 0, | ||
}); | ||
|
||
// Get the prompt to use - you can modify this! | ||
// If you want to see the prompt in full, you can at: | ||
// https://smith.langchain.com/hub/jacob/tool-calling-agent | ||
const prompt = await pull<ChatPromptTemplate>("jacob/tool-calling-agent"); | ||
|
||
const agent = await createToolCallingAgent({ | ||
llm, | ||
tools, | ||
prompt, | ||
}); | ||
|
||
const agentExecutor = new AgentExecutor({ | ||
agent, | ||
tools, | ||
}); | ||
|
||
const result = await agentExecutor.invoke({ | ||
input: | ||
"Create a Python program that prints the Python version and return the result.", | ||
}); | ||
|
||
console.log(result); |
16 changes: 16 additions & 0 deletions
16
examples/src/tools/azure_dynamic_sessions/azure_dynamic_sessions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { SessionsPythonREPLTool } from "@langchain/azure-dynamic-sessions"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey team, I've flagged a change in the PR for the |
||
|
||
const tool = new SessionsPythonREPLTool({ | ||
poolManagementEndpoint: | ||
process.env.AZURE_CONTAINER_APP_SESSION_POOL_MANAGEMENT_ENDPOINT || "", | ||
}); | ||
|
||
const result = await tool.invoke("print('Hello, World!')\n1+2"); | ||
|
||
console.log(result); | ||
|
||
// { | ||
// stdout: "Hello, World!\n", | ||
// stderr: "", | ||
// result: 3, | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Azure Container App Session Pool Management Endpoint | ||
AZURE_CONTAINER_APP_SESSION_POOL_MANAGEMENT_ENDPOINT=<your-endpoint> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
module.exports = { | ||
extends: [ | ||
"airbnb-base", | ||
"eslint:recommended", | ||
"prettier", | ||
"plugin:@typescript-eslint/recommended", | ||
], | ||
parserOptions: { | ||
ecmaVersion: 12, | ||
parser: "@typescript-eslint/parser", | ||
project: "./tsconfig.json", | ||
sourceType: "module", | ||
}, | ||
plugins: ["@typescript-eslint", "no-instanceof"], | ||
ignorePatterns: [ | ||
".eslintrc.cjs", | ||
"scripts", | ||
"node_modules", | ||
"dist", | ||
"dist-cjs", | ||
"*.js", | ||
"*.cjs", | ||
"*.d.ts", | ||
], | ||
rules: { | ||
"no-process-env": 2, | ||
"no-instanceof/no-instanceof": 2, | ||
"@typescript-eslint/explicit-module-boundary-types": 0, | ||
"@typescript-eslint/no-empty-function": 0, | ||
"@typescript-eslint/no-shadow": 0, | ||
"@typescript-eslint/no-empty-interface": 0, | ||
"@typescript-eslint/no-use-before-define": ["error", "nofunc"], | ||
"@typescript-eslint/no-unused-vars": ["warn", { args: "none" }], | ||
"@typescript-eslint/no-floating-promises": "error", | ||
"@typescript-eslint/no-misused-promises": "error", | ||
camelcase: 0, | ||
"class-methods-use-this": 0, | ||
"import/extensions": [2, "ignorePackages"], | ||
"import/no-extraneous-dependencies": [ | ||
"error", | ||
{ devDependencies: ["**/*.test.ts"] }, | ||
], | ||
"import/no-unresolved": 0, | ||
"import/prefer-default-export": 0, | ||
"keyword-spacing": "error", | ||
"max-classes-per-file": 0, | ||
"max-len": 0, | ||
"no-await-in-loop": 0, | ||
"no-bitwise": 0, | ||
"no-console": 0, | ||
"no-restricted-syntax": 0, | ||
"no-shadow": 0, | ||
"no-continue": 0, | ||
"no-void": 0, | ||
"no-underscore-dangle": 0, | ||
"no-use-before-define": 0, | ||
"no-useless-constructor": 0, | ||
"no-return-await": 0, | ||
"consistent-return": 0, | ||
"no-else-return": 0, | ||
"func-names": 0, | ||
"no-lonely-if": 0, | ||
"prefer-rest-params": 0, | ||
"new-cap": ["error", { properties: false, capIsNew: false }], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
index.cjs | ||
index.js | ||
index.d.ts | ||
index.d.cts | ||
node_modules | ||
dist | ||
.yarn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/prettierrc", | ||
"printWidth": 80, | ||
"tabWidth": 2, | ||
"useTabs": false, | ||
"semi": true, | ||
"singleQuote": false, | ||
"quoteProps": "as-needed", | ||
"jsxSingleQuote": false, | ||
"trailingComma": "es5", | ||
"bracketSpacing": true, | ||
"arrowParens": "always", | ||
"requirePragma": false, | ||
"insertPragma": false, | ||
"proseWrap": "preserve", | ||
"htmlWhitespaceSensitivity": "css", | ||
"vueIndentScriptAndStyle": false, | ||
"endOfLine": "lf" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"github": { | ||
"release": true, | ||
"autoGenerate": true, | ||
"tokenRef": "GITHUB_TOKEN_RELEASE" | ||
}, | ||
"npm": { | ||
"versionArgs": ["--workspaces-update=false"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License | ||
|
||
Copyright (c) 2023 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 | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# @langchain/azure-dynamic-sessions | ||
|
||
This package contains the [Azure Container Apps dynamic sessions](https://learn.microsoft.com/azure/container-apps/sessions) tool integration. | ||
|
||
Learn more about how to use this tool in the [LangChain documentation](https://js.langchain.com/docs/integrations/tools/azure_dynamic_sessions). | ||
|
||
## Installation | ||
|
||
```bash npm2yarn | ||
npm install @langchain/azure-dynamic-sessions | ||
``` | ||
|
||
This package, along with the main LangChain package, depends on [`@langchain/core`](https://npmjs.com/package/@langchain/core/). | ||
If you are using this package with other LangChain packages, you should make sure that all of the packages depend on the same instance of @langchain/core. | ||
You can do so by adding appropriate fields to your project's `package.json` like this: | ||
|
||
```json | ||
{ | ||
"name": "your-project", | ||
"version": "0.0.0", | ||
"dependencies": { | ||
"@langchain/azure-openai": "^0.0.4", | ||
"langchain": "0.0.207" | ||
}, | ||
"resolutions": { | ||
"@langchain/core": "0.1.5" | ||
}, | ||
"overrides": { | ||
"@langchain/core": "0.1.5" | ||
}, | ||
"pnpm": { | ||
"overrides": { | ||
"@langchain/core": "0.1.5" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
The field you need depends on the package manager you're using, but we recommend adding a field for the common `yarn`, `npm`, and `pnpm` to maximize compatibility. | ||
|
||
## Tool usage | ||
|
||
```typescript | ||
import { SessionsPythonREPLTool } from "@langchain/azure-dynamic-sessions"; | ||
|
||
const tool = new SessionsPythonREPLTool({ | ||
poolManagementEndpoint: | ||
process.env.AZURE_CONTAINER_APP_SESSION_POOL_MANAGEMENT_ENDPOINT || "", | ||
}); | ||
|
||
const result = await tool.invoke("print('Hello, World!')\n1+2"); | ||
|
||
console.log(result); | ||
|
||
// { | ||
// stdout: "Hello, World!\n", | ||
// stderr: "", | ||
// result: 3, | ||
// } | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
module.exports = { | ||
preset: "ts-jest/presets/default-esm", | ||
testEnvironment: "./jest.env.cjs", | ||
modulePathIgnorePatterns: ["dist/", "docs/"], | ||
moduleNameMapper: { | ||
"^(\\.{1,2}/.*)\\.js$": "$1", | ||
}, | ||
transform: { | ||
"^.+\\.tsx?$": ["@swc/jest"], | ||
}, | ||
transformIgnorePatterns: [ | ||
"/node_modules/", | ||
"\\.pnp\\.[^\\/]+$", | ||
"./scripts/jest-setup-after-env.js", | ||
], | ||
setupFiles: ["dotenv/config"], | ||
testTimeout: 20_000, | ||
passWithNoTests: true, | ||
collectCoverageFrom: ["src/**/*.ts"], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const { TestEnvironment } = require("jest-environment-node"); | ||
|
||
class AdjustedTestEnvironmentToSupportFloat32Array extends TestEnvironment { | ||
constructor(config, context) { | ||
// Make `instanceof Float32Array` return true in tests | ||
// to avoid https://github.com/xenova/transformers.js/issues/57 and https://github.com/jestjs/jest/issues/2549 | ||
super(config, context); | ||
this.global.Float32Array = Float32Array; | ||
} | ||
} | ||
|
||
module.exports = AdjustedTestEnvironmentToSupportFloat32Array; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { resolve, dirname } from "node:path"; | ||
import { fileURLToPath } from "node:url"; | ||
|
||
/** | ||
* @param {string} relativePath | ||
* @returns {string} | ||
*/ | ||
function abs(relativePath) { | ||
return resolve(dirname(fileURLToPath(import.meta.url)), relativePath); | ||
} | ||
|
||
export const config = { | ||
internals: [/node\:/, /@langchain\/core\//], | ||
entrypoints: { | ||
index: "index", | ||
}, | ||
requiresOptionalDependency: [], | ||
tsConfigPath: resolve("./tsconfig.json"), | ||
cjsSource: "./dist-cjs", | ||
cjsDestination: "./dist", | ||
abs, | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey there! 👋 I noticed that the recent code changes in
azure_dynamic_sessions-agent.ts
explicitly access an environment variable usingprocess.env
. I've flagged this for your review to ensure it aligns with our environment variable usage guidelines. Keep up the great work! 🚀