For the exam in Text Mining the professors asked me to create an agent OpenAI, train it on two text files about division of assets after divorce and inheritance in Italy. I had to create a streamlit app to interact with the agent with a conversation. Obviously the agent before answering has to take into account the ongoing conversation too. For this python code we used 2 libraries:
- Langchain: useful to work with the text files that will be processed and to create the agent we'll talk with.
- Streamlit: a library that gives us a framework to build the web application that we'll use to interact with the agent.
The code is based on three steps:
- Work with the text files: these two files are basically entire pieces of italian laws about the division of asset in case of divorce and inheritance. First thing was to split these documents in smaller chunks. Once done that, I used a langchain function in order to create embeddings for every chunk and Chroma to create a dataset with the documents and their embeddings.
- Definition of the agent: in order to work an agent has to be given the llm that will be the engine (in our case we used the model ChatOpenAI provided by the langchain library). Then the agent need the retriever tool, so a tool to retrieve information from the Chroma dataset defined above. Finally it needs a prompt (so a string in natural language that tells to the agent how "to behave"). In the prompt I told the agent to take into account the conversation memory before answering (in this case the conversation memory is the memory of the session in the streamlit app)
- Creation of the streamlit app: so a web application to interact with the agent. To initialize the conversation I used to if-statement in order to check if in the session keys there are memory or messages. Obviously when i run the app these two keys are not created yet. So we can define the memory as a ConversationBufferMemory with an access key (this kind of memory is defined to store conversations (so a set with two elements: role and content). The access key is used in the prompt of the agent to tell it that to answer it doesn't have to consider only the text files but also the conversation. So we can start the conversation, there's a chatbox in which we can type our question and when we press Enter the question is processed by the agent, we retrieve the answer and write it in the interface.