Skip to content

Commit

Permalink
Add langchain example
Browse files Browse the repository at this point in the history
  • Loading branch information
kvinwang committed Mar 1, 2024
1 parent 55069bc commit 1c398a8
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 3 deletions.
29 changes: 29 additions & 0 deletions sidevm-quickjs/examples/gpt-langchain-sdk/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
8 changes: 8 additions & 0 deletions sidevm-quickjs/examples/gpt-langchain-sdk/spack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
entry: {
web: __dirname + "/src/index.ts",
},
output: {
path: __dirname + "/dist",
},
};
14 changes: 14 additions & 0 deletions sidevm-quickjs/examples/gpt-langchain-sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -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());
10 changes: 10 additions & 0 deletions sidevm-quickjs/examples/gpt-langchain-sdk/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"outDir": "./dist/",
"noImplicitAny": true,
"module": "es6",
"target": "es2020",
"allowJs": true,
"moduleResolution": "node"
}
}
23 changes: 23 additions & 0 deletions sidevm-quickjs/examples/gpt-langchain-sdk/webpack.config.js
Original file line number Diff line number Diff line change
@@ -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',
};
2 changes: 1 addition & 1 deletion sidevm-quickjs/examples/gpt-openai-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions sidevm-quickjs/examples/gpt-openai-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());

0 comments on commit 1c398a8

Please sign in to comment.