From 1c398a8c27d12ec545189a526c83da8e4a65eff0 Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Fri, 1 Mar 2024 01:25:39 +0000 Subject: [PATCH] Add langchain example --- .../examples/gpt-langchain-sdk/package.json | 29 +++++++++++++++++++ .../gpt-langchain-sdk/spack.config.js | 8 +++++ .../examples/gpt-langchain-sdk/src/index.ts | 14 +++++++++ .../examples/gpt-langchain-sdk/tsconfig.json | 10 +++++++ .../gpt-langchain-sdk/webpack.config.js | 23 +++++++++++++++ .../examples/gpt-openai-sdk/package.json | 2 +- .../examples/gpt-openai-sdk/src/index.ts | 4 +-- 7 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 sidevm-quickjs/examples/gpt-langchain-sdk/package.json create mode 100644 sidevm-quickjs/examples/gpt-langchain-sdk/spack.config.js create mode 100644 sidevm-quickjs/examples/gpt-langchain-sdk/src/index.ts create mode 100644 sidevm-quickjs/examples/gpt-langchain-sdk/tsconfig.json create mode 100644 sidevm-quickjs/examples/gpt-langchain-sdk/webpack.config.js diff --git a/sidevm-quickjs/examples/gpt-langchain-sdk/package.json b/sidevm-quickjs/examples/gpt-langchain-sdk/package.json new file mode 100644 index 0000000..be9f170 --- /dev/null +++ b/sidevm-quickjs/examples/gpt-langchain-sdk/package.json @@ -0,0 +1,29 @@ +{ + "name": "langchain-sdk-demo", + "version": "1.0.0", + "main": "dist/index.js", + "scripts": { + "build": "webpack", + "test": "webpack && phatjs dist/index.js" + }, + "license": "Apache-2.0", + "dependencies": { + "@langchain/openai": "^0.0.15", + "@phala/sidevm-env": "^1.0.2", + "array.prototype.flatmap": "^1.3.1" + }, + "devDependencies": { + "@swc/cli": "^0.1.62", + "@swc/core": "^1.3.51", + "@typescript-eslint/eslint-plugin": "^5.0.0", + "eslint": "^8.0.1", + "eslint-config-standard-with-typescript": "^26.0.0", + "eslint-plugin-import": "^2.25.2", + "eslint-plugin-n": "^15.0.0", + "eslint-plugin-promise": "^6.0.0", + "ts-loader": "^9.4.2", + "typescript": "*", + "webpack": "^5.75.0", + "webpack-cli": "^5.0.1" + } +} diff --git a/sidevm-quickjs/examples/gpt-langchain-sdk/spack.config.js b/sidevm-quickjs/examples/gpt-langchain-sdk/spack.config.js new file mode 100644 index 0000000..ea80ed2 --- /dev/null +++ b/sidevm-quickjs/examples/gpt-langchain-sdk/spack.config.js @@ -0,0 +1,8 @@ +module.exports = { + entry: { + web: __dirname + "/src/index.ts", + }, + output: { + path: __dirname + "/dist", + }, +}; diff --git a/sidevm-quickjs/examples/gpt-langchain-sdk/src/index.ts b/sidevm-quickjs/examples/gpt-langchain-sdk/src/index.ts new file mode 100644 index 0000000..4b8d5c0 --- /dev/null +++ b/sidevm-quickjs/examples/gpt-langchain-sdk/src/index.ts @@ -0,0 +1,14 @@ +import "@phala/sidevm-env" +import { ChatOpenAI } from "@langchain/openai"; + +const OPENAI_API_KEY = ""; + +async function main() { + const chatModel = new ChatOpenAI({ + openAIApiKey: OPENAI_API_KEY + }); + const response = await chatModel.invoke("what is LangSmith?"); + console.log("response:", JSON.stringify(response)); +} + +main().catch(console.error).finally(() => Sidevm.exit()); diff --git a/sidevm-quickjs/examples/gpt-langchain-sdk/tsconfig.json b/sidevm-quickjs/examples/gpt-langchain-sdk/tsconfig.json new file mode 100644 index 0000000..dc65970 --- /dev/null +++ b/sidevm-quickjs/examples/gpt-langchain-sdk/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "outDir": "./dist/", + "noImplicitAny": true, + "module": "es6", + "target": "es2020", + "allowJs": true, + "moduleResolution": "node" + } +} diff --git a/sidevm-quickjs/examples/gpt-langchain-sdk/webpack.config.js b/sidevm-quickjs/examples/gpt-langchain-sdk/webpack.config.js new file mode 100644 index 0000000..e0d401f --- /dev/null +++ b/sidevm-quickjs/examples/gpt-langchain-sdk/webpack.config.js @@ -0,0 +1,23 @@ +const path = require('path'); + +module.exports = { + entry: './src/index', + mode: 'production', + output: { + filename: 'index.js', + path: path.resolve(__dirname, 'dist'), + }, + module: { + rules: [ + { + test: /\.tsx?$/, + use: 'ts-loader', + exclude: /node_modules/, + }, + ], + }, + resolve: { + extensions: ['.tsx', '.ts', '.js'], + }, + target: 'web', +}; diff --git a/sidevm-quickjs/examples/gpt-openai-sdk/package.json b/sidevm-quickjs/examples/gpt-openai-sdk/package.json index 1755ffe..7683396 100644 --- a/sidevm-quickjs/examples/gpt-openai-sdk/package.json +++ b/sidevm-quickjs/examples/gpt-openai-sdk/package.json @@ -4,7 +4,7 @@ "main": "dist/index.js", "scripts": { "build": "webpack", - "test": "webpack && sidevm-quickjs dist/index.js" + "test": "webpack && phatjs dist/index.js" }, "license": "Apache-2.0", "dependencies": { diff --git a/sidevm-quickjs/examples/gpt-openai-sdk/src/index.ts b/sidevm-quickjs/examples/gpt-openai-sdk/src/index.ts index 85a6d9c..2786170 100644 --- a/sidevm-quickjs/examples/gpt-openai-sdk/src/index.ts +++ b/sidevm-quickjs/examples/gpt-openai-sdk/src/index.ts @@ -12,7 +12,7 @@ async function main() { model: "gpt-3.5-turbo", messages: [{ role: "user", content: "Tell me a story" }], }); - console.log('response:', response.data); + console.log('response:', JSON.stringify(response.data)); } -main().catch(console.error); +main().catch(console.error).finally(() => process.exit());