Generative AI with LangChain by Ben Auffarth

Generative AI with LangChain by Ben Auffarth

Author:Ben Auffarth
Language: eng
Format: epub
Tags: COM044000 - COMPUTERS / Neural Networks, COM058000 - COMPUTERS / Desktop Applications / Word Processing, COM042000 - COMPUTERS / Natural Language Processing
Publisher: Packt
Published: 2023-12-14T11:52:18+00:00


In this example, the window size is set to 1, meaning that only the last interaction will be stored in memory.

We can use the save_context() method to save the context of each interaction. It takes two arguments: user_input and model_output. These represent the user’s input and the corresponding model’s output for a given interaction.

memory.save_context({"input": "hi"}, {"output": "whats up"}) memory.save_context({"input": "not much you"}, {"output": "not much"})

We can see the message with memory.load_memory_variables({}).

We can also customize the conversational memory in LangChain, which involves modifying the prefixes used for the AI and human messages, as well as updating the prompt template to reflect these changes.

To customize the conversational memory, you can follow these steps:

Import the necessary classes and modules from LangChain: from langchain.llms import OpenAI from langchain.chains import ConversationChain from langchain.memory import ConversationBufferMemory from langchain.prompts.prompt import PromptTemplate llm = OpenAI(temperature=0)



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.