Ir al contenido principal

Langchain Basic LLM Javascript

  • 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 Shells
    pnpm create vite my-llm-app --template react
    Mac
    pnpm create vite my-llm-app --template react
    Windows Command Prompt (cmd)
    pnpm create vite my-llm-app --template react
    Windows PowerShell
    pnpm create vite my-llm-app --template react
  • Dependencies

    Unix-like Shells
    pnpm add langchain @langchain/core @langchain/openai
    Mac
    pnpm add langchain @langchain/core @langchain/openai
    Windows Command Prompt (cmd)
    pnpm add langchain @langchain/core @langchain/openai
    Windows PowerShell
    pnpm 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 dev
    Mac
    pnpm install \
    pnpm run dev
    Windows Command Prompt (cmd)
    pnpm install ^
    pnpm run dev
    Windows PowerShell
    pnpm 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.

Entradas populares de este blog

How to copy files from and to a running Docker container

Sometimes you want to copy files to or from a container that doesn’t have a volume previously created, in this quick tips episode, you will learn how. Above is the vid and below you will find some useful notes. 1. Pre-reqs Have Docker installed 2. Start a Docker container For this video I will be using a Jenkins image as an example, so let’s first download it by using docker pull docker pull jenkins/jenkins:lts ...

Integrating Nodejs and Maven using The Maven Frontend Plugin

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 ...

How to create an AEM component using Reactjs

In this tutorial, I will show how to use use Adobe's archetype to create an AEM application with React.js support and also how to add a new React.js component so that it can be added into a page, above is the vid and below you will find some useful notes. In the second part we will see how to configure the Sling Model for the AEM React component. 1. Pre-reqs Have access to an Adobe Experience Manager instance. You will need aem 6.4 Service Pack 2 or newer. 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....