Revolutionizing Document Interaction: Unlocking Insights with LangChain Retrieval Chains

Enhancing Conversations with LangChain: Chat History in Action


import { ChatOpenAI } from "@langchain/openai";
import { HumanMessage, AIMessage } from "@langchain/core/messages";
import * as dotenv from "dotenv";
dotenv.config();

async function main() {
  const chatModel = new ChatOpenAI({
    modelName: "gpt-3.5-turbo",
    temperature: 0.7,
    openAIApiKey: process.env.OPENAI_API_KEY!,
  });

  const chatHistory = [
    new HumanMessage("What is LangChain?"),
    new AIMessage("LangChain is a framework for building applications powered by language models."),
    new HumanMessage("What are its key features?"),
  ];

  chatHistory.push(new HumanMessage("Can you provide an example of how to use it?"));

  const response = await chatModel.call(chatHistory);

  console.log("Chat History Example:");
  console.log("Response:", response.text || response);
}

main().catch(console.error);

Discover how to build dynamic and context-aware conversations using LangChain's ChatOpenAI module. This method demonstrates creating interactive chat sessions by maintaining a history of user and AI messages. By leveraging LangChain's HumanMessage and AIMessage classes, developers can manage conversational flows, ensuring the model responds contextually to previous interactions. Ideal for building chatbots, virtual assistants, and conversational AI applications, this approach simplifies handling chat histories while ensuring seamless integration with OpenAI's GPT models.

https://js.langchain.com/docs/

Conclusion

...

Under Construction
Under Construction