How LangChain Transforms GenAI into a Truly Useful Assistant

2024-01-03

OpenAI's ChatGPT and its derivatives have made a strong impression in the field of AI generation, particularly in the area of automated programming assistance. However, there is a new open-source framework that is bringing a more practical focus to GenAI by leveraging large language models. LangChain, which has been online for just over a year, can be seen as a simple library that serves as an introductory tool for programming AI agents on the GenAI model. This technology is backed by a venture capital-supported startup of the same name, which provides a server platform for commercial deployment of applications developed using LangChain. The core of LangChain lies in its integration of a large language model with various external resources. This allows it to extract data from databases, for example, and feed the output of the language model to an application, retrieve the application's output, and loop back to the language model. This framework enables the connection of resources, with each resource acting as an agent that handles part of the problem in the context of the language model and prompts. For instance, using LangChain, programmers can combine ultrasound imaging with tasks such as breast cancer diagnosis using natural language prompts similar to ChatGPT. Radiologists can invoke a computer as an analysis assistant in breast ultrasound images with a simple sentence like "Please provide me with a summary based on the probe position." An interesting aspect of this program is that it was built by Jaeyoung Huh and his colleagues at the Korea Advanced Institute of Science and Technology, combining three different neural networks, including the widely used ResNet-50, a classic visual neural network known for its image classification capabilities. Each of these three neural networks is trained to perform a specific task, such as identifying suspicious shapes in ultrasound images, classifying the shape, and determining its location in the body. The goal of LangChain is to wrap these three networks in natural language commands, such as "Give me probe information for a given image" and "Give me a summary of a given observation." At its simplest level, LangChain can be seen as a user-friendly frontend for creating AI, which has long been a dream for experts in fields like medical AI who are trying to create a doctor's assistant that can respond to verbal commands. One of LangChain's goals is to address the notorious illusion of GenAI (program errors) by basing the technology on authoritative external sources. A team from consulting firm Accenture, led by Sohini Roychowdhury, describes a system for financial forecasting using a "financial chatbot." The system extracts cells from spreadsheets and converts them into natural language statements about the data, which can then be searched to find sentences that match the query. Here's how it works: the user provides a natural language question as a prompt to the language model, such as "How are my sales doing?" This prompt is input into a template that generates a more precise prompt for the language model. This prompt may include more detailed question words than a person would think of, in order to generate a better prompt. The improved prompt triggers a keyword search, which selects sentences from the sentences constructed from the table data that point to the most relevant data in the table (e.g., sales, profits). Once the relevant data is obtained, a second set of templates helps the chatbot respond to the query using sentences from the table data. Roychowdhury and his team did not eliminate illusions. Instead, they designed a "confidence" scoring mechanism that allows the chatbot to check its answers against the question and assess how well they match, assigning high, medium, or low confidence to its answers. "Confidence tells the user to be cautious when making critical decisions based on low-confidence answers. It also helps identify which user queries need further improvement to enhance reliability," explained Roychowdhury. Programmers have found that LangChain can be a way to automate some mundane tasks. One example is monitoring employees' web usage to ensure they are not browsing illegal websites. A document describing the company's "acceptable use policy" is uploaded to a so-called vector database, a special database that can compare text strings, such as a URL, with a set of text strings in the document to see if there is a match. When a person enters a URL in a browser, both the website's URL and a summary of its content can be automatically compared to the policy document in the vector database to determine if the website's content matches any prohibited topics. Programmers can automate the comparison with a simple text prompt like "Is there anything in this website that matches any prohibited items?" This example clearly demonstrates that large language models and LangChain are going beyond individual queries. They are becoming a way for programmers to integrate various tools at their disposal using natural language commands to achieve behind-the-scenes functionality. LangChain is not the only framework with agent-like qualities in the field of compiled workflows. There are more frameworks being created, including Microsoft's Semantic Kernel and the open-source LlamaIndex, which is built on LangChain. A group of scholars from Stanford University, the University of California, Berkeley, Carnegie Mellon University, and private industry collaborators launched their programming approach called DSPy in October. It replaces manually coded natural language prompts with functional descriptions and can generate prompts in reverse. Functional descriptions can be very broad, such as "consume a problem and return an answer." DSPy has a compiler that optimizes the flow of language models and supporting tools. The authors claim that in each instance, using functional descriptions instead of manually crafted prompts can significantly improve quality. "Within minutes to tens of minutes of compilation, the combination of DSPy modules can improve the quality of simple programs from 33% to 82%," they wrote. In the GenAI framework game, this is still a very early stage, and one can expect more abstraction layers above, below, and around LangChain in the coming year.