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

Exposing Reactjs component methods to Javascript or non-reactjs applications

blog-static-generator-new If you want to integrate your javascript or non-reactjs application with a reactjs app and be able to access reactjs components and call their methods to execute actions or get information out of them, 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 node.js installed 2. Exposing React JS to Javascript or non-reactjs applications ...

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

Brighten Up Your AEM Assets: How to Configure Default Backgrounds and Transparent Modes in Adobe Dynamic Media

Introduction The Importance of a Good Background Defining Your Default Background in AEM Defining Your Default Background in Dynamic Media Classic Transparent Images with fmt=png-alpha Explicit Background Colors with ?bgc Final Thoughts Introduction Welcome to our deep dive into the wonderful world of image backgrounds! In this post, we're going to explore how to configure Dynamic Media Classic in Adobe Experience Manager (AEM) to not only define a default background image but also how to get your images to pop with ...