This lesson demonstrates how to use the LangChain library to create a simple language model application that translates English to French. Inspired by the LangChain documentation. Detailed explanation of the code is in the comments. Good for beginners to understand the basics of LangChain and how to create a simple LLM application.
-
Clone the repository:
git clone <repository-url> cd <repository-directory>
-
Install dependencies:
npm install
-
Create a
.env
file in the root directory and add your OpenAI API key:OPENAI_API_KEY=your_api_key_here
The main file is langchain_simple_llm.js
, which contains the following key components:
- Imports: The necessary modules from the LangChain library and dotenv for environment variable management.
- Environment Configuration: Loads the OpenAI API key from the
.env
file. - Model Initialization: Sets up the ChatOpenAI model with the specified model type.
- Message Setup: Defines the system and human messages for the translation task.
- Prompt Template: Creates a prompt template that formats the input for the model.
- LLM Chain: Pipes the prompt template through the model and output parser.
- Execution: An asynchronous function that invokes the LLM chain and logs the result.
- ChatOpenAI: A class that interfaces with OpenAI's chat models, allowing for conversational interactions.
- Messages: The
SystemMessage
andHumanMessage
classes are used to define the context and user input for the model. - StringOutputParser: Parses the output from the model into a string format.
- ChatPromptTemplate: A template that structures the input messages for the model.
- Piping: The use of
.pipe()
to connect different components (prompt template, model, and parser) into a single processing chain. - Asynchronous Function: The
run
function is defined as asynchronous to handle the promise returned by the model invocation.
To run the application, execute the following command in your terminal: