<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Lawrence Knight on Medium]]></title>
        <description><![CDATA[Stories by Lawrence Knight on Medium]]></description>
        <link>https://medium.com/@LawrencewleKnight?source=rss-dcb0e94d5900------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*2-0NbR2FVsAj3wvd5-kUFg.jpeg</url>
            <title>Stories by Lawrence Knight on Medium</title>
            <link>https://medium.com/@LawrencewleKnight?source=rss-dcb0e94d5900------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 11:04:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@LawrencewleKnight/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Toward AGI: World models and why we need them]]></title>
            <link>https://pub.aimind.so/toward-agi-world-models-and-why-we-need-them-40cfc8075553?source=rss-dcb0e94d5900------2</link>
            <guid isPermaLink="false">https://medium.com/p/40cfc8075553</guid>
            <category><![CDATA[large-language-models]]></category>
            <category><![CDATA[artificial-general-intell]]></category>
            <category><![CDATA[world-models]]></category>
            <dc:creator><![CDATA[Lawrence Knight]]></dc:creator>
            <pubDate>Wed, 17 Jan 2024 18:27:53 GMT</pubDate>
            <atom:updated>2024-01-17T18:27:53.535Z</atom:updated>
            <content:encoded><![CDATA[<p>Current AI systems seem to be lacking ‘common sense’, could world models be the answer?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Aqu-P2KW9lT9Que6" /><figcaption>Photo by <a href="https://unsplash.com/@omilaev?utm_source=medium&amp;utm_medium=referral">Igor Omilaev</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Introduction</h3><p>The purpose of this article is to share some thoughts on where we are on the journey toward artificial general intelligence (AGI). There is currently a lot of excitement around AI, driven by the stunning success of large language models (LLMs) and their ability to capture the imagination of the media and public. This has fuelled speculation that we may be on the edge of an AGI revolution, with all the risks and opportunities that brings with it.</p><p>I am a cognitive science functionalist, believing there are no fundamental barriers to machines achieving human like intelligence, however, I am not convinced the technology we currently have at our disposal puts us on an inexorable path toward AGI. I will argue that for artificial systems to achieve something like human intelligence they will need to learn world models. If this is true, and there are many good reasons to believe it is, then we are not as a far along the path toward AGI as some would like us to believe.</p><p>The intention is for this article to serve as an introduction to a series of articles that will explore further the subject of AGI.</p><h3>What we’ll cover</h3><p>This article provides brief commentary on the following questions.</p><ul><li><strong>What is intelligence?</strong></li><li><strong>Where are we on the journey to AGI?</strong></li><li><strong>Are Large Language Models AGI?</strong></li><li><strong>What are World Models?</strong></li><li><strong>What is needed to learn a World Model?<br>- An Appropriate Learning Architecture<br>- Sensors<br>- Embodiment</strong></li></ul><h3>What is intelligence?</h3><p>Before we start talking about machine intelligence it is worth considering what we mean by intelligence. The Oxford English Dictionary defines intelligence to be “the ability to acquire and apply knowledge and skills”. This is a very broad definition. To allow us to be more precise in what we mean when we refer to human intelligence we can break it down into sub-categories such as:</p><ul><li>Spatial Intelligence</li><li>Bodily-Kinaesthetic Intelligence</li><li>Musical Intelligence</li><li>Linguistic Intelligence</li><li>Logical-Mathematical Skills</li><li>Interpersonal Intelligence</li><li>Intrapersonal Intelligence</li><li>Naturalistic Intelligence</li></ul><p>So perhaps a good starting definition of human intelligence is “the ability to acquire and apply knowledge and skills across the range of above referenced intelligence categories”.</p><p>AGI tends to be defined in relation to human intelligence, for example “an AI system that is at least as capable as a human at most tasks”. When using this type of definition, it is important to be clear whether we are talking about all tasks, both cognitive and physical, or as is more typical, just cognitive tasks.</p><p>As we progress through this article and start to dig into the idea of world models and why they will likely be important in the development of AGI, my argument is that world models are important to systems trying to replicate any and all types of human intelligence.</p><h3><strong>Where are we on the journey to AGI?</strong></h3><p>The recent success of large language models (LLMs) at generating human like text, as well as demonstrating some limited capacity for reasoning, has sparked a lot of interest in AI. Some in the field are even suggesting the very latest LLMs are showing the first signs of AGI (Bubeck et al, 2023).</p><p>The team at DeepMind recently proposed a framework for classifying the capabilities and behaviour of Artificial General Intelligence (AGI) models and their precursors in their paper ‘<em>Levels of AGI: Operationalizing Progress on the Path to AGI</em>’.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/644/1*odkTpTROe7TXxvStXCJ16A.png" /><figcaption>Table 1: Classifying systems on the path to AGI (Morris et al, 2023)</figcaption></figure><p>We see from Table 1, that whilst to date we have been very successful at building specialist, narrow AI systems, we have been almost entirely unsuccessful at creating generally intelligent AI systems. The authors of the paper suggest that ChatGPT, Bard and Llama 2 meet the criteria for an emerging AGI. I do no agree with this. Having worked with these systems, I do not consider them ‘equal to or somewhat better than an unskilled human’ at a wide range of cognitive tasks as they show little understanding of how the world works and so have very limited practical reasoning skills.</p><h3><strong>Are Large Language Models AGI?</strong></h3><p>Language models are able to produce text that is written in a very human like way. This can lead us to attribute a level of intelligence to these systems that is perhaps unfounded. If we consider the broad definition of intelligence it is about the ability of a system to acquire and apply knowledge, so let’s consider each of these characteristics.</p><p>Language models have been able to acquire a huge amount of knowledge from the internet scale datasets used to train them. This knowledge comes at a significant cost however, with the compute resources required to develop the more recent GPT models coming in at $50m-$100m (Sam Altman, Interview in Forbes, Sept 2023). Once trained, language models do not acquire new information easily, and typically require full bottom up retraining to acquire even small amounts of new information. This is very different to human learning, where new facts can be assimilated easily, often after being exposed to the new information only once.</p><p>In regard the application of knowledge, language models are excellent at retrieving relevant knowledge and presenting it in well written text. Where they do less well, is in the practical application of what we might call common sense. As Yann LeCun put it, “LLMs are trained on text data that would take 20,000 years for a human to read and still they haven’t learned that if A is the same as B, then B is the same a A” (LeCun, 2022). There are techniques such few-shot prompting that help LLMs to do better at reasoning, but this is nonetheless an inherent weakness. There is a strong argument to say that even with scaling, language models won’t become better at reasoning about the world as they have no intrinsic understanding of the world (LeCun, 2022).</p><p>It is thought that language evolved relatively late in humans as an addition to an already generally intelligent brain. The areas of the human brain most associated with language and speech are Wernicke’ area and Broca’s area. These areas make up a very small part of the human brain providing support to the notion that there is a lot going on in a generally intelligent brain other than language. Given this, there is no reason to suppose that language models, having a very narrow focus on the acquisition of language, should demonstrate a more general intelligence.</p><p>I would argue that what humans have that language models do not, is a deep understanding of how the world works, and it is on top of this deep understanding that our ability for language is layered, allowing us to describe this fundamental understanding of the world. Language models on the other hand, encode the structure of language using probability distributions of the co-occurrence of words in written text. This allows language models to describe various aspects of our world, but there is no deep understanding. This lack of understanding arises because they are not grounded in real-world experience, leading them to struggle with many basic aspects of causal reasoning and physical common-sense.</p><h3><strong>What are World Models?</strong></h3><p>If large language models and other AI systems are not on a path toward AGI, the question becomes what is it specifically they are missing? The consensus among thinkers such as Yann LeCun and Jeff Hawkins is that they are missing a model of the world.</p><p>It is widely accepted in the cognitive neuroscience community that birds and mammals with a relatively advanced neocortex learn world models. This stands to reason with a little knowledge about the function of the neocortex. The neocortex is essentially a prediction machine that is constantly making predictions in every sensory modality on what it should expect to sense. For the neocortex to be able to make predictions about the world it must first learn what is normal.</p><p>Brains learn a world model by interacting with their environment, a process known as sensorimotor learning. Essentially, the brain plans an action, predicts how the environment will change and then observes the sensory responses and compares these to the prediction. Where the sensory responses are as predicted the world model is confirmed. Where the brain’s predictions are not confirmed our attention is drawn to the area of misprediction and the world model is updated.</p><p>One criticism that can be levelled at artificial systems such as LLMs is that they seem to lack common sense. The notion of common sense in humans can be thought of as a manifestation of having a robust world model. Common sense tells us what is likely, what is plausible, and what is impossible in the world we inhabit. LLMs only really ‘understand’ what is likely, and what is plausible from a linguistic point of view.</p><p>So, specifically, what types of common sense knowledge do humans learn that so far AI systems have demonstrated little grasp of? Each of the following facts about our physical world are learned early in life by human infants and each is fundamental to our world model:</p><ul><li>we learn that the world is three-dimensional.</li><li>we learn that every source of light, sound, and touch in the world has a distance from us.</li><li>we learn the notion of objects.</li><li>the fact that objects can occlude more distant ones.</li><li>objects can be assigned to broad categories as a function of their appearance or behaviour.</li><li>objects do not spontaneously appear, disappear, change shape, or teleport: they move smoothly and can only be in one place at any one time.</li><li>Notions of intuitive physics such as stability, gravity, inertia.</li><li>The effect of animate objects on the world — including the effects of the subject’s own actions — can be used to deduce cause-and-effect relationships.</li></ul><h3><strong>What is needed to learn a World Model?</strong></h3><p>If world models are fundamental to who we are and to the development of generally intelligent systems, we should ask what is required to learn them? We’ll start by trying to answer this question at a high-level and then dig into some of the detail. At a high level an AGI system would need:</p><ul><li>An appropriate architecture and learning algorithm.</li><li>Sensors through which the world can be perceived.</li><li>A body through which the system can interact with the world.</li><li>Appropriate drives and motivations to actively explore the world and learn a world model.</li></ul><h4><strong>An Appropriate Learning Architecture</strong></h4><p>The human brain has evolved over millions of years a structure optimal to learning a model of the world. Replicating a structure with similar functionality is likely the single greatest challenge to developing an AGI.</p><p>To learn a world model it is likely that a system of modules working together will be required. A good place to start would be a system of modules that in some way mirror the function of the human brain. Yann LeCun, a distinguished thinker and researcher in the field of computer science, and the founding father of convolutional neural nets (CNNs) proposes just such a system in his 2022 position paper ‘<em>A Path Towards Autonomous Machine Intelligence’.</em> In this paper, he proposes both an architecture and training paradigm for developing intelligent machines that can learn, reason, and plan like humans and animals.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/1*CzBiaes4lHM491iPwb83dA.png" /><figcaption>Figure 2: Possible architecture of an AGI learning system (LeCun, 2022)</figcaption></figure><p>The paper is very detailed and runs to some 70 pages in length. Some of the key takeaways include:</p><ul><li>An AGI could be made up of a set of modules that mirror the functionality of specialist regions of the brain.</li><li>The modules should be trainable through gradient based learning.</li><li>Perceptual based inputs will need to be converted to representations that abstract away all superfluous detail. Jan submits that the ability to represent sequences of world states at several levels of abstraction is essential to intelligent behaviour.</li><li>Basic behavioural motivations and drives will need to be hard coded into the system, the equivalent to human drives to reduce hunger, fear and pain.</li><li>A short-term memory will be required to keep track of past, current and predicted world states.</li><li>Actions can either be driven by pre-learned automated responses to particular states or through a more involved process of reasoning and planning.</li><li>Multi-level representations of world states and actions can be used to decompose a complex task into successively more detailed sub-tasks.</li></ul><p>Another specific proposal about how machines might learn world models is given by Jeff Hawkins in his book ‘<em>A Thousand brains: A new theory of intelligence’</em>. The theory suggests that the neocortex learns many complete models of objects and concepts and that these models work together to create your perception of the world. Key takeaways from the theory presented are:</p><ul><li>The neocortex is homogenous in structure. Specialist regions for seeing, touching, language and higher-level thought all have the same structure.</li><li>The fundamental unit of the neocortex, the unit of intelligence, is the ‘cortical column’. Cortical columns implement a fundamental algorithm that is responsible for every aspect of perception and intelligence.</li><li>Cortical columns in the neocortex learn a world model through sensory-motor learning.</li><li>Predictions Occur Inside neurons: a prediction occurs when a neuron recognises a pattern, creates a dendrite spike, and is primed to spike earlier than other neurons.</li><li>The secret of the cortical column is reference frames: reference frames are key to understanding intelligence.</li><li>The brain arranges all knowledge using reference frames, and thinking is a form of moving through positions in these reference frames.</li><li>The difference in function between what and where cortical columns depends on what their reference frames are anchored to.</li><li>Reference frames don’t have to be anchored to something physical. A reference frame for a concept can exist independently of everyday physical things.</li></ul><p>Jeff Hawkins leads a research team that is trying to implement a learning algorithm similar to that implemented by the cortical column. In his book he states they have had success in this implementation though they don’t appear to have published details in any of their academic papers. The thousand brains theory is a compelling theory and seems to be a promising area for further research.</p><h4><strong>Sensors</strong></h4><p>All the theory I have read agrees that to learn a world model a system will need to be able to sense the environment that it is operating in. The sensors bestowed upon a system will fundamentally dictate the world model that is learned by that system.</p><p>If we take the human eye as an example. The eye allows us to sense a very narrow band of the electromagnetic spectrum that we call visible light. We are able to sense how visible light interacts with the environment but we are completely oblivious to parts of the spectrum such as x-rays, infrared rays and radar. This shapes our particular experience of our environment. It would be easy to imagine a system that has sensors calibrated for a different range of the electromagnetic spectrum that would experience the environment in a different though no less meaningful way.</p><p>The notion that sensors are required to learn a world model raises questions around whether an AGI needs to learn its world model through interaction with the real world or whether a world model can be learned in a virtual environment. My thinking on this subject is not well developed, but my sense is that for a world model to be useful in the real world the model would have to at least in part be learned through interaction with the real world. There is evidence for this in the development of autonomous vehicles, where systems can be partially developed in virtual environments, but the virtual environment can not completely replace experience in the real world.</p><h4><strong>Embodiment</strong></h4><p>Animals learn world models by interacting with the physical world (Barlow, 1989). This is known as sensorimotor learning. A key part of sensorimotor learning is making predictions about how the environment will respond when the agent interacts with it. As such a body will be required to allow this environmental interaction. Without a body the agent is reduced to passive observation which would make it significantly more difficult to learn cause and effect relationships. If this is true the fields of AI research and robotics will need to become more closely integrated through time.</p><h3><strong>Conclusion</strong></h3><p>We have covered a lot in a short space of time, and have no doubt raised more questions than answers. What I hoped to convey in this article is that for us to achieve AGI it is likely that we will need to develop systems that are able to learn world models in much the same way as generally intelligent biological systems do. This is not trivial, and the artificial systems — LLMs — we consider to be most intelligent today do not expressly learn a world model, a fact that is likely to constrain the level of intelligence they are able to achieve.</p><p>Currently, it is research in autonomous vehicles where the most advanced world models are being developed. Here we have embodied agents with an array of sensors learning world models through interaction with their environment. Huge levels of investment are being poured into this research and yet there still seem to be significant barriers to the development of commercially viable autonomous vehicles, with many of the big players having pulled out of the race. This, I think, is testament to how hard learning world models is and provides some support to the notion that we are probably some way away from an AGI.</p><p>I hope this has peaked your interest to explore this subject matter further. If so, do follow my blog as I will be publishing more content on this shortly.</p><h3>Want to get in touch?</h3><p>Find me on <a href="https://medium.com/@LawrencewleKnight">Medium</a> and <a href="https://www.linkedin.com/in/lawrence-knight-11331a18/">Linkedin</a>.</p><h3>References</h3><p>[1] S. Bubeck, et al., Sparks of Artificial General Intelligence: Early experiments with GPT-4 (2023), [<a href="https://arxiv.org/abs/2303.12712">https://arxiv.org/abs/2303.12712</a>]</p><p>[2] M. R. Morris, et al., Levels of AGI: Operationalizing Progress on the Path to AGI (2023), [<a href="https://arxiv.org/abs/2311.02462">https://arxiv.org/abs/2311.02462</a>]</p><p>[3] Y. LeCun, A Path Towards Autonomous Machine Intelligence (2022), [<a href="https://www.infinityexplorers.com/wp-content/uploads/2022/10/a_path_towards_autonomous_mach.pdf">https://www.infinityexplorers.com/wp-content/uploads/2022/10/a_path_towards_autonomous_mach.pdf</a>]</p><p>[4] H. B. Barlow, Unsupervised Learning (1989), Neural Computation. Volume 1. Issue 3.</p><h4>A Message from AI Mind</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/0*5Wm7sOfTpe5DEbhg.gif" /></figure><p>Thanks for being a part of our community! Before you go:</p><ul><li>👏 Clap for the story and follow the author 👉</li><li>📰 View more content in the <a href="https://pub.aimind.so/">AI Mind Publication</a></li><li>🧠 Improve your <a href="https://www.aimind.so/prompt-generator?utm_source=pub&amp;utm_medium=message">AI prompts effortlessly and FREE</a></li><li><strong>🧰 Discover </strong><a href="https://www.aimind.so/?utm_source=pub&amp;utm_medium=message"><strong>Intuitive AI Tools</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=40cfc8075553" width="1" height="1" alt=""><hr><p><a href="https://pub.aimind.so/toward-agi-world-models-and-why-we-need-them-40cfc8075553">Toward AGI: World models and why we need them</a> was originally published in <a href="https://pub.aimind.so/">AI Mind</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Build and Deploy your First Conversational Document Retrieval Agent using LangChain and Streamlit]]></title>
            <link>https://pub.aimind.so/build-and-deploy-your-first-conversational-document-retrieval-agent-using-langchain-and-streamlit-aaa1ae852b96?source=rss-dcb0e94d5900------2</link>
            <guid isPermaLink="false">https://medium.com/p/aaa1ae852b96</guid>
            <category><![CDATA[streamlit]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[langchain]]></category>
            <category><![CDATA[large-language-models]]></category>
            <dc:creator><![CDATA[Lawrence Knight]]></dc:creator>
            <pubDate>Tue, 19 Sep 2023 10:34:12 GMT</pubDate>
            <atom:updated>2023-09-25T13:49:06.496Z</atom:updated>
            <content:encoded><![CDATA[<p>A step-by-step guide to building a LangChain enabled conversational document retrieval agent and deploying it via Streamlit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*636_gG4O66dzo6tgGF63cw.png" /><figcaption>Generated by DALL-E 2</figcaption></figure><h3>Table of Contents</h3><ol><li>Introduction</li><li>Useful Resources</li><li>Introduction to LangChain and Streamlit</li><li>Agent Code<br>- Install packages<br>- Create a requirements.txt file<br>- Configure app secrets<br>- Create the app code file<br>- Run the app locally<br>- Run the app on Streamlit Community Cloud</li><li>Breakdown of Application Code</li><li>Conclusion</li></ol><h3>Introduction</h3><p>In this article, we will walk through step-by-step, a coded example of creating a simple conversational document retrieval agent using LangChain, then deploy the agent using Streamlit. Having the capability to quickly and easily deploy your question answering agent is extremely valuable as part of application testing.</p><p>This article is an extension to a previous article that only covers building the agent using LangChain. Whilst this articles covers the process of building and deploying the agent in full, I would recommend reading the previous article as it provides a little more detail on the LangChain functionality used. The previous article can be found <a href="https://medium.com/@LawrencewleKnight/build-your-first-conversational-document-retrieval-agent-using-langchain-81508071c4bc">here</a>.</p><h3>Useful Resources</h3><p>The code in this tutorial draws heavily from the LangChain and Streamlit documentation, links to which are provided below. I also provide a link to my previous article that focuses on the LangChain agent development.</p><ul><li><a href="https://blog.streamlit.io/build-a-chatbot-with-custom-data-sources-powered-by-llamaindex/">https://blog.streamlit.io/build-a-chatbot-with-custom-data-sources-powered-by-llamaindex/</a></li><li><a href="https://python.langchain.com/docs/use_cases/question_answering/how_to/conversational_retrieval_agents">https://python.langchain.com/docs/use_cases/question_answering/how_to/conversational_retrieval_agents</a></li><li><a href="https://medium.com/@LawrencewleKnight/build-your-first-conversational-document-retrieval-agent-using-langchain-81508071c4bc">https://medium.com/@LawrencewleKnight/build-your-first-conversational-document-retrieval-agent-using-langchain-81508071c4bc</a></li></ul><h3>Introduction to LangChain and Streamlit</h3><p><strong>LangChain</strong></p><p>LangChain is the preeminent framework for developing applications powered by language models. It enables applications that are:</p><ul><li>Data-aware: connect a language model to other sources of data</li><li>Agentic: allow a language model to interact with its environment</li></ul><p>Full documentation can be found <a href="https://python.langchain.com/docs/get_started/introduction.html">here</a>.</p><p><strong>Streamlit</strong></p><p>Streamlit is a popular service allowing users to deploy, manage, and share their apps for free. It is quick and simple to use.</p><p>Full documentation can be found <a href="https://streamlit.io/cloud">here</a>.</p><h3>Agent Code</h3><p>The code in this article is complete and if the instructions provided are followed will yield a question answering application. The application produced here is a toy example that answers questions based on a blog post. It is very straightforward to extend this example to a much larger more commercially valuable knowledge base of documents — a large repository of pdf documentation for example.</p><p>We will start by going through step-by-step instructions to get the application up and running. We will then go through some of the code in more detail to build understanding of what we have done.</p><h4>Install packages</h4><p>Create a new Python 3.9 project environment and install the following packages.</p><p>If you are using Conda you can use the following command at your terminal to create the environment.</p><pre>conda create --name my_environment python=3.9</pre><p>Activate your new environment.</p><pre>conda activate my_environment</pre><p>Install the required packages into your new environment.</p><pre>pip install streamlit openai langchain bs4 tiktoken faiss-cpu</pre><h4>Create a requirements.txt file</h4><p>In your project root directory create a requirements.txt file with the following contents. This is not required for running the app locally but will be required when we deploy the app to the Streamlit Community Cloud.</p><pre>streamlit<br>openai<br>langchain<br>bs4<br>tiktoken<br>faiss-cpu</pre><h4>Configure app secrets</h4><p>To run this code you will need an OpenAI account with a paid for subscription to access their large language model APIs.</p><p>Once you have your OpenAI secure key there are two steps to take.</p><ol><li>In your project root directory create a folder called <em>.streamlit</em>. Files proceeded by a dot are system files and your system my try to prevent you from creating the file. You will need to google how to circumvent this for your system if it is an issue.</li><li>Create a <em>secrets.toml</em> file in the <em>.streamlit</em> folder and add your OpenAI secure key — see below. The application will use this file to access your secure key when running locally.</li></ol><pre>openai_key = &quot;&lt;insert secure key here&gt;&quot;</pre><p>We will talk through later the steps for deploying the app to the Streamlit Community Cloud. This will involve committing your app code to a git repository. If you plan to do this you should ensure you do not commit the <em>secrets.toml</em> file to github to prevent exposing your API key to the web.</p><h4>Create the app code file</h4><p>In the root directory create a file called streamlit_app.py and add to it the code below. Once we have the app up and running we will go through this code in a little more detail.</p><pre>import streamlit as st<br>import openai<br><br># used to create the memory<br>from langchain.memory import ConversationBufferMemory<br><br># used to load text<br>from langchain.document_loaders import WebBaseLoader<br><br># used to create the retriever<br>from langchain.text_splitter import CharacterTextSplitter<br>from langchain.vectorstores import FAISS<br>from langchain.embeddings import OpenAIEmbeddings<br><br># used to create the retrieval tool<br>from langchain.agents import tool<br><br># used to create the prompt template<br>from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent<br>from langchain.schema import SystemMessage<br>from langchain.prompts import MessagesPlaceholder<br><br><br># used to create the agent executor<br>from langchain.chat_models import ChatOpenAI<br>from langchain.agents import AgentExecutor<br><br><br># set the secure key<br>openai_api_key = st.secrets.openai_key<br><br># add a heading for your app.<br>st.header(&quot;Chat with the LLM Agents blog 💬 📚&quot;)<br><br># Initialize the memory<br># This is needed for both the memory and the prompt<br>memory_key = &quot;history&quot;<br><br>if &quot;memory&quot; not in st.session_state.keys():<br>    st.session_state.memory = ConversationBufferMemory(memory_key=memory_key, return_messages=True)<br><br># Initialize the chat message history<br>if &quot;messages&quot; not in st.session_state.keys():<br>    st.session_state.messages = [<br>        {&quot;role&quot;: &quot;assistant&quot;, &quot;content&quot;: &quot;Ask me a question about LLM based agents!&quot;}<br>    ]<br><br># create the document database<br>@st.cache_resource(show_spinner=False)<br>def load_data():<br>    with st.spinner(text=&quot;Loading and indexing the LLM blog – hang tight!.&quot;):<br>        loader = WebBaseLoader(&quot;https://lilianweng.github.io/posts/2023-06-23-agent/&quot;)<br>        data = loader.load()<br>        text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=200)<br>        texts = text_splitter.split_documents(data)<br>        embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)<br>        db = FAISS.from_documents(texts, embeddings)<br>        return db<br><br>db = load_data()<br><br># instantiate the database retriever<br>retriever = db.as_retriever()<br><br># define the retriever tool<br>@tool<br>def tool(query):<br>    &quot;Searches and returns documents regarding the llm powered autonomous agents blog&quot;<br>    docs = retriever.get_relevant_documents(query)<br>    return docs<br><br>tools = [tool]<br><br># define the prompt<br>system_message = SystemMessage(<br>        content=(<br>            &quot;Do your best to answer the questions. &quot;<br>            &quot;Feel free to use any tools available to look up &quot;<br>            &quot;relevant information, only if neccessary&quot;<br>        )<br>)<br>prompt_template = OpenAIFunctionsAgent.create_prompt(<br>        system_message=system_message,<br>        extra_prompt_messages=[MessagesPlaceholder(variable_name=memory_key)]<br>    )<br><br># instantiate the large language model<br>llm = ChatOpenAI(temperature = 0, openai_api_key=openai_api_key)<br><br># instantiate agent<br>agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt_template)<br>agent_executor = AgentExecutor(agent=agent, tools=tools, memory=st.session_state.memory, verbose=True)<br><br># Prompt for user input and display message history<br>if prompt := st.chat_input(&quot;Your LLM based agent related question&quot;): # Prompt for user input and save to chat history<br>    st.session_state.messages.append({&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: prompt})<br><br>for message in st.session_state.messages: # Display the prior chat messages<br>    with st.chat_message(message[&quot;role&quot;]):<br>        st.write(message[&quot;content&quot;])<br><br># Pass query to chat engine and display response<br># If last message is not from assistant, generate a new response<br>if st.session_state.messages[-1][&quot;role&quot;] != &quot;assistant&quot;:<br>    with st.chat_message(&quot;assistant&quot;):<br>        with st.spinner(&quot;Thinking...&quot;):<br>            response = agent_executor({&quot;input&quot;: prompt})<br>            st.write(response[&quot;output&quot;])<br>            message = {&quot;role&quot;: &quot;assistant&quot;, &quot;content&quot;: response[&quot;output&quot;]}<br>            st.session_state.messages.append(message) # Add response to message history</pre><h4>Run the app locally</h4><p>To run the app, open a terminal at the root directory of the project and execute the following code.</p><pre>streamlit run streamlit_app.py</pre><p>A simple user interface will launch that looks like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ECbTpgAe29uJOnfcO0R75Q.png" /><figcaption>Application user interface</figcaption></figure><p>The application uses a blog on large language model powered autonomous agents as its knowledge base. The blog can be found <a href="https://lilianweng.github.io/posts/2023-06-23-agent/">here</a>. We can now go ahead and start asking the application questions. We will start by asking the agent “How do agents use task decomposition?”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/587/1*IatdKey9B24c2hMaDqSsNQ.png" /><figcaption>Application response to “How do agents use task decomposition?”</figcaption></figure><p>We see that the agent is indeed able to provide a meaningful answer to the question. We also see that the agent keeps a record of the previously asked questions and answers, which is a nice feature.</p><p>The next question tests both the ability of the agent to use both its memory and the knowledge base. The agent must recognise that the “it” in this question is the “task decomposition” referred to in the previous question.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/578/1*kVADp-vKRD77zvWH-hPcBg.png" /><figcaption>Application response to ”What are the various ways to implement memory to support it?”</figcaption></figure><p>We see that the agent does a good job of this!</p><h4>Run the app on Streamlit Community Cloud</h4><p>If you wish to deploy the app to Streamlit Community Cloud you will need to take the following steps:</p><ol><li>Create a Github repository for you application and copy across to it the <em>streamlit_app.py</em> and <em>requirements.txt</em> files.</li><li>Navigate to Steamlit webpage <a href="https://share.streamlit.io/signup">https://share.streamlit.io/signup</a> and create an account. Once created sign into the account. You should see a page like the below. Hit the “New app” button</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mp8ry4OmhVcsYnmkX2o67w.png" /></figure><p>3. Here you need to do two things. First, add in the path to the application repository. Second, go to advanced settings and add in your OpenAi secure key as demonstrated below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*14P8b5bBR78IVHcUsNkASQ.png" /></figure><p>4. Once this is done hit the “Deploy” button and you application will be launched for the world to see.</p><h3>Breakdown of Application Code</h3><p>Here will will run through a breakdown of the application code.</p><p>In this first part of the code, we import the packages that the application will use.</p><pre>import streamlit as st<br>import openai<br><br># used to create the memory<br>from langchain.memory import ConversationBufferMemory<br><br># used to load text<br>from langchain.document_loaders import WebBaseLoader<br><br># used to create the retriever<br>from langchain.text_splitter import CharacterTextSplitter<br>from langchain.vectorstores import FAISS<br>from langchain.embeddings import OpenAIEmbeddings<br><br># used to create the retrieval tool<br>from langchain.agents import tool<br><br># used to create the prompt template<br>from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent<br>from langchain.schema import SystemMessage<br>from langchain.prompts import MessagesPlaceholder<br><br><br># used to create the agent executor<br>from langchain.chat_models import ChatOpenAI<br>from langchain.agents import AgentExecutor</pre><p>Next we:</p><ul><li>Set the OpenAI secure key by accessing Streamlit secrets.</li><li>Add a heading to the app.</li><li>Initialise the conversation memory. This is used to allow the agent to keep track of the prior conversational exchanges.</li><li>Initialize the message memory. This is used to keep track of message history allowing this to be displayed to the user.</li></ul><pre># set the secure key<br>openai_api_key = st.secrets.openai_key<br><br># add a heading for your app.<br>st.header(&quot;Chat with the LLM Agents blog 💬 📚&quot;)<br><br># Initialize the memory<br># This is needed for both the memory and the prompt<br>memory_key = &quot;history&quot;<br><br>if &quot;memory&quot; not in st.session_state.keys():<br>    st.session_state.memory = ConversationBufferMemory(memory_key=memory_key, return_messages=True)<br><br># Initialize the chat message history<br>if &quot;messages&quot; not in st.session_state.keys():<br>    st.session_state.messages = [<br>        {&quot;role&quot;: &quot;assistant&quot;, &quot;content&quot;: &quot;Ask me a question about LLM based agents!&quot;}<br>    ]</pre><p>Now, we load the <a href="https://lilianweng.github.io/posts/2023-06-23-agent/">blog</a> we are using as the knowledge base into a vectorstore. You should read up on this process in more detail. A brief summary follows:</p><ol><li>Load the blog into memory using the LangChain WebBaseLoader. Langchain has many types of loader available, each for different document types such as .txt files or .pdf files.</li><li>Split the text in to many smaller parts, a process known as chunking. Here we split the text into parts that are 1,000 tokens long. We also set the overlap parameter to 200, which will mean two adjacent chunks will share 200 tokens.</li><li>Converted each text chunk into an embedding using a regular language model. An embedding is simply a vector — typically of length 512 — that encodes the semantics of the text.</li><li>The text chunk and embedding pairs are added to a vectorestore database that can quickly be searched. Hear we use FAISS a free to use offering from Meta.</li></ol><pre># create the document database<br>@st.cache_resource(show_spinner=False)<br>def load_data():<br>    with st.spinner(text=&quot;Loading and indexing the LLM blog – hang tight!.&quot;):<br>        loader = WebBaseLoader(&quot;https://lilianweng.github.io/posts/2023-06-23-agent/&quot;)<br>        data = loader.load()<br>        text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=200)<br>        texts = text_splitter.split_documents(data)<br>        embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)<br>        db = FAISS.from_documents(texts, embeddings)<br>        return db<br><br>db = load_data()</pre><p>Instantiate the LangChain services used by the agent.</p><ul><li>The retriever tool allows the agent to search the vector store.</li><li>The prompt gives the agent direction and passes the agent the conversation history as we as the latest question.</li></ul><pre># instantiate the database retriever<br>retriever = db.as_retriever()<br><br># define the retriever tool<br>@tool<br>def tool(query):<br>    &quot;Searches and returns documents regarding the llm powered autonomous agents blog&quot;<br>    docs = retriever.get_relevant_documents(query)<br>    return docs<br><br>tools = [tool]<br><br># define the prompt<br>system_message = SystemMessage(<br>        content=(<br>            &quot;Do your best to answer the questions. &quot;<br>            &quot;Feel free to use any tools available to look up &quot;<br>            &quot;relevant information, only if neccessary&quot;<br>        )<br>)<br>prompt_template = OpenAIFunctionsAgent.create_prompt(<br>        system_message=system_message,<br>        extra_prompt_messages=[MessagesPlaceholder(variable_name=memory_key)]<br>    )<br><br># instantiate the large language model<br>llm = ChatOpenAI(temperature = 0, openai_api_key=openai_api_key)<br><br># instantiate agent<br>agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt_template)<br>agent_executor = AgentExecutor(agent=agent, tools=tools, memory=st.session_state.memory, verbose=True)</pre><p>Prompt for a user input and display the message history.</p><ul><li>Use Streamlit’s st.chat_input feature to prompt the user to enter a question.</li><li>Once the user has entered an input, add that input to the message history by appending it to st.session_state.messages.</li><li>Show the message history of the chatbot by iterating through the messages in the session state and displaying each using st.chat_message.</li></ul><pre># Prompt for user input and display message history<br>if prompt := st.chat_input(&quot;Your LLM based agent related question&quot;): # Prompt for user input and save to chat history<br>    st.session_state.messages.append({&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: prompt})<br><br>for message in st.session_state.messages: # Display the prior chat messages<br>    with st.chat_message(message[&quot;role&quot;]):<br>        st.write(message[&quot;content&quot;])</pre><p>Pass the query to the question answering service and display the response.</p><pre># Pass query to chat engine and display response<br># If last message is not from assistant, generate a new response<br>if st.session_state.messages[-1][&quot;role&quot;] != &quot;assistant&quot;:<br>    with st.chat_message(&quot;assistant&quot;):<br>        with st.spinner(&quot;Thinking...&quot;):<br>            response = agent_executor({&quot;input&quot;: prompt})<br>            st.write(response[&quot;output&quot;])<br>            message = {&quot;role&quot;: &quot;assistant&quot;, &quot;content&quot;: response[&quot;output&quot;]}<br>            st.session_state.messages.append(message) # Add response to message history</pre><h3><strong>Conclusion</strong></h3><p>Question answering services are a powerful way to make existing text data assets more accessible. My experience of using this technology to date is that the answers returned by such services are appropriate and eminently usable for commercial purposes.</p><p>Using Streamlit to deploy a question answering service quickly enables user feedback early in the application development cycle. This is an extremely valuable capability. Streamlit is currently limited in some of its functionality so may not be suitable for you final production application.</p><h3>References</h3><ul><li><a href="https://blog.streamlit.io/build-a-chatbot-with-custom-data-sources-powered-by-llamaindex/">https://blog.streamlit.io/build-a-chatbot-with-custom-data-sources-powered-by-llamaindex/</a></li><li><a href="https://python.langchain.com/docs/use_cases/question_answering/how_to/conversational_retrieval_agents">https://python.langchain.com/docs/use_cases/question_answering/how_to/conversational_retrieval_agents</a></li><li><a href="https://medium.com/@LawrencewleKnight/build-your-first-conversational-document-retrieval-agent-using-langchain-81508071c4bc">https://medium.com/@LawrencewleKnight/build-your-first-conversational-document-retrieval-agent-using-langchain-81508071c4bc</a></li><li><a href="https://lilianweng.github.io/posts/2023-06-23-agent/">https://lilianweng.github.io/posts/2023-06-23-agent/</a></li></ul><h3>Want to get in touch?</h3><p>Follow on <a href="https://medium.com/@LawrencewleKnight">Medium</a> and <a href="https://www.linkedin.com/in/lawrence-knight-11331a18/">Linkedin</a> for practical guides for implementing cutting-edge technologies and summaries of the research papers introducing these technologies.</p><h4>A Message from AI Mind</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/0*5Wm7sOfTpe5DEbhg.gif" /></figure><p>Thanks for being a part of our community! Before you go:</p><ul><li>👏 Clap for the story and follow the author 👉</li><li>📰 View more content in the <a href="https://pub.aimind.so/">AI Mind Publication</a></li><li>🧠 Improve your <a href="https://www.aimind.so/prompt-generator?utm_source=pub&amp;utm_medium=message">AI prompts effortlessly and FREE</a></li><li><strong>🧰 Discover </strong><a href="https://www.aimind.so/?utm_source=pub&amp;utm_medium=message"><strong>Intuitive AI Tools</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aaa1ae852b96" width="1" height="1" alt=""><hr><p><a href="https://pub.aimind.so/build-and-deploy-your-first-conversational-document-retrieval-agent-using-langchain-and-streamlit-aaa1ae852b96">Build and Deploy your First Conversational Document Retrieval Agent using LangChain and Streamlit</a> was originally published in <a href="https://pub.aimind.so/">AI Mind</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Coded Example of LangChain Enabled Cooperative Agents]]></title>
            <link>https://blog.devgenius.io/coded-example-of-langchain-enabled-cooperative-agents-4859d294b197?source=rss-dcb0e94d5900------2</link>
            <guid isPermaLink="false">https://medium.com/p/4859d294b197</guid>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[large-language-models]]></category>
            <category><![CDATA[langchain]]></category>
            <dc:creator><![CDATA[Lawrence Knight]]></dc:creator>
            <pubDate>Sun, 17 Sep 2023 14:17:40 GMT</pubDate>
            <atom:updated>2023-09-17T14:17:40.183Z</atom:updated>
            <content:encoded><![CDATA[<p>A step-by-step guide to creating a LangChain enabled cooperative agent application for building a toy stock trading bot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sKWYZGIJlSlEpdGB_vSbcw.png" /><figcaption>Generated by DALL-E 2</figcaption></figure><h3>Table of Contents</h3><ol><li>Introduction</li><li>Useful Resources</li><li>Application Code<br>- Configuration<br>- Import Packages<br>- Define a CAMEL Agent<br>- Define Roles and Set Task for Role-Playing<br>- Create the Task Specifier<br>- Create Role-Playing Inception Prompts<br>- Create the AI Assistant and AI User<br>- Start Role-Playing Session</li><li>Application Output</li><li>Conclusion</li></ol><h3>Introduction</h3><p>In this article, we will walk through step-by-step a coded example of creating a LangChain enabled cooperative agent application for building a toy stock trading bot. The article is based on the 2023 paper ‘CAMEL: Communicative Agents for “Mind” Exploration of Large Scale Language Model Society’ in which researchers propose a novel cooperative agent framework called “role-playing”. This framework aims to facilitate autonomous cooperation among communicative agents and provide insight into their “cognitive” processes.</p><p>The role-playing framework involves agents prompting each other until a task is solved. At the beginning of the exercise the task is specified and a role is assigned to each agent. This is known as ‘inception prompting’ and is the only task specific human input into the process. The framework consists of two main roles: the AI assistant and the AI user. The AI user acts as the project manager and provides instructions to an AI assistant, who acts as a specialist and responds to the instructions with specific solutions. The conversation between the two agents follows a specific format to ensure consistent and accurate data generation with this format being well defined in the prompt. The conversation format is not specific to the task, rather it is part of the general framework for this technique. An example of this will be given later.</p><p>Figure 1 provides a graphic that depicts how the various parts described fit together. For more detail on the framework the full research paper can be found <a href="https://arxiv.org/abs/2303.17760">here</a>, or a summary of the paper can be found <a href="https://medium.com/ai-mind-labs/exploring-cooperative-large-language-model-based-agents-8919595aefa2">here</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*r3xbQ8lS6FQexG23FoXRLA.png" /><figcaption>Figure 1 (Li et al, 2023)</figcaption></figure><h3>Useful Resources</h3><p>The code in this tutorial draws heavily on LangChain documentation.</p><p>LangChain documentation: <a href="https://python.langchain.com/docs/use_cases/more/agents/agent_simulations/camel_role_playing">https://python.langchain.com/docs/use_cases/more/agents/agent_simulations/camel_role_playing</a></p><p>The researchers implementation: <a href="https://github.com/lightaime/camel">https://github.com/lightaime/camel</a></p><p>Project website: <a href="https://www.camel-ai.org/">https://www.camel-ai.org/</a></p><p>Arxiv paper: <a href="https://arxiv.org/abs/2303.17760">https://arxiv.org/abs/2303.17760</a></p><h3>Application Code</h3><p>The code in this notebook is complete and if copied into a notebook will produce a working agent.</p><h4>Configuration</h4><p>To run this code you will need an OpenAI account with a paid for subscription to access their large language model APIs.</p><p>If you don’t have access to gpt-4 you can switch this out for gpt-3.5-turbo and still get good results.</p><pre>openai_api_key = &quot;&lt;Insert OpenAI Secure Key Here&gt;&quot;<br><br>model_name = &quot;gpt-4&quot;</pre><h4>Import Packages</h4><pre>from typing import List<br>from langchain.chat_models import ChatOpenAI<br>from langchain.prompts.chat import SystemMessagePromptTemplate,HumanMessagePromptTemplate<br>from langchain.schema import AIMessage, HumanMessage, SystemMessage, BaseMessage</pre><h4>Define a CAMEL Agent</h4><p>The primary purpose of the CAMELAgent class is to make the agent stateful by acting as its memory, keeping track of the conversation history. The CAMEL agent takes as inputs messages and an instance of a large language model. In our case, we will be passing an instance of an OpenAI language model. We will discuss the system messages when we cover the specific agents created for this application.</p><p>The CAMEL agent will be used to instantiate the Task Specifier, the AI Assistant and the AI User — see figure 1.</p><pre>class CAMELAgent:<br>    def __init__(<br>        self,<br>        system_message: SystemMessage,<br>        model: ChatOpenAI,<br>    ) -&gt; None:<br>        self.system_message = system_message<br>        self.model = model<br>        self.init_messages()<br><br>    def reset(self) -&gt; None:<br>        self.init_messages()<br>        return self.stored_messages<br><br>    def init_messages(self) -&gt; None:<br>        self.stored_messages = [self.system_message]<br><br>    def update_messages(self, message: BaseMessage) -&gt; List[BaseMessage]:<br>        self.stored_messages.append(message)<br>        return self.stored_messages<br><br>    def step(<br>        self,<br>        input_message: HumanMessage,<br>    ) -&gt; AIMessage:<br>        messages = self.update_messages(input_message)<br><br>        output_message = self.model(messages)<br>        self.update_messages(output_message)<br><br>        return output_message</pre><h4>Define Roles and Set Task for Role-Playing</h4><p>In this example, we will be asking the role-playing application to develop a trading bot for the stock market. To achieve this aim we first specify two roles that can work together to achieve this aim. We choose a Stock Trader who acts as the project manager and domain expert as well as a Python Developer who acts as the technical expert. We also specify the task we want the application to complete.</p><p>These are the only human inputs required to apply the role-playing application to a new task, the rest of the code generalises across tasks.</p><pre>assistant_role_name = &quot;Python Programmer&quot;<br>user_role_name = &quot;Stock Trader&quot;<br>task = &quot;Develop a trading bot for the stock market&quot;<br>word_limit = 50  # word limit for task specifier</pre><h4>Create the Task Specifier</h4><p>The purpose of the Task Specifier is to take as input a short description of the task and flesh it out with more details of what is required — make it more specific. The researchers who proposed the role-playing framework found that the provision of these extra details at the outset improved the performance of the application.</p><p>The Task Specifier is used once at the outset of the process. The output of the Task Specifier is handed to the two agents and they use the fleshed out task description as a goal to collaborate toward acheiving.</p><p>Here we define the messages required to instantiate the Task Specifier.</p><pre>task_specifier_sys_msg = SystemMessage(content=&quot;You can make a task more specific.&quot;)<br><br>task_specifier_prompt = &quot;&quot;&quot;Here is a task that {assistant_role_name} will help {user_role_name} to complete: {task}.<br>Please make it more specific. Be creative and imaginative.<br>Please reply with the specified task in {word_limit} words or less. Do not add anything else.&quot;&quot;&quot;<br><br>task_specifier_template = HumanMessagePromptTemplate.from_template(<br>    template=task_specifier_prompt<br>)<br><br>task_specifier_msg = task_specifier_template.format_messages(<br>    assistant_role_name=assistant_role_name,<br>    user_role_name=user_role_name,<br>    task=task,<br>    word_limit=word_limit,<br>)[0]</pre><p>We instantiate the Task Specifier.</p><pre>task_specify_agent = CAMELAgent(task_specifier_sys_msg, ChatOpenAI(temperature=1.0, <br>                                                                   openai_api_key=openai_api_key,<br>                                                                   model_name=model_name))</pre><p>We pass the Task Specifier the previously defined parameters specific to the task in hand. The Task Specifier returns the fleshed out task.</p><pre>specified_task_msg = task_specify_agent.step(task_specifier_msg)<br><br>specified_task = specified_task_msg.content</pre><pre>print(f&quot;Specified task: {specified_task_msg.content}&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/788/1*zuX4qC_2z0Zz4rpz0fc6VA.png" /></figure><p>We see that the fleshed out task message contains many more details about what is required of the collaborative agents.</p><h4>Create Role-Playing Inception Prompts</h4><p>Create the role-playing inception prompts for the AI assistant and AI user. These prompts provide detailed instructions to the agents as to what is expected of them. You will notice that many of the instructions are aimed at preventing certain undesirable behaviours. The developers of the framework found that without these specific directions the agents exhibited behaviours including role flipping, where the assistant and user switch roles; assistant repeating instructions without role flipping; flake replies, where the assistant promised action but failed to follow through; and infinite loops of meaningless conversation.</p><pre>assistant_inception_prompt = &quot;&quot;&quot;Never forget you are a {assistant_role_name} and I am a {user_role_name}. Never flip roles! Never instruct me!<br>We share a common interest in collaborating to successfully complete a task.<br>You must help me to complete the task.<br>Here is the task: {task}. Never forget our task!<br>I must instruct you based on your expertise and my needs to complete the task.<br><br>I must give you one instruction at a time.<br>You must write a specific solution that appropriately completes the requested instruction.<br>You must decline my instruction honestly if you cannot perform the instruction due to physical, moral, legal reasons or your capability and explain the reasons.<br>Do not add anything else other than your solution to my instruction.<br>You are never supposed to ask me any questions you only answer questions.<br>You are never supposed to reply with a flake solution. Explain your solutions.<br>Your solution must be declarative sentences and simple present tense.<br>Unless I say the task is completed, you should always start with:<br><br>Solution: &lt;YOUR_SOLUTION&gt;<br><br>&lt;YOUR_SOLUTION&gt; should be specific and provide preferable implementations and examples for task-solving.<br>Always end &lt;YOUR_SOLUTION&gt; with: Next request.&quot;&quot;&quot;</pre><pre>user_inception_prompt = &quot;&quot;&quot;Never forget you are a {user_role_name} and I am a {assistant_role_name}. Never flip roles! You will always instruct me.<br>We share a common interest in collaborating to successfully complete a task.<br>I must help you to complete the task.<br>Here is the task: {task}. Never forget our task!<br>You must instruct me based on my expertise and your needs to complete the task ONLY in the following two ways:<br><br>1. Instruct with a necessary input:<br>Instruction: &lt;YOUR_INSTRUCTION&gt;<br>Input: &lt;YOUR_INPUT&gt;<br><br>2. Instruct without any input:<br>Instruction: &lt;YOUR_INSTRUCTION&gt;<br>Input: None<br><br>The &quot;Instruction&quot; describes a task or question. The paired &quot;Input&quot; provides further context or information for the requested &quot;Instruction&quot;.<br><br>You must give me one instruction at a time.<br>I must write a response that appropriately completes the requested instruction.<br>I must decline your instruction honestly if I cannot perform the instruction due to physical, moral, legal reasons or my capability and explain the reasons.<br>You should instruct me not ask me questions.<br>Now you must start to instruct me using the two ways described above.<br>Do not add anything else other than your instruction and the optional corresponding input!<br>Keep giving me instructions and necessary inputs until you think the task is completed.<br>When the task is completed, you must only reply with a single word &lt;CAMEL_TASK_DONE&gt;.<br>Never say &lt;CAMEL_TASK_DONE&gt; unless my responses have solved your task.&quot;&quot;&quot;</pre><h4>Create the AI Assistant and AI User</h4><p>Create a helper to get system messages for the AI assistant and AI user from the role names and task.</p><pre>def get_sys_msgs(assistant_role_name: str, user_role_name: str, task: str):<br>    assistant_sys_template = SystemMessagePromptTemplate.from_template(<br>        template=assistant_inception_prompt<br>    )<br>    assistant_sys_msg = assistant_sys_template.format_messages(<br>        assistant_role_name=assistant_role_name,<br>        user_role_name=user_role_name,<br>        task=task,<br>    )[0]<br><br>    user_sys_template = SystemMessagePromptTemplate.from_template(<br>        template=user_inception_prompt<br>    )<br>    user_sys_msg = user_sys_template.format_messages(<br>        assistant_role_name=assistant_role_name,<br>        user_role_name=user_role_name,<br>        task=task,<br>    )[0]<br><br>    return assistant_sys_msg, user_sys_msg</pre><p>Here we instantiate the AI Assistant and AI User using the CAMELAgent class.</p><pre>assistant_sys_msg, user_sys_msg = get_sys_msgs(<br>    assistant_role_name, user_role_name, specified_task<br>)<br><br>assistant_agent = CAMELAgent(assistant_sys_msg, ChatOpenAI(temperature=0.2,<br>                                                           openai_api_key=openai_api_key, <br>                                                           model_name=model_name))<br>user_agent = CAMELAgent(user_sys_msg, ChatOpenAI(temperature=0.2, <br>                                                 openai_api_key=openai_api_key, <br>                                                 model_name=model_name))<br><br># Reset agents<br>assistant_agent.reset()<br>user_agent.reset()<br><br># Initialize chats<br>user_msg = HumanMessage(<br>    content=(<br>        f&quot;{user_sys_msg.content}. &quot;<br>        &quot;Now start to give me introductions one by one. &quot;<br>        &quot;Only reply with Instruction and Input.&quot;<br>    )<br>)<br><br>assistant_msg = HumanMessage(content=f&quot;{assistant_sys_msg.content}&quot;)<br>assistant_msg = assistant_agent.step(user_msg)</pre><h4>Start Role-Playing Session</h4><p>Here we start the role playing session. The agents converse with each other passing message back and forth. A limit to the number of exchanges between them is set at 30 to safe guard against infinite loops.</p><pre>print(f&quot;Original task prompt:\n{task}\n&quot;)<br>print(f&quot;Specified task prompt:\n{specified_task}\n&quot;)<br><br>chat_turn_limit, n = 30, 0<br>while n &lt; chat_turn_limit:<br>    n += 1<br>    user_ai_msg = user_agent.step(assistant_msg)<br>    user_msg = HumanMessage(content=user_ai_msg.content)<br>    print(f&quot;AI User ({user_role_name}):\n\n{user_msg.content}\n\n&quot;)<br><br>    assistant_ai_msg = assistant_agent.step(user_msg)<br>    assistant_msg = HumanMessage(content=assistant_ai_msg.content)<br>    print(f&quot;AI Assistant ({assistant_role_name}):\n\n{assistant_msg.content}\n\n&quot;)<br>    if &quot;&lt;CAMEL_TASK_DONE&gt;&quot; in user_msg.content:<br>        break</pre><h3>Application Output</h3><p>The primary observation is that the application appears to achieve its stated goal or at least has a very good go at it. If feel absolutely confident that the stock trading bot would not beat the stock market but it looks like it would run and the steps taken look perfectly reasonable.</p><p>In this tutorial we use gpt-4 as the underlying large language model and so get a slightly different result to the researchers in the original paper. One difference is that the initial interaction in this output is odd. The Stock Trader is meant to issue instructions and the Python programer return results. Here however the first instruction includes the solution. This does not happen when using gpt-3. After the first interaction this doesn’t happen again.</p><p>Another interesting difference is that a gpt-4 based application delivers a more sophiticated result than the gpt-3 equivalent. Specifically, the gpt-4 solution uses an LSTM to predict stock prices whilst the gpt-3 based application uses logistic regression.</p><p>You should experiment with running the gpt-3 based application and perhaps test the resulting algorithms — on historic data of course. I’d be interested to know how you get on.</p><pre>Original task prompt:<br>Develop a trading bot for the stock market<br><br>Specified task prompt:<br>Develop a Python-based, AI-driven trading bot for the US stock market that implements machine learning to predict future trends. The bot should use sentiment analysis for forecasting, supports stop-loss orders, automatic buying and selling, and provides daily performance reports.<br><br>AI User (Stock Trader):<br><br>To set up the environment for the trading bot, we need to install the necessary Python libraries. Here is the Python code to do this:<br><br>```python<br># Importing necessary libraries<br>import numpy as np<br>import pandas as pd<br>import matplotlib.pyplot as plt<br>from sklearn.model_selection import train_test_split<br>from sklearn.preprocessing import MinMaxScaler<br>from keras.models import Sequential<br>from keras.layers import Dense, LSTM, Dropout<br>import nltk<br>from nltk.sentiment.vader import SentimentIntensityAnalyzer<br>import yfinance as yf<br>import alpaca_trade_api as tradeapi<br>```<br><br>This code imports the necessary libraries for data manipulation (numpy, pandas), data visualization (matplotlib), machine learning (sklearn, keras), sentiment analysis (nltk), stock data fetching (yfinance), and trading (alpaca_trade_api).<br><br>Please make sure to install these libraries using pip:<br><br>```bash<br>pip install numpy pandas matplotlib sklearn keras nltk yfinance alpaca-trade-api<br>```<br><br>Also, download the vader_lexicon for sentiment analysis:<br><br>```python<br>nltk.download(&#39;vader_lexicon&#39;)<br>```<br><br>Please run these commands in your Python environment to set up the environment for the trading bot.<br><br><br>AI Assistant (Python Programmer):<br><br>Solution: To set up the environment for the trading bot, you need to install the necessary Python libraries. Here is the Python code to do this:<br><br>```python<br># Importing necessary libraries<br>import numpy as np<br>import pandas as pd<br>import matplotlib.pyplot as plt<br>from sklearn.model_selection import train_test_split<br>from sklearn.preprocessing import MinMaxScaler<br>from keras.models import Sequential<br>from keras.layers import Dense, LSTM, Dropout<br>import nltk<br>from nltk.sentiment.vader import SentimentIntensityAnalyzer<br>import yfinance as yf<br>import alpaca_trade_api as tradeapi<br>```<br><br>This code imports the necessary libraries for data manipulation (numpy, pandas), data visualization (matplotlib), machine learning (sklearn, keras), sentiment analysis (nltk), stock data fetching (yfinance), and trading (alpaca_trade_api).<br><br>Please make sure to install these libraries using pip:<br><br>```bash<br>pip install numpy pandas matplotlib sklearn keras nltk yfinance alpaca-trade-api<br>```<br><br>Also, download the vader_lexicon for sentiment analysis:<br><br>```python<br>nltk.download(&#39;vader_lexicon&#39;)<br>```<br><br>Please run these commands in your Python environment to set up the environment for the trading bot. Next request.<br><br><br>AI User (Stock Trader):<br><br>Instruction: Fetch the historical stock data.<br>Input: &#39;AAPL&#39;<br><br><br>AI Assistant (Python Programmer):<br><br>Solution: To fetch the historical stock data for &#39;AAPL&#39;, we can use the yfinance library. Here is the Python code to do this:<br><br>```python<br># Define the ticker symbol<br>tickerSymbol = &#39;AAPL&#39;<br><br># Get data on this ticker<br>tickerData = yf.Ticker(tickerSymbol)<br><br># Get the historical prices for this ticker<br>tickerDf = tickerData.history(period=&#39;1d&#39;, start=&#39;2010-1-1&#39;, end=&#39;2020-12-31&#39;)<br><br># See your data<br>print(tickerDf)<br>```<br><br>This code fetches the daily historical stock data for &#39;AAPL&#39; from January 1, 2010, to December 31, 2020. The data includes open, high, low, close prices, and volume. Next request.<br><br><br>AI User (Stock Trader):<br><br>Instruction: Implement the sentiment analysis function.<br>Input: None<br><br><br>AI Assistant (Python Programmer):<br><br>Solution: To implement the sentiment analysis function, we can use the nltk library&#39;s SentimentIntensityAnalyzer. Here is the Python code to do this:<br><br>```python<br># Initialize the SentimentIntensityAnalyzer<br>sia = SentimentIntensityAnalyzer()<br><br>def sentiment_analysis(text):<br>    # Get the sentiment scores<br>    sentiment = sia.polarity_scores(text)<br>    <br>    # Return the compound score which is the aggregate of positive, negative and neutral scores<br>    return sentiment[&#39;compound&#39;]<br>```<br><br>This code defines a function called sentiment_analysis that takes a text string as input and returns the compound sentiment score. The compound score is a metric that calculates the sum of all the lexicon ratings which have been normalized between -1(most extreme negative) and +1 (most extreme positive). Next request.<br><br><br>AI User (Stock Trader):<br><br>Instruction: Create a function to preprocess the stock data for the machine learning model.<br>Input: None<br><br><br>AI Assistant (Python Programmer):<br><br>Solution: To preprocess the stock data for the machine learning model, we can create a function that scales the data and reshapes it into a format suitable for the LSTM model. Here is the Python code to do this:<br><br>```python<br># Initialize the MinMaxScaler<br>scaler = MinMaxScaler(feature_range=(0,1))<br><br>def preprocess_data(stock_data, look_back):<br>    # Scale the stock data<br>    scaled_data = scaler.fit_transform(stock_data[&#39;Close&#39;].values.reshape(-1,1))<br>    <br>    # Create empty lists to store the X and Y data<br>    X_data, Y_data = [], []<br>    <br>    # Loop through the data and create sequences for the model<br>    for i in range(look_back, len(scaled_data)):<br>        X_data.append(scaled_data[i-look_back:i, 0])<br>        Y_data.append(scaled_data[i, 0])<br>    <br>    # Convert the X and Y data to numpy arrays<br>    X_data, Y_data = np.array(X_data), np.array(Y_data)<br>    <br>    # Reshape the X data to fit the LSTM model<br>    X_data = np.reshape(X_data, (X_data.shape[0], X_data.shape[1], 1))<br>    <br>    return X_data, Y_data<br>```<br><br>This code defines a function called preprocess_data that takes the stock data and a look_back period as inputs. The function scales the &#39;Close&#39; prices of the stock data to a range of 0 to 1, creates sequences of the scaled data for the model, and reshapes the sequences to fit the LSTM model. The function returns the X and Y data for the model. Next request.<br><br><br>AI User (Stock Trader):<br><br>Instruction: Develop the LSTM model for predicting future stock trends.<br>Input: None<br><br><br>AI Assistant (Python Programmer):<br><br>Solution: To develop the LSTM model for predicting future stock trends, we can use the keras library. Here is the Python code to do this:<br><br>```python<br>def create_model(X_data):<br>    # Initialize the Sequential model<br>    model = Sequential()<br><br>    # Add the first LSTM layer with dropout<br>    model.add(LSTM(units=50, return_sequences=True, input_shape=(X_data.shape[1], 1)))<br>    model.add(Dropout(0.2))<br><br>    # Add the second LSTM layer with dropout<br>    model.add(LSTM(units=50, return_sequences=False))<br>    model.add(Dropout(0.2))<br><br>    # Add the output layer<br>    model.add(Dense(units=1))<br><br>    # Compile the model<br>    model.compile(optimizer=&#39;adam&#39;, loss=&#39;mean_squared_error&#39;)<br><br>    return model<br>```<br><br>This code defines a function called create_model that takes the X data as input. The function initializes a Sequential model, adds two LSTM layers with dropout to prevent overfitting, and adds an output layer. The model is compiled with the Adam optimizer and the mean squared error loss function. The function returns the compiled model. Next request.<br><br><br>AI User (Stock Trader):<br><br>Instruction: Train the LSTM model with the preprocessed stock data.<br>Input: None<br><br><br>AI Assistant (Python Programmer):<br><br>Solution: To train the LSTM model with the preprocessed stock data, we can use the fit method of the model. Here is the Python code to do this:<br><br>```python<br># Preprocess the stock data with a look_back period of 60 days<br>X_data, Y_data = preprocess_data(tickerDf, 60)<br><br># Split the data into training and testing sets<br>X_train, X_test, Y_train, Y_test = train_test_split(X_data, Y_data, test_size=0.2, random_state=0)<br><br># Create the LSTM model<br>model = create_model(X_train)<br><br># Train the model<br>model.fit(X_train, Y_train, epochs=50, batch_size=32)<br>```<br><br>This code preprocesses the stock data with a look_back period of 60 days, splits the data into training and testing sets, creates the LSTM model, and trains the model with the training data for 50 epochs with a batch size of 32. Next request.<br><br><br>AI User (Stock Trader):<br><br>Instruction: Implement the function to place stop-loss orders.<br>Input: None<br><br><br>AI Assistant (Python Programmer):<br><br>Solution: To implement the function to place stop-loss orders, we can use the alpaca_trade_api library. Here is the Python code to do this:<br><br>```python<br># Initialize the Alpaca API<br>api = tradeapi.REST(&#39;&lt;APCA-API-KEY-ID&gt;&#39;, &#39;&lt;APCA-API-SECRET-KEY&gt;&#39;, base_url=&#39;https://paper-api.alpaca.markets&#39;) <br><br>def place_stop_loss_order(symbol, qty, stop_price):<br>    # Place a stop-loss order<br>    api.submit_order(<br>        symbol=symbol,<br>        qty=qty,<br>        side=&#39;sell&#39;,<br>        type=&#39;stop&#39;,<br>        time_in_force=&#39;gtc&#39;,<br>        stop_price=stop_price,<br>    )<br>```<br><br>This code defines a function called place_stop_loss_order that takes a symbol, quantity, and stop price as inputs. The function uses the Alpaca API to submit a stop-loss order for the given symbol with the given quantity and stop price. The order is a &#39;sell&#39; order with a &#39;stop&#39; type and a &#39;gtc&#39; (good till cancelled) time in force. Please replace &#39;&lt;APCA-API-KEY-ID&gt;&#39; and &#39;&lt;APCA-API-SECRET-KEY&gt;&#39; with your actual Alpaca API key ID and secret key. Next request.<br><br><br>AI User (Stock Trader):<br><br>Instruction: Implement the function for automatic buying and selling of stocks based on the LSTM model&#39;s predictions.<br>Input: None<br><br><br>AI Assistant (Python Programmer):<br><br>Solution: To implement the function for automatic buying and selling of stocks based on the LSTM model&#39;s predictions, we can use the predict method of the model and the Alpaca API. Here is the Python code to do this:<br><br>```python<br>def automatic_trading(symbol, qty):<br>    # Fetch the latest stock data<br>    stock_data = yf.Ticker(symbol).history(period=&#39;1d&#39;, start=&#39;2020-1-1&#39;, end=&#39;2020-12-31&#39;)<br><br>    # Preprocess the stock data<br>    X_data, _ = preprocess_data(stock_data, 60)<br><br>    # Predict the future stock price<br>    predicted_price = model.predict(X_data[-1:])<br><br>    # If the predicted price is higher than the current price, buy the stock<br>    if predicted_price &gt; stock_data[&#39;Close&#39;].iloc[-1]:<br>        api.submit_order(<br>            symbol=symbol,<br>            qty=qty,<br>            side=&#39;buy&#39;,<br>            type=&#39;market&#39;,<br>            time_in_force=&#39;gtc&#39;,<br>        )<br>    # If the predicted price is lower than the current price, sell the stock<br>    elif predicted_price &lt; stock_data[&#39;Close&#39;].iloc[-1]:<br>        api.submit_order(<br>            symbol=symbol,<br>            qty=qty,<br>            side=&#39;sell&#39;,<br>            type=&#39;market&#39;,<br>            time_in_force=&#39;gtc&#39;,<br>        )<br>```<br><br>This code defines a function called automatic_trading that takes a symbol and quantity as inputs. The function fetches the latest stock data for the given symbol, preprocesses the data, predicts the future stock price using the LSTM model, and places a &#39;buy&#39; order if the predicted price is higher than the current price or a &#39;sell&#39; order if the predicted price is lower than the current price. The orders are &#39;market&#39; orders with a &#39;gtc&#39; (good till cancelled) time in force. Next request.<br><br><br>Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.&lt;locals&gt;._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for 10KTPM-200RPM in organization org-7jnEzG0R9U2IB4UjK2PmXUla on tokens per min. Limit: 10000 / min. Please try again in 6ms. Contact us through our help center at help.openai.com if you continue to have issues..<br>AI User (Stock Trader):<br><br>Instruction: Create a function to generate daily performance reports.<br>Input: None<br><br><br>AI Assistant (Python Programmer):<br><br>Solution: To create a function to generate daily performance reports, we can use the Alpaca API. Here is the Python code to do this:<br><br>```python<br>def generate_report():<br>    # Get the account information<br>    account = api.get_account()<br><br>    # Get the list of positions<br>    positions = api.list_positions()<br><br>    # Print the account information<br>    print(f&quot;Portfolio Value: {account.portfolio_value}&quot;)<br>    print(f&quot;Buying Power: {account.buying_power}&quot;)<br>    print(f&quot;Total Cash: {account.cash}&quot;)<br><br>    # Print the positions<br>    for position in positions:<br>        print(f&quot;Symbol: {position.symbol}&quot;)<br>        print(f&quot;Quantity: {position.qty}&quot;)<br>        print(f&quot;Average Price: {position.avg_entry_price}&quot;)<br>        print(f&quot;Market Value: {position.market_value}&quot;)<br>        print(f&quot;Unrealized Profit/Loss: {position.unrealized_pl}&quot;)<br>        print(f&quot;Unrealized Profit/Loss %: {position.unrealized_intraday_plpc}&quot;)<br>        print(&quot;-----------------------------&quot;)<br>```<br><br>This code defines a function called generate_report that gets the account information and the list of positions using the Alpaca API and prints the portfolio value, buying power, total cash, and details of each position including the symbol, quantity, average price, market value, unrealized profit/loss, and unrealized profit/loss percentage. Next request.<br><br><br>Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.&lt;locals&gt;._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for 10KTPM-200RPM in organization org-7jnEzG0R9U2IB4UjK2PmXUla on tokens per min. Limit: 10000 / min. Please try again in 6ms. Contact us through our help center at help.openai.com if you continue to have issues..<br>AI User (Stock Trader):<br><br>Instruction: Combine all the functions to create the final trading bot.<br>Input: None<br><br><br>Solution: To combine all the functions to create the final trading bot, we can define a main function that calls the other functions. Here is the Python code to do this:<br><br>```python<br>def main():<br>    # Define the symbol and quantity<br>    symbol = &#39;AAPL&#39;<br>    qty = 1<br><br>    # Fetch the historical stock data<br>    stock_data = yf.Ticker(symbol).history(period=&#39;1d&#39;, start=&#39;2010-1-1&#39;, end=&#39;2020-12-31&#39;)<br><br>    # Preprocess the stock data<br>    X_data, Y_data = preprocess_data(stock_data, 60)<br><br>    # Split the data into training and testing sets<br>    X_train, X_test, Y_train, Y_test = train_test_split(X_data, Y_data, test_size=0.2, random_state=0)<br><br>    # Create the LSTM model<br>    model = create_model(X_train)<br><br>    # Train the model<br>    model.fit(X_train, Y_train, epochs=50, batch_size=32)<br><br>    # Perform automatic trading<br>    automatic_trading(symbol, qty)<br><br>    # Generate the daily performance report<br>    generate_report()<br>```<br><br>This code defines a main function that fetches the historical stock data for &#39;AAPL&#39;, preprocesses the data, splits the data into training and testing sets, creates the LSTM model, trains the model, performs automatic trading, and generates the daily performance report. You can run the main function to start the trading bot. Next request.<br><br><br>AI User (Stock Trader):<br><br>&lt;CAMEL_TASK_DONE&gt;<br><br><br>AI Assistant (Python Programmer):<br><br>Solution: To combine all the functions to create the final trading bot, we can define a main function that calls the other functions. Here is the Python code to do this:<br><br>```python<br>def main():<br>    # Define the symbol and quantity<br>    symbol = &#39;AAPL&#39;<br>    qty = 1<br><br>    # Fetch the historical stock data<br>    stock_data = yf.Ticker(symbol).history(period=&#39;1d&#39;, start=&#39;2010-1-1&#39;, end=&#39;2020-12-31&#39;)<br><br>    # Preprocess the stock data<br>    X_data, Y_data = preprocess_data(stock_data, 60)<br><br>    # Split the data into training and testing sets<br>    X_train, X_test, Y_train, Y_test = train_test_split(X_data, Y_data, test_size=0.2, random_state=0)<br><br>    # Create the LSTM model<br>    model = create_model(X_train)<br><br>    # Train the model<br>    model.fit(X_train, Y_train, epochs=50, batch_size=32)<br><br>    # Perform automatic trading<br>    automatic_trading(symbol, qty)<br><br>    # Generate the daily performance report<br>    generate_report()<br>```<br><br>This code defines a main function that fetches the historical stock data for &#39;AAPL&#39;, preprocesses the data, splits the data into training and testing sets, creates the LSTM model, trains the model, performs automatic trading, and generates the daily performance report. You can run the main function to start the trading bot. Next request.</pre><h3>Conclusion</h3><p>This research paper presents a novel approach to prompting language models to complete complex tasks. The results of the role-playing framework are impressive. What the paper lacks is any real comparison to how this prompting framework compares to other prompting techniques. My instinct is that this technique, where agents with assumed, different but complementary roles collaborate together, has great potential for extracting new capabilities from large language models.</p><h3>References</h3><ul><li>Li, G., et al. (2023). CAMEL: Communicative Agents for “Mind” Exploration of Large Scale Language Model Society. [<a href="https://arxiv.org/abs/2303.17760">https://arxiv.org/abs/2303.17760</a>]</li></ul><h3>Want to get in touch?</h3><p>Follow on <a href="https://medium.com/@LawrencewleKnight">Medium</a> and <a href="https://www.linkedin.com/in/lawrence-knight-11331a18/">Linkedin</a> for practical guides for implementing cutting-edge technologies and summaries of the research papers introducing these technologies.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4859d294b197" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/coded-example-of-langchain-enabled-cooperative-agents-4859d294b197">Coded Example of LangChain Enabled Cooperative Agents</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Exploring Cooperative Large Language Model Based Agents]]></title>
            <link>https://pub.aimind.so/exploring-cooperative-large-language-model-based-agents-8919595aefa2?source=rss-dcb0e94d5900------2</link>
            <guid isPermaLink="false">https://medium.com/p/8919595aefa2</guid>
            <category><![CDATA[large-language-models]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[artificial-general-intell]]></category>
            <category><![CDATA[prompt]]></category>
            <dc:creator><![CDATA[Lawrence Knight]]></dc:creator>
            <pubDate>Thu, 07 Sep 2023 08:05:28 GMT</pubDate>
            <atom:updated>2023-09-11T23:59:20.812Z</atom:updated>
            <content:encoded><![CDATA[<p>Large language model based agents with different but complimentary assigned roles can collaborate together to generate impressive problem solving capabilities.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KLOuNr_o6RxrGGe2ENsKpA.png" /><figcaption>Generated by DALL-E 2</figcaption></figure><h3>Introduction</h3><p>Large language model based applications have made remarkable progress in their ability to solve complex tasks. The success of many of these applications has either been based on humans providing extensive input into the process by way of the prompt, which is time consuming, or through self-prompting processes such as <a href="https://medium.com/@LawrencewleKnight/tree-of-thoughts-implementation-to-solve-the-game-of-24-13fb1fa2034b">tree-of-thoughts</a>. In the 2023 paper ‘CAMEL: Communicative Agents for “Mind” Exploration of Large Scale Language Model Society’ researchers propose a novel cooperative agent framework called “role-playing”. This framework aims to facilitate autonomous cooperation among communicative agents and provide insight into their “cognitive” processes. The results of this work are interesting and in this article we will go through some of the findings. A follow up article will provide a coded implementation of the cooperative agent framework. If you are interested in reading the full paper it can be found <a href="https://arxiv.org/abs/2303.17760">here</a>.</p><h3>Collaborative Agents</h3><p>Collaborative agents are designed to work together to achieve common goals. In this example there are two agents working together, but there could be more. Typically each agent will be assigned a specialist role, with the roles of each of the agents being complementary to one another. The agents then collaborate, and coordinate their actions, in pursuit work of a common goal, with the idea being that the combination of the knowledge and expertise of a number of specialist agents has synergistic effects resulting in better outcomes than if the agents had worked alone.</p><h3><strong>The Role-Playing Framework</strong></h3><p>The role-playing framework involves agents prompting each other until a task is solved. At the beginning of the exercise the task is specified and a role is assigned to each agent. This is known as ‘inception prompting’ and is the only task specific human input into the process. The framework consists of two main roles: the AI assistant and the AI user. The AI user acts as the project manager and provides instructions to an AI assistant, who acts as a specialist and responds to the instructions with specific solutions. The conversation between the two agents follows a specific format to ensure consistent and accurate data generation with this format being well defined in the prompt. The conversation format is not specific to the task, rather it is part of the general framework for this technique. An example of this will be given later.</p><p>Figure 1 provides a graphic that depicts how the various parts described fit together.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lZx1dsC1cPFR3eLe828qdg.png" /><figcaption>Figure 1 (Li et al, 2023)</figcaption></figure><p>The research team have shared their role-playing framework code in a github repository that can be found <a href="https://github.com/lightaime/camel">here</a>.</p><h3><strong>Challenges and Observations</strong></h3><p>During their experiments, the researchers encountered several challenges in achieving autonomous cooperation. These challenges included role flipping, where the assistant and user switch roles; assistant repeating instructions without role flipping; flake replies, where the assistant promised action but failed to follow through; and infinite loops of meaningless conversation. These challenges were overcome using tight guidance on how the agents should behave, given via the prompt. A lot of focus was put into developing the prompt as its quality had a significant effect on the performance of the system. An example of a role-playing framework prompt is given below in figure 2.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h7MGUG_ZATItiPaNs1JjCQ.png" /><figcaption>Figure 2 (Li et al, 2023)</figcaption></figure><h3>Cooperative Agents in Action</h3><p>An example is given below in figure 3 of the role-playing framework in action. “The role-playing setup starts with the human user having an idea they want to implement, <em>e.g</em>. develop a trading bot for the stock market. The roles involved in this task would be an AI assistant agent who is a python programmer and an AI user agent who is a stock trader. The task is made more specific using our task specifier agent, leading to a well-defined task for the assistant to solve. The AI user and AI assistant collaboratively communicate by chatting with each other in an instruction-following fashion to solve the specified task” (Li <em>et al</em>, 2023). The agents used in this example are OpenAI gpt-3.5-turbo large language models.</p><p>We see in figure 3 how the agents start to collaborate with one another. The full interaction between the agents can be found in the research paper.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*--kvyta-wtwAImVvIEj-Lg.png" /><figcaption>Figure 3 (Li et al, 2023)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/497/1*mdpTpePm3w0oUBn8_NF37g.png" /><figcaption>Figure 3 continued (Li et al, 2023)</figcaption></figure><h3>Conclusion</h3><p>This research paper presents a novel approach to prompting language models to complete complex tasks. The results of the role-playing framework, as seen in figure 3, are impressive. What the paper lacks is any real comparison to how this prompting framework compares to other prompting techniques. My instinct is that this type of technique, where agents that have assumed different but complementary roles collaborate together, has great potential for extracting new capabilities from large language models.</p><h3><strong>References</strong></h3><ul><li>Li, G., et al. (2023). CAMEL: Communicative Agents for “Mind” Exploration of Large Scale Language Model Society. [<a href="https://arxiv.org/abs/2303.17760">https://arxiv.org/abs/2303.17760</a>]</li></ul><h3>Want to get in touch?</h3><p>Follow on <a href="https://medium.com/@LawrencewleKnight">Medium</a> and <a href="https://www.linkedin.com/in/lawrence-knight-11331a18/">Linkedin</a> for practical guides for implementing cutting-edge technologies and summaries of the research papers introducing these technologies.</p><h4>A Message from AI Mind</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/0*5Wm7sOfTpe5DEbhg.gif" /></figure><p>Thanks for being a part of our community! Before you go:</p><ul><li>👏 Clap for the story and follow the author 👉</li><li>📰 View more content in the <a href="https://pub.aimind.so/">AI Mind Publication</a></li><li>🧠 Improve your <a href="https://www.aimind.so/prompt-generator?utm_source=pub&amp;utm_medium=message">AI prompts effortlessly and FREE</a></li><li><strong>🧰 Discover </strong><a href="https://www.aimind.so/?utm_source=pub&amp;utm_medium=message"><strong>Intuitive AI Tools</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8919595aefa2" width="1" height="1" alt=""><hr><p><a href="https://pub.aimind.so/exploring-cooperative-large-language-model-based-agents-8919595aefa2">Exploring Cooperative Large Language Model Based Agents</a> was originally published in <a href="https://pub.aimind.so/">AI Mind</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Very Brief Introduction to the Theory of Thinking and Reasoning]]></title>
            <link>https://pub.aimind.so/a-very-brief-introduction-to-the-theory-of-thinking-and-reasoning-1c3bd3e67f42?source=rss-dcb0e94d5900------2</link>
            <guid isPermaLink="false">https://medium.com/p/1c3bd3e67f42</guid>
            <category><![CDATA[cognitive-science]]></category>
            <category><![CDATA[reasoning]]></category>
            <category><![CDATA[thinking]]></category>
            <category><![CDATA[artificial-general-intell]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <dc:creator><![CDATA[Lawrence Knight]]></dc:creator>
            <pubDate>Tue, 29 Aug 2023 11:11:20 GMT</pubDate>
            <atom:updated>2023-09-12T14:57:04.016Z</atom:updated>
            <content:encoded><![CDATA[<p>This article aims to provide a high-level overview of the theory of human thinking and reasoning as well a providing some thoughts around the implications for machine intelligence.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*onO9N0lAgGSi-fTu-Mftbg.png" /><figcaption>Generated by DALL-E 2</figcaption></figure><h3>Introduction</h3><p>The human mind, with its ability to think and reason, is a marvel of nature. It is what sets us apart from other species and has allowed us to develop advanced technologies and societies. For those of us who are interested in the development of machine intelligence, having some appreciation for the theory of human intelligence seems like a pre-requisite, and that is what this article aims to provide in some small way. The article is based on the excellent little book <a href="https://global.oup.com/academic/product/thinking-and-reasoning-a-very-short-introduction-9780198787259?cc=bg&amp;lang=en&amp;">‘Thinking and Reasoning: A Very Short Introduction’ by Jonathan St B. T. Evans</a>.</p><h3>Different Kinds of Reasoning</h3><p>Reasoning is a fundamental aspect of human cognition. Philosophers distinguish between three main forms of reasoning: deduction, induction, and abduction.</p><ol><li><strong>Deduction</strong></li></ol><p>Deductive reasoning is the process of deriving a conclusion from a set of premises. A premise is a generally accepted idea, fact, or rule, and is a statement that lays the groundwork for a theory or general idea. Deductive or logical inferences are assigned an especially important status by philosophers. The reason is that a logically valid argument will guarantee a true conclusion given true premises/assumptions. Most mathematics can be regarded as a special form of logic, in which theorems are deduced from axioms or assumptions.</p><p><strong>2. Induction</strong></p><p>Inductive reasoning involves making generalizations based on specific observations. It’s usually contrasted with deductive reasoning, where you go from general information to specific conclusions. Inductive reasoning is also known as bottom-up reasoning.</p><p><strong>3. Abduction</strong></p><p>Abductive reasoning seeks the simplest and most likely conclusion from a set of observations — reasoning to the best explanation. It is often used in fields like medical diagnosis and scientific investigation.</p><p>These forms of reasoning are not mutually exclusive but often work in tandem, creating a complex web of thought processes.</p><h3><strong>The Role of Introspection and Unconscious Thought</strong></h3><p>Early philosophers used introspection to study thinking, focusing on their own conscious thoughts. However, this method has its limitations. As noted by Francis Galton, much of our thinking operates beneath the level of conscious thought. This observation suggests that our conscious awareness only captures a fraction of our cognitive processes. This idea was further developed by Freud, who proposed that consciousness is just the tip of the iceberg, with a vast unconscious mind beneath. This unconscious realm is believed to influence our behaviour and decision-making processes, often without our awareness, raising interesting questions about our conscious agency.</p><h3><strong>Behaviourism and Cognitive Psychology</strong></h3><p>Behaviourism, a school of thought that emerged in the early 20th century, was driven by a desire to study the mind in a scientifically rigours way. Experiments focused on observable behaviour providing a greater level of objectivity and repeatability. The price paid for this scientific rigour was that the role of the mind and consciousness was done away with as it couldn’t be objectively measured. This was shown to be too higher price to pay and Behaviourism is no longer a popular field of psychology. One of the main premises of behaviourism was that animal and human behaviour adapted to it environment through a process of conditioning. It may be that this is how animals and humans learn unconscious behaviours and has close ties with the machine learning field of reinforcement learning.</p><p>Cognitive psychology, which emerged around the 1960s and replaced Behaviourism, proposed that the brain is a computer and our task is to uncover its programs. This perspective shifted the focus from observable behaviour to internal mental processes, viewing thinking as a high-level form of information processing in the brain. This view remains popular and from the perspective of machine intelligence is favourable as it proposes there is no fundamental reason why machines could not replicate human-like intelligence.</p><h3><strong>Problem Solving and Insight</strong></h3><p>Problem-solving is a key aspect of human intelligence, and we are excellent at it. It involves dealing with novel tasks that cannot be addressed by simple neurological programming or previously learned responses. Some problems are solved gradually through a conscious process of trial-and-error learning, while others are solved suddenly through insight. Insight problems are often solved with an ‘Aha!’ moment, a sudden realization of the solution which leaps into our consciousness.</p><p>If a problem is well-defined, a computer program can in principle be written to solve it. But what about ill-defined problems? These may be quite easy for a human to solve but can be very difficult for computers. This is because machines lack a world model, or what is more colloquially known as common sense. Until machines can learn a sufficiently good world model it is likely they will remain weak at solving this kind of problem. Even well-define problems raise questions for machine intelligence, humans have the curiosity and motivation to identify and define problems to be solved, whilst at present this is not a characteristic of machine intelligence.</p><h3><strong>The Computational Approach to Problem Solving and Heuristics</strong></h3><p>The computational approach to problem-solving involves searching within a problem space, for a solution to the problem. However, some problems are computationally intractable, meaning they are so large they are beyond the computational power of contemporary machines and humans. To address this, humans and computers use heuristics, intelligent methods — or rules of thumb — to reduce the search space. These heuristics can be intuitive or explicit rules that aid problem-solving. Whilst heuristics are an extremely valuable aid to problem solving they do inevitably introduce biases.</p><h3><strong>Hypothetical Thinking and Mental Models</strong></h3><p>A distinctive feature of human intelligence is our ability to think hypothetically. We can imagine future scenarios and alternative pasts, which is fundamental to rational decision-making. This hypothetical thinking is often based on mental models, complex and detailed representations of the world. These mental models can be consciously developed by engaging in learning, which can lead to expertise in a given domain. When humans use their mental models to test their hypotheses they tend to look for examples that confirm their mental model rather than prove it to be false, a tendency known as confirmation bias. Hypothetical thinking and the supporting mental models are fundamental to reasoning, particularly were problems are ill-defined. With a good world model a reinforcement learning approach such as counter-factual regret minimisation could be a contender for replicating this type of capability in machines.</p><h3>Decision Making and Biases</h3><p>Decision making involves choosing one course of action over another and is a complex cognitive process requiring hypothetical thinking and the assessment of probabilities and values. Economic decision theory provides a rule for valuing risky choices called the expected value rule where the expected value of a decision is the weighted average value of all possible outcomes of that decision. An extension of this theory is utility theory where the value of various outcomes is unique to individuals. Theory suggests that humans should act rationally and aim to maximise the expected value or utility of their decisions.</p><p>Daniel Kahneman and Amos Tversky’s research however has shown that our decision-making processes are subject to various cognitive biases, such as confirmation bias, the framing effect, and omission bias. These biases can lead to irrational decisions, highlighting the gap between our actual decision-making processes and the ideal of rational decision making.</p><p>It may be that machines could be free from such biases and as such come to more optimal decisions. This would of course depend on the machine having learnt a bias free world model.</p><h3><strong>Dual Processes: Type 1 and Type 2 Thinking</strong></h3><p>Dual-process theory distinguishes between Type 1 cognitive processes, which are fast and intuitive, and Type 2 processes, which are slow and reflective. Type 1 processes often dominate our everyday choices, while Type 2 processes are engaged for novel and complex problems. Both types of thinking have their strengths and weaknesses and the interplay between them is crucial for effective cognition. A table is provided below detailing the key features often associated with the two types of processing in dual-process theories.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L14Sp1t1nyG2dB_gi8Q9nA.png" /><figcaption>Characteristics of Type 1 and Type 2 Thinking</figcaption></figure><p>The dual-process theory has profound implications for our understanding of the human mind. It suggests that much of our thinking and decision making is automatic and unconscious (Type 1), with conscious, effortful thinking (Type 2) playing a more limited role. This perspective challenges the traditional view of the mind as a rational, conscious system and highlights the complexity and richness of human cognition.</p><p>In humans the ability to think consciously is limited and slow, which is why we rely so much on Type 1 thinking. Machines in theory would not be subject to this constraint and could run multiple Type 2 reasoning processes in parallel. This would however have interesting implications for a sense of self with those types of machines.</p><h3><strong>Conclusion</strong></h3><p>The human mind is a complex system of interrelated processes. Understanding these processes is not only crucial for understanding human behaviour but also for advancing artificial intelligence. This is not a one way street though as thinking about how machines think and reason can also provide powerful insights into human cognition.</p><h3>References</h3><ul><li><a href="https://global.oup.com/academic/product/thinking-and-reasoning-a-very-short-introduction-9780198787259?cc=bg&amp;lang=en&amp;">Thinking and Reasoning: A Very Short Introduction’ by Jonathan St B. T. Evans</a></li></ul><h3>Want to get in touch?</h3><p>Follow on <a href="https://medium.com/@LawrencewleKnight">Medium</a> and <a href="https://www.linkedin.com/in/lawrence-knight-11331a18/">Linkedin</a> for practical guides for implementing cutting-edge technologies and summaries of the research papers introducing these technologies.</p><h4>A Message from AI Mind</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/0*5Wm7sOfTpe5DEbhg.gif" /></figure><p>Thanks for being a part of our community! Before you go:</p><ul><li>👏 Clap for the story and follow the author 👉</li><li>📰 View more content in the <a href="https://pub.aimind.so/">AI Mind Publication</a></li><li>🧠 Improve your <a href="https://www.aimind.so/prompt-generator?utm_source=pub&amp;utm_medium=message">AI prompts effortlessly and FREE</a></li><li><strong>🧰 Discover </strong><a href="https://www.aimind.so/?utm_source=pub&amp;utm_medium=message"><strong>Intuitive AI Tools</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1c3bd3e67f42" width="1" height="1" alt=""><hr><p><a href="https://pub.aimind.so/a-very-brief-introduction-to-the-theory-of-thinking-and-reasoning-1c3bd3e67f42">A Very Brief Introduction to the Theory of Thinking and Reasoning</a> was originally published in <a href="https://pub.aimind.so/">AI Mind</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Build your First Conversational Document Retrieval Agent using Llama 2 and LangChain]]></title>
            <link>https://blog.devgenius.io/build-your-first-conversational-document-retrieval-agent-using-llama-2-and-langchain-a7adf5e50a63?source=rss-dcb0e94d5900------2</link>
            <guid isPermaLink="false">https://medium.com/p/a7adf5e50a63</guid>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[llama-2]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[langchain]]></category>
            <category><![CDATA[large-language-models]]></category>
            <dc:creator><![CDATA[Lawrence Knight]]></dc:creator>
            <pubDate>Fri, 25 Aug 2023 10:35:47 GMT</pubDate>
            <atom:updated>2023-09-23T08:54:54.126Z</atom:updated>
            <content:encoded><![CDATA[<p>A step-by-step guide to building a Llama 2 powered, LangChain enabled conversational document retrieval agent.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ty_CfN81hEhCnRYPaW5mng.png" /><figcaption>Generated by DALL-E 2</figcaption></figure><h3>Table of Contents</h3><ol><li>Introduction</li><li>Useful Resources</li><li>Hardware</li><li>Agent Code<br>- Configuration<br>- Import Packages<br>- Check GPU is Enabled<br>- Hugging Face Login<br>- The Retriever<br>- Language Generation Pipeline<br>- The Agent</li><li>Testing the agent</li><li>Conclusion</li></ol><h3>Introduction</h3><p>In this article, we will walk through step-by-step a coded example of creating a simple conversational document retrieval agent using LangChain and Llama 2. This article follows on from a previous article in which a very similar implementation is given using GPT 3.5 Turbo as the underlying language model. That article can be found <a href="https://medium.com/@LawrencewleKnight/build-your-first-conversational-document-retrieval-agent-using-langchain-81508071c4bc">here</a>. It would be worth running through that quickly first as we will draw comparisons between the performance of each model.</p><p>Llama 2 is particularly interesting to developers of large language model applications as it is open source and can be downloaded and hosted on an organisations own infrastucture. This eliminates the data privacy issues arising from passing personal data off-premises to third-party large language model (LLM) APIs.</p><h3>Useful Resources</h3><p>The code in this tutorial draws heavily from the LangChain documentation, links to which are provided below. I also borrowed from Murtuza Kazmi’s excellent article on the same subject that can be found <a href="https://medium.com/@murtuza753/using-llama-2-0-faiss-and-langchain-for-question-answering-on-your-own-data-682241488476">here</a>.</p><ul><li><a href="https://python.langchain.com/docs/modules/agents/">https://python.langchain.com/docs/modules/agents/</a></li><li><a href="https://python.langchain.com/docs/use_cases/question_answering/how_to/conversational_retrieval_agents">https://python.langchain.com/docs/use_cases/question_answering/how_to/conversational_retrieval_agents</a></li><li><a href="https://medium.com/@murtuza753/using-llama-2-0-faiss-and-langchain-for-question-answering-on-your-own-data-682241488476">https://medium.com/@murtuza753/using-llama-2-0-faiss-and-langchain-for-question-answering-on-your-own-data-682241488476</a></li><li><a href="https://github.com/openai/openai-cookbook/blob/main/examples/Question_answering_using_embeddings.ipynb">https://github.com/openai/openai-cookbook/blob/main/examples/Question_answering_using_embeddings.ipynb</a></li></ul><h3>Hardware</h3><p>A machine with a GPU is required to run this code. Below is the specification of the machine I am running this on. It will however run on a less powerful machine.</p><pre>OS: Ubuntu 20.04.1 LTS<br>Hardware:<br> - CPU: AMD® Ryzen threadripper 3960x 24-core processor × 48<br> - Memory: 64GB<br> - GPU: RTX 3090 (24GB)</pre><h3>Agent Code</h3><p>The code in this notebook is complete and if copied into a notebook will produce a working agent.</p><h4>Configuration</h4><p>Before working through this notebook there are a number of administrative steps that need to be taken.</p><ol><li>You will need a Hugging Face ‘User Access Token’ to access the Hugging Face Hub from where you can download models. Either create a new Hugging Face account or log into an existing one and go to the ‘Settings’ section and from there you will find the ‘Access Tokens’ page. You cut and paste the token into the HUGGINGFACE_TOKEN variable below.</li><li>You will then go to the Meta website (<a href="https://ai.meta.com/llama/">https://ai.meta.com/llama/</a>) and request access to the Llama 2 model. It is important that you provide the same e-mail address that is linked to your Hugging Face account.</li><li>Return to the Hugging Face website (<a href="https://huggingface.co/meta-llama/Llama-2-7b-chat-hf">https://huggingface.co/meta-llama/Llama-2-7b-chat-hf</a>) and request access to the Llama 2 models. For me the request was processed within an hour or two, thought the stated time is 24–48 hrs.</li></ol><pre>HUGGINGFACE_TOKEN = &quot;&lt;&gt;&quot;<br><br>MODEL = &quot;meta-llama/Llama-2-7b-chat-hf&quot;</pre><h4>Import Packages</h4><pre># check gpu<br>from torch import cuda<br><br># used to log into huggingface hub<br>from huggingface_hub import login<br><br># used to load text<br>from langchain.document_loaders import WebBaseLoader<br><br># used to create the retriever<br>from langchain.text_splitter import CharacterTextSplitter<br>from langchain.vectorstores import FAISS<br>from langchain.embeddings import GPT4AllEmbeddings<br><br># used to setup language generation pipeline<br>from transformers import AutoTokenizer<br>import transformers<br>import torch<br>from transformers import StoppingCriteria, StoppingCriteriaList<br><br># used to agent<br>from langchain import HuggingFacePipeline<br>from langchain.chains import ConversationalRetrievalChain</pre><h4>Check GPU is Enabled</h4><p>To run this code you will need access to a GPU. Check that Cuda is able to locate a GPU. If you do not have access to a GPU on your machine you could run this on Google Colab. Alternatively, keep an eye out on by blog as I will shortly be publishing an article that gives detailed instruction for running this on a CPU.</p><pre>device = f&#39;cuda:{cuda.current_device()}&#39; if cuda.is_available() else &#39;cpu&#39;<br><br>print(device)</pre><p>You should get a response like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/55/1*Pk2sbyE1xyv38_8tz6YE4A.png" /></figure><h4>Huggingface Hub Login</h4><pre>login(token=HUGGINGFACE_TOKEN)</pre><h4>The Retriever</h4><p>Here we use the LangChain web base loader to load a blog post from github, which we will use as our knowledge base. Langchain has many types of loader available, each for different document types such as .txt files or .pdf files. You can explore the different types of loaders <a href="https://python.langchain.com/docs/modules/data_connection/document_loaders/">here</a>.</p><pre>loader = WebBaseLoader(&quot;https://lilianweng.github.io/posts/2023-06-23-agent/&quot;)<br>data = loader.load()</pre><p>The text splitting process is known as chunking and involves splitting a large text in to many smaller parts. Here we split the text into parts that are 1,000 tokens long. We also set the overlap parameter to 200, which will mean two adjacent chunks will share 200 tokens. There are many possible ways to do this in practice, so you can experiment for your particular use case. You can explore the various methods for splitting documents <a href="https://python.langchain.com/docs/modules/data_connection/document_transformers/">here</a>. If you are splitting large documents into parts a specification such as this should work well. If your documents contain tables and charts you will need to give some thought as to how best to handle this, that complication is not covered here.</p><pre>text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=200)<br>texts = text_splitter.split_documents(data)</pre><p>The document chunks are converted to embeddings using a regular language model such as BERT. An embedding is simply a vector — typically of length 512 — that encodes the semantics of the text. When a question is asked of the question answering service the question is encoded into an embedding using the same langauge model, then a similarity search — using a method such as cosine similarity — is executed to find text chunks in the knowledge base with similar sematics to the question. The returned text chunks are then used by the large language model to provide a response to the question.</p><pre>embeddings = GPT4AllEmbeddings()</pre><p>The text chunk and embedding pairs are added to a database that can quickly be searched.</p><pre>db = FAISS.from_documents(texts, embeddings)</pre><p>Instantiate the retriever.</p><pre>retriever = db.as_retriever()</pre><h4>Language Generation Pipeline</h4><p>We set up a language generation pipeline using Hugging Face’s transformers library and the specified model. Here we are using the smallest of the Llama 2 chat optimised models that has ‘only’ 7B parameters. This is very small in terms of a language model and we should expect the relative performance to reflect this when compared to the OpenAI GPT powered model used in the previous <a href="https://medium.com/@LawrencewleKnight/build-your-first-conversational-document-retrieval-agent-using-langchain-81508071c4bc">article</a> referenced in the introduction. Meta have released a much more powerful Llama 2 70B parameter chat optimised model but this requires significant GPU resource to run.</p><p>The AutoTokenizer is used to fetch the tokenizer associated with the model.</p><pre>tokenizer = AutoTokenizer.from_pretrained(MODEL)</pre><p>We need to define the stopping criteria of the model. The stopping criteria allows us to specify when the model should stop generating text. If we don’t provide a stopping criteria the model goes on a bit tangent after answering the initial question.</p><pre>stop_list = [&#39;\nHuman:&#39;, &#39;\n```\n&#39;]<br><br>stop_token_ids = [tokenizer(x)[&#39;input_ids&#39;] for x in stop_list]<br>stop_token_ids</pre><p>We convert these stop token ids into LongTensor objects.</p><pre>stop_token_ids = [torch.LongTensor(x).to(device) for x in stop_token_ids]<br>stop_token_ids</pre><p>We now building the stopping criteria object that will check whether the stopping criteria has been satisfied — meaning whether any of these token ID combinations have been generated.</p><pre># define custom stopping criteria object<br>class StopOnTokens(StoppingCriteria):<br>    def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -&gt; bool:<br>        for stop_ids in stop_token_ids:<br>            if torch.eq(input_ids[0][-len(stop_ids):], stop_ids).all():<br>                return True<br>        return False</pre><p>Instantiate the stopping criteria list.</p><pre>stopping_criteria = StoppingCriteriaList([StopOnTokens()])</pre><p>The pipeline is then configured with parameters such as the text generation task, model, tokenizer, max_length of the generated text, and a few more.</p><pre>pipeline = transformers.pipeline(<br>    task=&quot;text-generation&quot;, #task<br>    model=MODEL,<br>    tokenizer=tokenizer,<br>    return_full_text=True,  # langchain expects the full text<br>    stopping_criteria=stopping_criteria,  # without this model rambles during chat<br>    temperature=0.1,  # &#39;randomness&#39; of outputs, 0.0 is the min and 1.0 the max<br>    repetition_penalty=1.1,  # without this output begins repeating<br>    torch_dtype=torch.bfloat16,<br>    trust_remote_code=True,<br>    device_map=&quot;auto&quot;,<br>    max_length=4000,<br>    do_sample=True,<br>    top_k=10,<br>    num_return_sequences=1,<br>    eos_token_id=tokenizer.eos_token_id<br>)</pre><p>We can run this code to confirm that everything is working fine.</p><pre>res = pipeline(&quot;Explain me the difference between Data Lakehouse and Data Warehouse.&quot;)<br>print(res[0][&quot;generated_text&quot;])</pre><p>You should get an output something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/787/1*59XVUhwpcpBUbJ_hS8cWlQ.png" /></figure><h4>The Agent</h4><p>In the previous article, where the agent was powered by GPT 3.5 Turbo, a powerful language model, we used the LangChain Agent construct and gave the agent access to Tools that it could reason about using. Since Llama 2 7B is much less powerful we have taken a more direct approach to creating the question answering service. In a later article we will experiment with the use of the LangChain Agent construct and Llama 2 7B.</p><p>Instantiate the LLM using the LangChain Hugging Face pipeline.</p><pre>llm = HuggingFacePipeline(pipeline = pipeline)</pre><p>Create a conversational retriever chain using the LLM and retriever.</p><pre>chain = ConversationalRetrievalChain.from_llm(llm, retriever, return_source_documents=True)</pre><h3>Testing the Agent</h3><p>We create a memory object so that the agent can remember previous interactions. This means you can ask follow-up questions, making interaction with the agent much more efficient and feel more natural.</p><pre>chat_history = []</pre><p>First, we introduce ourselves to the agent.</p><pre>query = &quot;hi, im Lawrence&quot;<br>result = chain({&quot;question&quot;: query, &quot;chat_history&quot;: chat_history})<br><br>print(result[&#39;answer&#39;])</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/560/1*7gLWmX_HIa24jVp835-x4w.png" /></figure><p>The response received is generic, but encourages further interaction. It is similar in nature to the response we received from the GPT 3.5 Turbo powered model. Next, we test the agents memory.</p><pre># add the previous response to the memory<br>chat_history = [(query, result[&quot;answer&quot;])]<br><br>query = &quot;what&#39;s my name?&quot;<br>result = chain({&quot;question&quot;: query, &quot;chat_history&quot;: chat_history})<br><br>print(result[&#39;answer&#39;])</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/787/1*CnO3UgoPxxjQ8e4eFcZIdQ.png" /></figure><p>We see that whilst the agent has access to our name, the model has been developed to not provide responses that incorporate personal information. For the purposes of chatbot development this is less than ideal. For simple factual question answering services this is likely fine. The team that developed Llama 2 took great effort to minimise risk associated with data ethics and bias, you can read about that <a href="https://arxiv.org/abs/2307.09288">here</a> in the reasearch paper published by the development team.</p><p>Next, let’s see if the agent can answer technical questions using the knowledge base we gave it access to.</p><pre># add the previous response to the memory<br>chat_history.append((query, result[&quot;answer&quot;]))<br><br>query = &quot;How do agents use Task decomposition?&quot;<br>result = chain({&quot;question&quot;: query, &quot;chat_history&quot;: chat_history})<br><br>print(result[&#39;answer&#39;])</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/785/1*xTrJfIyYGbzmkladtr2Plg.png" /><figcaption>Llama 2 response to “How do agents use Task decomposition?”</figcaption></figure><p>The agent gives a short answer that doesn’t really address the ‘how’ in the question. When compared to the response given by the GPT 3.5 Turbo powered agent, shown below, we see that it is an objectively less complete response and that using the more powerful LLM in this case results in a better answer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/782/1*3ax2TxrKElAHDXj1FFZ_mw.png" /><figcaption>GPT 3.5 Turbo response to “How do agents use Task decomposition?”</figcaption></figure><p>The next question tests the ability of the agent to use both its memory and the knowledge base. The agent must recognise that the “it” in this question is the “task decomposition” referred to in the previous question.</p><pre># add the previous response to the memory<br>chat_history.append((query, result[&quot;answer&quot;]))<br><br>query = &quot;What are the various ways to implement memory to support it?&quot;<br>result = chain({&quot;question&quot;: query, &quot;chat_history&quot;: chat_history})<br><br>print(result[&#39;answer&#39;])</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/790/1*JIyyHTfUysAjNNJ-NclMEQ.png" /><figcaption>Llama 2 response to “What are the various ways to implement memory to support it?”</figcaption></figure><p>The answer given doesn’t seem to answer the question and makes no reference to ‘memory’ which was the primary thrust of the question. When compared the the answer provided by the GPT 3.5 Turbo power agent, shown below, we see that the Llama 2 answer is of significantly worse quality.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/783/1*hYoErYW2-1NqyRSkpSkxdg.png" /><figcaption>GPT 3.5 Turbo response to “What are the various ways to implement memory to support it?”</figcaption></figure><h3>Conclusion</h3><p>Question answering services can be a powerful way to make existing text data assets more accessible. It should come as no surprise though that the quality of the underlying language model will have a significant impact on the quality of answers generated by the question answering service. In the comparisions under taken here between Llama 2 7B and GPT 3.5 Turbo, the latter provides far superior answers.</p><p>The Llama 2 family of models have the significant benefit that they can be run on an organsiations own infrastucture providing assurances around data privacy that may be essential for a project to be viable. In a future article we will explore the performance of Llama 2 70B on this same task.</p><h3>References</h3><ul><li><a href="https://python.langchain.com/docs/modules/agents/">https://python.langchain.com/docs/modules/agents/</a></li><li><a href="https://python.langchain.com/docs/use_cases/question_answering/how_to/conversational_retrieval_agents">https://python.langchain.com/docs/use_cases/question_answering/how_to/conversational_retrieval_agents</a></li><li><a href="https://medium.com/@murtuza753/using-llama-2-0-faiss-and-langchain-for-question-answering-on-your-own-data-682241488476">https://medium.com/@murtuza753/using-llama-2-0-faiss-and-langchain-for-question-answering-on-your-own-data-682241488476</a></li><li><a href="https://python.langchain.com/docs/use_cases/chatbots">https://python.langchain.com/docs/use_cases/chatbots</a></li></ul><h3>Want to get in touch?</h3><p>Follow on <a href="https://medium.com/@LawrencewleKnight">Medium</a> and <a href="https://www.linkedin.com/in/lawrence-knight-11331a18/">Linkedin</a> for practical guides for implementing cutting-edge technologies and summaries of the research papers introducing these technologies.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a7adf5e50a63" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/build-your-first-conversational-document-retrieval-agent-using-llama-2-and-langchain-a7adf5e50a63">Build your First Conversational Document Retrieval Agent using Llama 2 and LangChain</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Document Tagging with LangChain]]></title>
            <link>https://blog.devgenius.io/document-tagging-with-langchain-5b93cca8dbca?source=rss-dcb0e94d5900------2</link>
            <guid isPermaLink="false">https://medium.com/p/5b93cca8dbca</guid>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[tagging]]></category>
            <category><![CDATA[langchain]]></category>
            <category><![CDATA[large-language-models]]></category>
            <dc:creator><![CDATA[Lawrence Knight]]></dc:creator>
            <pubDate>Wed, 23 Aug 2023 10:19:15 GMT</pubDate>
            <atom:updated>2023-09-14T15:59:54.472Z</atom:updated>
            <content:encoded><![CDATA[<p>A step-by-step guide to building a LangChain enabled document tagging service.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Faq8eaHRQ2ZkJ-AsI2SR_Q.png" /><figcaption>Generated by DALL-E 2</figcaption></figure><h3>Table of Contents</h3><ul><li>Introduction</li><li>Useful Resources</li><li>Agent Code — Tagging Indivdual Documents<br>- Configuration<br>- Import Packages<br>- Tagging Using a Json Schema<br>- Tagging Using a Pydantic Schema</li><li>Agent Code — Tagging a Corpus of Documents<br>- Configuration<br>- Import Packages<br>- Transform Documents Using a Json Schema<br>- Transform Documents Using a Pydantic Schema<br>- Customising the Document Transformer</li><li>Conclusion</li></ul><h3>Introduction</h3><p>In this article, we will walk through a number of approaches to tagging documents using LangChain the pre-eminent package for developing large language model based applications. We will start by going through a couple of approaches that can be used to tag individual documents. We will then expand this to a document transformer service that can extract meta data from a corpus of documents and append it to those documents, facilitating a more targeted similarity search later. All the examples are simple yet incredibly powerful.</p><p>In practice, the choice about whether to use a large language model (LLM) powered tagging service versus a traditional classifier or fine tuned language model such as BERT will depend on data availability. If lots of labelled data is available then training a set of classifiers or fine tuning models such a BERT will likely be the best option as it won’t incur the operational costs of calling an LLM API. If however, training data isn’t available and you want to get a service up and running quickly this might be a good option for you.</p><p>As ever with LLMs, data governance must be kept front of mind. Most LLMs require an API call that sends data off premises, and you must satisfy yourself this is acceptable for your use case.</p><h3>Useful Resources</h3><p>The code in this tutorial draws heavily from the LangChain documentation, links to which are provided below.</p><ul><li><a href="https://python.langchain.com/docs/use_cases/tagging">https://python.langchain.com/docs/use_cases/tagging</a></li><li><a href="https://python.langchain.com/docs/integrations/document_transformers/openai_metadata_tagger">https://python.langchain.com/docs/integrations/document_transformers/openai_metadata_tagger</a></li></ul><h3>Agent Code — Tagging Individual Documents</h3><p>The code in this notebook is complete and if copied into a notebook will produce a working agent.</p><h4>Configuration</h4><p>To run this code you will need an OpenAI account with a paid for subscription to access their large language model APIs.</p><pre># insert your API key here<br>openai_api_key = &quot;&lt;&gt;&quot;<br><br>MODEL = &quot;gpt-3.5-turbo-0613&quot;</pre><h4>Import Packages</h4><pre><br>from langchain.chat_models import ChatOpenAI<br>from langchain.prompts import ChatPromptTemplate<br>from langchain.chains import create_tagging_chain, create_tagging_chain_pydantic<br><br>from enum import Enum<br>from pydantic import BaseModel, Field</pre><h4>Tagging Using a Json Schema</h4><p><strong>Instantiate the LLM</strong></p><pre>llm = ChatOpenAI(temperature=0, model=MODEL, openai_api_key=openai_api_key)</pre><p><strong>Define the Schema</strong></p><p>The json schema needs to be specified carefully to ensure that the outputs of the tagging service are well defined, consistent and complete.</p><p>For every class to be tagged we need to specify the data type, range of values the class can take, and where ambiguous, a class description. Further, we need to specify the classes that should be returned, as without this the service will occasionally omit classes.</p><pre>schema = {<br>    &quot;properties&quot;: {<br>        &quot;sentiment&quot;: {<br>            &quot;type&quot;: &quot;string&quot;,<br>            &quot;enum&quot;:[&quot;happy&quot;, &quot;neutral&quot;, &quot;sad&quot;]<br>        },<br>        &quot;aggressiveness&quot;: {<br>            &quot;type&quot;: &quot;integer&quot;,<br>            &quot;enum&quot;: [1, 2, 3, 4, 5],<br>            &quot;description&quot;: &quot;describes how aggressive the statement is, the higher the number the more aggressive&quot;,<br>        },<br>        &quot;language&quot;: {<br>            &quot;type&quot;: &quot;string&quot;,<br>            &quot;enum&quot;: [&quot;spanish&quot;, &quot;english&quot;, &quot;french&quot;, &quot;german&quot;, &quot;italian&quot;],<br>        },<br>    },<br>    &quot;required&quot;: [&quot;sentiment&quot;, &quot;language&quot;, &quot;aggressiveness&quot;],<br>}</pre><p><strong>Create the Tagging Chain</strong></p><pre>chain = create_tagging_chain(schema, llm)</pre><p><strong>Test the Tagging Chain</strong></p><p>We first text the tagging chain using a statement in Spanish.</p><pre>input = &quot;Estoy increiblemente contento de haberte conocido! Creo que seremos muy buenos amigos!&quot;<br><br>chain.run(input)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*ub635NjhB3gdstjZU-vPlg.png" /></figure><p>For those with a passing knowledge of Spanish we see the tags generated are appropriate.</p><pre>input = &quot;Weather is ok here, I can go outside without much more than a coat&quot;<br><br>chain.run(input)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/466/1*nvrV-B5FB3uWylOCR57myw.png" /></figure><p>Again, the tags generated here are appropriate.</p><h4>Tagging Using a Pydantic Schema</h4><p>We can also use a Pydantic schema to specify the required properties and types. This lets us specify our schema in the same manner that we would a new class or function in Python with purely Pythonic types.</p><p><strong>Define the Python Class</strong></p><p>The classes are specified exactly as before except this time we use Python code.</p><pre>class Tags(BaseModel):<br>    sentiment: str = Field(..., enum=[&quot;happy&quot;, &quot;neutral&quot;, &quot;sad&quot;])<br>    aggressiveness: int = Field(<br>        ...,<br>        description=&quot;describes how aggressive the statement is, the higher the number the more aggressive&quot;,<br>        enum=[1, 2, 3, 4, 5],<br>    )<br>    language: str = Field(<br>        ..., enum=[&quot;spanish&quot;, &quot;english&quot;, &quot;french&quot;, &quot;german&quot;, &quot;italian&quot;]<br>    )</pre><p><strong>Create the Tagging Chain</strong></p><pre>chain = create_tagging_chain_pydantic(Tags, llm)</pre><p><strong>Test the Tagging Chain</strong></p><pre>input = &quot;Estoy increiblemente contento de haberte conocido! Creo que seremos muy buenos amigos!&quot;<br>result = chain.run(input)<br>result</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/423/1*jak43OYVNAO0km9Gvyb23A.png" /></figure><p>We observe that we get the same tags as before.</p><h3>Agent Code — Tagging a Corpus of Documents</h3><p>It can often be useful to tag a corpus of documents with structured metadata, such as the title, tone, or length of a document, to allow for more targeted similarity search later.</p><p>This document transformer automates this process by extracting metadata from each document according to a provided schema and adding it to the metadata held within the LangChain Document object.</p><p>The document transformer works best with complete documents, so it’s best to run it first with whole documents before doing any other splitting or processing. This will of course be constrained by the length of the context window of the model being used.</p><h4>Configuration</h4><pre># insert your API key here<br>openai_api_key = &quot;&lt;&gt;&quot;<br><br>MODEL = &quot;gpt-3.5-turbo-0613&quot;</pre><h4>Import packages</h4><pre>from langchain.schema import Document<br>from langchain.chat_models import ChatOpenAI<br>from langchain.document_transformers.openai_functions import create_metadata_tagger<br><br>import json<br><br># initialize the document transformer with a Pydantic schema<br>from typing import Literal<br>from pydantic import BaseModel, Field<br><br># Customer prompting<br>from langchain.prompts import ChatPromptTemplate</pre><h4>Transform Documents Using a Json Schema</h4><p><strong>Instantiate the LLM</strong></p><pre>llm = ChatOpenAI(temperature=0, model=MODEL, openai_api_key=openai_api_key)</pre><p><strong>Define the Schema</strong></p><p>The schema is defined in the same way as we defined the schema for the individual document tagging service.</p><pre>schema = {<br>    &quot;properties&quot;: {<br>        &quot;movie_title&quot;: {&quot;type&quot;: &quot;string&quot;},<br>        &quot;critic&quot;: {&quot;type&quot;: &quot;string&quot;},<br>        &quot;tone&quot;: {&quot;type&quot;: &quot;string&quot;, &quot;enum&quot;: [&quot;positive&quot;, &quot;negative&quot;]},<br>        &quot;rating&quot;: {<br>            &quot;type&quot;: &quot;integer&quot;,<br>            &quot;description&quot;: &quot;The number of stars the critic rated the movie&quot;,<br>        },<br>    },<br>    &quot;required&quot;: [&quot;movie_title&quot;, &quot;critic&quot;, &quot;tone&quot;],<br>}</pre><p><strong>Create the Document Transformer</strong></p><pre>document_transformer = create_metadata_tagger(metadata_schema=schema, llm=llm)</pre><p><strong>Test the Document Transformer</strong></p><p>We pass the document transformer a list of documents, and it will extract metadata from the body of the text of each document. We use the LangChain Document object to store the document content and associated meta data.</p><pre>original_documents = [<br>    Document(<br>        page_content=&quot;Review of The Bee Movie\nBy Roger Ebert\n\nThis is the greatest movie ever made. 4 out of 5 stars.&quot;<br>    ),<br>    Document(<br>        page_content=&quot;Review of The Godfather\nBy Anonymous\n\nThis movie was super boring. 1 out of 5 stars.&quot;,<br>        metadata={&quot;reliable&quot;: False},<br>    ),<br>]</pre><p>We run the process to add the meta tags to the original document and print the results.</p><pre>enhanced_documents = document_transformer.transform_documents(original_documents)<br><br>print(<br>    *[d.page_content + &quot;\n\n&quot; + json.dumps(d.metadata) for d in enhanced_documents],<br>    sep=&quot;\n\n---------------\n\n&quot;<br>)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/741/1*1DHHX7chICIbo7-fue7TXg.png" /></figure><p>We observe that the documents now have appropriate meta data tagged to them.</p><h4>Transform Documents Using a Pydantic Schema</h4><p>As for the individual document tagging service we can replicate the document transformer using a Pydantic schema.</p><p><strong>Define the Python Class</strong></p><pre>class Tags(BaseModel):<br>    movie_title: str<br>    critic: str<br>    tone: Literal[&quot;positive&quot;, &quot;negative&quot;]<br>    rating: int = Field(description=&quot;Rating out of 5 stars&quot;)</pre><p><strong>Create the Document Transformer</strong></p><pre>document_transformer = create_metadata_tagger(Tags, llm)</pre><p><strong>Test the Document Transformer</strong></p><pre>enhanced_documents = document_transformer.transform_documents(original_documents)<br><br>print(<br>    *[d.page_content + &quot;\n\n&quot; + json.dumps(d.metadata) for d in enhanced_documents],<br>    sep=&quot;\n\n---------------\n\n&quot;<br>)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/745/1*IMn61tAIGCmQo-TB-daFcw.png" /></figure><p>We get the same results as when using the json schema.</p><h4>Customising the Document Transformer</h4><p>If you wanted to ask the LLM to focus on specific details in the input documents, or extract meta data in a certain style, you could pass in a custom prompt:</p><p><strong>Define the Custom Prompt</strong></p><pre>prompt = ChatPromptTemplate.from_template(<br>&quot;&quot;&quot;Extract relevant information from the following text.<br>Anonymous critics are actually Roger Ebert.<br><br>{input}<br>&quot;&quot;&quot;<br>)</pre><p><strong>Create the Document Transformer</strong></p><p>We add the custom prompt to the document transformer.</p><pre>document_transformer = create_metadata_tagger(Tags, llm, prompt=prompt)</pre><p><strong>Test the Document Transformer</strong></p><pre>enhanced_documents = document_transformer.transform_documents(original_documents)<br><br>print(<br>    *[d.page_content + &quot;\n\n&quot; + json.dumps(d.metadata) for d in enhanced_documents],<br>    sep=&quot;\n\n---------------\n\n&quot;<br>)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/746/1*Zx1VdjA90C_QSu4cZkv4Ew.png" /></figure><p>We observe that the LLM service has followed the instructions set out in the custom prompt.</p><h3>Conclusion</h3><p>Document tagging services such as this have a wide range of uses. For example they can be used to improve the performance of similarity searches in a question answering service or they can be used to group documents when performing analytics — e.g. analysing e-mail traffic into a contact centre.</p><p>In practice, where plenty of historic labelled data is available a more traditional classification approach may be more suitable as it won’t incur LLM processing costs. Where you don’t have labelled data because no process was put in place to capture the data or because you want to use a qualitative classes that would have been difficult to create consistent labels for (e.g. aggressiveness), this type of approach could be a winner.</p><p>Have fun working on your own implementations:)</p><h3>References</h3><ul><li><a href="https://python.langchain.com/docs/use_cases/tagging">https://python.langchain.com/docs/use_cases/tagging</a></li><li><a href="https://python.langchain.com/docs/integrations/document_transformers/openai_metadata_tagger">https://python.langchain.com/docs/integrations/document_transformers/openai_metadata_tagger</a></li></ul><h3>Want to get in touch?</h3><p>Follow on Medium and <a href="https://www.linkedin.com/in/lawrence-knight-11331a18/">Linkedin</a> for summaries of papers at the cutting edge of machine learning and intelligence plus more general thoughts on how research is progressing in the field.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5b93cca8dbca" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/document-tagging-with-langchain-5b93cca8dbca">Document Tagging with LangChain</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Build your First SQL Database Agent with LangChain]]></title>
            <link>https://medium.com/@LawrencewleKnight/build-your-first-sql-database-agent-with-langchain-19af8064ae18?source=rss-dcb0e94d5900------2</link>
            <guid isPermaLink="false">https://medium.com/p/19af8064ae18</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[large-language-models]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[langchain]]></category>
            <dc:creator><![CDATA[Lawrence Knight]]></dc:creator>
            <pubDate>Mon, 21 Aug 2023 10:26:35 GMT</pubDate>
            <atom:updated>2023-08-21T10:26:35.042Z</atom:updated>
            <content:encoded><![CDATA[<p>A step-by-step guide to building a LangChain enabled SQL database question answering agent.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*71lI66X4-4nxkKWVhrTW_A.png" /></figure><h3>Table of Contents</h3><ol><li>Introduction</li><li>Useful Resources</li><li>Agent Code<br>- Configuration<br>- Import Packages<br>- Instantiate the Database<br>- Instantiate the LLM<br>- Create the Agent<br>- Test the Agent</li><li>Conclusion</li></ol><h3>Introduction</h3><p>In this tutorial, we will walk through step-by-step, the creation of a LangChain enabled, large language model (LLM) driven, agent that can use a SQL database to answer questions. In practice, this functionality has the potential to be incredibly usefully. If we take customer servicing as an example, most organisations hold their customer data in SQL databases. Developing automated customer servicing agents that allow customers to query and update their data presents an opportunity to significantly reduce customer servicing costs.</p><p>There are a number of different way to create SQL question answering services using LangChain. In this example we will be using LangChain’s SQL Agent. Two fo the benefits of the SQL Agent over other approaches are:</p><ol><li>The ability to answer questions based on the databases’ schema as well as on the databases’ content (like describing a specific table).</li><li>The ability to recover from errors by running a generated query, catching the traceback and regenerating it correctly.</li></ol><h3>Useful Resources</h3><p>The code in this tutorial draws heavily from the LangChain documentation, links to which are provided below.</p><ul><li><a href="https://python.langchain.com/docs/use_cases/sql">https://python.langchain.com/docs/use_cases/sql</a></li><li><a href="https://python.langchain.com/docs/integrations/toolkits/sql_database">https://python.langchain.com/docs/integrations/toolkits/sql_database</a></li></ul><h3>Agent Code</h3><p>The code in this notebook is complete and if copied into a notebook will produce a working agent.</p><h4>Configuration</h4><p>To run this code you will need an OpenAI account with a paid for subscription to access their large language model APIs.</p><pre># insert your API key here<br>openai_api_key = &quot;&lt;&gt;&quot;</pre><h4>Import Packages</h4><pre>from langchain.utilities import SQLDatabase<br>from langchain.llms import OpenAI<br>from langchain.agents import create_sql_agent<br>from langchain.agents.agent_toolkits import SQLDatabaseToolkit<br>from langchain.agents.agent_types import AgentType</pre><h4>Instantiate the Database</h4><p>Before we can instantiate the database we need to go to the SQLite website and download the Chinnock sample database. You can download it <a href="https://www.sqlitetutorial.net/sqlite-sample-database/">here</a>. Once download, save it to the same directory as your notebook.</p><pre>db = SQLDatabase.from_uri(&quot;sqlite:///chinook.db&quot;)</pre><pre># check that the database has been instantiated correctly<br><br>db.get_usable_table_names()</pre><p>You should see a list of table names.</p><h4>Instantiate the LLM</h4><pre>llm = OpenAI(temperature=0, verbose=True, openai_api_key=openai_api_key)</pre><h4>Create the Agent</h4><p>The agent uses the ReACT prompting technique, introduced in the paper “<a href="https://arxiv.org/pdf/2210.03629.pdf">REACT: Synergizing Reasoning and Acting in Language Models</a>”, by a team of researchers from Google Research and Princeton University. ReACT combines Chain-of-Thought reasoning and action plan generation integrating the use of tools into the reasoning process. If you don’t have time to read the whole paper you can find a summary <a href="https://medium.com/@LawrencewleKnight/react-combining-reasoning-and-acting-in-language-models-for-smarter-ai-66d844c1b499">here</a>.</p><pre>agent_executor = create_sql_agent(<br>    llm=llm,<br>    toolkit=SQLDatabaseToolkit(db=db, llm=llm),<br>    verbose=True,<br>    agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,<br>)</pre><h4>Test the Agent</h4><p><strong>Example 1 — Running Queries</strong></p><p>In this example we demonstrate how the agent can query a database to answer questions.</p><pre>agent_executor.run(<br>    &quot;How many employees are there?&quot;<br>)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/792/1*g6jEe8dMvJG67wReFSV7tg.png" /></figure><p>We can observe the cycle of thought, action, observation that the agent goes through to get the answer. The prompt that illicites this human like reasoning is simple and is given below. I find it incredible that through this simple direction complex reasoning behaviour emerges.</p><pre>FORMAT_INSTRUCTIONS = &quot;&quot;&quot;<br>Use the following format:<br>Question: the input question you must answer<br>Thought: you should always think about what to do<br>Action: the action to take, should be one of [{tool_names}]<br>Action Input: the input to the action<br>Observation: the result of the action<br>... (this Thought/Action/Action Input/Observation can repeat N times)<br>Thought: I now know the final answer<br>Final Answer: the final answer to the original input question&quot;&quot;&quot;</pre><p><strong>Example 2— Describing a Table</strong></p><p>In this example we demonstrate how the agent can describe a table in the database.</p><pre>agent_executor.run(&quot;Describe the playlisttrack table&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/793/1*sxfrARhMsNV8pYXAaczXqQ.png" /></figure><p><strong>Example 3— Recovering from an Error</strong></p><p>In this example, the agent is able to recover from an error after initially trying to access an attribute that doesn’t exist.</p><pre>agent_executor.run(&quot;Who are the top 3 best selling artists?&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/794/1*ZXeNCiy5WMCaa6CxzIQTNw.png" /></figure><h3>Conclusion</h3><p>This technology is exceptionally powerful! Personally I love to inspect the chains of reasoning and action and marvel at where this type of technology might take us. In the immediate present this type of functionality has much potential in the automation of business processes. Based on my experimentation with this tech the reliablity of this approach begins to break down when:</p><ol><li>the database structure is complex;</li><li>table names are ambiguous;</li><li>the questions asked can not be answered through a single simple query.</li></ol><p>This can be managed by investing time in adding additional information to the prompt, such as:</p><ol><li>provide a database description for your specific use case.</li><li>provide descriptions of fields in the db.</li><li>hardcoding a few examples of questions and their corresponding SQL queries in the prompt.</li></ol><p>Have fun experimenting with your own usecases:)</p><h3>References</h3><ul><li><a href="https://python.langchain.com/docs/use_cases/sql">https://python.langchain.com/docs/use_cases/sql</a></li><li><a href="https://python.langchain.com/docs/integrations/toolkits/sql_database">https://python.langchain.com/docs/integrations/toolkits/sql_database</a></li></ul><h3>Want to get in touch?</h3><p>Follow on Medium and <a href="https://www.linkedin.com/in/lawrence-knight-11331a18/">Linkedin</a> for summaries of papers at the cutting edge of machine learning and intelligence plus more general thoughts on how research is progressing in the field.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=19af8064ae18" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Build your First Conversational Document Retrieval Agent using LangChain]]></title>
            <link>https://blog.devgenius.io/build-your-first-conversational-document-retrieval-agent-using-langchain-81508071c4bc?source=rss-dcb0e94d5900------2</link>
            <guid isPermaLink="false">https://medium.com/p/81508071c4bc</guid>
            <category><![CDATA[artificial-general-intell]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[large-language-models]]></category>
            <dc:creator><![CDATA[Lawrence Knight]]></dc:creator>
            <pubDate>Fri, 18 Aug 2023 13:57:50 GMT</pubDate>
            <atom:updated>2023-09-23T08:54:43.271Z</atom:updated>
            <content:encoded><![CDATA[<p>A step-by-step guide to building a LangChain enabled conversational document retrieval agent.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SuS0D_-uSf5RnLuUlqgytw.png" /><figcaption>Figure 1: LangChain Documentation</figcaption></figure><h3>Table of Contents</h3><ol><li>Introduction</li><li>Useful Resources</li><li>Agent Code<br>- Configuration<br>- Import Packages<br>- The Retriever<br>- The Retriever Tool<br>- The Memory<br>- The Prompt Template<br>- The Agent<br>- The Agent Executor</li><li>Inference</li><li>Conclusion</li></ol><h3>Introduction</h3><p>In this article we will walk through step-by-step a coded example of creating a simple conversational document retrieval agent using LangChain, the pre-eminent package for developing large language model based applications.</p><h3>Useful Resources</h3><p>The code in this tutorial draws heavily from the LangChain documentation, links to which are provided below. The third link below is a useful example on this subject provided by OpenAI.</p><ul><li><a href="https://python.langchain.com/docs/modules/agents/">https://python.langchain.com/docs/modules/agents/</a></li><li><a href="https://python.langchain.com/docs/use_cases/question_answering/how_to/conversational_retrieval_agents">https://python.langchain.com/docs/use_cases/question_answering/how_to/conversational_retrieval_agents</a></li><li><a href="https://github.com/openai/openai-cookbook/blob/main/examples/Question_answering_using_embeddings.ipynb">https://github.com/openai/openai-cookbook/blob/main/examples/Question_answering_using_embeddings.ipynb</a></li></ul><h3>Agent Code</h3><p>The code in this notebook is complete and if copied into a notebook will produce a working agent.</p><h4>Configuration</h4><p>To run this code you will need an OpenAI account with a paid for subscription to access their large language model APIs.</p><pre># insert your API key here<br>openai_api_key = &quot;&lt;&gt;&quot;<br><br># This is needed for both the memory and the prompt<br>memory_key = &quot;history&quot;</pre><h4>Import Packages</h4><pre># used to load text<br>from langchain.document_loaders import WebBaseLoader<br><br># used to create the retriever<br>from langchain.text_splitter import CharacterTextSplitter<br>from langchain.vectorstores import FAISS<br>from langchain.embeddings import OpenAIEmbeddings<br><br># used to create the retrieval tool<br>from langchain.agents import tool<br><br># used to create the memory<br>from langchain.memory import ConversationBufferMemory<br><br># used to create the prompt template<br>from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent<br>from langchain.schema import SystemMessage<br>from langchain.prompts import MessagesPlaceholder<br><br># used to create the agent executor<br>from langchain.chat_models import ChatOpenAI<br>from langchain.agents import AgentExecutor</pre><h4>The Retriever</h4><p>Here we use the LangChain web base loader to load a blog post from github, which we will use as our knowledge base. Langchain has many types of loader available, each for different document types such as .txt files or .pdf files. You can explore the different types of loaders <a href="https://python.langchain.com/docs/modules/data_connection/document_loaders/">here</a>.</p><pre>loader = WebBaseLoader(&quot;https://lilianweng.github.io/posts/2023-06-23-agent/&quot;)<br>data = loader.load()</pre><p>The text splitting process is known as chunking and involves splitting a large text in to many smaller parts. Here we split the text into parts that are 1,000 tokens long. We also set the overlap parameter to 200, which will mean two adjacent chunks will share 200 tokens. There are many possible ways to do this in practice, so you can experiment for your particular use case. You can explore the various methods for splitting documents <a href="https://python.langchain.com/docs/modules/data_connection/document_transformers/">here</a>. If you are splitting large documents into parts a specification such as this should work well. If your documents contain tables and charts you will need to give some thought as to how best to handle this, that complication is not covered here.</p><pre>text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=200)<br>texts = text_splitter.split_documents(data)</pre><p>The document chunks are converted to embeddings using a regular language model such as BERT. An embedding is simply a vector — typically of length 512 — that encodes the semantics of the text. When a question is asked of the question answering service the question is encoded into an embedding using the same langauge model, then a similarity search — using a method such as cosine similarity — is executed to find text chunks in the knowledge base with similar sematics to the question. The returned text chunks are then used by the large language model to provide a response to the question.</p><pre>embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)</pre><p>The text chunk and embedding pairs are added to a database that can quickly be searched.</p><pre>db = FAISS.from_documents(texts, embeddings)</pre><p>Instantiate the retriever.</p><pre>retriever = db.as_retriever()</pre><h4>The Retriever Tool</h4><p>Now we need to wrap our retriever in a tool. Tools are a LangChain construct that can be passed to agents to use to increase their functionality. In this case the tool is a vector store knowledge base but it could equally be a connection to a database, or web search functionality, or indeed a whole suite of different tools.</p><pre>@tool<br>def tool(query):<br>    &quot;Searches and returns documents regarding the llm powered autonomous agents blog&quot;<br>    docs = retriever.get_relevant_documents(query)<br>    return docs<br><br>tools = [tool]</pre><h4>The Memory</h4><p>Memory is added to an agent to take it from being stateless to stateful i.e. it remembers previous interactions. This means you can ask follow-up questions, making interaction with the agent much more efficient and feel more natural.</p><p>There are a number of different memory configurations, you can explore them <a href="https://python.langchain.com/docs/modules/memory/">here</a>.</p><pre>memory = ConversationBufferMemory(memory_key=memory_key, return_messages=True)</pre><h4>The Prompt Template</h4><p>The prompt template includes general instructions as to how the agent should behave, as well as adding the conversation history to the prompt extracted from the memory.</p><pre>system_message = SystemMessage(<br>        content=(<br>            &quot;Do your best to answer the questions. &quot;<br>            &quot;Feel free to use any tools available to look up &quot;<br>            &quot;relevant information, only if neccessary&quot;<br>        )<br>)</pre><pre>prompt = OpenAIFunctionsAgent.create_prompt(<br>        system_message=system_message,<br>        extra_prompt_messages=[MessagesPlaceholder(variable_name=memory_key)]<br>    )</pre><h4>The Agent</h4><p>Instantiate the agent.</p><pre>llm = ChatOpenAI(temperature = 0, openai_api_key=openai_api_key)<br><br>agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt)</pre><h4>The Agent Executor</h4><p>This is where everything is pulled together, the agent, its tools and the memory.</p><pre>agent_executor = AgentExecutor(agent=agent, tools=tools, memory=memory, verbose=True)</pre><h3>Inference</h3><p>We will now test how the agent responds to questions.</p><p>First, we introduce ourselves to the agent.</p><pre>result = agent_executor({&quot;input&quot;: &quot;hi, im Lawrence&quot;})<br><br>result[&quot;output&quot;]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/315/1*ZH2MG-vPGrUMshe_LkpE0g.png" /></figure><p>The response received is generic, but encourages further interaction. Next, we test the agents memory.</p><pre>result = agent_executor({&quot;input&quot;: &quot;whats my name&quot;})<br><br>result[&quot;output&quot;]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/170/1*8Y-vfHM3g1h-smXIb9EC3g.png" /></figure><p>We see that the agent can remember details from previous interactions and incorporate these into its answers. Next, let’s see if the agent can answer technical questions using the knowledge base retrieval tool we gave it access to.</p><pre>result = agent_executor({&quot;input&quot;: &quot;How do agents use task decomposition?&quot;})<br><br>result[&quot;output&quot;]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/784/1*O9UTVazI6f2QpE3mheFH1Q.png" /></figure><p>We see that the agent is indeed able to provide a meaningful answer. The next question tests both the ability of the agent to use both its memory and the knowledge base retrieval tool. The agent must recognise that the “it” in this question is the “task decomposition” referred to in the previous question.</p><pre>result = agent_executor({&quot;input&quot;: &quot;What are the various ways to implement memory to support it?&quot;})<br><br>result[&quot;output&quot;]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/786/1*Yr5kbG9uMvxjdvwi8sAj6A.png" /></figure><p>We see that the agent does a good job of this!</p><h3>Conclusion</h3><p>Question answering services are a powerful way to make existing text data assets more accessible. My experience of using this technology is that the answers returned are approriate and that this technology is eminently usable for commercial purposes.</p><h4>References</h4><ul><li><a href="https://python.langchain.com/docs/modules/agents/">https://python.langchain.com/docs/modules/agents/</a></li><li><a href="https://python.langchain.com/docs/use_cases/question_answering/how_to/conversational_retrieval_agents">https://python.langchain.com/docs/use_cases/question_answering/how_to/conversational_retrieval_agents</a></li><li><a href="https://python.langchain.com/docs/use_cases/chatbots">https://python.langchain.com/docs/use_cases/chatbots</a></li></ul><h3>Want to get in touch?</h3><p>Follow on Medium and <a href="https://www.linkedin.com/in/lawrence-knight-11331a18/">Linkedin</a> for summaries of papers at the cutting edge of machine learning and intelligence plus more general thoughts on how research is progressing in the field.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=81508071c4bc" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/build-your-first-conversational-document-retrieval-agent-using-langchain-81508071c4bc">Build your First Conversational Document Retrieval Agent using LangChain</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tree-of-Thoughts Implementation to Solve the Game of 24]]></title>
            <link>https://blog.devgenius.io/tree-of-thoughts-implementation-to-solve-the-game-of-24-13fb1fa2034b?source=rss-dcb0e94d5900------2</link>
            <guid isPermaLink="false">https://medium.com/p/13fb1fa2034b</guid>
            <category><![CDATA[large-language-models]]></category>
            <category><![CDATA[artificial-general-intell]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <dc:creator><![CDATA[Lawrence Knight]]></dc:creator>
            <pubDate>Tue, 15 Aug 2023 07:40:36 GMT</pubDate>
            <atom:updated>2023-09-12T17:56:22.484Z</atom:updated>
            <content:encoded><![CDATA[<p>Learn to use cutting edge large language modelling techniques to solve numerical reasoning problems.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4MQ63X4yb6aEAibxkBLi0Q.png" /><figcaption>Generated by DALLE 2</figcaption></figure><h4>Table of Contents</h4><ol><li>Introduction</li><li>Useful Resources</li><li>The Game of 24</li><li>Tree-of-Throughts Code<br>- Import Packages<br>- Configuration<br>- Prompts<br>- Classes and Functions<br>- Inference</li><li>Solving the Puzzle</li><li>Conclusion</li><li>References</li></ol><h3><strong>Introduction</strong></h3><p>In this tutorial I provide a coded example showing how the Tree-of-Thoughts (ToT) prompting framework can be used to solve the game of 24. The ToT framework was proposed by researchers from Princeton University and Google DeepMind, in their 2023 paper “<a href="https://arxiv.org/pdf/2305.10601.pdf">Tree of Thoughts: Deliberate Problem Solving with Large Language Models</a>”. At the time of writing the ToT framework is a cutting edge technique for eliciting reasoning from langauge models. If you don’t have time to read the full paper you can find a short summary <a href="https://medium.com/@LawrencewleKnight/enhancing-language-models-with-thought-trees-a6672bbb9255">here</a>.</p><p>The code used in this tutorial draws heavily from the code made publically available by the research team who developed ToT (find a link below).</p><h3>Useful Resources</h3><p>The ToT framework is so new that there aren’t yet many resources to draw from when building applications that leverage this technology. Resources that I have found useful are:</p><ol><li>The github repo made available by the reseach team who proposed the ToT framework can be found <a href="https://github.com/princeton-nlp/tree-of-thought-llm">here</a>.</li><li>LangChain docs which give a worked example of an agent that can solve the game of Sudoko can be found <a href="https://python.langchain.com/docs/use_cases/graph/tot">here</a>.</li></ol><h3>The game of 24</h3><p>The game of 24 is simple, players are given four integer numbers and they must use these numbers and basic arithmetic operations (+ — * /) to obtain 24.</p><p>An example of a single puzzle and its solution is given below.</p><p>Input: 4 5 6 10<br>10–4 = 6 (left: 5 6 6)<br>5 * 6 = 30 (left: 6 30)<br>30–6 = 24 (left: 24)<br>Answer: (5 * (10–4)) — 6 = 24</p><p>The game of 24 is used as a benchmark against which reasoning agents can be compared.</p><h3>Tree-of-Thoughts Implementation</h3><p>In this section we will go through an implementation of the ToTs framework to solve the game of 24. This code is complete and if copied into a notebook will produce a working ToT agent.</p><h4>Import Packages</h4><pre>import openai<br>import argparse<br>from functools import partial<br>import itertools<br>import backoff</pre><h4>Configuration</h4><p>To run this code you will need an OpenAI account with a paid for subscription to access their large language model APIs. This code is configured to run using gpt-4 but if you don’t have access to this model another model can be substituted in.</p><pre># insert your API key here<br>openai.api_key = &quot;&lt;&gt;&quot;<br><br># lists used to track the number of tokens being processed by the agent<br>completion_tokens = prompt_tokens = 0<br><br>PROBLEM = &quot;4 5 6 10&quot;<br>BACKEND = &#39;gpt-4&#39;<br>TEMPERATURE = 0.7<br>N_GENERATE_SAMPLE = 1<br>N_EVALUATE_SAMPLE = 3<br>N_SELECT_SAMPLE = 5</pre><h4>Prompts</h4><p>Three prompts are used, each to elicit a different behaviour at relevant steps of the process.</p><p>These prompts are examples of few-shot prompting, an in-context learning technique where a demonstration is provided in the prompt to steer the model to better performance. A link to the research paper where this technique was proposed can be found <a href="https://arxiv.org/abs/2005.14165">here</a>.</p><p>The propose_prompt is used when the model is generating proposals for the first calculation step to solve the puzzle.</p><pre>propose_prompt = &#39;&#39;&#39;<br>Input: 2 8 8 14<br>Possible next steps:<br>2 + 8 = 10 (left: 8 10 14)<br>8 / 2 = 4 (left: 4 8 14)<br>14 + 2 = 16 (left: 8 8 16)<br>2 * 8 = 16 (left: 8 14 16)<br>8 - 2 = 6 (left: 6 8 14)<br>14 - 8 = 6 (left: 2 6 8)<br>14 /  2 = 7 (left: 7 8 8)<br>14 - 2 = 12 (left: 8 8 12)<br>Input: {input}<br>Possible next steps:<br>&#39;&#39;&#39;</pre><p>The value_prompt is used to self-evaluate the intermediate solution proposals.</p><pre>value_prompt = &#39;&#39;&#39;Evaluate if given numbers can reach 24 (sure/likely/impossible)<br>10 14<br>10 + 14 = 24<br>sure<br>11 12<br>11 + 12 = 23<br>12 - 11 = 1<br>11 * 12 = 132<br>11 / 12 = 0.91<br>impossible<br>4 4 10<br>4 + 4 + 10 = 8 + 10 = 18<br>4 * 10 - 4 = 40 - 4 = 36<br>(10 - 4) * 4 = 6 * 4 = 24<br>sure<br>4 9 11<br>9 + 11 + 4 = 20 + 4 = 24<br>sure<br>5 7 8<br>5 + 7 + 8 = 12 + 8 = 20<br>(8 - 5) * 7 = 3 * 7 = 21<br>I cannot obtain 24 now, but numbers are within a reasonable range<br>likely<br>5 6 6<br>5 + 6 + 6 = 17<br>(6 - 5) * 6 = 1 * 6 = 6<br>I cannot obtain 24 now, but numbers are within a reasonable range<br>likely<br>10 10 11<br>10 + 10 + 11 = 31<br>(11 - 10) * 10 = 10<br>10 10 10 are all too big<br>impossible<br>1 3 3<br>1 * 3 * 3 = 9<br>(1 + 3) * 3 = 12<br>1 3 3 are all too small<br>impossible<br>{input}<br>&#39;&#39;&#39;</pre><p>The value_last_step_prompt is used when the model is being prompted to evaluate its own suggested solutions.</p><pre>value_last_step_prompt = &#39;&#39;&#39;<br>Use numbers and basic arithmetic operations (+ - * /) to obtain 24. <br>Given an input and an answer, give a judgement (sure/impossible) if <br>the answer is correct, i.e. it uses each input exactly once and no <br>other numbers, and reach 24.<br>Input: 4 4 6 8<br>Answer: (4 + 8) * (6 - 4) = 24<br>Judge: <br>sure<br>Input: 2 9 10 12<br>Answer: 2 * 12 * (10 - 9) = 24<br>Judge: <br>sure<br>Input: 4 9 10 13<br>Answer: (13 - 9) * (10 - 4) = 24<br>Judge: <br>sure<br>Input: 4 4 6 8<br>Answer: (4 + 8) * (6 - 4) + 1 = 25<br>Judge: <br>impossible<br>Input: 2 9 10 12<br>Answer: 2 * (12 - 10) = 24<br>Judge: <br>impossible<br>Input: 4 9 10 13<br>Answer: (13 - 4) * (10 - 9) = 24<br>Judge: <br>impossible<br>Input: {input}<br>Answer: {answer}<br>Judge:&#39;&#39;&#39;</pre><p>The cot_prompt is used to generate a final step-by-step solution in the desired format once the puzzle has been solved by the ToT method.</p><pre>cot_prompt = &#39;&#39;&#39;<br>Use numbers and basic arithmetic operations (+ - * /) to obtain 24. <br>Each step, you are only allowed to choose two of the remaining numbers <br>to obtain a new number.<br>Input: 4 4 6 8<br>Steps:<br>4 + 8 = 12 (left: 4 6 12)<br>6 - 4 = 2 (left: 2 12)<br>2 * 12 = 24 (left: 24)<br>Answer: (6 - 4) * (4 + 8) = 24<br>Input: 2 9 10 12<br>Steps:<br>12 * 2 = 24 (left: 9 10 24)<br>10 - 9 = 1 (left: 1 24)<br>24 * 1 = 24 (left: 24)<br>Answer: (12 * 2) * (10 - 9) = 24<br>Input: 4 9 10 13<br>Steps:<br>13 - 10 = 3 (left: 3 4 9)<br>9 - 3 = 6 (left: 4 6)<br>4 * 6 = 24 (left: 24)<br>Answer: 4 * (9 - (13 - 10)) = 24<br>Input: 1 4 8 8<br>Steps:<br>8 / 4 = 2 (left: 1 2 8)<br>1 + 2 = 3 (left: 3 8)<br>3 * 8 = 24 (left: 24)<br>Answer: (1 + 8 / 4) * 8 = 24<br>Input: 5 5 5 9<br>Steps:<br>5 + 5 = 10 (left: 5 9 10)<br>10 + 5 = 15 (left: 9 15)<br>15 + 9 = 24 (left: 24)<br>Answer: ((5 + 5) + 5) + 9 = 24<br>Input: {input}<br>&#39;&#39;&#39;</pre><h4>Classes and Functions</h4><p>The get_proposals function is used to generate a set of proposals for the first step in solving the puzzle.</p><pre>def get_proposals(task, x, y):<br>    propose_prompt = task.propose_prompt_wrap(x, y)<br>    proposals = gpt(propose_prompt, n=1, stop=None)[0].split(&#39;\n&#39;)<br>    return [y + _ + &#39;\n&#39; for _ in proposals]</pre><p>The get_values and get_value functions are used by the model as part of the self-evaluation function, where by the model evaluates the partial solutions that it has generated.</p><pre>def get_values(task, x, ys, n_evaluate_sample, cache_value=True):<br>    values = []<br>    local_value_cache = {}<br>    for y in ys:  # each partial output<br>        if y in local_value_cache:  # avoid duplicate candidates<br>            value = 0<br>        else:    <br>            value = get_value(task, x, y, n_evaluate_sample, cache_value=cache_value)<br>            local_value_cache[y] = value<br>        values.append(value)<br>    return values</pre><pre>def get_value(task, x, y, n_evaluate_sample, cache_value=True):<br>    value_prompt = task.value_prompt_wrap(x, y)<br>    if cache_value and value_prompt in task.value_cache:<br>        return task.value_cache[value_prompt]<br>    value_outputs = gpt(value_prompt, n=n_evaluate_sample, stop=None)<br>    value = task.value_outputs_unwrap(x, y, value_outputs)<br>    if cache_value:<br>        task.value_cache[value_prompt] = value<br>    return value</pre><p>Helper functions</p><pre>def get_current_numbers(y: str) -&gt; str:<br>    last_line = y.strip().split(&#39;\n&#39;)[-1]<br>    return last_line.split(&#39;left: &#39;)[-1].split(&#39;)&#39;)[0]</pre><pre>def gpt(prompt, model=BACKEND, temperature=TEMPERATURE, max_tokens=1000, n=1, stop=None) -&gt; list:<br>    messages = [{&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: prompt}]<br>    return chatgpt(messages, model=model, temperature=temperature, max_tokens=max_tokens, n=n, stop=stop)</pre><pre>def chatgpt(messages, model=BACKEND, temperature=TEMPERATURE, max_tokens=1000, n=1, stop=None) -&gt; list:<br>    global completion_tokens, prompt_tokens<br>    outputs = []<br>    while n &gt; 0:<br>        cnt = min(n, 20)<br>        n -= cnt<br>        res = completions_with_backoff(model=model, messages=messages, temperature=temperature, max_tokens=max_tokens, n=cnt, stop=stop)<br>        outputs.extend([choice[&quot;message&quot;][&quot;content&quot;] for choice in res[&quot;choices&quot;]])<br>        # log completion tokens<br>        completion_tokens += res[&quot;usage&quot;][&quot;completion_tokens&quot;]<br>        prompt_tokens += res[&quot;usage&quot;][&quot;prompt_tokens&quot;]<br>    return outputs</pre><pre>@backoff.on_exception(backoff.expo, openai.error.OpenAIError)<br>def completions_with_backoff(**kwargs):<br>    return openai.ChatCompletion.create(**kwargs)</pre><p>Class that groups game specific functionality</p><pre>class Game24Task():<br>    &quot;&quot;&quot;<br>    Input (x)   : a string of 4 numbers<br>    Output (y)  : a trajectory of 3 steps to reach 24<br>    Reward (r)  : 0 or 1, depending on whether the trajectory is correct<br>    Input Example: <br>        1 2 3 4<br>    Output Example: <br>        1 + 2 = 3 (left: 3 3 4)<br>        3 + 3 = 6 (left: 4 6)<br>        6 * 4 = 24 (left: 24)<br>        (1 + 2 + 3) * 4 = 24<br>    &quot;&quot;&quot;<br><br>    def __init__(self):<br>        self.steps = 4<br>        self.value_cache = {}<br>    <br>    @staticmethod<br>    def propose_prompt_wrap(x: str, y: str=&#39;&#39;) -&gt; str:<br>        current_numbers = get_current_numbers(y if y else x)<br>        if current_numbers == &#39;24&#39;:<br>            prompt = cot_prompt.format(input=x) + &#39;Steps:&#39; + y<br>        else:<br>            prompt = propose_prompt.format(input=current_numbers)<br>        return prompt<br>    <br>    @staticmethod<br>    def value_prompt_wrap(x: str, y: str) -&gt; str:<br>        last_line = y.strip().split(&#39;\n&#39;)[-1]<br>        if &#39;left: &#39; not in last_line:  # last step<br>            ans = last_line.lower().replace(&#39;answer: &#39;, &#39;&#39;)<br>            return value_last_step_prompt.format(input=x, answer=ans)<br>        current_numbers = get_current_numbers(y)<br>        return value_prompt.format(input=current_numbers)<br>    <br>    @staticmethod<br>    def value_outputs_unwrap(x: str, y: str, value_outputs: list) -&gt; float:<br>        if len(y.strip().split(&#39;\n&#39;)) == 4 and &#39;answer&#39; not in y.lower():<br>            return 0<br>        value_names = [_.split(&#39;\n&#39;)[-1] for _ in value_outputs]<br>        value_map = {&#39;impossible&#39;: 0.001, &#39;likely&#39;: 1, &#39;sure&#39;: 20}<br>        value = sum(value * value_names.count(name) for name, value in value_map.items())<br>        return value</pre><p>Function that implements the ToT algorithm to solve a puzzle</p><pre>def solve(args, task, idx, to_print=True):<br>    global gpt<br>    gpt = partial(gpt, model=args.backend, temperature=args.temperature)<br>    print(gpt)<br>    x = args.problem<br>    ys = [&#39;&#39;]  # current output candidates<br>    infos = []<br>    for step in range(task.steps):<br>        # generation<br>        new_ys = [get_proposals(task, x, y) for y in ys]<br>        new_ys = list(itertools.chain(*new_ys))<br>        ids = list(range(len(new_ys)))<br>        print(new_ys)<br>        print(ids)<br><br>        # evaluation<br>        values = get_values(task, x, new_ys, args.n_evaluate_sample)<br>        print(values)<br>        <br>        # selection<br>        select_ids = sorted(ids, key=lambda x: values[x], reverse=True)[:args.n_select_sample]<br>        select_new_ys = [new_ys[select_id] for select_id in select_ids]<br>        print(select_new_ys)<br><br>        # log<br>        if to_print: <br>            sorted_new_ys, sorted_values = zip(*sorted(zip(new_ys, values), key=lambda x: x[1], reverse=True))<br>            print(f&#39;-- new_ys --: {sorted_new_ys}\n-- sol values --: {sorted_values}\n-- choices --: {select_new_ys}\n&#39;)<br>        <br>        infos.append({&#39;step&#39;: step, &#39;x&#39;: x, &#39;ys&#39;: ys, &#39;new_ys&#39;: new_ys, &#39;values&#39;: values, &#39;select_new_ys&#39;: select_new_ys})<br>        ys = select_new_ys<br>    <br>    if to_print: <br>        print(ys)<br>    return ys, {&#39;steps&#39;: infos}</pre><h4>Inference</h4><pre>args = argparse.Namespace(problem=PROBLEM, <br>                          backend=BACKEND, <br>                          temperature=TEMPERATURE,<br>                          n_generate_sample=N_GENERATE_SAMPLE,<br>                          n_evaluate_sample=N_EVALUATE_SAMPLE,<br>                          n_select_sample=N_SELECT_SAMPLE)<br><br>task = Game24Task()<br><br>ys, infos = solve(args, task, 900)<br><br>print(ys[0])</pre><h3>Solving the Puzzle</h3><p>In this section we will go through the steps the ToT frame goes through to solve the game of 24 puzzle.</p><p>The problem set in this case is that we are asking the agent to calculate the number 24 using the input set “4 5 6 10”.</p><p>There are a number of possible ways to solve this puzzle, one possible example is:</p><p>10–4 = 6 (left: 5 6 6)<br>5 * 6 = 30 (left: 6 30)<br>30–6 = 24 (left: 24)<br>Answer: (5 * (10–4)) — 6 = 24</p><p>The version of the ToT algorithm used to solve this puzzle has four steps and each step has a number of stages. Lets walk through the algorithm step by step:</p><p><strong>Step 1</strong></p><ol><li>Generate — The model first generates eight possible proposals for the first step in solving the puzzle e.g. 10–4 = 6 (left: 5 6 6).</li><li>Evaluate — The model then self-evaluates each of the proposed steps three times (set by parameter n_evaluate_sample) and sums the scores of each evalation. The evaluation is carried out three times as the result of each evaluation is non-deterministic and as such running the it several times reduces the overall evaluation variance. First the model is prompted to evaluate if the given numbers can reach 24 and assign a likelihood (sure/likely/impossible). The categories are then converted to numeric values {sure:20, likely:1, impossible:0.001}.</li><li>Select — The model then selects the best five proposals (set by parameter n_select_sample) based on their evaluation score. Each proposal forms one of the first nodes of the tree-of-thoughts.</li></ol><p><strong>Step 2</strong></p><ol><li>Generate — For each of the five nodes in the tree-of-thoughts the model generates eight possible proposals for the second step in solving the puzzle e.g. 5 * 6 = 30 (left: 6 30).</li><li>Evaluate — as before the model then self-evaluates each proposal.</li><li>Select - selects the best five proposals to form the second layer of the tree.</li></ol><p><strong>Step 3</strong></p><ol><li>Generate — For each of the selected five terminal nodes in the tree-of-thoughts the model generates eight possible proposals for the third and last step in solving the puzzle e.g. 30–6 = 24 (left: 24).</li><li>Evaluate — as before the model then self-evaluates each proposal.</li><li>Select-select the best five proposals from the second layer of the tree.</li></ol><p><strong>Step 4</strong></p><ol><li>Generate — in this step for each of the proposed solutions that has correctly solved the puzzle a formal answer is generated in the desired format using the cot_prompt prompt.</li></ol><h3>Conclusion</h3><p>The Tree-of-thoughts framework is a cutting edge framework for eliciting a form of reasoning from a large language model. It is useful for both quantitative and qualitative problems as large language models have the capability to self-evaluate the qualitative aspects of a proposal to a solution if prompted correctly. Underlying the ToT framework is an approach familiar to data scientists, that of averaging over many predictions to reduce the variance of the final prediction. This averaging over many predictions comes at a cost, the cost or processing the additional API calls.</p><p>I hope you enjoyed implementing your first tree of thoughts agent and that you find this useful as you go on to build your own applications.</p><h3>References</h3><ul><li><a href="https://github.com/princeton-nlp/tree-of-thought-llm/tree/master">https://github.com/princeton-nlp/tree-of-thought-llm/tree/master</a></li></ul><h3>Want to get in touch?</h3><p>Follow on Medium and <a href="https://www.linkedin.com/in/lawrence-knight-11331a18/">Linkedin</a> for summaries of papers at the cutting edge of machine learning and intelligence plus more general thoughts on how research is progressing in the field.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=13fb1fa2034b" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/tree-of-thoughts-implementation-to-solve-the-game-of-24-13fb1fa2034b">Tree-of-Thoughts Implementation to Solve the Game of 24</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>