-
This post shows how to create a simple React application that communicates with an LLM using LangChain. It includes steps for installing dependencies, building a prompt, integrating with OpenAI’s GPT-3.5-turbo, and extracting country, state, and ZIP code information.
-
-
Project Setup
-
Dependencies
-
llm.js
-
AddressInformationExtractor.jsx
-
App.jsx
-
Running the App
-
-
Project Setup
Unix-like Shellspnpm create vite my-llm-app --template reactMacpnpm create vite my-llm-app --template reactWindows Command Prompt (cmd)pnpm create vite my-llm-app --template reactWindows PowerShellpnpm create vite my-llm-app --template react -
Dependencies
Unix-like Shellspnpm add langchain @langchain/core @langchain/openaiMacpnpm add langchain @langchain/core @langchain/openaiWindows Command Prompt (cmd)pnpm add langchain @langchain/core @langchain/openaiWindows PowerShellpnpm add langchain @langchain/core @langchain/openai -
llm.js
-
llm.js
import { OpenAI } from "@langchain/openai"; import { LLMChain } from "langchain/chains"; import { PromptTemplate } from "@langchain/core/prompts"; const model = new OpenAI({ modelName: "gpt-3.5-turbo", openAIApiKey: "API_KEY" }); const myPrompt = new PromptTemplate({ template: `Extract from the following text the country, state and zipcode: ${text}`, inputVariables: ["text"] }); const myChain = new LLMChain({ llm: model, prompt: myPrompt, outputKey: "questions", verbose: true }); export const executePrompt = async (promptKeys) => { const response = await myChain.call(promptKeys); return response; }; -
AddressInformationExtractor.jsx
-
AddressInformationExtractor.jsx
import React, { useState } from "react"; import { executePrompt } from "./llm"; export const AddressInformationExtractor = () => { const [text, setText] = useState( "there is a place in USA, a really interesting place and inside something called New York, in particular 10001." ); const [addressInfo, setAddressInfo] = useState(""); const extractAddressInfo = async () => { const response = await executePrompt({ text }); setAddressInfo(response); }; return ( <div> <h1>Address Information Extractor</h1> <textarea value={text} onChange={(e) => setText(e.target.value)} placeholder="Enter text here" /> <button onClick={extractAddressInfo}>Extract Address Information</button> <p>{addressInfo.questions}</p> </div> ); }; -
App.jsx
-
App.jsx
import './App.css'; import { AddressInformationExtractor } from './components/AddressInformationExtractor'; function App() { return ( <> <AddressInformationExtractor /> </> ); } export default App; -
Running the App
-
Unix-like Shells
pnpm install \ pnpm run devMacpnpm install \ pnpm run devWindows Command Prompt (cmd)pnpm install ^ pnpm run devWindows PowerShellpnpm install ` pnpm run dev -
Once the development server is running, open your browser at the provided URL and enter a text with a country, state, and ZIP code. Click “Extract Address Information” to see the response.
In this tutorial I show how to integrate nodejs with maven using the Maven Frontend Plugin, above is the vid and below you will find some useful notes. 1. Pre-reqs Have access to an Adobe Experience Manager instance if you want to install the AEM application and test it. The same pom configs shown here can be used for different types of applications Have Maven installed, understand how it works and also understand how to use Adobe's archetype, you can watch my video about maven here: Creating an AEM application using Maven and Adobe's archetype 2. Creating the base app ...
