LangChain Language Correctness Detector
Learn how to detect grammatical errors, sentiment, and aggressiveness in text using Langchain and OpenAI or Google Cloud language models.
Join the DZone community and get the full member experience.
Join For FreeThis project implements a simple LangChain language correctness detector that detects grammatical errors, sentiment, and aggressiveness, and provides solutions for the errors in the text.
Features
- Detects grammatical errors in the text
- Analyzes the sentiment of the text
- Measures the aggressiveness of the text
- Provides solutions for the detected errors
Stack Used
- Node.js: JavaScript runtime environment
- TypeScript: Typed superset of JavaScript
- LangChain: Language processing library
- OpenAI API: For language model capabilities
- Google Cloud: For additional language processing services
Installation
- Clone the repository:
git clone https://github.com/xavidop/langchain-example.git cd langchain-example
2. Install the dependencies:
yarn install
3. Create a .env
file in the root directory and add your OpenAI API key and Google Application credentials:
OPENAI_API_KEY="your-openai-api-key"
GOOGLE_APPLICATION_CREDENTIALS=credentials.json
LLM_PROVIDER='OPENAI'
Usage
- Build the project:
yarn run build
2. Start the application:
yarn start
3. For development, you can use:
yarn run dev
Code Explanation
Imports and Environment Setup
import { ChatOpenAI, ChatOpenAICallOptions } from "@langchain/openai";
import { ChatVertexAI } from "@langchain/google-vertexai";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { z } from "zod";
import * as dotenv from "dotenv";
// Load environment variables from .env file
dotenv.config();
- Imports: The code imports necessary modules from LangChain, Zod for schema validation, and
dotenv
for environment variable management. - Environment Setup: Loads environment variables from a
.env
file
System Template and Schema Definition
const systemTemplate = "You are an expert in {language}, you have to detect grammar problems sentences";
const classificationSchema = z.object({
sentiment: z.enum(["happy", "neutral", "sad", "angry", "frustrated"]).describe("The sentiment of the text"),
aggressiveness: z.number().int().min(1).max(10).describe("How aggressive the text is on a scale from 1 to 10"),
correctness: z.number().int().min(1).max(10).describe("How the sentence is correct grammatically on a scale from 1 to 10"),
errors: z.array(z.string()).describe("The errors in the text. Specify the proper way to write the text and where it is wrong. Explain it in a human-readable way. Write each error in a separate string"),
solution: z.string().describe("The solution to the errors in the text. Write the solution in {language}"),
language: z.string().describe("The language the text is written in"),
});
- System template: Defines a template for the system message, indicating the language and the task of detecting grammar problems
- Classification schema: Uses Zod to define a schema for the expected output, including sentiment, aggressiveness, correctness, errors, solution, and language
Prompt Template and Model Selection
const promptTemplate = ChatPromptTemplate.fromMessages([
["system", systemTemplate],
["user", "{text}"],
]);
let model: any;
if (process.env.LLM_PROVIDER == "OPENAI") {
model = new ChatOpenAI({
model: "gpt-4",
temperature: 0,
});
} else {
model = new ChatVertexAI({
model: "gemini-1.5-pro-001",
temperature: 0,
});
}
- Prompt template: Creates a prompt template using the system message and user input
- Model selection: Selects the language model based on the
LLM_PROVIDER
environment variable; it can either be OpenAI’s GPT-4 or Google’s Vertex AI.
Main Function
export const run = async () => {
const llmWihStructuredOutput = model.withStructuredOutput(classificationSchema, {
name: "extractor",
});
const chain = await promptTemplate.pipe(llmWihStructuredOutput);
const result = await chain.invoke({ language: "Spanish", text: "Yo soy enfadado" });
console.log({ result });
};
run();
- Structured output: Configures the model to use the defined classification schema
- Pipeline: Creates a pipeline by combining the prompt template and the structured output model
- Invocation: Invokes the pipeline with a sample text in Spanish, and logs the result
Prompts Used for Detecting Correctness
The following prompts are used to detect the correctness of the text:
- Grammatical errors:
"Please check the following text for grammatical errors: {text}"
- Sentiment analysis:
"Analyze the sentiment of the following text: {text}"
- Aggressiveness detection:
"Measure the aggressiveness of the following text: {text}"
- Error solutions:
"Provide solutions for the errors found in the following text: {text}"
Examples
This project can be used with different language models to detect language correctness. Here are some examples using OpenAI and Gemini models.
OpenAI
With OpenAI’s GPT-4 model, the system can detect grammatical errors, sentiment, and aggressiveness in the text.
- Input:
{ language: "Spanish", text: "Yo soy enfadado" }
- Output:
{
result: {
sentiment: 'angry',
aggressiveness: 2,
correctness: 7,
errors: [
"The correct form of the verb 'estar' should be used instead of 'ser' when expressing emotions or states."
],
solution: 'Yo estoy enfadado',
language: 'Spanish'
}
}
Gemini
With Google’s Vertex AI Gemini model, the output is quite similar:
- Input:
{ language: "Spanish", text: "Yo soy enfadado" }
- Output:
{
result: {
sentiment: 'angry',
aggressiveness: 1,
correctness: 8,
errors: [
'The correct grammar is "estoy enfadado" because "ser" is used for permanent states and "estar" is used for temporary states. In this case, being angry is a temporary state.'
],
solution: 'Estoy enfadado',
language: 'Spanish'
}
}
Conclusion
This project demonstrates how to use Langchain to detect language correctness using different language models. By combining the system template, classification schema, prompt template, and language model, you can create a powerful language processing system. OpenAI and Gemini models provide accurate results for detecting grammatical errors, sentiment, and aggressiveness in the text.
You can find the full code of this example in the GitHub repository.
License
This project is licensed under the Apache License, Version 2.0. See the LICENSE file for more details.
Contributing
Contributions are welcome! Please open an issue or submit a pull request for any changes.
Resources
Happy coding!
Published at DZone with permission of Xavier Portilla Edo, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments