<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Chip Huyen</title>
    <description>I work to bring AI into production. I write about AI system design.
</description>
    <link>https://huyenchip.com</link>
    <atom:link href="https://huyenchip.com/feed.xml" rel="self" type="application/rss+xml" />
    
      <item>
        <title>Common pitfalls when building generative AI applications</title>
        <description>&lt;p&gt;As we’re still in the early days of building applications with foundation models, it’s normal to make mistakes. This is a quick note with examples of some of the most common pitfalls that I’ve seen, both from public case studies and from my personal experience.&lt;/p&gt;

&lt;p&gt;Because these pitfalls are common, if you’ve worked on any AI product, you’ve probably seen them before.&lt;/p&gt;

&lt;h2 id=&quot;1_use_generative_ai_when_you_don_t_need_generative_ai&quot;&gt;1. Use generative AI when you don&apos;t need generative AI&lt;/h2&gt;

&lt;p&gt;Every time there’s a new technology, I can hear the collective sigh of senior engineers everywhere: “Not everything is a nail.” Generative AI isn’t an exception — its seemingly limitless capabilities only exacerbate the tendency to use generative AI for everything.&lt;/p&gt;

&lt;p&gt;A team pitched me the idea of using generative AI to optimize energy consumption. They fed a household’s list of energy-intensive activities and hourly electricity prices into an LLM, then asked it to create a schedule to minimize energy costs. Their experiments showed that this could help reduce a household’s electricity bill by 30%. Free money. Why wouldn’t anyone want to use their app?&lt;/p&gt;

&lt;p&gt;I asked: “How does it compare to simply scheduling the most energy-intensive activities when electricity is cheapest? Say, doing your laundry and charging your car after 10pm?”&lt;/p&gt;

&lt;p&gt;They said they would try it later and let me know. They never followed up, but they abandoned this app soon after. I suspect that this greedy scheduling can be quite effective. Even if it’s not, there are other much cheaper and more reliable optimization solutions than generative AI, like linear programming.&lt;/p&gt;

&lt;p&gt;I’ve seen this scenario over and over again. A big company wants to use generative AI to detect anomalies in network traffic. Another wants to predict upcoming customer call volume. A hospital wants to detect whether a patient is malnourished (really not recommended).&lt;/p&gt;

&lt;p&gt;It can often be beneficial to explore a new approach to get a sense of what’s possible, as long as you’re aware that your goal isn’t to solve a problem but to test a solution. “We solve the problem” and “We use generative AI” are two very different headlines, and unfortunately, so many people would rather have the latter.&lt;/p&gt;

&lt;h2 id=&quot;2_confuse_bad_product_with_bad_ai&quot;&gt;2. Confuse &apos;bad product&apos; with &apos;bad AI&apos;&lt;/h2&gt;

&lt;p&gt;At the other end of the spectrum, many teams dismiss gen AI as a valid solution for their problems because they tried it out and their users hated it. However, other teams successfully used gen AI for similar use cases. I could only look into two of these teams. In both cases, the issue wasn’t with AI, but with product.&lt;/p&gt;

&lt;p&gt;Many people have told me that the technical aspects of their AI applications are straightforward. The hard part is user experience (UX). What should the product interface look like? How to seamlessly integrate the product into the user workflow? How to incorporate human-in-the-loop?&lt;/p&gt;

&lt;p&gt;UX has always been challenging, but it’s even more so with generative AI. While we know that generative AI is changing how we read, write, learn, teach, work, entertain, etc., we don’t quite know how yet. What will the future of reading/learning/working be like?&lt;/p&gt;

&lt;p&gt;Here are some simple examples to show how what users want can be counter-intuitive and need rigorous user study.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;My friend works on an application that summarizes meeting transcripts. Initially, her team focused on getting the right summary length. Would users prefer 3-sentence summaries or 5-sentence summaries? &lt;br /&gt;
 &lt;br /&gt;
However, it turned out that their users didn’t care about the actual summary. They only wanted action items specific to them from each meeting.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;When &lt;a href=&quot;https://www.linkedin.com/blog/engineering/generative-ai/musings-on-building-a-generative-ai-product?_l=en_US&quot;&gt;LinkedIn&lt;/a&gt; developed a chatbot for skill fit assessment, they discovered that users didn’t want correct responses. Users wanted helpful responses. &lt;br /&gt;
 &lt;br /&gt;
For example, if a user asks a bot whether they’re a fit for a job and the bot responds with: “You’re a terrible fit,” this response might be correct but not very helpful to the user. Users want tips on what the gaps are and what they can do to close the gaps.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Intuit built a chatbot to help users answer tax questions. Initially, they got lukewarm feedback — users didn’t find the bot useful. After investigation, they found out that users actually hated typing. Facing a blank chatbot, users didn’t know what the bot could do and what to type. &lt;br /&gt;
 &lt;br /&gt;
So, for each interaction, Intuit added a few suggested questions for users to click on. This reduced the friction for users to use the bot and gradually built users’ trust. The feedback from users then became much more positive. &lt;br /&gt;
&lt;em&gt;(Shared by &lt;a href=&quot;https://www.linkedin.com/in/nhungho/&quot;&gt;Nhung Ho&lt;/a&gt;, VP of AI at Intuit, during our panel at Grace Hopper.)&lt;/em&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Because everyone uses the same models nowadays, the AI components of AI products are similar, and the differentiation is product.&lt;/p&gt;

&lt;h2 id=&quot;3_start_too_complex&quot;&gt;3. Start too complex&lt;/h2&gt;

&lt;p&gt;Examples of this pitfall:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Use an agentic framework when direct API calls work.&lt;/li&gt;
  &lt;li&gt;Agonize over what vector database to use when a simple term-based retrieval solution (that doesn’t require a vectordb) works.&lt;/li&gt;
  &lt;li&gt;Insist on finetuning when prompting works.&lt;/li&gt;
  &lt;li&gt;Use semantic caching.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Given so many shiny new technologies, it’s tempting to jump straight into using them. However, incorporating external tools too early can cause 2 problems:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Abstract away critical details, making it hard to understand and debug your system.&lt;/li&gt;
  &lt;li&gt;Introduce unnecessary bugs.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Tool developers can make mistakes. For example, I often find typos in default prompts when reviewing a framework’s codebase. If the framework you use updates its prompt without telling you, your application’s behaviors might change and you might not know why.&lt;/p&gt;

&lt;p&gt;Eventually, abstractions are good. But abstractions need to incorporate best practices and be tested overtime. As we’re still in the early days of AI engineering, best practices are still evolving, we should be more vigilant when adopting any abstraction.&lt;/p&gt;

&lt;h2 id=&quot;4_over_index_on_early_success&quot;&gt;4. Over-index on early success&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;It took &lt;a href=&quot;https://www.linkedin.com/blog/engineering/generative-ai/musings-on-building-a-generative-ai-product&quot;&gt;LinkedIn&lt;/a&gt; &lt;em&gt;1 month to achieve 80% of the experience they wanted, and an additional 4 months to surpass 95%&lt;/em&gt;. The initial success made them grossly underestimate how challenging it is to improve the product, especially around hallucinations. They found it discouraging how difficult it was to achieve each subsequent 1% gain.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;A startup that develops AI sales assistants for ecommerce told me that &lt;em&gt;getting from 0 to 80% took as long as from 80% to 90%&lt;/em&gt;. The challenges they faced:
    &lt;ul&gt;
      &lt;li&gt;Accuracy/latency tradeoff: more planning/self-correction = more nodes = higher latency&lt;/li&gt;
      &lt;li&gt;Tool calling: hard for agents to differentiate similar tools&lt;/li&gt;
      &lt;li&gt;Hard for tonal requests (e.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;talk like a luxury brand concierge&quot;&lt;/code&gt;) in the system prompt to be perfectly obeyed&lt;/li&gt;
      &lt;li&gt;Hard for the agent to completely understand customers’ intent&lt;/li&gt;
      &lt;li&gt;Hard to create a specific set of unit tests because the combination of queries is basically infinite&lt;/li&gt;
    &lt;/ul&gt;

    &lt;p&gt;&lt;em&gt;Thanks &lt;a href=&quot;https://www.linkedin.com/in/jasontjahjono/&quot;&gt;Jason Tjahjono&lt;/a&gt; for sharing this.&lt;/em&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;In the paper UltraChat, &lt;a href=&quot;https://arxiv.org/abs/2305.14233&quot;&gt;Ding et al. (2023)&lt;/a&gt; shared that “&lt;em&gt;the journey from 0 to 60 is easy, whereas progressing from 60 to 100 becomes exceedingly challenging&lt;/em&gt;.”&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is perhaps one of the first painful lessons anyone who has built an AI product quickly learns. It’s easy to build a demo, but hard to build a product. Other than the issues of hallucinations, latency, latency/accuracy tradeoff, tool use, prompting, testing, … as mentioned, teams also run into issues, such as:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Reliability&lt;/strong&gt; from the API providers. A team told me that 10% of their API calls timed out. Or product’s behaviors change because the underlying model changes.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compliance&lt;/strong&gt;, e.g. around AI output copyrights, data access/sharing, user privacy, security risks from retrieval/caching systems, and ambiguity around training data lineage.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Safety&lt;/strong&gt;, e.g. bad actors abusing your product, your product generates insensitive or offensive responses.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When planning a product’s milestones and resources, make sure to take into account these potential roadblocks. A friend calls this “being cautiously optimistic”. However, remember that many cool demos don’t lead to wonderful products.&lt;/p&gt;

&lt;h2 id=&quot;5_forgo_human_evaluation&quot;&gt;5. Forgo human evaluation&lt;/h2&gt;

&lt;p&gt;To automatically evaluate AI applications, many teams opt for the AI-as-a-judge (also called LLM-as-a-judge) approach — using AI models to evaluate AI outputs. A common pitfall is forgoing human evaluation to rely entirely on AI judges.&lt;/p&gt;

&lt;p&gt;While AI judges can be very useful, they aren’t deterministic. The quality of a judge depends on the underlying judge’s model, the judge’s prompt, and the use case. If the AI judge isn’t developed properly, it can give misleading evaluations about your application’s performance. AI judges must be evaluated and iterated over time, just like all other AI applications.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Agent pattern&quot; src=&quot;/assets/pics/aie-pitfalls/ai-judge.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;

&lt;p&gt;The teams with the best products I’ve seen all have human evaluation to supplement their automated evaluation. Every day, they have human experts evaluate a subset of their application’s outputs, which can be anywhere from 30 - 1000 examples.&lt;/p&gt;

&lt;p&gt;Daily manual evaluation serves 3 purposes:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Correlate human judgments with AI judgments. If the score by human evaluators is decreasing but the score by AI judges is increasing, you might want to investigate your AI judges.&lt;/li&gt;
  &lt;li&gt;Gain a better understanding of how users use your application, which can give you ideas to improve your application.&lt;/li&gt;
  &lt;li&gt;Detect patterns and changes in users’ behaviors, using your knowledge of current events, that automated data exploration might miss.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The reliability of human evaluation also depends on well-crafted annotation guidelines. These annotation guidelines can help improve the model’s instructions (if a human has a hard time following the instructions, the model will, too). It can also be reused to create finetuning data later if you choose to finetune.&lt;/p&gt;

&lt;p&gt;In every project I’ve worked on, &lt;em&gt;staring at data for just 15 minutes usually gives me some insight that could save me hours of headaches&lt;/em&gt;. &lt;a href=&quot;https://x.com/gdb/status/1622683988736479232&quot;&gt;Greg Brockman&lt;/a&gt; tweeted: “&lt;em&gt;Manual inspection of data has probably the highest value-to-prestige ratio of any activity in machine learning.&lt;/em&gt;”&lt;/p&gt;

&lt;h2 id=&quot;6_crowdsource_use_cases&quot;&gt;6. Crowdsource use cases&lt;/h2&gt;

&lt;p&gt;This is a mistake I saw in the early days when enterprises were in a frenzy to adopt generative AI. Unable to come up with a strategy for what use cases to focus on, many tech executives crowdsourced ideas from the whole company. “We hire smart people. Let them tell us what to do.” They then try to implement these ideas one by one.&lt;/p&gt;

&lt;p&gt;And that’s how we ended up with a million text-to-SQL models, a million Slack bots, and a billion code plugins.&lt;/p&gt;

&lt;p&gt;While it’s indeed a good idea to listen to the smart people that you hire, individuals might be biased toward the problems that immediately affect their day-to-day work instead of problems that might bring the highest returns on investment. Without an overall strategy that considers the big picture, it’s easy to get sidetracked into a series of small, low-impact applications and come to the wrong conclusion that gen AI has no ROI.&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;In short, here are the common AI engineering pitfalls:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Use generative AI when you don’t need generative AI&lt;/strong&gt;&lt;br /&gt;
 Gen AI isn’t a one-size-fits-all solution to all problems. Many problems don’t even need AI.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Confuse ‘bad product’ with ‘bad AI’&lt;/strong&gt;&lt;br /&gt;
 For many AI product, AI is the easy part, product is the hard part.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Start too complex&lt;/strong&gt;&lt;br /&gt;
 While fancy new frameworks and finetuning can be useful for many projects, they shouldn’t be your first course of action.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Over-index on early success&lt;/strong&gt;&lt;br /&gt;
 Initial success can be misleading. Going from demo-ready to production-ready can take much longer than getting to the first demo.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Forgo human evaluation&lt;/strong&gt;&lt;br /&gt;
 AI judges should be validated and correlated with systematic human evaluation.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Crowdsource use cases&lt;/strong&gt;&lt;br /&gt;
 Have a big-picture strategy to maximize return on investment.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;
</description>
        <pubDate>Thu, 16 Jan 2025 00:00:00 +0000</pubDate>
        <link>https://huyenchip.com//2025/01/16/ai-engineering-pitfalls.html</link>
        <guid isPermaLink="true">https://huyenchip.com/2025/01/16/ai-engineering-pitfalls.html</guid>
      </item>
    
      <item>
        <title>Agents</title>
        <description>&lt;p&gt;Intelligent agents are considered by many to be the ultimate goal of AI. The classic book by Stuart Russell and Peter Norvig, &lt;em&gt;Artificial Intelligence: A Modern Approach&lt;/em&gt; (Prentice Hall, 1995), defines the field of AI research as “&lt;em&gt;the study and design of rational agents.&lt;/em&gt;”&lt;/p&gt;

&lt;p&gt;The unprecedented capabilities of foundation models have opened the door to agentic applications that were previously unimaginable. These new capabilities make it finally possible to develop autonomous, intelligent agents to act as our assistants, coworkers, and coaches. They can help us create a website, gather data, plan a trip, do market research, manage a customer account, automate data entry, prepare us for interviews, interview our candidates, negotiate a deal, etc. The possibilities seem endless, and the potential economic value of these agents is enormous.&lt;/p&gt;

&lt;p&gt;This section will start with an overview of agents and then continue with two aspects that determine the capabilities of an agent: tools and planning. Agents, with their new modes of operations, have new modes of failure. This section will end with a discussion on how to evaluate agents to catch these failures.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This post is adapted from the &lt;strong&gt;Agents&lt;/strong&gt; section of &lt;a href=&quot;https://amzn.to/49j1cGS&quot;&gt;&lt;strong&gt;AI Engineering&lt;/strong&gt;&lt;/a&gt; (2025) with minor edits to make it a standalone post.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Notes&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;AI-powered agents are an emerging field with no established theoretical frameworks for defining, developing, and evaluating them. This section is a best-effort attempt to build a framework from the existing literature, but it will evolve as the field does. Compared to the rest of the book, this section is more experimental. I received helpful feedback from early reviewers, and I hope to get feedback from readers of this blog post, too.&lt;/li&gt;
  &lt;li&gt;Just before this book came out, Anthropic published a blog post on &lt;a href=&quot;https://www.anthropic.com/research/building-effective-agents&quot;&gt;Building effective agents&lt;/a&gt; (Dec 2024). I’m glad to see that Anthropic’s blog post and my agent section are &lt;a href=&quot;https://cedricchee.com/blog/the-dna-of-ai-agents/&quot;&gt;conceptually aligned&lt;/a&gt;, though with slightly different terminologies. However, Anthropic’s post focuses on isolated patterns, whereas my post covers why and how things work. I also focus more on planning, tool selection, and failure modes.&lt;/li&gt;
  &lt;li&gt;The post contains a lot of background information. Feel free to skip ahead if it feels a little too in the weeds!&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;agent_overview&quot;&gt;Agent Overview&lt;/h2&gt;

&lt;p&gt;The term &lt;em&gt;agent&lt;/em&gt; has been used in many different engineering contexts, including but not limited to a software agent, intelligent agent, user agent, conversational agent, and reinforcement learning agent. So, what exactly is an agent?&lt;/p&gt;

&lt;p&gt;An agent is anything that can perceive its environment and act upon that environment. &lt;em&gt;Artificial Intelligence: A Modern Approach&lt;/em&gt; (1995) defines an agent as anything that can be viewed as perceiving its environment through sensors and acting upon that environment through actuators.&lt;/p&gt;

&lt;p&gt;This means that an agent is characterized by the &lt;em&gt;environment&lt;/em&gt; it operates in and &lt;em&gt;the set of actions&lt;/em&gt; it can perform.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;environment&lt;/em&gt; an agent can operate in is defined by its use case. If an agent is developed to play a game (e.g., &lt;em&gt;Minecraft,&lt;/em&gt; Go, &lt;em&gt;Dota&lt;/em&gt;), that game is its environment. If you want an agent to scrape documents from the internet, the environment is the internet. A self-driving car agent’s environment is the road system and its adjacent areas.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;set of actions&lt;/em&gt; an AI agent can perform is augmented by the &lt;em&gt;tools&lt;/em&gt; it has access to. Many generative AI-powered applications you interact with daily are agents with access to tools, albeit simple ones. ChatGPT is an agent. It can search the web, execute Python code, and generate images. RAG systems are agents—text retrievers, image retrievers, and SQL executors are their tools.&lt;/p&gt;

&lt;p&gt;There’s a strong dependency between an agent’s environment and its set of tools. The environment determines what tools an agent can potentially use. For example, if the environment is a chess game, the only possible actions for an agent are the valid chess moves. However, an agent’s tool inventory restricts the environment it can operate in. For example, if a robot’s only action is swimming, it’ll be confined to a water environment.&lt;/p&gt;

&lt;p&gt;Figure 6-8 shows a visualization of &lt;a href=&quot;https://arxiv.org/abs/2405.15793&quot;&gt;SWE-agent&lt;/a&gt; (Yang et al., 2024), an agent built on top of GPT-4. Its environment is the computer with the terminal and the file system. Its set of actions include navigate repo, search files, view files, and edit lines.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;A coding agent&quot; src=&quot;/assets/pics/agents/1-swe-agent.png&quot; style=&quot;float: center; max-width: 90%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
    Figure 6-8. SWE-agent is a coding agent whose environment is the computer and whose actions include navigation, search, view files, and editing
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;An AI agent is meant to accomplish tasks typically provided by the users. In an AI agent, AI is the brain that processes the task, plans a sequence of actions to achieve this task, and determines whether the task has been accomplished.&lt;/p&gt;

&lt;p&gt;Let’s return to the RAG system with tabular data in the Kitty Vogue example above. This is a simple agent with three actions:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;response generation,&lt;/li&gt;
  &lt;li&gt;SQL query generation, and&lt;/li&gt;
  &lt;li&gt;SQL query execution.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Given the query &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Project the sales revenue for Fruity Fedora over the next three months&quot;&lt;/code&gt;, the agent might perform the following sequence of actions:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Reason about how to accomplish this task. It might decide that to predict future sales, it first needs the sales numbers from the last five years. An agent’s reasoning can be shown as intermediate responses.&lt;/li&gt;
  &lt;li&gt;Invoke SQL query generation to generate the query to get sales numbers from the last five years.&lt;/li&gt;
  &lt;li&gt;Invoke SQL query execution to execute this query.&lt;/li&gt;
  &lt;li&gt;Reason about the tool outputs (outputs from the SQL query execution) and how they help with sales prediction. It might decide that these numbers are insufficient to make a reliable projection, perhaps because of missing values. It then decides that it also needs information about past marketing campaigns.&lt;/li&gt;
  &lt;li&gt;Invoke SQL query generation to generate the queries for past marketing campaigns.&lt;/li&gt;
  &lt;li&gt;Invoke SQL query execution.&lt;/li&gt;
  &lt;li&gt;Reason that this new information is sufficient to help predict future sales. It then generates a projection.&lt;/li&gt;
  &lt;li&gt;Reason that the task has been successfully completed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Compared to non-agent use cases, agents typically require more powerful models for two reasons:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Compound mistakes&lt;/strong&gt;: an agent often needs to perform multiple steps to accomplish a task, and the overall accuracy decreases as the number of steps increases. If the model’s accuracy is 95% per step, over 10 steps, the accuracy will drop to 60%, and over 100 steps, the accuracy will be only 0.6%.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Higher stakes&lt;/strong&gt;: with access to tools, agents are capable of performing more impactful tasks, but any failure could have more severe consequences.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A task that requires many steps can take time and money to run. A common complaint is that agents are only good for burning through your API credits. However, if agents can be autonomous, they can save human time, making their costs worthwhile.&lt;/p&gt;

&lt;p&gt;Given an environment, the success of an agent in an environment depends on &lt;strong&gt;the tool it has access to&lt;/strong&gt; and &lt;strong&gt;the strength of its AI planner&lt;/strong&gt;. Let’s start by looking into different kinds of tools a model can use. We’ll analyze AI’s capability for planning next.&lt;/p&gt;

&lt;h2 id=&quot;tools&quot;&gt;Tools&lt;/h2&gt;

&lt;p&gt;A system doesn’t need access to external tools to be an agent. However, without external tools, the agent’s capabilities would be limited. By itself, a model can typically perform one action—an LLM can generate text and an image generator can generate images. External tools make an agent vastly more capable.&lt;/p&gt;

&lt;p&gt;Tools help an agent to both perceive the environment and act upon it. Actions that allow an agent to perceive the environment are &lt;em&gt;read-only actions&lt;/em&gt;, whereas actions that allow an agent to act upon the environment are &lt;em&gt;write actions&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The set of tools an agent has access to is its tool inventory. Since an agent’s tool inventory determines what an agent can do, it’s important to think through what and how many tools to give an agent. More tools give an agent more capabilities. However, the more tools there are, the more challenging it is to understand and utilize them well. Experimentation is necessary to find the right set of tools, as discussed later in the “&lt;strong&gt;Tool selection&lt;/strong&gt;” section.&lt;/p&gt;

&lt;p&gt;Depending on the agent’s environment, there are many possible tools. Here are three categories of tools that you might want to consider: knowledge augmentation (i.e., context construction), capability extension, and tools that let your agent act upon its environment.&lt;/p&gt;

&lt;h3 id=&quot;knowledge_augmentation&quot;&gt;Knowledge augmentation&lt;/h3&gt;

&lt;p&gt;I hope that this book, so far, has convinced you of the importance of having the relevant context for a model’s response quality. An important category of tools includes those that help augment the knowledge of your agent. Some of them have already been discussed: text retriever, image retriever, and SQL executor. Other potential tools include internal people search, an inventory API that returns the status of different products, Slack retrieval, an email reader, etc.&lt;/p&gt;

&lt;p&gt;Many such tools augment a model with your organization’s private processes and information. However, tools can also give models access to public information, especially from the internet.&lt;/p&gt;

&lt;p&gt;Web browsing was among the earliest and most anticipated capabilities to be incorporated into ChatGPT. Web browsing prevents a model from going stale. A model goes stale when the data it was trained on becomes outdated. If the model’s training data was cut off last week, it won’t be able to answer questions that require information from this week unless this information is provided in the context. Without web browsing, a model won’t be able to tell you about the weather, news, upcoming events, stock prices, flight status, etc.&lt;/p&gt;

&lt;p&gt;I use web browsing as an umbrella term to cover all tools that access the internet, including web browsers and APIs such as search APIs, news APIs, GitHub APIs, or social media APIs.&lt;/p&gt;

&lt;p&gt;While web browsing allows your agent to reference up-to-date information to generate better responses and reduce hallucinations, it can also open up your agent to the cesspools of the internet. Select your Internet APIs with care.&lt;/p&gt;

&lt;h3 id=&quot;capability_extension&quot;&gt;Capability extension&lt;/h3&gt;

&lt;p&gt;You might also consider tools that address the inherent limitations of AI models. They are easy ways to give your model a performance boost. For example, AI models are notorious for being bad at math. If you ask a model what is 199,999 divided by 292, the model will likely fail. However, this calculation would be trivial if the model had access to a calculator. Instead of trying to train the model to be good at arithmetic, it’s a lot more resource-efficient to just give the model access to a tool.&lt;/p&gt;

&lt;p&gt;Other simple tools that can significantly boost a model’s capability include a calendar, timezone converter, unit converter (e.g., from lbs to kg), and translator that can translate to and from the languages that the model isn’t good at.&lt;/p&gt;

&lt;p&gt;More complex but powerful tools are code interpreters. Instead of training a model to understand code, you can give it access to a code interpreter to execute a piece of code, return the results, or analyze the code’s failures. This capability lets your agents act as coding assistants, data analysts, and even research assistants that can write code to run experiments and report results. However, automated code execution comes with the risk of code injection attacks, as discussed in Chapter 5 in the section “Defensive Prompt Engineering&lt;strong&gt;“&lt;/strong&gt;. Proper security measurements are crucial to keep you and your users safe.&lt;/p&gt;

&lt;p&gt;Tools can turn a text-only or image-only model into a multimodal model. For example, a model that can generate only texts can leverage a text-to-image model as a tool, allowing it to generate both texts and images. Given a text request, the agent’s AI planner decides whether to invoke text generation, image generation, or both. This is how ChatGPT can generate both text and images—it uses DALL-E as its image generator.&lt;/p&gt;

&lt;p&gt;Agents can also use a code interpreter to generate charts and graphs, a LaTex compiler to render math equations, or a browser to render web pages from HTML code.&lt;/p&gt;

&lt;p&gt;Similarly, a model that can process only text inputs can use an image captioning tool to process images and a transcription tool to process audio. It can use an OCR (optical character recognition) tool to read PDFs.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Tool use can significantly boost a model’s performance compared to just prompting or even finetuning&lt;/em&gt;. &lt;a href=&quot;https://arxiv.org/abs/2304.09842&quot;&gt;Chameleon&lt;/a&gt; (Lu et al., 2023) shows that a GPT-4-powered agent, augmented with a set of 13 tools, can outperform GPT-4 alone on several benchmarks. Examples of tools this agent used are knowledge retrieval, a query generator, an image captioner, a text detector, and Bing search.&lt;/p&gt;

&lt;p&gt;On ScienceQA, a science question answering benchmark, Chameleon improves the best published few-shot result by 11.37%. On TabMWP (Tabular Math Word Problems) (Lu et al., 2022), a benchmark involving tabular math questions, Chameleon improves the accuracy by 17%.&lt;/p&gt;

&lt;h3 id=&quot;write_actions&quot;&gt;Write actions&lt;/h3&gt;

&lt;p&gt;So far, we’ve discussed read-only actions that allow a model to read from its data sources. But tools can also perform write actions, making changes to the data sources. An SQL executor can retrieve a data table (read) and change or delete the table (write). An email API can read an email but can also respond to it. A banking API can retrieve your current balance, but can also initiate a bank transfer.&lt;/p&gt;

&lt;p&gt;Write actions enable a system to do more. They can enable you to automate the whole customer outreach workflow: researching potential customers, finding their contacts, drafting emails, sending first emails, reading responses, following up, extracting orders, updating your databases with new orders, etc.&lt;/p&gt;

&lt;p&gt;However, the prospect of giving AI the ability to automatically alter our lives is frightening. Just as you shouldn’t give an intern the authority to delete your production database, you shouldn’t allow an unreliable AI to initiate bank transfers. Trust in the system’s capabilities and its security measures is crucial. You need to ensure that the system is protected from bad actors who might try to manipulate it into performing harmful actions.&lt;/p&gt;

&lt;hr /&gt;
&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Sidebar: Agents and security&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Whenever I talk about autonomous AI agents to a group of people, there is often someone who brings up self-driving cars. “&lt;em&gt;What if someone hacks into the car to kidnap you?&lt;/em&gt;” While the self-driving car example seems visceral because of its physicality, an AI system can cause harm without a presence in the physical world. It can manipulate the stock market, steal copyrights, violate privacy, reinforce biases, spread misinformation and propaganda, and more, as discussed in the section “Defensive Prompt Engineering” in Chapter 5.&lt;/p&gt;

&lt;p&gt;These are all valid concerns, and any organization that wants to leverage AI needs to take safety and security seriously. However, this doesn’t mean that AI systems should never be given the ability to act in the real world. If we can trust a machine to take us into space, I hope that one day, security measures will be sufficient for us to trust autonomous AI systems. Besides, humans can fail, too. Personally, I would trust a self-driving car more than the average stranger to give me a lift.&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;br /&gt;
Just as the right tools can help humans be vastly more productive—can you imagine doing business without Excel or building a skyscraper without cranes?—tools enable models to accomplish many more tasks. Many model providers already support tool use with their models, a feature often called function calling. Going forward, I would expect function calling with a wide set of tools to be common with most models.&lt;/p&gt;

&lt;h2 id=&quot;planning&quot;&gt;Planning&lt;/h2&gt;

&lt;p&gt;At the heart of a foundation model agent is the model responsible for solving user-provided tasks. A task is defined by its goal and constraints. For example, one task is to schedule a two-week trip from San Francisco to India with a budget of $5,000. The goal is the two-week trip. The constraint is the budget.&lt;/p&gt;

&lt;p&gt;Complex tasks require planning. The output of the planning process is a plan, which is a roadmap outlining the steps needed to accomplish a task. Effective planning typically requires the model to understand the task, consider different options to achieve this task, and choose the most promising one.&lt;/p&gt;

&lt;p&gt;If you’ve ever been in any planning meeting, you know that planning is hard. As an important computational problem, planning is well studied and would require several volumes to cover. I’ll only be able to cover the surface here.&lt;/p&gt;

&lt;h3 id=&quot;planning_overview&quot;&gt;Planning overview&lt;/h3&gt;

&lt;p&gt;Given a task, there are many possible ways to solve it, but not all of them will lead to a successful outcome. Among the correct solutions, some are more efficient than others. Consider the query, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;How many companies without revenue have raised at least $1 billion?&quot;&lt;/code&gt;, and the two example solutions:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Find all companies without revenue, then filter them by the amount raised.&lt;/li&gt;
  &lt;li&gt;Find all companies that have raised at least $1 billion, then filter them by revenue.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The second option is more efficient. There are vastly more companies without revenue than companies that have raised $1 billion. Given only these two options, an intelligent agent should choose option 2.&lt;/p&gt;

&lt;p&gt;You can couple planning with execution in the same prompt. For example, you give the model a prompt, ask it to think step by step (such as with a chain-of-thought prompt), and then execute those steps all in one prompt. But what if the model comes up with a 1,000-step plan that doesn’t even accomplish the goal? Without oversight, an agent can run those steps for hours, wasting time and money on API calls, before you realize that it’s not going anywhere.&lt;/p&gt;

&lt;p&gt;To avoid fruitless execution, &lt;em&gt;planning&lt;/em&gt; should be decoupled from &lt;em&gt;execution&lt;/em&gt;. You ask the agent to first generate a plan, and only after this plan is &lt;em&gt;validated&lt;/em&gt; is it executed. The plan can be validated using heuristics. For example, one simple heuristic is to eliminate plans with invalid actions. If the generated plan requires a Google search and the agent doesn’t have access to Google Search, this plan is invalid. Another simple heuristic might be eliminating all plans with more than X steps.&lt;/p&gt;

&lt;p&gt;A plan can also be validated using AI judges. You can ask a model to evaluate whether the plan seems reasonable or how to improve it.&lt;/p&gt;

&lt;p&gt;If the generated plan is evaluated to be bad, you can ask the planner to generate another plan. If the generated plan is good, execute it.&lt;/p&gt;

&lt;p&gt;If the plan consists of external tools, function calling will be invoked. Outputs from executing this plan will then again need to be evaluated. Note that the generated plan doesn’t have to be an end-to-end plan for the whole task. It can be a small plan for a subtask. The whole process looks like Figure 6-9.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Agent pattern&quot; src=&quot;/assets/pics/agents/2-agent-pattern.png&quot; style=&quot;float: center; max-width: 90%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
    Figure 6-9. Decoupling planning and execution so that only validated plans are executed
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Your system now has three components: one to generate plans, one to validate plans, and another to execute plans. If you consider each component an agent, this can be considered a multi-agent system. Because most agentic workflows are sufficiently complex to involve multiple components, most agents are multi-agent.&lt;/p&gt;

&lt;p&gt;To speed up the process, instead of generating plans sequentially, you can generate several plans in parallel and ask the evaluator to pick the most promising one. This is another latency–cost tradeoff, as generating multiple plans simultaneously will incur extra costs.&lt;/p&gt;

&lt;p&gt;Planning requires understanding the intention behind a task: what’s the user trying to do with this query? An intent classifier is often used to help agents plan. As shown in Chapter 5 in the section “Break complex tasks into simpler subtasks&lt;strong&gt;“&lt;/strong&gt;, intent classification can be done using another prompt or a classification model trained for this task. The intent classification mechanism can be considered another agent in your multi-agent system.&lt;/p&gt;

&lt;p&gt;Knowing the intent can help the agent pick the right tools. For example, for customer support, if the query is about billing, the agent might need access to a tool to retrieve a user’s recent payments. But if the query is about how to reset a password, the agent might need to access documentation retrieval.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Tip&lt;/strong&gt;:&lt;/p&gt;

  &lt;p&gt;Some queries might be out of the scope of the agent. The intent classifier should be able to classify requests as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IRRELEVANT&lt;/code&gt; so that the agent can politely reject those instead of wasting FLOPs coming up with impossible solutions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So far, we’ve assumed that the agent automates all three stages: generating plans, validating plans, and executing plans. In reality, humans can be involved at any stage to aid with the process and mitigate risks.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A human expert can provide a plan, validate a plan, or execute parts of a plan. For example, for complex tasks for which an agent has trouble generating the whole plan, a human expert can provide a high-level plan that the agent can expand upon.&lt;/li&gt;
  &lt;li&gt;If a plan involves risky operations, such as updating a database or merging a code change, the system can ask for explicit human approval before executing or defer to humans to execute these operations. To make this possible, you need to clearly define the level of automation an agent can have for each action.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To summarize, solving a task typically involves the following processes. Note that reflection isn’t mandatory for an agent, but it’ll significantly boost the agent’s performance.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;em&gt;Plan generation&lt;/em&gt;: come up with a plan for accomplishing this task. A plan is a sequence of manageable actions, so this process is also called task decomposition.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Reflection and error correction&lt;/em&gt;: evaluate the generated plan. If it’s a bad plan, generate a new one.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Execution&lt;/em&gt;: take actions outlined in the generated plan. This often involves calling specific functions.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Reflection and error correction&lt;/em&gt;: upon receiving the action outcomes, evaluate these outcomes and determine whether the goal has been accomplished. Identify and correct mistakes. If the goal is not completed, generate a new plan.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You’ve already seen some techniques for plan generation and reflection in this book. When you ask a model to “think step by step”, you’re asking it to decompose a task. When you ask a model to “verify if your answer is correct”, you’re asking it to reflect.&lt;/p&gt;

&lt;h3 id=&quot;foundation_models_as_planners&quot;&gt;Foundation models as planners&lt;/h3&gt;

&lt;p&gt;An open question is how well foundation models can plan. Many researchers believe that foundation models, at least those built on top of autoregressive language models, cannot. Meta’s Chief AI Scientist Yann LeCun states unequivocably that &lt;a href=&quot;https://x.com/ylecun/status/1702027572077326505&quot;&gt;autoregressive LLMs can’t plan&lt;/a&gt; (2023).&lt;/p&gt;

&lt;center&gt;
&lt;blockquote class=&quot;twitter-tweet&quot;&gt;&lt;p lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;Auto-Regressive LLMs can&amp;#39;t plan &lt;br /&gt;(and can&amp;#39;t really reason).&lt;br /&gt;&lt;br /&gt;&amp;quot;While our own limited experiments didn&amp;#39;t show any significant improvement [in planning abilities] through fine tuning, it is possible that with even more fine tuning data and effort, the empirical performance may well… &lt;a href=&quot;https://t.co/rHA5QHi90C&quot;&gt;https://t.co/rHA5QHi90C&lt;/a&gt;&lt;/p&gt;&amp;mdash; Yann LeCun (@ylecun) &lt;a href=&quot;https://twitter.com/ylecun/status/1702027572077326505?ref_src=twsrc%5Etfw&quot;&gt;September 13, 2023&lt;/a&gt;&lt;/blockquote&gt; &lt;script async=&quot;&quot; src=&quot;https://platform.twitter.com/widgets.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;
&lt;/center&gt;

&lt;p&gt;While there is a lot of anecdotal evidence that LLMs are poor planners, it’s unclear whether it’s because we don’t know how to use LLMs the right way or because LLMs, fundamentally, can’t plan.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Planning, at its core, is a search problem&lt;/strong&gt;. You search among different paths towards the goal, predict the outcome (reward) of each path, and pick the path with the most promising outcome. Often, you might determine that no path exists that can take you to the goal.&lt;/p&gt;

&lt;p&gt;Search often requires &lt;em&gt;backtracking&lt;/em&gt;. For example, imagine you’re at a step where there are two possible actions: A and B. After taking action A, you enter a state that’s not promising, so you need to backtrack to the previous state to take action B.&lt;/p&gt;

&lt;p&gt;Some people argue that an autoregressive model can only generate forward actions. It can’t backtrack to generate alternate actions. Because of this, they conclude that autoregressive models can’t plan. However, this isn’t necessarily true. After executing a path with action A, if the model determines that this path doesn’t make sense, it can revise the path using action B instead, effectively backtracking. The model can also always start over and choose another path.&lt;/p&gt;

&lt;p&gt;It’s also possible that LLMs are poor planners because they aren’t given the toolings needed to plan. To plan, it’s necessary to know not only the available actions but also &lt;em&gt;the potential outcome of each action&lt;/em&gt;. As a simple example, let’s say you want to walk up a mountain. Your potential actions are turn right, turn left, turn around, or go straight ahead. However, if turning right will cause you to fall off the cliff, you might not consider this action. In technical terms, an action takes you from one state to another, and it’s necessary to know the outcome state to determine whether to take an action.&lt;/p&gt;

&lt;p&gt;This means that prompting a model to generate only a sequence of actions like what the popular chain-of-thought prompting technique does isn’t sufficient. The paper “&lt;a href=&quot;https://arxiv.org/abs/2305.14992&quot;&gt;Reasoning with Language Model is Planning with World Model&lt;/a&gt;” (Hao et al., 2023) argues that an LLM, by containing so much information about the world, is capable of predicting the outcome of each action. This LLM can incorporate this outcome prediction to generate coherent plans.&lt;/p&gt;

&lt;p&gt;Even if AI can’t plan, it can still be a part of a planner. It might be possible to augment an LLM with a search tool and state tracking system to help it plan.&lt;/p&gt;

&lt;hr /&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Sidebar: Foundation model (FM) versus reinforcement learning (RL) planners&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;em&gt;agent&lt;/em&gt; is a core concept in RL, which is defined in &lt;a href=&quot;https://en.wikipedia.org/wiki/Reinforcement_learning&quot;&gt;Wikipedia&lt;/a&gt; as a field “&lt;em&gt;concerned with how an intelligent agent ought to take actions in a dynamic environment in order to maximize the cumulative reward.&lt;/em&gt;”&lt;/p&gt;

&lt;p&gt;RL agents and FM agents are similar in many ways. They are both characterized by their environments and possible actions. The main difference is in how their planners work.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;In an RL agent, the planner is trained by an RL algorithm. Training this RL planner can require a lot of time and resources.&lt;/li&gt;
  &lt;li&gt;In an FM agent, the model is the planner. This model can be prompted or finetuned to improve its planning capabilities, and generally requires less time and fewer resources.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, there’s nothing to prevent an FM agent from incorporating RL algorithms to improve its performance. I suspect that in the long run, FM agents and RL agents will merge.&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;plan_generation&quot;&gt;Plan generation&lt;/h3&gt;

&lt;p&gt;The simplest way to turn a model into a plan generator is with prompt engineering. Imagine that you want to create an agent to help customers learn about products at Kitty Vogue. You give this agent access to three external tools: retrieve products by price, retrieve top products, and retrieve product information. Here’s an example of a prompt for plan generation. This prompt is for illustration purposes only. Production prompts are likely more complex.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SYSTEM PROMPT&lt;/strong&gt;:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Propose a plan to solve the task. You have access to 5 actions:

* get_today_date()
* fetch_top_products(start_date, end_date, num_products)
* fetch_product_info(product_name)
* generate_query(task_history, tool_output)
* generate_response(query)

The plan must be a sequence of valid actions.

Examples
Task: &quot;Tell me about Fruity Fedora&quot;
Plan: [fetch_product_info, generate_query, generate_response]

Task: &quot;What was the best selling product last week?&quot;
Plan: [fetch_top_products, generate_query, generate_response]

Task: {USER INPUT}
Plan:

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There are two things to note about this example:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The plan format used here—a list of functions whose parameters are inferred by the agent—is just one of many ways to structure the agent control flow.&lt;/li&gt;
  &lt;li&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;generate_query&lt;/code&gt; function takes in the task’s current history and the most recent tool outputs to generate a query to be fed into the response generator. The tool output at each step is added to the task’s history.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Given the user input “What’s the price of the best-selling product last week”, a generated plan might look like this:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get_time()&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fetch_top_products()&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fetch_product_info()&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;generate_query()&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;generate_response()&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You might wonder, “What about the parameters needed for each function?” The exact parameters are hard to predict in advance since they are often extracted from the previous tool outputs. If the first step, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get_time()&lt;/code&gt;, outputs “2030-09-13”, the agent can reason that the parameters for the next step should be called with the following parameters:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;fetch_top_products(
    start_date=&quot;2030-09-07&quot;,
    end_date=&quot;2030-09-13&quot;,
    num_products=1
)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Often, there’s insufficient information to determine the exact parameter values for a function. For example, if a user asks “What’s the average price of best-selling products?”, the answers to the following questions are unclear:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;How many best-selling products the user wants to look at?&lt;/li&gt;
  &lt;li&gt;Does the user want the best-selling products last week, last month, or of all time?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This means that models frequently have to make guesses, and guesses can be wrong.&lt;/p&gt;

&lt;p&gt;Because both the action sequence and the associated parameters are generated by AI models, they can be hallucinated. Hallucinations can cause the model to call an invalid function or call a valid function but with wrong parameters. Techniques for improving a model’s performance in general can be used to improve a model’s planning capabilities.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Tips&lt;/strong&gt; for making an agent better at planning.&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;Write a better system prompt with more examples.&lt;/li&gt;
    &lt;li&gt;Give better descriptions of the tools and their parameters so that the model understands them better.&lt;/li&gt;
    &lt;li&gt;Rewrite the functions themselves to make them simpler, such as refactoring a complex function into two simpler functions.&lt;/li&gt;
    &lt;li&gt;Use a stronger model. In general, stronger models are better at planning.&lt;/li&gt;
    &lt;li&gt;Finetune a model for plan generation.&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;function_calling&quot;&gt;Function calling&lt;/h4&gt;

&lt;p&gt;Many model providers offer tool use for their models, effectively turning their models into agents. A tool is a function. Invoking a tool is, therefore, often called &lt;em&gt;function calling&lt;/em&gt;. Different model APIs work differently, but in general, function calling works as follows:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;em&gt;Create a tool inventory.&lt;/em&gt; 
Declare all the tools that you might want a model to use. Each tool is described by its execution entry point (e.g., its function name), its parameters, and its documentation (e.g., what the function does and what parameters it needs).&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Specify what tools the agent can use for a query.&lt;/em&gt; &lt;br /&gt;
Because different queries might need different tools, many APIs let you specify a list of declared tools to be used per query. Some let you control tool use further by the following settings:
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;required&lt;/code&gt;: the model must use at least one tool.&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;none&lt;/code&gt;: the model shouldn’t use any tool.&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auto&lt;/code&gt;: the model decides which tools to use.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Function calling is illustrated in Figure 6-10. This is written in pseudocode to make it representative of multiple APIs. To use a specific API, please refer to its documentation.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;An example of function calling&quot; src=&quot;/assets/pics/agents/3-function-calling.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
    Figure 6-10. An example of a model using two simple tools
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Given a query, an agent defined as in Figure 6-10 will automatically generate what tools to use and their parameters. Some function calling APIs will make sure that only valid functions are generated, though they won’t be able to guarantee the correct parameter values.&lt;/p&gt;

&lt;p&gt;For example, given the user query “How many kilograms are 40 pounds?”, the agent might decide that it needs the tool &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lbs_to_kg_tool&lt;/code&gt; with one parameter value of 40. The agent’s response might look like this.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;response = ModelResponse(
    finish_reason=&apos;tool_calls&apos;,
    message=chat.Message(
        content=None,
        role=&apos;assistant&apos;,
        tool_calls=[
            ToolCall(
                function=Function(
                    arguments=&apos;{&quot;lbs&quot;:40}&apos;,
                    name=&apos;lbs_to_kg&apos;),
                type=&apos;function&apos;)
        ])
)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;From this response, you can evoke the function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lbs_to_kg(lbs=40)&lt;/code&gt; and use its output to generate a response to the users.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Tip&lt;/strong&gt;: When working with agents, always ask the system to report what parameter values it uses for each function call. Inspect these values to make sure they are correct.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;planning_granularity&quot;&gt;Planning granularity&lt;/h4&gt;

&lt;p&gt;A plan is a roadmap outlining the steps needed to accomplish a task. A roadmap can be of different levels of granularity. To plan for a year, a quarter-by-quarter plan is higher-level than a month-by-month plan, which is, in turn, higher-level than a week-to-week plan.&lt;/p&gt;

&lt;p&gt;There’s a planning/execution tradeoff. A detailed plan is harder to generate, but easier to execute. A higher-level plan is easier to generate, but harder to execute. An approach to circumvent this tradeoff is to plan hierarchically. First, use a planner to generate a high-level plan, such as a quarter-to-quarter plan. Then, for each quarter, use the same or a different planner to generate a month-to-month plan.&lt;/p&gt;

&lt;p&gt;So far, all examples of generated plans use the exact function names, which is very granular. A problem with this approach is that an agent’s tool inventory can change over time. For example, the function to get the current date &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get_time()&lt;/code&gt; can be renamed to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get_current_time()&lt;/code&gt;. When a tool changes, you’ll need to update your prompt and all your examples. Using the exact function names also makes it harder to reuse a planner across different use cases with different tool APIs.&lt;/p&gt;

&lt;p&gt;If you’ve previously finetuned a model to generate plans based on the old tool inventory, you’ll need to finetune the model again on the new tool inventory.&lt;/p&gt;

&lt;p&gt;To avoid this problem, plans can also be generated using a more natural language, which is higher-level than domain-specific function names. For example, given the query “What’s the price of the best-selling product last week”, an agent can be instructed to output a plan that looks like this:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get current date&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;retrieve the best-selling product last week&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;retrieve product information&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;generate query&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;generate response&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Using more natural language helps your plan generator become robust to changes in tool APIs. If your model was trained mostly on natural language, it’ll likely be better at understanding and generating plans in natural language and less likely to hallucinate.&lt;/p&gt;

&lt;p&gt;The downside of this approach is that you need a translator to translate each natural language action into executable commands. &lt;a href=&quot;https://arxiv.org/abs/2304.09842&quot;&gt;Chameleon&lt;/a&gt; (Lu et al., 2023) calls this translator a program generator. However, translating is a much simpler task than planning and can be done by weaker models with a lower risk of hallucination.&lt;/p&gt;

&lt;h4 id=&quot;complex_plans&quot;&gt;Complex plans&lt;/h4&gt;

&lt;p&gt;The plan examples so far have been sequential: the next action in the plan is &lt;em&gt;always&lt;/em&gt; executed after the previous action is done. The order in which actions can be executed is called a &lt;em&gt;control flow&lt;/em&gt;. The sequential form is just one type of control flow. Other types of control flows include the parallel, if statement, and for loop. The list below provides an overview of each control flow, including sequential for comparison:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Sequential&lt;/strong&gt;&lt;/p&gt;

    &lt;p&gt;Executing task B after task A is complete, possibly because task B depends on task A. For example, the SQL query can only be executed after it’s been translated from the natural language input.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Parallel&lt;/strong&gt;&lt;/p&gt;

    &lt;p&gt;Executing tasks A and B at the same time. For example, given the query “Find me best-selling products under $100”, an agent might first retrieve the top 100 best-selling products and, for each of these products, retrieve its price.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;If statement&lt;/strong&gt;&lt;/p&gt;

    &lt;p&gt;Executing task B or task C depending on the output from the previous step. For example, the agent first checks NVIDIA’s earnings report. Based on this report, it can then decide to sell or buy NVIDIA stocks. &lt;a href=&quot;https://www.anthropic.com/research/building-effective-agents&quot;&gt;Anthropic’s post&lt;/a&gt; calls this pattern “routing”.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;For loop&lt;/strong&gt;&lt;/p&gt;

    &lt;p&gt;Repeat executing task A until a specific condition is met. For example, keep on generating random numbers until a prime number.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These different control flows are visualized in Figure 6-11.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Agent control flow&quot; src=&quot;/assets/pics/agents/4-agent-control-flow.png&quot; style=&quot;float: center; max-width: 90%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
    Figure 6-11. Examples of different orders in which a plan can be executed
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;In traditional software engineering, conditions for control flows are exact. With AI-powered agents, AI models determine control flows. Plans with non-sequential control flows are more difficult to both generate and translate into executable commands.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Tip&lt;/strong&gt;:&lt;/p&gt;

  &lt;p&gt;When evaluating an agent framework, check what control flows it supports. For example, if the system needs to browse ten websites, can it do so simultaneously? Parallel execution can significantly reduce the latency perceived by users.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;reflection_and_error_correction&quot;&gt;Reflection and error correction&lt;/h3&gt;

&lt;p&gt;Even the best plans need to be constantly evaluated and adjusted to maximize their chance of success. While reflection isn’t strictly necessary for an agent to operate, it’s necessary for an agent to succeed.&lt;/p&gt;

&lt;p&gt;There are many places during a task process where reflection can be useful:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;After receiving a user query to evaluate if the request is feasible.&lt;/li&gt;
  &lt;li&gt;After the initial plan generation to evaluate whether the plan makes sense.&lt;/li&gt;
  &lt;li&gt;After each execution step to evaluate if it’s on the right track.&lt;/li&gt;
  &lt;li&gt;After the whole plan has been executed to determine if the task has been accomplished.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Reflection and error correction are two different mechanisms that go hand in hand. Reflection generates insights that help uncover errors to be corrected.&lt;/p&gt;

&lt;p&gt;Reflection can be done with the same agent with self-critique prompts. It can also be done with a separate component, such as a specialized scorer: a model that outputs a concrete score for each outcome.&lt;/p&gt;

&lt;p&gt;First proposed by &lt;a href=&quot;https://arxiv.org/abs/2210.03629&quot;&gt;ReAct&lt;/a&gt; (Yao et al., 2022), interleaving &lt;span style=&quot;text-decoration:underline;&quot;&gt;re&lt;/span&gt;asoning and &lt;span style=&quot;text-decoration:underline;&quot;&gt;ac&lt;/span&gt;tion has become a common pattern for agents. Yao et al. used the term “reasoning” to encompass both planning and reflection. At each step, the agent is asked to explain its thinking (planning), take actions, then analyze observations (reflection), until the task is considered finished by the agent. The agent is typically prompted, using examples, to generate outputs in the following format:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Thought 1: …
Act 1: …
Observation 1: …

… [continue until reflection determines that the task is finished] …

Thought N: … 
Act N: Finish [Response to query]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Figure 6-12 shows an example of an agent following the ReAct framework responding to a question from HotpotQA (&lt;a href=&quot;https://arxiv.org/abs/1809.09600&quot;&gt;Yang et al., 2018&lt;/a&gt;), a benchmark for multi-hop question answering.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;ReAct example&quot; src=&quot;/assets/pics/agents/5-ReAct.png&quot; style=&quot;float: center; max-width: 80%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
    Figure 6-12: A ReAct agent in action.
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;You can implement reflection in a multi-agent setting: one agent plans and takes actions and another agent evaluates the outcome after each step or after a number of steps.&lt;/p&gt;

&lt;p&gt;If the agent’s response failed to accomplish the task, you can prompt the agent to reflect on why it failed and how to improve. Based on this suggestion, the agent generates a new plan. This allows agents to learn from their mistakes.&lt;/p&gt;

&lt;p&gt;For example, given a coding generation task, an evaluator might evaluate that the generated code fails ⅓ of the test cases. The agent then reflects that it failed because it didn’t take into account arrays where all numbers are negative. The actor then generates new code, taking into account all-negative arrays.&lt;/p&gt;

&lt;p&gt;This is the approach that Reflexion (&lt;a href=&quot;https://arxiv.org/abs/2303.11366&quot;&gt;Shinn et al., 2023&lt;/a&gt;) took. In this framework, reflection is separated into two modules: an evaluator that evaluates the outcome and a self-reflection module that analyzes what went wrong. Figure 6-13 shows examples of Reflexion agents in action. The authors used the term “trajectory” to refer to a plan. At each step, after evaluation and self-reflection, the agent proposes a new trajectory.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Reflexion example&quot; src=&quot;/assets/pics/agents/6-reflexion.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
    Figure 6-13. Examples of how Reflexion agents work.
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Compared to plan generation, reflection is relatively easy to implement and can bring surprisingly good performance improvement. The downside of this approach is latency and cost. Thoughts, observations, and sometimes actions can take a lot of tokens to generate, which increases cost and user-perceived latency, especially for tasks with many intermediate steps. To nudge their agents to follow the format, both ReAct and Reflexion authors used plenty of examples in their prompts. This increases the cost of computing input tokens and reduces the context space available for other information.&lt;/p&gt;

&lt;h3 id=&quot;tool_selection&quot;&gt;Tool selection&lt;/h3&gt;

&lt;p&gt;Because tools often play a crucial role in a task’s success, tool selection requires careful consideration. The tools to give your agent depend on the environment and the task, but also depends on the AI model that powers the agent.&lt;/p&gt;

&lt;p&gt;There’s no foolproof guide on how to select the best set of tools. Agent literature consists of a wide range of tool inventories. For example:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2302.04761&quot;&gt;Toolformer&lt;/a&gt; (Schick et al., 2023) finetuned GPT-J to learn 5 tools.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2304.09842&quot;&gt;Chameleon&lt;/a&gt; (Lu et al., 2023) uses 13 tools.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2305.15334&quot;&gt;Gorilla&lt;/a&gt; (Patil et al., 2023) attempted to prompt agents to select the right API call among 1,645 APIs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;More tools give the agent more capabilities. However, the more tools there are, the harder it is to efficiently use them. It’s similar to how it’s harder for humans to master a large set of tools. Adding tools also means increasing tool descriptions, which might not fit into a model’s context.&lt;/p&gt;

&lt;p&gt;Like many other decisions while building AI applications, tool selection requires experimentation and analysis. Here are a few things you can do to help you decide:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Compare how an agent performs with different sets of tools.&lt;/li&gt;
  &lt;li&gt;Do an ablation study to see how much the agent’s performance drops if a tool is removed from its inventory. If a tool can be removed without a performance drop, remove it.&lt;/li&gt;
  &lt;li&gt;Look for tools that the agent frequently makes mistakes on. If a tool proves too hard for the agent to use—for example, extensive prompting and even finetuning can’t get the model to learn to use it—change the tool.&lt;/li&gt;
  &lt;li&gt;Plot the distribution of tool calls to see what tools are most used and what tools are least used. Figure 6-14 shows the differences in tool use patterns of GPT-4 and ChatGPT in &lt;a href=&quot;https://arxiv.org/abs/2304.09842&quot;&gt;Chameleon&lt;/a&gt; (Lu et al., 2023).&lt;/li&gt;
&lt;/ul&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Different models have different tool preferences&quot; src=&quot;/assets/pics/agents/7-tool-preference.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
    Figure 6-14. Different models and tasks express different tool use patterns.
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Experiments by &lt;a href=&quot;https://arxiv.org/abs/2304.09842&quot;&gt;Chameleon&lt;/a&gt; (Lu et al., 2023) also demonstrate two points:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Different tasks require different tools. ScienceQA, the science question answering task, relies much more on knowledge retrieval tools than TabMWP, a tabular math problem-solving task.&lt;/li&gt;
  &lt;li&gt;Different models have different tool preferences. For example, GPT-4 seems to select a wider set of tools than ChatGPT. ChatGPT seems to favor image captioning, while GPT-4 seems to favor knowledge retrieval.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Tip&lt;/strong&gt;:&lt;/p&gt;

  &lt;p&gt;When evaluating an agent framework, evaluate what planners and tools it supports. Different frameworks might focus on different categories of tools. For example, AutoGPT focuses on social media APIs (Reddit, X, and Wikipedia), whereas Composio focuses on enterprise APIs (Google Apps, GitHub, and Slack).&lt;/p&gt;

  &lt;p&gt;As your needs will likely change over time, evaluate how easy it is to extend your agent to incorporate new tools.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As humans, we become more productive not just by using the tools we’re given, but also by creating progressively more powerful tools from simpler ones. Can AI create new tools from its initial tools?&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/2304.09842&quot;&gt;Chameleon&lt;/a&gt; (Lu et al., 2023) proposes the study of tool transition: after tool X, how likely is the agent to call tool Y? Figure 6-15 shows an example of tool transition. If two tools are frequently used together, they can be combined into a bigger tool. If an agent is aware of this information, the agent itself can combine initial tools to continually build more complex tools.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Tool transition&quot; src=&quot;/assets/pics/agents/8-tool-transition.png&quot; style=&quot;float: center; max-width: 70%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
    Figure 6-15. A tool transition tree by Chameleon (Lu et al., 2023).
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/2305.16291&quot;&gt;Vogager&lt;/a&gt; (Wang et al., 2023) proposes a skill manager to keep track of new skills (tools) that an agent acquires for later reuse. Each skill is a coding program. When the skill manager determines a newly created skill is to be useful (e.g., because it’s successfully helped an agent accomplish a task), it adds this skill to the skill library (conceptually similar to the tool inventory). This skill can be retrieved later to use for other tasks.&lt;/p&gt;

&lt;p&gt;Earlier in this section, we mentioned that the success of an agent in an environment depends on its tool inventory and its planning capabilities. Failures in either aspect can cause the agent to fail. The next section will discuss different failure modes of an agent and how to evaluate them.&lt;/p&gt;

&lt;h2 id=&quot;agent_failure_modes_and_evaluation&quot;&gt;Agent Failure Modes and Evaluation&lt;/h2&gt;

&lt;p&gt;Evaluation is about detecting failures. The more complex a task an agent performs, the more possible failure points there are. Other than the failure modes common to all AI applications discussed in Chapters 3 and 4, agents also have unique failures caused by planning, tool execution, and efficiency. Some of the failures are easier to catch than others.&lt;/p&gt;

&lt;p&gt;To evaluate an agent, identify its failure modes and measure how often each of these failure modes happens.&lt;/p&gt;

&lt;h3 id=&quot;planning_failures&quot;&gt;Planning failures&lt;/h3&gt;

&lt;p&gt;Planning is hard and can fail in many ways. The most common mode of planning failure is tool use failure. The agent might generate a plan with one or more of these errors.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Invalid tool&lt;/strong&gt;&lt;/p&gt;

    &lt;p&gt;For example, it generates a plan that contains &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bing_search&lt;/code&gt;, which isn’t in the tool inventory.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Valid tool, invalid parameters.&lt;/strong&gt;&lt;/p&gt;

    &lt;p&gt;For example, it calls &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lbs_to_kg&lt;/code&gt; with two parameters, but this function requires only one parameter, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lbs&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Valid tool, incorrect parameter values&lt;/strong&gt;&lt;/p&gt;

    &lt;p&gt;For example, it calls &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lbs_to_kg with &lt;/code&gt;one parameter, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lbs&lt;/code&gt;, but uses the value 100 for lbs when it should be 120.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Another mode of planning failure is goal failure: the agent fails to achieve the goal. This can be because the plan doesn’t solve a task, or it solves the task without following the constraints. To illustrate this, imagine you ask the model to plan a two-week trip from San Francisco to India with a budget of $5,000. The agent might plan a trip from San Francisco to Vietnam, or plan you a two-week trip from San Francisco to India that will cost you way over the budget.&lt;/p&gt;

&lt;p&gt;A common constraint that is often overlooked by agent evaluation is time. In many cases, the time an agent takes matters less because you can assign a task to an agent and only need to check in when it’s done. However, in many cases, the agent becomes less useful with time. For example, if you ask an agent to prepare a grant proposal and the agent finishes it after the grant deadline, the agent isn’t very helpful.&lt;/p&gt;

&lt;p&gt;An interesting mode of planning failure is caused by errors in reflection. The agent is convinced that it’s accomplished a task when it hasn’t. For example, you ask the agent to assign 50 people to 30 hotel rooms. The agent might assign only 40 people and insist that the task has been accomplished.&lt;/p&gt;

&lt;p&gt;To evaluate an agent for planning failures, one option is to create a planning dataset where each example is a tuple &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(task, tool inventory)&lt;/code&gt;. For each task, use the agent to generate a K number of plans. Compute the following metrics:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Out of all generated plans, how many are valid?&lt;/li&gt;
  &lt;li&gt;For a given task, how many plans does the agent have to generate to get a valid plan?&lt;/li&gt;
  &lt;li&gt;Out of all tool calls, how many are valid?&lt;/li&gt;
  &lt;li&gt;How often are invalid tools called?&lt;/li&gt;
  &lt;li&gt;How often are valid tools called with invalid parameters?&lt;/li&gt;
  &lt;li&gt;How often are valid tools called with incorrect parameter values?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Analyze the agent’s outputs for patterns. What types of tasks does the agent fail more on? Do you have a hypothesis why? What tools does the model frequently make mistakes with? Some tools might be harder for an agent to use. You can improve an agent’s ability to use a challenging tool by better prompting, more examples, or finetuning. If all fail, you might consider swapping out this tool for something easier to use.&lt;/p&gt;

&lt;h3 id=&quot;tool_failures&quot;&gt;Tool failures&lt;/h3&gt;

&lt;p&gt;Tool failures happen when the correct tool is used, but the tool output is wrong. One failure mode is when a tool just gives the wrong outputs. For example, an image captioner returns a wrong description, or an SQL query generator returns a wrong SQL query.&lt;/p&gt;

&lt;p&gt;If the agent generates only high-level plans and a translation module is involved in translating from each planned action to executable commands, failures can happen because of translation errors.&lt;/p&gt;

&lt;p&gt;Tool failures are tool-dependent. Each tool needs to be tested independently. Always print out each tool call and its output so that you can inspect and evaluate them. If you have a translator, create benchmarks to evaluate it.&lt;/p&gt;

&lt;p&gt;Detecting missing tool failures requires an understanding of what tools should be used. If your agent frequently fails on a specific domain, this might be because it lacks tools for this domain. Work with human domain experts and observe what tools they would use.&lt;/p&gt;

&lt;h3 id=&quot;efficiency&quot;&gt;Efficiency&lt;/h3&gt;

&lt;p&gt;An agent might generate a valid plan using the right tools to accomplish a task, but it might be inefficient. Here are a few things you might want to track to evaluate an agent’s efficiency:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;How many steps does the agent need, on average, to complete a task?&lt;/li&gt;
  &lt;li&gt;How much does the agent cost, on average, to complete a task?&lt;/li&gt;
  &lt;li&gt;How long does each action typically take? Are there any actions that are especially time-consuming or expensive?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can compare these metrics with your baseline, which can be another agent or a human operator. When comparing AI agents to human agents, keep in mind that humans and AI have very different modes of operation, so what’s considered efficient for humans might be inefficient for AI and vice versa. For example, visiting 100 web pages might be inefficient for a human agent who can only visit one page at a time but trivial for an AI agent that can visit all the web pages at once.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;At its core, the concept of an agent is fairly simple. An agent is defined by the environment it operates in and the set of tools it has access to. In an AI-powered agent, the AI model is the brain that leverages its tools and feedback from the environment to plan how best to accomplish a task. Access to tools makes a model vastly more capable, so the agentic pattern is inevitable.&lt;/p&gt;

&lt;p&gt;While the concept of “agents” sounds novel, they are built upon many concepts that have been used since the early days of LLMs, including self-critique, chain-of-thought, and structured outputs.&lt;/p&gt;

&lt;p&gt;This post covered conceptually how agents work and different components of an agent. In a future post, I’ll discuss how to evaluate agent frameworks.&lt;/p&gt;

&lt;p&gt;The agentic pattern often deals with information that exceeds a model’s context limit. A memory system that supplements the model’s context in handling information can significantly enhance an agent’s capabilities. Since this post is already long, I’ll explore how a memory system works in a future blog post.&lt;/p&gt;
</description>
        <pubDate>Tue, 07 Jan 2025 00:00:00 +0000</pubDate>
        <link>https://huyenchip.com//2025/01/07/agents.html</link>
        <guid isPermaLink="true">https://huyenchip.com/2025/01/07/agents.html</guid>
      </item>
    
      <item>
        <title>Building A Generative AI Platform</title>
        <description>&lt;p&gt;After studying how companies deploy generative AI applications, I noticed many similarities in their platforms. This post outlines the common components of a generative AI platform, what they do, and how they are implemented. I try my best to keep the architecture general, but certain applications might deviate. This is what the overall architecture looks like.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Overview of a genai platform&quot; src=&quot;/assets/pics/genai-platform/1-genai-platform.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;
This is a pretty complex system. This post will start from the simplest architecture and progressively add more components. In its simplest form, your application receives a query and sends it to the model. The model generates a response, which is returned to the user. There are no guardrails, no augmented context, and no optimization. The &lt;strong&gt;Model API&lt;/strong&gt; box refers to both third-party APIs (e.g., OpenAI, Google, Anthropic) and self-hosted APIs.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Overview of a genai platform&quot; src=&quot;/assets/pics/genai-platform/2.png&quot; style=&quot;float: center; max-width: 50%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;
From this, you can add more components as needs arise. The order discussed in this post is common, though you don’t need to follow the exact same order. A component can be skipped if your system works well without it. Evaluation is necessary at every step of the development process.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Enhance context input into a model by giving the model access to external data sources and tools for information gathering.&lt;/li&gt;
  &lt;li&gt;Put in guardrails to protect your system and your users.&lt;/li&gt;
  &lt;li&gt;Add model router and gateway to support complex pipelines and add more security.&lt;/li&gt;
  &lt;li&gt;Optimize for latency and costs with cache.&lt;/li&gt;
  &lt;li&gt;Add complex logic and write actions to maximize your system’s capabilities.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Observability, which allows you to gain visibility into your system for monitoring and debugging, and orchestration, which involves chaining all the components together, are two essential components of the platform. We will discuss them at the end of this post.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» What this post is not «&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This post focuses on the overall architecture for deploying AI applications. It discusses what components are needed and considerations when building these components. It’s not about how to build AI applications and, therefore, does NOT discuss model evaluation, application evaluation, prompt engineering, finetuning, data annotation guidelines, or chunking strategies for RAGs. All these topics are covered in my upcoming book &lt;a href=&quot;https://oreillymedia.pxf.io/GmaeBn&quot;&gt;AI Engineering&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2 id=&quot;step_1_enhance_context&quot;&gt;Step 1. Enhance Context&lt;/h2&gt;

&lt;p&gt;The initial expansion of a platform usually involves adding mechanisms to allow the system to augment each query with the necessary information. Gathering the relevant information is called context construction.&lt;/p&gt;

&lt;p&gt;Many queries require context to answer. The more relevant information there is in the context, the less the model has to rely on its internal knowledge, which can be unreliable due to its training data and training methodology. Studies have shown that having access to relevant information in the context can help the model generate more detailed responses while reducing hallucinations (&lt;a href=&quot;https://arxiv.org/abs/2005.11401&quot;&gt;Lewis et al.&lt;/a&gt;, 2020).&lt;/p&gt;

&lt;p&gt;For example, given the query “Will Acme’s fancy-printer-A300 print 100pps?”, the model will be able to respond better if it’s given the specifications of fancy-printer-A300. (Thanks Chetan Tekur for the example.)&lt;/p&gt;

&lt;p&gt;Context construction for foundation models is equivalent to feature engineering for classical ML models. They serve the same purpose: giving the model the necessary information to process an input.&lt;/p&gt;

&lt;p&gt;In-context learning, learning from the context, is a form of continual learning. It enables a model to incorporate new information continually to make decisions, preventing it from becoming outdated. For example, a model trained on last-week data won’t be able to answer questions about this week unless the new information is included in its context. By updating a model’s context with the latest information, e.g. fancy-printer-A300’s latest specifications, the model remains up-to-date and can respond to queries beyond its cut-off date.&lt;/p&gt;

&lt;h3 id=&quot;rags&quot;&gt;RAGs&lt;/h3&gt;

&lt;p&gt;The most well-known pattern for context construction is RAG, Retrieval-Augmented Generation. RAG consists of two components: a generator (e.g. a language model) and a retriever, which retrieves relevant information from external sources.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Overview of a genai platform&quot; src=&quot;/assets/pics/genai-platform/3-rag.png&quot; style=&quot;float: center; max-width: 63%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;
Retrieval isn’t unique to RAGs. It’s the backbone of search engines, recommender systems, log analytics, etc. Many retrieval algorithms developed for traditional retrieval systems can be used for RAGs.&lt;/p&gt;

&lt;p&gt;External memory sources typically contain unstructured data, such as memos, contracts, news updates, etc. They can be collectively called &lt;em&gt;documents&lt;/em&gt;. A document can be 10 tokens or 1 million tokens. Naively retrieving whole documents can cause your context to be arbitrarily long. RAG typically requires documents to be split into &lt;em&gt;manageable chunks&lt;/em&gt;, which can be determined from the model’s maximum context length and your application’s latency requirements. To learn more about chunking and the optimal chunk size, see &lt;a href=&quot;https://www.pinecone.io/learn/chunking-strategies/&quot;&gt;Pinecone&lt;/a&gt;, &lt;a href=&quot;https://js.langchain.com/v0.1/docs/modules/data_connection/document_transformers/&quot;&gt;Langchain&lt;/a&gt;, &lt;a href=&quot;https://docs.llamaindex.ai/en/stable/optimizing/production_rag/&quot;&gt;Llamaindex&lt;/a&gt;, and &lt;a href=&quot;https://www.youtube.com/watch?v=8OJC21T2SL4&quot;&gt;Greg Kamradt&lt;/a&gt;’s tutorials.&lt;/p&gt;

&lt;p&gt;Once data from external memory sources has been loaded and chunked, retrieval is performed using two main approaches.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Term-based retrieval&lt;/strong&gt; &lt;br /&gt;
This can be as simple as keyword search. For example, given the query “transformer”, fetch all documents containing this keyword. More sophisticated algorithms include BM25 (which leverages TF-IDF) and Elasticsearch (which leverages inverted index). &lt;br /&gt;
 &lt;br /&gt;
Term-based retrieval is usually used for text data, but it also works for images and videos that have text metadata such as titles, tags, captions, comments, etc.
&lt;br /&gt;
&lt;br /&gt;&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Embedding-based retrieval&lt;/strong&gt; (also known as vector search) &lt;br /&gt;
You convert chunks of data into embedding vectors using an embedding model such as &lt;a href=&quot;https://arxiv.org/abs/1810.04805&quot;&gt;BERT&lt;/a&gt;, &lt;a href=&quot;https://github.com/UKPLab/sentence-transformers&quot;&gt;sentence-transformers&lt;/a&gt;, and proprietary embedding models provided by OpenAI or Google. Given a query, the data whose vectors are closest to the query embedding, as determined by the vector search algorithm, is retrieved. &lt;br /&gt;
 &lt;br /&gt;
Vector search is usually framed as nearest-neighbor search, using approximate nearest neighbor (ANN) algorithms such as &lt;a href=&quot;https://arxiv.org/abs/1702.08734&quot;&gt;FAISS&lt;/a&gt; (Facebook AI Similarity Search), Google’s &lt;a href=&quot;https://research.google/blog/announcing-scann-efficient-vector-similarity-search/&quot;&gt;ScaNN&lt;/a&gt;, Spotify’s &lt;a href=&quot;https://github.com/spotify/annoy&quot;&gt;ANNOY&lt;/a&gt;, and &lt;a href=&quot;https://github.com/nmslib/hnswlib&quot;&gt;hnswlib&lt;/a&gt; (&lt;a href=&quot;https://arxiv.org/abs/1603.09320&quot;&gt;Hierarchical Navigable Small World&lt;/a&gt;).
  &lt;br /&gt;
The &lt;a href=&quot;https://ann-benchmarks.com/&quot;&gt;ANN-benchmarks website &lt;/a&gt;compares different ANN algorithms on multiple datasets using four main metrics, taking into account the tradeoffs between indexing and querying.&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Recall&lt;/strong&gt;: the fraction of the nearest neighbors found by the algorithm.&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Query per second (QPS)&lt;/strong&gt;: the number of queries the algorithm can handle per second. This is crucial for high-traffic applications.&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Build time&lt;/strong&gt;: the time required to build the index. This metric is important especially if you need to frequently update your index (e.g. because your data changes).&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Index size&lt;/strong&gt;: the size of the index created by the algorithm, which is crucial for assessing its scalability and storage requirements.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This works with not just text documents, but also images, videos, audio, and code. Many teams even try to summarize SQL tables and dataframes and then use these summaries to generate embeddings for retrieval.&lt;/p&gt;

&lt;p&gt;Term-based retrieval is much faster and cheaper than embedding-based retrieval. It can work well out of the box, making it an attractive option to start. Both BM25 and Elasticsearch are widely used in the industry and serve as formidable baselines for more complex retrieval systems. Embedding-based retrieval, while computationally expensive, can be significantly improved over time to outperform term-based retrieval.&lt;/p&gt;

&lt;p&gt;A production retrieval system typically combines several approaches. Combining term-based retrieval and embedding-based retrieval is called &lt;em&gt;hybrid search&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;One common pattern is sequential. First, a cheap, less precise retriever, such as a term-based system, fetches candidates. Then, a more precise but more expensive mechanism, such as k-nearest neighbors, finds the best of these candidates. The second step is also called reranking.&lt;/p&gt;

&lt;p&gt;For example, given the term “transformer”, you can fetch all documents that contain the word transformer, regardless of whether they are about the electric device, the neural architecture, or the movie. Then you use vector search to find among these documents those that are actually related to your transformer query.&lt;/p&gt;

&lt;p&gt;Context reranking differs from traditional search reranking in that the exact position of items is less critical. In search, the rank (e.g., first or fifth) is crucial. In context reranking, the order of documents still matters because it affects how well a model can process them. Models might better understand documents at the beginning and end of the context, as suggested by the paper &lt;a href=&quot;https://arxiv.org/abs/2307.03172&quot;&gt;Lost in the middle&lt;/a&gt; (Liu et al., 2023). However, as long as a document is included, the impact of its order is less significant compared to in search ranking.&lt;/p&gt;

&lt;p&gt;Another pattern is ensemble. Remember that a retriever works by ranking documents by their relevance scores to the query. You use multiple retrievers to fetch candidates at the same time, then combine these different rankings together to generate a final ranking.&lt;/p&gt;

&lt;h4 id=&quot;rags_with_tabular_data&quot;&gt;RAGs with tabular data&lt;/h4&gt;

&lt;p&gt;External data sources can also be structured, such as dataframes or SQL tables. Retrieving data from an SQL table is significantly different from retrieving data from unstructured documents. Given a query, the system works as follows.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Text-to-SQL&lt;/strong&gt;: Based on the user query and the table schemas, determine what SQL query is needed.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;SQL execution&lt;/strong&gt;: Execute the SQL query.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Generation&lt;/strong&gt;: Generate a response based on the SQL result and the original user query.&lt;/li&gt;
&lt;/ol&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Overview of a genai platform&quot; src=&quot;/assets/pics/genai-platform/4-rag-with-tabular-data.png&quot; style=&quot;float: center; max-width: 63%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;
For the text-to-SQL step, if there are many available tables whose schemas can’t all fit into the model context, you might need an intermediate step to predict what tables to use for each query. Text-to-SQL can be done by the same model used to generate the final response or one of many specialized text-to-SQL models.&lt;/p&gt;

&lt;h4 id=&quot;agentic_rags&quot;&gt;Agentic RAGs&lt;/h4&gt;

&lt;p&gt;An important source of data is the Internet. A web search tool like Google or Bing API can give the model access to a rich, up-to-date resource to gather relevant information for each query. For example, given the query “Who won Oscar this year?”, the system searches for information about the latest Oscar and uses this information to generate the final response to the user.&lt;/p&gt;

&lt;p&gt;Term-based retrieval, embedding-based retrieval, SQL execution, and web search are actions that a model can take to augment its context. You can think of each action as a function the model can call. A workflow that can incorporate external actions is also called &lt;em&gt;agentic&lt;/em&gt;. The architecture then looks like this.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Overview of a genai platform&quot; src=&quot;/assets/pics/genai-platform/5-agentic-rag.png&quot; style=&quot;float: center; max-width: 90%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» Action vs. tool «&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A tool allows one or more actions. For example, a people search tool might allow two actions: search by name and search by email. However, the difference is minimal, so many people use &lt;em&gt;action&lt;/em&gt; and &lt;em&gt;tool&lt;/em&gt; interchangeably.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;» Read-only actions vs. write actions «&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Actions that retrieve information from external sources but don’t change their states are read-only actions. Giving a model write actions, e.g. updating the values in a table, enables the model to perform more tasks but also poses more risks, which will be discussed later.&lt;/p&gt;

&lt;h3 id=&quot;query_rewriting&quot;&gt;Query rewriting&lt;/h3&gt;

&lt;p&gt;Often, a user query needs to be rewritten to increase the likelihood of fetching the right information. Consider the following conversation.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;User: When was the last time John Doe bought something from us?
AI: John last bought a Fruity Fedora hat from us two weeks ago, on January 3, 2030.
User: How about Emily Doe?
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The last question, “How about Emily Doe?”, is ambiguous. If you use this query verbatim to retrieve documents, you’ll likely get irrelevant results. You need to rewrite this query to reflect what the user is actually asking. The new query should make sense on its own. The last question should be rewritten to “When was the last time Emily Doe bought something from us?”&lt;/p&gt;

&lt;p&gt;Query rewriting is typically done using other AI models, using a prompt similar to “Given the following conversation, rewrite the last user input to reflect what the user is actually asking.”&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Overview of a genai platform&quot; src=&quot;/assets/pics/genai-platform/6-query-rewriting.png&quot; style=&quot;float: center; max-width: 70%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Query rewriting can get complicated, especially if you need to do identity resolution or incorporate other knowledge. If the user asks “How about his wife?”, you will first need to query your database to find out who his wife is. If you don’t have this information, the rewriting model should acknowledge that this query isn’t solvable instead of hallucinating a name, leading to a wrong answer.&lt;/p&gt;

&lt;h2 id=&quot;step_2_put_in_guardrails&quot;&gt;Step 2. Put in Guardrails&lt;/h2&gt;

&lt;p&gt;Guardrails help reduce AI risks and protect not just your users but also you, the developers. They should be placed whenever there is potential for failures. This post discusses two types of guardrails: input guardrails and output guardrails.&lt;/p&gt;

&lt;h3 id=&quot;input_guardrails&quot;&gt;Input guardrails&lt;/h3&gt;

&lt;p&gt;Input guardrails are typically protection against two types of risks: leaking private information to external APIs, and executing bad prompts that compromise your system (model jailbreaking).&lt;/p&gt;

&lt;h4 id=&quot;leaking_private_information_to_external_apis&quot;&gt;Leaking private information to external APIs&lt;/h4&gt;

&lt;p&gt;This risk is specific to using external model APIs when you need to send your data outside your organization. For example, an employee might copy the company’s secret or a user’s private information into a prompt and send it to wherever the model is hosted. &lt;br /&gt;
 &lt;br /&gt;
One of the most notable early incidents was when Samsung employees put Samsung’s proprietary information into ChatGPT, accidentally &lt;a href=&quot;https://www.techradar.com/news/samsung-workers-leaked-company-secrets-by-using-chatgpt&quot;&gt;leaking the company’s secrets&lt;/a&gt;. It’s unclear how Samsung discovered this leak and how the leaked information was used against Samsung. However, the incident was serious enough for Samsung to &lt;a href=&quot;https://www.bloomberg.com/news/articles/2023-05-02/samsung-bans-chatgpt-and-other-generative-ai-use-by-staff-after-leak&quot;&gt;ban ChatGPT in May 2023&lt;/a&gt;. &lt;br /&gt;
 &lt;br /&gt;
There’s no airtight way to eliminate potential leaks when using third-party APIs. However, you can mitigate them with guardrails. You can use one of the many available tools that automatically detect sensitive data. What sensitive data to detect is specified by you. Common sensitive data classes are:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Personal information (ID numbers, phone numbers, bank accounts).&lt;/li&gt;
  &lt;li&gt;Human faces.&lt;/li&gt;
  &lt;li&gt;Specific keywords and phrases associated with the company’s intellectual properties or privileged information.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Many sensitive data detection tools use AI to identify potentially sensitive information, such as determining if a string resembles a valid home address. If a query is found to contain sensitive information, you have two options: block the entire query or remove the sensitive information from it. For instance, you can mask a user’s phone number with the placeholder [PHONE NUMBER]. If the generated response contains this placeholder, use a PII reversible dictionary that maps this placeholder to the original information so that you can unmask it, as shown below.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Overview of a genai platform&quot; src=&quot;/assets/pics/genai-platform/7-reversible-pii-mapping.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;h4 id=&quot;jailbreaking&quot;&gt;Model jailbreaking&lt;/h4&gt;

&lt;p&gt;It’s become an online sport to try to jailbreak AI models, getting them to say or do bad things. While some might find it amusing to get ChatGPT to make controversial statements, it’s much less fun if your customer support chatbot, branded with your name and logo, does the same thing. This can be especially dangerous for AI systems that have access to tools. Imagine if a user finds a way to get your system to execute an SQL query that corrupts your data. &lt;br /&gt;
 &lt;br /&gt;
To combat this, you should first put guardrails on your system so that no harmful actions can be automatically executed. For example, no SQL queries that can insert, delete, or update data can be executed without human approval. The downside of this added security is that it can slow down your system.&lt;/p&gt;

&lt;p&gt;To prevent your application from making outrageous statements it shouldn’t be making, you can define out-of-scope topics for your application. For example, if your application is a customer support chatbot, it shouldn’t answer political or social questions. A simple way to do so is to filter out inputs that contain predefined phrases typically associated with controversial topics, such as “immigration” or “antivax”. More sophisticated algorithms use AI to classify whether an input is about one of the pre-defined restricted topics. &lt;br /&gt;
 &lt;br /&gt;
If harmful prompts are rare in your system, you can use an anomaly detection algorithm to identify unusual prompts.&lt;/p&gt;

&lt;h3 id=&quot;output_guardrails&quot;&gt;Output guardrails&lt;/h3&gt;

&lt;p&gt;AI models are probabilistic, making their outputs unreliable. You can put in guardrails to significantly improve your application’s reliability. Output guardrails have two main functionalities:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Evaluate the quality of each generation.&lt;/li&gt;
  &lt;li&gt;Specify the policy to deal with different failure modes.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4 id=&quot;output_quality_measurement&quot;&gt;Output quality measurement&lt;/h4&gt;

&lt;p&gt;To catch outputs that fail to meet your standards, you need to understand what failures look like. Here are examples of failure modes and how to catch them.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Empty responses&lt;/strong&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Malformatted responses&lt;/strong&gt; that don’t follow the expected output format. For example, if the application expects JSON and the generated response has a missing closing bracket. There are validators for certain formats, such as regex, JSON, and Python code validators. There are also tools for &lt;a href=&quot;https://huyenchip.com/2024/01/16/sampling.html#constraint_sampling&quot;&gt;constrained sampling&lt;/a&gt; such as guidance, outlines, and instructor.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Toxic responses&lt;/strong&gt;, such as those that are racist or sexist. These responses can be caught using one of many toxicity detection tools.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Factual inconsistent responses&lt;/strong&gt; hallucinated by the model. Hallucination detection is an active area of research with solutions such as &lt;a href=&quot;https://arxiv.org/abs/2303.08896&quot;&gt;SelfCheckGPT&lt;/a&gt; (Manakul et al., 2023) and &lt;a href=&quot;https://arxiv.org/abs/2403.18802&quot;&gt;SAFE&lt;/a&gt;, Search Engine Factuality Evaluator (Wei et al., 2024). You can mitigate hallucinations by providing models with sufficient context and prompting techniques such as chain-of-thought. Hallucination detection and mitigation are discussed further in my upcoming book &lt;a href=&quot;https://learning.oreilly.com/library/view/ai-engineering/9781098166298/&quot;&gt;AI Engineering&lt;/a&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Responses that contain sensitive information&lt;/strong&gt;. This can happen in two scenarios.
    &lt;ol&gt;
      &lt;li&gt;Your model was trained on sensitive data and regurgitates it back.&lt;/li&gt;
      &lt;li&gt;Your system retrieves sensitive information from your internal database to enrich its context, and then it passes this sensitive information on to the response.&lt;/li&gt;
    &lt;/ol&gt;

    &lt;p&gt;This failure mode can be prevented by not training your model on sensitive data and not allowing it to retrieve sensitive data in the first place. Sensitive data in outputs can be detected using the same tools used for input guardrails.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Brand-risk responses&lt;/strong&gt;, such as responses that mischaracterize your company or your competitors. An example is when Grok, a model trained by X, generated a response &lt;a href=&quot;https://x.com/JaxWinterbourne/status/1733339886155968714&quot;&gt;suggesting that Grok was trained by OpenAI&lt;/a&gt;, causing the Internet to suspect X of stealing OpenAI’s data. This failure mode can be mitigated with keyword monitoring. Once you’ve identified outputs concerning your brands and competitors, you can either block these outputs, pass them onto human reviewers, or use other models to detect the sentiment of these outputs to ensure that only the right sentiments are returned.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Generally bad responses&lt;/strong&gt;. For example, if you ask the model to write an essay and that essay is just bad, or if you ask the model for a low-calorie cake recipe and the generated recipe contains an excessive amount of sugar. It’s become a popular practice to use AI judges to evaluate the quality of models’ responses. These AI judges can be general-purpose models (think ChatGPT, Claude) or specialized scorers trained to output a concrete score for a response given a query.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4 id=&quot;failure_management&quot;&gt;Failure management&lt;/h4&gt;

&lt;p&gt;AI models are probabilistic, which means that if you try a query again, you might get a different response. Many failures can be mitigated using a basic retry logic. For example, if the response is empty, try again X times or until you get a non-empty response. Similarly, if the response is malformatted, try again until the model generates a correctly formatted response.&lt;/p&gt;

&lt;p&gt;This retry policy, however, can incur extra latency and cost. One retry means 2x the number of API calls. If the retry is carried out after failure, the latency experienced by the user will double. To reduce latency, you can make calls in parallel. For example, for each query, instead of waiting for the first query to fail before retrying, you send this query to the model twice at the same time, get back two responses, and pick the better one. This increases the number of redundant API calls but keeps latency manageable.&lt;/p&gt;

&lt;p&gt;It’s also common to fall back on humans to handle tricky queries. For example, you can transfer a query to human operators if it contains specific key phrases. Some teams use a specialized model, potentially trained in-house, to decide when to transfer a conversation to humans. One team, for instance, transfers a conversation to human operators when their sentiment analysis model detects that the user is getting angry. Another team transfers a conversation after a certain number of turns to prevent users from getting stuck in an infinite loop.&lt;/p&gt;

&lt;h3 id=&quot;guardrail_tradeoffs&quot;&gt;Guardrail tradeoffs&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Reliability vs. latency tradeoff&lt;/strong&gt;: While acknowledging the importance of guardrails, some teams told me that latency is more important. They decided not to implement guardrails because they can significantly increase their application’s latency. However, these teams are in the minority. Most teams find that the increased risks are more costly than the added latency.&lt;/p&gt;

&lt;p&gt;Output guardrails might not work well in the stream completion mode. By default, the whole response is generated before shown to the user, which can take a long time. In the stream completion mode, new tokens are streamed to the user as they are generated, reducing the time the user has to wait to see the response. The downside is that it’s hard to evaluate partial responses, so unsafe responses might be streamed to users before the system guardrails can determine that they should be blocked.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Self-hosted vs. third-party API tradeoff&lt;/strong&gt;: Self-hosting your models means that you don’t have to send your data to a third party, reducing the need for input guardrails. However, it also means that you must implement all the necessary guardrails yourself, rather than relying on the guardrails provided by third-party services.&lt;/p&gt;

&lt;p&gt;Our platform now looks like this. Guardrails can be independent tools or parts of model gateways, as discussed later. Scorers, if used, are grouped under model APIs since scorers are typically AI models, too. Models used for scoring are typically smaller and faster than models used for generation.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Overview of a genai platform&quot; src=&quot;/assets/pics/genai-platform/8-guardrails.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2 id=&quot;step_3_add_model_router_and_gateway&quot;&gt;Step 3. Add Model Router and Gateway&lt;/h2&gt;

&lt;p&gt;As applications grow in complexity and involve more models, two types of tools emerged to help you work with multiple models: routers and gateways.&lt;/p&gt;

&lt;h3 id=&quot;router&quot;&gt;Router&lt;/h3&gt;
&lt;p&gt;An application can use different models to respond to different types of queries. Having different solutions for different queries has several benefits. First, this allows you to have specialized solutions, such as one model specialized in technical troubleshooting and another specialized in subscriptions. Specialized models can potentially perform better than a general-purpose model. Second, this can help you save costs. Instead of routing all queries to an expensive model, you can route simpler queries to cheaper models.&lt;/p&gt;

&lt;p&gt;A router typically consists of &lt;strong&gt;an intent classifier&lt;/strong&gt; that predicts what the user is trying to do. Based on the predicted intent, the query is routed to the appropriate solution. For example, for a customer support chatbot, if the intent is:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;To reset a password –&amp;gt; route this user to the page about password resetting.&lt;/li&gt;
  &lt;li&gt;To correct a billing mistake –&amp;gt; route this user to a human operator.&lt;/li&gt;
  &lt;li&gt;To troubleshoot a technical issue –&amp;gt; route this query to a model finetuned for troubleshooting.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An intent classifier can also help your system avoid out-of-scope conversations. For example, you can have an intent classifier that predicts whether a query is out of the scope. If the query is deemed inappropriate (e.g. if the user asks who you would vote for in the upcoming election), the chatbot can politely decline to engage using one of the stock responses (“As a chatbot, I don’t have the ability to vote. If you have questions about our products, I’d be happy to help.”) without wasting an API call.&lt;/p&gt;

&lt;p&gt;If your system has access to multiple actions, a router can involve a &lt;strong&gt;next-action predictor&lt;/strong&gt; to help the system decide what action to take next. One valid action is to ask for clarification if the query is ambiguous. For example, in response to the query “Freezing,” the system might ask, “Do you want to freeze your account or are you talking about the weather?” or simply say, “I’m sorry. Can you elaborate?”&lt;/p&gt;

&lt;p&gt;Intent classifiers and next-action predictors can be general-purpose models or specialized classification models. Specialized classification models are typically much smaller and faster than general-purpose models, allowing your system to use multiple of them without incurring significant extra latency and cost.&lt;/p&gt;

&lt;p&gt;When routing queries to models with varying context limits, the query’s context might need to be adjusted accordingly. Consider a query of 1,000 tokens that is slated for a model with a 4K context limit. The system then takes an action, e.g. web search, that brings back 8,000-token context. You can either truncate the query’s context to fit the originally intended model or route the query to a model with a larger context limit.&lt;/p&gt;

&lt;h3 id=&quot;gateway&quot;&gt;Gateway&lt;/h3&gt;
&lt;p&gt;A model gateway is an intermediate layer that allows your organization to interface with different models in a unified and secure manner. The most basic functionality of a model gateway is to enable developers to access different models – be it self-hosted models or models behind commercial APIs such as OpenAI or Google – the same way. A model gateway makes it easier to maintain your code. If a model API changes, you only need to update the model gateway instead of having to update all applications that use this model API.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Overview of a genai platform&quot; src=&quot;/assets/pics/genai-platform/9-llm-gateway.png&quot; style=&quot;float: center; max-width: 80%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;In its simplest form, a model gateway is a unified wrapper that looks like the following code example. This example is to give you an idea of how a model gateway might be implemented. It’s not meant to be functional as it doesn’t contain any error checking or optimization.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;import google.generativeai as genai
import openai

def openai_model(input_data, model_name, max_tokens):
    openai.api_key = os.environ[&quot;OPENAI_API_KEY&quot;]
    response = openai.Completion.create(
        engine=model_name,
        prompt=input_data,
        max_tokens=max_tokens
    )
    return {&quot;response&quot;: response.choices[0].text.strip()}

def gemini_model(input_data, model_name, max_tokens):
    genai.configure(api_key=os.environ[&quot;GOOGLE_API_KEY&quot;])
    model = genai.GenerativeModel(model_name=model_name)
    response = model.generate_content(input_data, max_tokens=max_tokens)
    return {&quot;response&quot;: response[&quot;choices&quot;][0][&quot;message&quot;][&quot;content&quot;]}

@app.route(&apos;/model&apos;, methods=[&apos;POST&apos;])
def model_gateway():
    data = request.get_json()
    model_type = data.get(&quot;model_type&quot;)
    model_name = data.get(&quot;model_name&quot;)
    input_data = data.get(&quot;input_data&quot;)
    max_tokens = data.get(&quot;max_tokens&quot;)

    if model_type == &quot;openai&quot;:
        result = openai_model(input_data, model_name, max_tokens)
    elif model_type == &quot;gemini&quot;:
        result = gemini_model(input_data, model_name, max_tokens)
    return jsonify(result)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A model gateway is &lt;strong&gt;access control and cost management&lt;/strong&gt;. Instead of giving everyone who wants access to the OpenAI API your organizational tokens, which can be easily leaked, you only give people access to the model gateway, creating a centralized and controlled point of access. The gateway can also implement fine-grained access controls, specifying which user or application should have access to which model. Moreover, the gateway can monitor and limit the usage of API calls, preventing abuse and managing costs effectively.&lt;/p&gt;

&lt;p&gt;A model gateway can also be used to implement fallback policies to overcome rate limits or API failures (the latter is unfortunately common). When the primary API is unavailable, the gateway can route requests to alternative models, retry after a short wait, or handle failures in other graceful manners. This ensures that your application can operate smoothly without interruptions.&lt;/p&gt;

&lt;p&gt;Since requests and responses are already flowing through the gateway, it’s a good place to implement other functionalities such as load balancing, logging, and analytics. Some gateway services even provide caching and guardrails.&lt;/p&gt;

&lt;p&gt;Given that gateways are relatively straightforward to implement, there are many off-the-shelf gateways. Examples include Portkey’s &lt;a href=&quot;https://github.com/Portkey-AI/gateway&quot;&gt;gateway&lt;/a&gt;, &lt;a href=&quot;https://mlflow.org/docs/latest/llms/gateway/index.html&quot;&gt;MLflow AI Gateway&lt;/a&gt;, WealthSimple’s &lt;a href=&quot;https://github.com/wealthsimple/llm-gateway&quot;&gt;llm-gateway&lt;/a&gt;, &lt;a href=&quot;https://docs.truefoundry.com/docs/ai-gateway&quot;&gt;TrueFoundry&lt;/a&gt;, &lt;a href=&quot;https://konghq.com/products/kong-ai-gateway&quot;&gt;Kong&lt;/a&gt;, and &lt;a href=&quot;https://developers.cloudflare.com/ai-gateway/&quot;&gt;Cloudflare&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;With the added gateway and routers, our platform is getting more exciting. Like scoring, routing is also in the model gateway. Like models used for scoring, models used for routing are typically smaller than models used for generation.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Overview of a genai platform&quot; src=&quot;/assets/pics/genai-platform/10-model-gateway.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2 id=&quot;step_4_reduce_latency_with_cache&quot;&gt;Step 4. Reduce Latency with Cache&lt;/h2&gt;

&lt;p&gt;When I shared this post with my friend Eugene Yan, he said that cache is perhaps the most underrated component of an AI platform. Caching can significantly reduce your application’s latency and cost.&lt;/p&gt;

&lt;p&gt;Cache techniques can also be used during training, but since this post is about deployment, I’ll focus on cache for inference. Some common inference caching techniques include prompt cache, exact cache, and semantic cache. Prompt cache are typically implemented by the inference APIs that you use. When evaluating an inference library, it’s helpful to understand what cache mechanism it supports.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;KV cache for the attention mechanism is out of scope for this discussion.&lt;/em&gt;&lt;/p&gt;

&lt;h3 id=&quot;prompt_cache&quot;&gt;Prompt cache&lt;/h3&gt;

&lt;p&gt;Many prompts in an application have overlapping text segments. For example, all queries can share the same system prompt. A prompt cache stores these overlapping segments for reuse, so you only need to process them once. A common overlapping text segment in different prompts is the system prompt. Without prompt cache, your model needs to process the system prompt with every query. With prompt cache, it only needs to process the system prompt once for the first query.&lt;/p&gt;

&lt;p&gt;For applications with long system prompts, prompt cache can significantly reduce both latency and cost. If your system prompt is 1000 tokens and your application generates 1 million model API calls today, a prompt cache will save you from processing approximately 1 billion repetitive input tokens a day! However, this isn’t entirely free. Like KV cache, prompt cache size can be quite large and require significant engineering effort.&lt;/p&gt;

&lt;p&gt;Prompt cache is also useful for queries that involve long documents. For example, if many of your user queries are related to the same long document (such as a book or a codebase), this long document can be cached for reuse across queries.&lt;/p&gt;

&lt;p&gt;Since its introduction in November 2023 by &lt;a href=&quot;https://arxiv.org/pdf/2311.04934&quot;&gt;Gim et al.&lt;/a&gt;, prompt cache has already been incorporated into model APIs. Google announced that Gemini APIs will offer this functionality in June 2024 under the name &lt;em&gt;&lt;a href=&quot;https://ai.google.dev/gemini-api/docs/caching&quot;&gt;context cache&lt;/a&gt;&lt;/em&gt;. Cached input tokens are given a 75% discount compared to regular input tokens, but you’ll have to pay extra for cache storage (as of writing, $1.00 / 1 million tokens per hour). Given the obvious benefits of prompt cache, I wouldn’t be surprised if it becomes as popular as KV cache.&lt;/p&gt;

&lt;p&gt;While llama.cpp also has &lt;a href=&quot;https://github.com/ggerganov/llama.cpp/blob/master/examples/main/README.md#prompt-caching&quot;&gt;prompt cache&lt;/a&gt;, it seems to only cache whole prompts and work for queries in the same chat session. Its documentation is limited, but my guess from reading the code is that in a long conversation, it caches the previous messages and only processes the newest message.&lt;/p&gt;

&lt;h3 id=&quot;exact_cache&quot;&gt;Exact cache&lt;/h3&gt;

&lt;p&gt;If prompt cache and KV cache are unique to foundation models, exact cache is more general and straightforward. Your system stores processed items for reuse later when the exact items are requested. For example, if a user asks a model to summarize a product, the system checks the cache to see if a summary of this product is cached. If yes, fetch this summary. If not, summarize the product and cache the summary.&lt;/p&gt;

&lt;p&gt;Exact cache is also used for embedding-based retrieval to avoid redundant vector search. If an incoming query is already in the vector search cache, fetch the cached search result. If not, perform a vector search for this query and cache the result.&lt;/p&gt;

&lt;p&gt;Cache is especially appealing for queries that require multiple steps (e.g. chain-of-thought) and/or time-consuming actions (e.g. retrieval, SQL execution, or web search).&lt;/p&gt;

&lt;p&gt;An exact cache can be implemented using in-memory storage for fast retrieval. However, since in-memory storage is limited, a cache can also be implemented using databases like PostgreSQL, Redis, or tiered storage to balance speed and storage capacity. Having an eviction policy is crucial to manage the cache size and maintain performance. Common eviction policies include Least Recently Used (LRU), Least Frequently Used (LFU), and First In, First Out (FIFO).&lt;/p&gt;

&lt;p&gt;How long to cache a query depends on how likely this query is to be called again. User-specific queries such as “What’s the status of my recent order” are less likely to be reused by other users, and therefore, shouldn’t be cached. Similarly, it makes less sense to cache time-sensitive queries such as “How’s the weather?” Some teams train a small classifier to predict whether a query should be cached.&lt;/p&gt;

&lt;h3 id=&quot;semantic_cache&quot;&gt;Semantic cache&lt;/h3&gt;

&lt;p&gt;Unlike exact cache, semantic cache doesn’t require the incoming query to be identical to any of the cached queries. Semantic cache allows the reuse of similar queries. Imagine one user asks “What’s the capital of Vietnam?” and the model generates the answer “Hanoi”. Later, another user asks “What’s the capital &lt;strong&gt;&lt;em&gt;city&lt;/em&gt;&lt;/strong&gt; of Vietnam?”, which is the same question but with the extra word “city”. The idea of semantic cache is that the system can reuse the answer “Hanoi” instead of computing the new query from scratch.&lt;/p&gt;

&lt;p&gt;Semantic cache only works if you have a reliable way to determine if two queries are semantically similar. One common approach is embedding-based similarity, which works as follows:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;For each query, generate its embedding using an embedding model.&lt;/li&gt;
  &lt;li&gt;Use vector search to find the cached embedding closest to the current query embedding. Let’s say this similarity score is X.&lt;/li&gt;
  &lt;li&gt;If X is more than the similarity threshold you set, the cached query is considered the same as the current query, and the cached results are returned. If not, process this current query and cache it together with its embedding and results.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This approach requires a vector database to store the embeddings of cached queries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compared to other caching techniques, semantic cache’s value is more dubious because many of its components are prone to failure.&lt;/strong&gt; Its success relies on high-quality embeddings, functional vector search, and a trustworthy similarity metric. Setting the right similarity threshold can also be tricky and require a lot of trial and error. If the system mistakes the incoming query as being similar to another query, the returned response, fetched from the cache, will be incorrect.&lt;/p&gt;

&lt;p&gt;In addition, semantic cache can be time-consuming and compute-intensive, as it involves a vector search. The speed and cost of this vector search depend on the size of your database of cached embeddings.&lt;/p&gt;

&lt;p&gt;Semantic cache might still be worth it if the cache hit rate is high, meaning that a good portion of queries can be effectively answered by leveraging the cached results. However, before incorporating the complexities of semantic cache, make sure to evaluate the efficiency, cost, and performance risks associated with it.&lt;/p&gt;

&lt;p&gt;With the added cache systems, the platform looks as follows. KV cache and prompt cache are typically implemented by model API providers, so they aren’t shown in this image. If I must visualize them, I’d put them in the Model API box. There’s a new arrow to add generated responses to the cache.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Overview of a genai platform&quot; src=&quot;/assets/pics/genai-platform/12-cache.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2 id=&quot;step_5_add_complex_logic_and_write_actions&quot;&gt;Step 5. Add Complex Logic and Write Actions&lt;/h2&gt;

&lt;p&gt;The applications we’ve discussed so far have fairly simple flows. The outputs generated by foundation models are mostly returned to users (unless they don’t pass the guardrails). However, an application flow can be more complex with loops and conditional branching. A model’s outputs can also be used to invoke write actions, such as composing an email or placing an order.&lt;/p&gt;

&lt;h3 id=&quot;complex_logic&quot;&gt;Complex logic&lt;/h3&gt;

&lt;p&gt;Outputs from a model can be conditionally passed onto another model or fed back to the same model as part of the input to the next step. This goes on until a model in the system decides that the task has been completed and that a final response should be returned to the user.&lt;/p&gt;

&lt;p&gt;This can happen when you give your system the ability to plan and decide what to do next. As an example, consider the query “Plan a weekend itinerary for Paris.” The model might first generate a list of potential activities: visiting the Eiffel Tower, having lunch at a café, touring the Louvre, etc. Each of these activities can then be fed back into the model to generate more detailed plans. For instance, “visiting the Eiffel Tower” could prompt the model to generate sub-tasks like checking the opening hours, buying tickets, and finding nearby restaurants. This iterative process continues until a comprehensive and detailed itinerary is created.&lt;/p&gt;

&lt;p&gt;Our infrastructure now has an arrow pointing the generated response back to context construction, which in turn feeds back to models in the model gateway.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Overview of a genai platform&quot; src=&quot;/assets/pics/genai-platform/13-loop.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h3 id=&quot;write_actions&quot;&gt;Write actions&lt;/h3&gt;

&lt;p&gt;Actions used for context construction are &lt;em&gt;read-only actions&lt;/em&gt;. They allow a model to read from its data sources to gather context. But a system can also &lt;em&gt;write actions&lt;/em&gt;, making changes to the data sources and the world. For example, if the model outputs: “send an email to X with the message Y”, the system will invoke the action &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;send_email(recipient=X, message=Y)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Write actions make a system vastly more capable. They can enable you to automate the whole customer outreach workflow: researching potential customers, finding their contacts, drafting emails, sending first emails, reading responses, following up, extracting orders, updating your databases with new orders, etc.&lt;/p&gt;

&lt;p&gt;However, the prospect of giving AI the ability to automatically alter our lives is frightening. Just as you shouldn’t give an intern the authority to delete your production database, you shouldn’t allow an unreliable AI to initiate bank transfers. Trust in the system’s capabilities and its security measures is crucial. You need to ensure that the system is protected from bad actors who might try to manipulate it into performing harmful actions.&lt;/p&gt;

&lt;p&gt;AI systems are vulnerable to cyber attacks like other software systems, but they also have another weakness: &lt;em&gt;prompt injection&lt;/em&gt;. Prompt injection happens when an attacker manipulates input prompts into a model to get it to express undesirable behaviors. You can think of prompt injection as social engineering done on AI instead of humans.&lt;/p&gt;

&lt;p&gt;A scenario that many companies fear is that they give an AI system access to their internal databases, and attackers trick this system into revealing private information from these databases. If the system has write access to these databases, attackers can trick the system into corrupting the data.&lt;/p&gt;

&lt;p&gt;Any organization that wants to leverage AI needs to take safety and security seriously. However, these risks don’t mean that AI systems should never be given the ability to act in the real world. AI systems can fail, but humans can fail too. If we can get people to trust a machine to take us up into space, I hope that one day, securities will be sufficient for us to trust autonomous AI systems.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Overview of a genai platform&quot; src=&quot;/assets/pics/genai-platform/14-write-actions.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2 id=&quot;observability&quot;&gt;Observability&lt;/h2&gt;

&lt;p&gt;While I have placed observability in its own section, it should be integrated into the platform from the beginning rather than added later as an afterthought. Observability is crucial for projects of all sizes, and its importance grows with the complexity of the system.&lt;/p&gt;

&lt;p&gt;This section provides the least information compared to the others. It’s impossible to cover all the nuances of observability in a blog post. Therefore, I will only give a brief overview of the three pillars of monitoring: logs, traces, and metrics. I won’t go into specifics or cover user feedback, drift detection, and debugging.&lt;/p&gt;

&lt;h3 id=&quot;metrics&quot;&gt;Metrics&lt;/h3&gt;

&lt;p&gt;When discussing monitoring, most people think of metrics. What metrics to track depends on what you want to track about your system, which is application-specific. However, in general, there are two types of metrics you want to track: model metrics and system metrics.&lt;/p&gt;

&lt;p&gt;System metrics tell you the state of your overall system. Common metrics are throughput, memory usage, hardware utilization, and service availability/uptime. System metrics are common to all software engineering applications. In this post, I’ll focus on model metrics.&lt;/p&gt;

&lt;p&gt;Model metrics assess your model’s performance, such as accuracy, toxicity, and hallucination rate. Different steps in an application pipeline also have their own metrics. For example, in a RAG application, the retrieval quality is often evaluated using context relevance and context precision. A vector database can be evaluated by how much storage it needs to index the data and how long it takes to query the data&lt;/p&gt;

&lt;p&gt;There are various ways a model’s output can fail. It’s crucial to identify these issues and develop metrics to monitor them. For example, you might want to track how often your model times out, returns empty responses or produces malformatted responses. If you’re worried about your model revealing sensitive information, find a way to track that too.&lt;/p&gt;

&lt;p&gt;Length-related metrics such as query, context, and response length are helpful for understanding your model’s behaviors. Is one model more verbose than another? Are certain types of queries more likely to result in lengthy answers? They are especially useful for detecting changes in your application. If the average query length suddenly decreases, it could indicate an underlying issue that needs investigation.&lt;/p&gt;

&lt;p&gt;Length-related metrics are also important for tracking latency and costs, as longer contexts and responses typically increase latency and incur higher costs.&lt;/p&gt;

&lt;p&gt;Tracking latency is essential for understanding the user experience. Common latency metrics include:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Time to First Token&lt;/strong&gt; (TTFT): The time it takes for the first token to be generated.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Time Between Tokens&lt;/strong&gt; (TBT): The interval between each token generation.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tokens Per Second&lt;/strong&gt; (TPS): The rate at which tokens are generated.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Time Per Output Token&lt;/strong&gt; (TPOT): The time it takes to generate each output token.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Total Latency&lt;/strong&gt;: The total time required to complete a response.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You’ll also want to track costs. Cost-related metrics are the number of queries and the volume of input and output tokens. If you use an API with rate limits, tracking the number of requests per second is important to ensure you stay within your allocated limits and avoid potential service interruptions.&lt;/p&gt;

&lt;p&gt;When calculating metrics, you can choose between spot checks and exhaustive checks. Spot checks involve sampling a subset of data to quickly identify issues, while exhaustive checks evaluate every request for a comprehensive performance view. The choice depends on your system’s requirements and available resources, with a combination of both providing a balanced monitoring strategy.&lt;/p&gt;

&lt;p&gt;When computing metrics, ensure they can be broken down by relevant axes, such as users, releases, prompt/chain versions, prompt/chain types, and time. This granularity helps in understanding performance variations and identifying specific issues.&lt;/p&gt;

&lt;h3 id=&quot;logs&quot;&gt;Logs&lt;/h3&gt;

&lt;p&gt;Since this blog post is getting long and I’ve written at length about &lt;a href=&quot;https://learning.oreilly.com/library/view/designing-machine-learning/9781098107956/ch08.html#software_system_failures&quot;&gt;logs&lt;/a&gt; in &lt;a href=&quot;https://www.amazon.com/Designing-Machine-Learning-Systems-Production-Ready/dp/1098107969?&amp;amp;_encoding=UTF8&amp;amp;tag=chiphuyen-20&amp;amp;linkCode=ur2&amp;amp;linkId=0a1dbab0e76f5996e29e1a97d45f14a5&amp;amp;camp=1789&amp;amp;creative=9325&quot;&gt;Designing Machine Learning Systems&lt;/a&gt;, I will be quick here. The philosophy for logging is simple: log everything. Log the system configurations. Log the query, the output, and the intermediate outputs. Log when a component starts, ends, when something crashes, etc. When recording a piece of log, make sure to give it tags and IDs that can help you know where in the system this log comes from.&lt;/p&gt;

&lt;p&gt;Logging everything means that the amount of logs you have can grow very quickly. Many tools for automated log analysis and log anomaly detection are powered by AI.&lt;/p&gt;

&lt;p&gt;While it’s impossible to manually process logs, it’s useful to manually inspect your production data daily to get a sense of how users are using your application. &lt;a href=&quot;https://arxiv.org/abs/2404.12272&quot;&gt;Shankar et al.&lt;/a&gt; (2024) found that the developers’ perceptions of what constitutes good and bad outputs change as they interact with more data, allowing them to both rewrite their prompts to increase the chance of good responses and update their evaluation pipeline to catch bad responses.&lt;/p&gt;

&lt;h3 id=&quot;traces&quot;&gt;Traces&lt;/h3&gt;

&lt;p&gt;Trace refers to the detailed recording of a request’s execution path through various system components and services. In an AI application, tracing reveals the entire process from when a user sends a query to when the final response is returned, including the actions the system takes, the documents retrieved, and the final prompt sent to the model. It should also show how much time each step takes and its associated cost, if measurable. As an example, this is a visualization of a &lt;a href=&quot;https://blog.langchain.dev/announcing-langsmith/&quot;&gt;Langsmith&lt;/a&gt; trace.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Overview of a genai platform&quot; src=&quot;/assets/pics/genai-platform/15-traces.png&quot; style=&quot;float: center; max-width: 75%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;
Ideally, you should be able to trace each query’s transformation through the system step-by-step. If a query fails, you should be able to pinpoint the exact step where it went wrong: whether it was incorrectly processed, the retrieved context was irrelevant, or the model generated a wrong response.&lt;/p&gt;

&lt;h2 id=&quot;ai_pipeline_orchestration&quot;&gt;AI Pipeline Orchestration&lt;/h2&gt;

&lt;p&gt;An AI application can get fairly complex, consisting of multiple models, retrieving data from many databases, and having access to a wide range of tools. An orchestrator helps you specify how these different components are combined (chained) together to create an end-to-end application flow.&lt;/p&gt;

&lt;p&gt;At a high level, an orchestrator works in two steps: components definition and chaining (also known as pipelining).&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Components Definition&lt;/strong&gt; &lt;br /&gt;
You need to tell the orchestrator what components your system uses, such as models (including models for generation, routing, and scoring), databases from which your system can retrieve data, and actions that your system can take. Direct integration with model gateways can help simplify model onboarding, and some orchestrator tools want to be gateways. Many orchestrators also support integration with tools for evaluation and monitoring.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Chaining (or pipelining)&lt;/strong&gt; &lt;br /&gt;
You tell the orchestrator the sequence of steps your system takes from receiving the user query until completing the task. In short, chaining is just function composition. Here’s an example of what a pipeline looks like.&lt;/p&gt;
    &lt;ol&gt;
      &lt;li&gt;Process the raw query.&lt;/li&gt;
      &lt;li&gt;Retrieve the relevant data based on the processed query.&lt;/li&gt;
      &lt;li&gt;The original query and the retrieved data are combined to create a prompt in the format expected by the model.&lt;/li&gt;
      &lt;li&gt;The model generates a response based on the prompt.&lt;/li&gt;
      &lt;li&gt;Evaluate the response.&lt;/li&gt;
      &lt;li&gt;If the response is considered good, return it to the user. If not, route the query to a human operator.&lt;/li&gt;
    &lt;/ol&gt;

    &lt;p&gt;The orchestrator is responsible for passing data between steps and can provide toolings that help ensure that the output from the current step is in the format expected by the next step.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When designing the pipeline for an application with strict latency requirements, try to do as much in parallel as possible. For example, if you have a routing component (deciding where to send a query to) and a PII removal component, they can do both at the same time.&lt;/p&gt;

&lt;p&gt;There are many AI orchestration tools, including &lt;a href=&quot;https://github.com/langchain-ai/langchain&quot;&gt;LangChain&lt;/a&gt;, &lt;a href=&quot;https://github.com/run-llama/llama_index&quot;&gt;LlamaIndex&lt;/a&gt;, &lt;a href=&quot;https://github.com/FlowiseAI/Flowise&quot;&gt;Flowise&lt;/a&gt;, &lt;a href=&quot;https://github.com/langflow-ai/langflow&quot;&gt;Langflow&lt;/a&gt;, and &lt;a href=&quot;https://github.com/deepset-ai/haystack&quot;&gt;Haystack&lt;/a&gt;. Each tool has its own APIs so I won’t show the actual code here.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;While it’s tempting to jump straight to an orchestration tool when starting a project, start building your application without one first.&lt;/strong&gt; Any external tool brings added complexity. An orchestrator can abstract away critical details of how your system works, making it hard to understand and debug your system.&lt;/p&gt;

&lt;p&gt;As you advance to the later stages of your application development process, you might decide that an orchestrator can make your life easier. Here are three aspects to keep in mind when evaluating orchestrators.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Integration and extensibility&lt;/strong&gt; &lt;br /&gt;
Evaluate whether the orchestrator supports the components you’re already using or might adopt in the future. For example, if you want to use a Llama model, check if the orchestrator supports that. Given how many models, databases, and frameworks there are, it’s impossible for an orchestrator to support everything. Therefore, you’ll also need to consider an orchestrator’s extensibility. If it doesn’t support a specific component, how hard it is to change that?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Support for complex pipelines&lt;/strong&gt; &lt;br /&gt;
As your applications grow in complexity, you might need to manage intricate pipelines involving multiple steps and conditional logic. An orchestrator that supports advanced features like branching, parallel processing, and error handling will help you manage these complexities efficiently.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ease of use, performance, and scalability&lt;/strong&gt; &lt;br /&gt;
Consider the user-friendliness of the orchestrator. Look for intuitive APIs, comprehensive documentation, and strong community support, as these can significantly reduce the learning curve for you and your team. Avoid orchestrators that initiate hidden API calls or introduce latency to your applications. Additionally, ensure that the orchestrator can scale effectively as the number of applications, developers, and traffic grows.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;This post started with a basic architecture and then gradually added components to address the growing application complexities. Each addition brings its own set of benefits and challenges, requiring careful consideration and implementation.&lt;/p&gt;

&lt;p&gt;While the separation of components is important to keep your system modular and maintainable, this separation is fluid. There are many overlaps between components. For example, a model gateway can share functionalities with guardrails. Cache can be implemented in different components, such as in vector search and inference services.&lt;/p&gt;

&lt;p&gt;This post is much longer than I intended it to be, and yet there are many details I haven’t been able to explore further, especially around observability, context construction, complex logic, cache, and guardrails. I’ll dive deeper into all these components in my upcoming book &lt;a href=&quot;https://learning.oreilly.com/library/view/ai-engineering/9781098166298/&quot;&gt;AI Engineering&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This post also didn’t discuss how to serve models, assuming that most people will be using models provided by third-party APIs. AI Engineering will also have a chapter dedicated to inference and model optimization.&lt;/p&gt;

&lt;h2 id=&quot;references_and_acknowledgments&quot;&gt;References and Acknowledgments&lt;/h2&gt;

&lt;p&gt;Special thanks to &lt;a href=&quot;https://x.com/luke_metz&quot;&gt;Luke Metz&lt;/a&gt;, &lt;a href=&quot;https://www.linkedin.com/in/findalexli/&quot;&gt;Alex Li&lt;/a&gt;, &lt;a href=&quot;https://www.linkedin.com/in/chetantekur/&quot;&gt;Chetan Tekur&lt;/a&gt;, &lt;a href=&quot;https://www.linkedin.com/in/kittipat-bot-kampa-1b1965/&quot;&gt;Kittipat “Bot” Kampa&lt;/a&gt;, &lt;a href=&quot;https://www.linkedin.com/in/hienluu/&quot;&gt;Hien Luu&lt;/a&gt;, and &lt;a href=&quot;https://www.linkedin.com/in/denyslinkov/&quot;&gt;Denys Linkov&lt;/a&gt; for feedback on the early versions of this post. Their insights greatly improved the content. Any remaining errors are my own.&lt;/p&gt;

&lt;p&gt;I read many case studies shared by companies on how they adopted generative AI, and here are some of my favorites.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.linkedin.com/blog/engineering/generative-ai/musings-on-building-a-generative-ai-product?_l=en_US&quot;&gt;Musings on Building a Generative AI Product&lt;/a&gt; (LinkedIn, 2024)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://medium.com/pinterest-engineering/how-we-built-text-to-sql-at-pinterest-30bad30dabff&quot;&gt;How we built Text-to-SQL at Pinterest&lt;/a&gt; (Pinterest, 2024)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://medium.com/vimeo-engineering-blog/from-idea-to-reality-elevating-our-customer-support-through-generative-ai-101a2c5ea680&quot;&gt;From idea to reality: Elevating our customer support through generative AI&lt;/a&gt; (Vimeo, 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.shortwave.com/blog/deep-dive-into-worlds-smartest-email-ai/&quot;&gt;A deep dive into the world’s smartest email AI&lt;/a&gt; (Shortwave, 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://engineering.grab.com/llm-powered-data-classification&quot;&gt;LLM-powered data classification for data entities at scale&lt;/a&gt; (Grab, 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.uber.com/blog/from-predictive-to-generative-ai/&quot;&gt;From Predictive to Generative - How Michelangelo Accelerates Uber’s AI Journey&lt;/a&gt; (Uber, 2024)&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Thu, 25 Jul 2024 00:00:00 +0000</pubDate>
        <link>https://huyenchip.com//2024/07/25/genai-platform.html</link>
        <guid isPermaLink="true">https://huyenchip.com/2024/07/25/genai-platform.html</guid>
      </item>
    
      <item>
        <title>Measuring personal growth</title>
        <description>&lt;p&gt;My founder friends constantly think about growth. They think about how to measure their business growth and how to get to the next order of magnitude scale. If they’re making $1M ARR today, they think about how to get to $10M ARR. If they have 1,000 users today, they think about how to get to 10,000 users.&lt;/p&gt;

&lt;p&gt;This made me wonder if/how people are measuring personal growth. I don’t want to use metrics like net worth or the number of followers, because that’s not what I live for. After talking with a lot of friends, I found three interesting metrics: rate of change, time to solve problems, and number of future options.&lt;/p&gt;

&lt;p&gt;Some friends told me they find this blog post mildly sociopathic. Why do I have to measure everything? Life is to be lived, not to be measured. As someone lowkey fascinated by numbers, I don’t see why measuring and living have to be mutually exclusive – measuring often helps me live better – but I see where they come from. This post is more of a thought exercise than a rigorous experiment.&lt;/p&gt;

&lt;h2 id=&quot;rate-of-change&quot;&gt;Rate of change&lt;/h2&gt;

&lt;p&gt;I have this theory that life has a circadian rhythm. Every 3-6 years, you become a different person. You work on different problems. Your lifestyle changes. The people you hang out with are different. If you haven’t caught up with a friend in 5 years, you might no longer have anything in common. It’s not a coincidence that schools are structured into chunks of 3-6 years.&lt;/p&gt;

&lt;p&gt;Looking back, I realized that every 3-6 years, my life completely changed. From grade 3 to grade 10, I did competitive math. For the next 5 years, I worked as a writer. Then I went to college and studied computer science for 4 years. After that, I fumbled around for almost 6 years. It was only recently that I felt like I had a handle on life.&lt;/p&gt;

&lt;p&gt;Sami, a new friend who loves designing strategy games, told me about the rule of 72 in finance. It’s a simple formula that estimates the number of years it will take for an investment to double in value. If the annual interest rate is 8%, it’ll take 72/8 = 9 years for the value of your investment to double.&lt;/p&gt;

&lt;p&gt;I wonder if I could treat myself as an investment, and measure my growth by how long it’d take me to become a new person. Becoming a new person isn’t always a good thing, and probably not the goal for everyone. But for me, it is. I want to be able to see things from a new perspective. I want to be exposed to new challenges. I treasure old friends (I still talk to my best friends in elementary school), but I like learning from new friends.&lt;/p&gt;

&lt;h2 id=&quot;time-to-solve-problems&quot;&gt;Time to solve problems&lt;/h2&gt;

&lt;p&gt;Quynh, an old friend who runs a publishing house in Vietnam, believes that there are three big problems in life: career, family, and finance. It usually takes people a decade to figure each out.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;For the first decade after graduation, you figure out what you want to do with your life.&lt;/li&gt;
  &lt;li&gt;For the next decade, you get married, buy a house, and have kids.&lt;/li&gt;
  &lt;li&gt;For the next decade, you build out your savings to retire.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Her goal is to solve these problems as fast as possible, so she can focus on more interesting problems.&lt;/p&gt;

&lt;p&gt;This made me think that perhaps I can measure my growth by looking at what big problems I’ve solved. What big problems was I worried about 5 years ago that I no longer worry about now? What big problems am I worried about now that I don’t want to worry about in 5 years?&lt;/p&gt;

&lt;p&gt;What is considered a big problem depends on each person. For me, it’s career, finance, social, immigration, family, and health. Here are a couple of concrete examples that made me feel like I’ve made progress. 5 years ago, I was anxious about being in the US on a visa. This problem went away when I got my green card. 5 years ago, I constantly felt insecure like I was an imposter in the Bay. Today, I feel at home here.&lt;/p&gt;

&lt;h2 id=&quot;number-of-future-options&quot;&gt;Number of future options&lt;/h2&gt;

&lt;p&gt;A friend I’ve met through my Discord, Denys, told me that his friend has this theory that every few years, half of your dreams die. People give up on their dreams because they realize that they can no longer achieve them.&lt;/p&gt;

&lt;p&gt;I disagree. As I grow older, I have more dreams. I now know many things that I didn’t know before, and I have access to more resources than I ever did. This allows me to do things that I used to think of as impossible.&lt;/p&gt;

&lt;p&gt;During a reinforcement learning course in college, I learned about empowerment maximization. It’s a simple principle that enables robots/agents to exhibit relatively intelligent behavior. In the face of uncertainty, an agent following empowerment maximization would choose the action that maximizes future options. For example, facing multiple switches, it’d choose the switch that opens the most doors.&lt;/p&gt;

&lt;p&gt;I realized that this is the same principle that I’ve followed. In the face of uncertainty, I lean towards the decision that would give me the most future options. For example, I’d choose a job that pays less but gives me more job options in the future (e.g. if the job gives me exposure like allowing me to work on open source or publish papers). I’d prioritize tasks that teach me transferable skills instead of tasks that teach me niche, narrow skills.&lt;/p&gt;

&lt;p&gt;Perhaps I can measure my growth by how many new options I have gained/lost. What options are available to me today that were not available to me 5 years ago? What options were available to me 5 years ago that aren’t available to me now? More importantly, what options that are not available to me today do I want 5 years from now?&lt;/p&gt;

&lt;p&gt;Sami pointed me to this &lt;a href=&quot;https://twitter.com/waitbutwhy/status/1367871165319049221&quot;&gt;image&lt;/a&gt; from Wait But Why. As time goes by, many doors are closed to us, but many new doors open up. Denys’s friend was referring to the black lines on the left, and I focus on the green lines on the right.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Generative AI Stack&quot; src=&quot;/assets/pics/life_path_waitbywhy.jpeg&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;There are three heuristics that I follow for personal growth:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;I try to become a new person every 3-6 years.&lt;/li&gt;
  &lt;li&gt;I try to solve big problems as fast as possible. I think of this as creating safety nets that allow me to take bigger risks and explore more things in the future.&lt;/li&gt;
  &lt;li&gt;I take actions that help me maximize future options.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These heuristics work for me (so far) because I have a strong bias towards novelty and exploration. Maybe one day, I’ll get tired of exploration, and these heuristics will change. When that happens, that’ll be growth.&lt;/p&gt;
</description>
        <pubDate>Wed, 17 Apr 2024 00:00:00 +0000</pubDate>
        <link>https://huyenchip.com//2024/04/17/personal-growth.html</link>
        <guid isPermaLink="true">https://huyenchip.com/2024/04/17/personal-growth.html</guid>
      </item>
    
      <item>
        <title>What I learned from looking at 900 most popular open source AI tools</title>
        <description>&lt;p&gt;[&lt;em&gt;&lt;a href=&quot;https://news.ycombinator.com/item?id=39709912&quot;&gt;Hacker News discussion&lt;/a&gt;, &lt;a href=&quot;https://www.linkedin.com/posts/chiphuyen_generativeai-aiapplications-llmops-activity-7174153467844820993-ztSE&quot;&gt;LinkedIn discussion&lt;/a&gt;, &lt;a href=&quot;https://twitter.com/chipro/status/1768388213008445837&quot;&gt;Twitter thread&lt;/a&gt;&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update (Feb 2026)&lt;/strong&gt;: &lt;em&gt;The full list of open source AI repos is hosted at &lt;a href=&quot;https://goodailist.com&quot;&gt;Good AI List&lt;/a&gt;, updated daily. It’s balooned to 15K repos, and you can submit missing repos. You can also find some of them on my &lt;a href=&quot;https://github.com/stars/chiphuyen/lists/cool-llm-repos&quot;&gt;cool-llm-repos&lt;/a&gt; list on GitHub.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Four years ago, I did an analysis of the &lt;a href=&quot;https://huyenchip.com/2020/06/22/mlops.html&quot;&gt;open source ML ecosystem&lt;/a&gt;. Since then, the landscape has changed, so I revisited the topic. This time, I focused exclusively on the stack around foundation models.&lt;/p&gt;

&lt;h2 id=&quot;data&quot;&gt;Data&lt;/h2&gt;

&lt;p&gt;I searched GitHub using the keywords &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gpt&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;llm&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;generative ai&lt;/code&gt;. If AI feels so overwhelming right now, it’s because it is. There are 118K results for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gpt&lt;/code&gt; alone.&lt;/p&gt;

&lt;p&gt;To make my life easier, I limited my search to the repos with at least 500 stars. There were 590 results for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;llm&lt;/code&gt;, 531 for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gpt&lt;/code&gt;, and 38 for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;generative ai&lt;/code&gt;. I also occasionally checked GitHub trending and social media for new repos.&lt;/p&gt;

&lt;p&gt;After MANY hours, I found 896 repos. Of these, 51 are tutorials (e.g. &lt;a href=&quot;https://github.com/dair-ai/Prompt-Engineering-Guide&quot;&gt;dair-ai/Prompt-Engineering-Guide&lt;/a&gt;) and aggregated lists (e.g. &lt;a href=&quot;https://github.com/f/awesome-chatgpt-prompts&quot;&gt;f/awesome-chatgpt-prompts&lt;/a&gt;). While these tutorials and lists are helpful, I’m more interested in software. I still include them in the final list, but the analysis is done with the 845 software repositories.&lt;/p&gt;

&lt;p&gt;It was a painful but rewarding process. It gave me a much better understanding of what people are working on, how incredibly collaborative the open source community is, and just how much China’s open source ecosystem diverges from the Western one.&lt;/p&gt;

&lt;h2 id=&quot;the_new_ai_stack&quot;&gt;The New AI Stack&lt;/h2&gt;

&lt;p&gt;I think of the AI stack as consisting of 3 layers: infrastructure, model development, and application development.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Generative AI Stack&quot; src=&quot;/assets/pics/ai-oss/1-ai-stack.png&quot; style=&quot;float: center; max-width: 70%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Infrastructure&lt;/strong&gt;&lt;/p&gt;

    &lt;p&gt;At the bottom is the stack is infrastructure, which includes toolings for serving (&lt;a href=&quot;https://github.com/vllm-project/vllm&quot;&gt;vllm&lt;/a&gt;, &lt;a href=&quot;https://github.com/triton-inference-server/server&quot;&gt;NVIDIA’s Triton&lt;/a&gt;), compute management (&lt;a href=&quot;https://github.com/skypilot-org/skypilot&quot;&gt;skypilot&lt;/a&gt;), vector search and database (&lt;a href=&quot;https://github.com/facebookresearch/faiss&quot;&gt;faiss&lt;/a&gt;, &lt;a href=&quot;https://milvus.io/&quot;&gt;milvus&lt;/a&gt;, &lt;a href=&quot;https://github.com/qdrant/qdrant&quot;&gt;qdrant&lt;/a&gt;, &lt;a href=&quot;https://github.com/lancedb/lancedb&quot;&gt;lancedb&lt;/a&gt;), ….&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Model development&lt;/strong&gt;&lt;/p&gt;

    &lt;p&gt;This layer provides toolings for developing models, including frameworks for modeling &amp;amp; training (transformers, pytorch, DeepSpeed), inference optimization (ggml, openai/triton), dataset engineering, evaluation, ….. Anything that involves changing a model’s weights happens in this layer, including finetuning.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Application development&lt;/strong&gt;
 With readily available models, anyone can develop applications on top of them. This is the layer that has seen the most actions in the last 2 years and is still rapidly evolving. This layer is also known as AI engineering.&lt;/p&gt;

    &lt;p&gt;Application development involves prompt engineering, RAG, AI interface, …&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Outside of these 3 layers, I also have two other categories:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Model repos&lt;/strong&gt;, which are created by companies and researchers to share the code associated with their models. Examples of repos in this category are &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CompVis/stable-diffusion&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;openai/whisper&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;facebookresearch/llama&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Applications&lt;/strong&gt; built on top of existing models. The most popular types of applications are coding, workflow automation, information aggregation, …&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: &lt;em&gt;In an older version of this post, &lt;strong&gt;Applications&lt;/strong&gt; was included as another layer in the stack.&lt;/em&gt;&lt;/p&gt;

&lt;h3 id=&quot;ai_stack_over_time&quot;&gt;AI stack over time&lt;/h3&gt;
&lt;p&gt;I plotted the cumulative number of repos in each category month-over-month. There was an explosion of new toolings in 2023, after the introduction of Stable Diffusion and ChatGPT. The curve seems to flatten in September 2023 because of three potential reasons.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;I only include repos with at least 500 stars in my analysis, and it takes time for repos to gather these many stars.&lt;/li&gt;
  &lt;li&gt;Most low-hanging fruits have been picked. What is left takes more effort to build, hence fewer people can build them.&lt;/li&gt;
  &lt;li&gt;People have realized that it’s hard to be competitive in the generative AI space, so the excitement has calmed down. Anecdotally, in early 2023, all AI conversations I had with companies centered around gen AI, but the recent conversations are more grounded. Several even brought up scikit-learn. I’d like to revisit this in a few months to verify if it’s true.&lt;/li&gt;
&lt;/ol&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Generative AI Stack Over Time&quot; src=&quot;/assets/pics/ai-oss/2-ai-timeline.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;In 2023, the layers that saw the highest increases were the applications and application development layers. The infrastructure layer saw a little bit of growth, but it was far from the level of growth seen in other layers.&lt;/p&gt;

&lt;h4 id=&quot;applications&quot;&gt;Applications&lt;/h4&gt;

&lt;p&gt;Not surprisingly, the most popular types of applications are coding, bots (e.g. role-playing, WhatsApp bots, Slack bots), and information aggregation (e.g. “let’s connect this to our Slack and ask it to summarize the messages each day”).&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Breakdown of popular AI applications&quot; src=&quot;/assets/pics/ai-oss/3-ai-applications.png&quot; style=&quot;float: center; max-width: 65%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h4 id=&quot;ai_engineering&quot;&gt;AI engineering&lt;/h4&gt;

&lt;p&gt;2023 was the year of AI engineering. Since many of them are similar, it’s hard to categorize the tools. I currently put them into the following categories: prompt engineering, AI interface, Agent, and AI engineering (AIE) framework.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prompt engineering&lt;/strong&gt; goes way beyond fiddling with prompts to cover things like constrained sampling (structured outputs), long-term memory management, prompt testing &amp;amp; evaluation, etc.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;A list of prompt engineering tools&quot; src=&quot;/assets/pics/ai-oss/4-prompt-engineering.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AI interface&lt;/strong&gt; provides an interface for your end users to interact with your AI application. This is the category I’m the most excited about. Some of the interfaces that are gaining popularity are:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Web and desktop apps.&lt;/li&gt;
  &lt;li&gt;Browser extensions that let users quickly query AI models while browsing.&lt;/li&gt;
  &lt;li&gt;Bots via chat apps like Slack, Discord, WeChat, and WhatsApp.&lt;/li&gt;
  &lt;li&gt;Plugins that let developers embed AI applications to applications like VSCode, Shopify, and Microsoft Offices. The plugin approach is common for AI applications that can use tools to complete complex tasks (agents).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;AIE framework&lt;/strong&gt; is a catch-all term for all platforms that help you develop AI applications. Many of them are built around RAG, but many also provide other toolings such as monitoring, evaluation, etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Agent&lt;/strong&gt; is a weird category, as many agent toolings are just sophisticated prompt engineering with potentially constrained generation (e.g. the model can only output the predetermined action) and plugin integration (e.g. to let the agent use tools).&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;AI engineering stack over time&quot; src=&quot;/assets/pics/ai-oss/5-ai-engineering.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h4 id=&quot;model_development&quot;&gt;Model development&lt;/h4&gt;

&lt;p&gt;Pre-ChatGPT, the AI stack was dominated by model development. Model development’s biggest growth in 2023 came from increasing interest in inference optimization, evaluation, and parameter-efficient finetuning (which is grouped under Modeling &amp;amp; training).&lt;/p&gt;

&lt;p&gt;Inference optimization has always been important, but the scale of foundation models today makes it crucial for latency and cost. The core approaches for optimization remain the same (quantization, low-ranked factorization, pruning, distillation), but many new techniques have been developed especially for the transformer architecture and the new generation of hardware. For example, in 2020, 16-bit quantization was considered state-of-the-art. Today, we’re seeing &lt;a href=&quot;https://arxiv.org/abs/2212.09720&quot;&gt;2-bit quantization&lt;/a&gt; and &lt;a href=&quot;https://arxiv.org/abs/2402.17764&quot;&gt;even lower than 2-bit&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Similarly, evaluation has always been essential, but with many people today treating models as blackboxes, evaluation has become even more so. There are many new evaluation benchmarks and evaluation methods, such as comparative evaluation (see &lt;a href=&quot;https://huyenchip.com/2024/02/28/predictive-human-preference.html#correctness_of_chatbot_arena_ranking&quot;&gt;Chatbot Arena&lt;/a&gt;) and AI-as-a-judge.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Model Development Stack Over Time&quot; src=&quot;/assets/pics/ai-oss/6-model-development.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h4 id=&quot;infrastructure&quot;&gt;Infrastructure&lt;/h4&gt;

&lt;p&gt;Infrastructure is about managing data, compute, and toolings for serving, monitoring, and other platform work. Despite all the changes that generative AI brought, the open source AI infrastructure layer remained more or less the same. This could also be because infrastructure products are typically not open sourced.&lt;/p&gt;

&lt;p&gt;The newest category in this layer is vector database with companies like Qdrant, Pinecone, and LanceDB. However, many argue this shouldn’t be a category at all. Vector search has been around for a long time. Instead of building new databases just for vector search, existing database companies like DataStax and Redis are bringing vector search into where the data already is.&lt;/p&gt;

&lt;h2 id=&quot;open_source_ai_developers&quot;&gt;Open source AI developers&lt;/h2&gt;

&lt;p&gt;Open source software, like many things, follows the long tail distribution. A handful of accounts control a large portion of the repos.&lt;/p&gt;

&lt;h3 id=&quot;the_rise_of_one_person_companies&quot;&gt;One-person billion-dollar companies?&lt;/h3&gt;
&lt;p&gt;845 repos are hosted on 594 unique GitHub accounts. There are 20 accounts with at least 4 repos. These top 20 accounts host 195 of the repos, or 23% of all the repos on the list. These 195 repos have gained a total of 1,650,000 stars.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Most active GitHub accounts&quot; src=&quot;/assets/pics/ai-oss/7-top-accounts.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;On Github, an account can be either an organization or an individual. 19/20 of the top accounts are organizations. Of those, 3 belong to Google: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;google-research&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;google&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorflow&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The only individual account in these top 20 accounts is lucidrains. Among the top 20 accounts with the most number of stars (counting only gen AI repos), 4 are individual accounts:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/lucidrains&quot;&gt;lucidrains&lt;/a&gt; (Phil Wang): who can implement state-of-the-art models insanely fast.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ggerganov&quot;&gt;ggerganov&lt;/a&gt; (Georgi Gerganov): an optimization god who comes from a physics background.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/lllyasviel&quot;&gt;Illyasviel&lt;/a&gt; (Lyumin Zhang): creator of Foocus and ControlNet who’s currently a Stanford PhD.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/xtekky&quot;&gt;xtekky&lt;/a&gt;: a full-stack developer who created gpt4free.&lt;/li&gt;
&lt;/ul&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Most active GitHub accounts&quot; src=&quot;/assets/pics/ai-oss/8-top-accounts-stars.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Unsurprisingly, the lower we go in the stack, the harder it is for individuals to build. Software in the infrastructure layer is the least likely to be started and hosted by individual accounts, whereas more than half of the applications are hosted by individuals.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Can you do this alone?&quot; src=&quot;/assets/pics/ai-oss/9-indie.png&quot; style=&quot;float: center; max-width: 65%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Applications started by individuals, on average, have gained more stars than applications started by organizations. Several people have speculated that we’ll see many very valuable one-person companies (see &lt;a href=&quot;https://fortune.com/2024/02/04/sam-altman-one-person-unicorn-silicon-valley-founder-myth/&quot;&gt;Sam Altman’s interview&lt;/a&gt; and &lt;a href=&quot;https://www.reddit.com/r/ChatGPT/comments/1ajwj5z/one_person_billion_dollar_company/&quot;&gt;Reddit discussion&lt;/a&gt;). I think they might be right.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Can you do this alone?&quot; src=&quot;/assets/pics/ai-oss/10-indie-stars.png&quot; style=&quot;float: center; max-width: 95%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h3 id=&quot;1_million_commits&quot;&gt;1 million commits&lt;/h3&gt;

&lt;p&gt;Over 20,000 developers have contributed to these 845 repos. In total, they’ve made almost a million contributions!&lt;/p&gt;

&lt;p&gt;Among them, the 50 most active developers have made over 100,000 commits, averaging over 2,000 commits each. See the full list of the top 50 most active open source developers &lt;a href=&quot;https://huyenchip.com/llama-devs&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Most active open source developers&quot; src=&quot;/assets/pics/ai-oss/11-devs.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2 id=&quot;the_growing_china_open_source_ecosystem&quot;&gt;The growing China&apos;s open source ecosystem&lt;/h2&gt;

&lt;p&gt;It’s been known for a long time that China’s AI ecosystem has diverged from the US (I also mentioned that in a &lt;a href=&quot;https://huyenchip.com/2020/12/27/real-time-machine-learning.html#mlops_china_vs_us&quot;&gt;2020 blog post&lt;/a&gt;). At that time, I was under the impression that GitHub wasn’t widely used in China, and my view back then was perhaps colored by China’s 2013 ban on GitHub.&lt;/p&gt;

&lt;p&gt;However, this impression is no longer true. There are many, many popular AI repos on GitHub targeting Chinese audiences, such that their descriptions are written in Chinese. There are repos for models developed for Chinese or Chinese + English, such as &lt;a href=&quot;https://github.com/QwenLM/Qwen&quot;&gt;Qwen&lt;/a&gt;, &lt;a href=&quot;https://github.com/THUDM/ChatGLM3&quot;&gt;ChatGLM3&lt;/a&gt;, &lt;a href=&quot;https://github.com/ymcui/Chinese-LLaMA-Alpaca&quot;&gt;Chinese-LLaMA&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;While in the US, many research labs have moved away from the RNN architecture for language models, the RNN-based model family &lt;a href=&quot;https://github.com/BlinkDL/RWKV-LM&quot;&gt;RWKV&lt;/a&gt; is still popular.&lt;/p&gt;

&lt;p&gt;There are also AI engineering tools providing ways to integrate AI models into products popular in China like WeChat, QQ, DingTalk, etc. Many popular prompt engineering tools also have mirrors in Chinese.&lt;/p&gt;

&lt;p&gt;Among the top 20 accounts on GitHub, 6 originated in China:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/THUDM&quot;&gt;THUDM&lt;/a&gt;: Knowledge Engineering Group (KEG) &amp;amp; Data Mining at Tsinghua University.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/OpenGVLab&quot;&gt;OpenGVLab&lt;/a&gt;: General Vision team of Shanghai AI Laboratory&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/OpenBMB&quot;&gt;OpenBMB&lt;/a&gt;: Open Lab for Big Model Base, founded by ModelBest &amp;amp; the NLP group at Tsinghua University.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/InternLM&quot;&gt;InternLM&lt;/a&gt;: from Shanghai AI Laboratory.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/open-mmlab&quot;&gt;OpenMMLab&lt;/a&gt;: from The Chinese University of Hong Kong.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/QwenLM&quot;&gt;QwenLM&lt;/a&gt;: Alibaba’s AI lab, which publishes the Qwen model family.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;live_fast_die_young&quot;&gt;Live fast, die young&lt;/h2&gt;

&lt;p&gt;One pattern that I saw last year is that many repos quickly gained a massive amount of eyeballs, then quickly died down. Some of my friends call this the “hype curve”. Out of these 845 repos with at least 500 GitHub stars, 158 repos (18.8%) haven’t gained any new stars in the last 24 hours, and 37 repos (4.5%) haven’t gained any new stars in the last week.&lt;/p&gt;

&lt;p&gt;Here are examples of the growth trajectory of two of such repos compared to the growth curve of two more sustained software. Even though these two examples shown here are no longer used, I think they were valuable in showing the community what was possible, and it was cool that the authors were able to get things out so fast.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Hype curve&quot; src=&quot;/assets/pics/ai-oss/12-hype-curve.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2 id=&quot;my_personal_favorite_ideas&quot;&gt;My personal favorite ideas&lt;/h2&gt;

&lt;p&gt;So many cool ideas are being developed by the community. Here are some of my favorites.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Batch inference optimization: &lt;a href=&quot;https://github.com/FMInference/FlexGen&quot;&gt;FlexGen&lt;/a&gt;, &lt;a href=&quot;https://github.com/ggerganov/llama.cpp/pull/1375&quot;&gt;llama.cpp&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Faster decoder with techniques such as &lt;a href=&quot;https://github.com/FasterDecoding/Medusa&quot;&gt;Medusa&lt;/a&gt;, &lt;a href=&quot;https://github.com/hao-ai-lab/LookaheadDecoding&quot;&gt;LookaheadDecoding&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Model merging: &lt;a href=&quot;https://github.com/cg123/mergekit&quot;&gt;mergekit&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Constrained sampling: &lt;a href=&quot;https://github.com/outlines-dev&quot;&gt;outlines&lt;/a&gt;, &lt;a href=&quot;https://github.com/guidance-ai/guidance&quot;&gt;guidance&lt;/a&gt;, &lt;a href=&quot;https://github.com/sgl-project/sglang&quot;&gt;SGLang&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Seemingly niche tools that solve one problem really well, such as &lt;a href=&quot;https://github.com/arogozhnikov/einops&quot;&gt;einops&lt;/a&gt; and &lt;a href=&quot;https://github.com/huggingface/safetensors&quot;&gt;safetensors&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Even though I included only 845 repos in my analysis, I went through several thousands of repos. I found this helpful for me to get a big-picture view of the seemingly overwhelming AI ecosystem. I hope the &lt;a href=&quot;https://goodailist.com&quot;&gt;list&lt;/a&gt; is useful for you too. Please do let me know what repos I’m missing, and I’ll add them to the list!&lt;/p&gt;
</description>
        <pubDate>Thu, 14 Mar 2024 00:00:00 +0000</pubDate>
        <link>https://huyenchip.com//2024/03/14/ai-oss.html</link>
        <guid isPermaLink="true">https://huyenchip.com/2024/03/14/ai-oss.html</guid>
      </item>
    
      <item>
        <title>Predictive Human Preference: From Model Ranking to Model Routing</title>
        <description>&lt;p&gt;A challenge of building AI applications is choosing which model to use. What if we don’t have to? What if we can predict the best model for any prompt? Predictive human preference aims to predict which model users might prefer for a specific query.&lt;/p&gt;

&lt;p&gt;Human preference has emerged to be both the Northstar and a powerful tool for AI model development. Human preference guides post-training techniques including &lt;a href=&quot;https://huyenchip.com/2023/05/02/rlhf.html&quot;&gt;RLHF&lt;/a&gt; and &lt;a href=&quot;https://arxiv.org/abs/2305.18290&quot;&gt;DPO&lt;/a&gt;. Human preference is also used to rank AI models, as used by LMSYS’s &lt;a href=&quot;https://arena.lmsys.org/&quot;&gt;Chatbot Arena&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Chatbot Arena aims to determine which model is generally preferred. I wanted to see if it’s possible to predict which model is preferred &lt;em&gt;for each query&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;One use case of predictive human preference is model routing. For example, if we know in advance that for a prompt, users will prefer Claude Instant’s response over GPT-4, and Claude Instant is cheaper/faster than GPT-4, we can route this prompt to Claude Instant. Model routing has the potential to increase response quality while reducing costs and latency.&lt;/p&gt;

&lt;p&gt;Another use case of predictive human preference is interpretability. Mapping out a model’s performance on different prompts can help us understand this model’s strengths and weaknesses. See section &lt;strong&gt;Experiment results&lt;/strong&gt; for examples.&lt;/p&gt;

&lt;p&gt;Here’s what predictive human preference for different model pairs looks like for the prompt “&lt;em&gt;What’s the best way to cluster text embeddings?&lt;/em&gt;”. The predictions were generated by my toy preference predictor. The bright yellow color for the (GPT-4, GPT-3.5-Turbo) cell means that my predictor thinks GPT-4’s response is very likely to be preferred to that of GPT-3.5-Turbo’s for this prompt.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Predictive human preference for all LLM model pairs&quot; src=&quot;/assets/pics/predictive-preference/1-cluster.png&quot; style=&quot;float: center; max-width: 90%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;This post first discusses the correctness of Chatbot Arena, which will then be used as a baseline to evaluate the correctness of preference predictions. It then discusses how to build a preference predictor and the initial results.&lt;/p&gt;

&lt;h2 id=&quot;ranking_models_using_human_preference&quot;&gt;Ranking Models Using Human Preference&lt;/h2&gt;

&lt;p&gt;Using preferential signals (comparisons) to rank models has grown in popularity in the last few years. Other than powering LMSYS’s &lt;a href=&quot;https://arena.lmsys.org/&quot;&gt;Chatbot Arena&lt;/a&gt;, it’s also used by many model providers (&lt;a href=&quot;https://arxiv.org/abs/2112.00861&quot;&gt;Anthropic&lt;/a&gt;, Gemini, ChatGPT, etc.) to evaluate their models in production.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Predictive human preference for all LLM model pairs&quot; src=&quot;/assets/pics/predictive-preference/2-chatgpt.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Side note&lt;/strong&gt;: Friends who have deployed this in production told me that most users don’t read both options and just randomly vote for one. This introduces a lot of noise. However, the signals from the small percentage of users who vote correctly can sometimes be sufficient to help determine which model is preferred, as long as there’s minimal bias in the random voting.&lt;/p&gt;

&lt;h3 id=&quot;how_preferential_ranking_works&quot;&gt;How Preferential Ranking Works&lt;/h3&gt;

&lt;p&gt;Preferential ranking works in two steps:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Collect comparison data about user preference.&lt;/li&gt;
  &lt;li&gt;Compute a model ranking from these comparisons.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For each request, two or more models are selected to respond. An evaluator, which can be human or AI, picks the winner. The evaluator shouldn’t know which models are being judged. Each comparison is called a &lt;em&gt;match&lt;/em&gt;. This process results in a series of comparisons.&lt;/p&gt;

&lt;table&gt;
    &lt;style&gt;
        table {
          border-collapse: collapse;
          width: 100%;
        }

        th, td {
          padding: 8px;
          text-align: left;
          border-bottom: 1px solid #ddd;
        }
    &lt;/style&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Match ID&lt;/strong&gt;&lt;/td&gt;
    &lt;td&gt;&lt;strong&gt;Prompt&lt;/strong&gt;
    &lt;/td&gt;
    &lt;td&gt;&lt;strong&gt;Model A&lt;/strong&gt;
    &lt;/td&gt;
    &lt;td&gt;&lt;strong&gt;Model B&lt;/strong&gt;
    &lt;/td&gt;
    &lt;td&gt;&lt;strong&gt;Winner&lt;/strong&gt;
    &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td&gt;1
    &lt;/td&gt;
    &lt;td&gt;…
    &lt;/td&gt;
    &lt;td&gt;Model 1
    &lt;/td&gt;
    &lt;td&gt;Model 2
    &lt;/td&gt;
    &lt;td&gt;Model 1
    &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td&gt;2
    &lt;/td&gt;
    &lt;td&gt;…
    &lt;/td&gt;
    &lt;td&gt;Model 3
    &lt;/td&gt;
    &lt;td&gt;Model 1
    &lt;/td&gt;
    &lt;td&gt;Model 1
    &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td&gt;3
    &lt;/td&gt;
    &lt;td&gt;…
    &lt;/td&gt;
    &lt;td&gt;Model 1
    &lt;/td&gt;
    &lt;td&gt;Model 4
    &lt;/td&gt;
    &lt;td&gt;Model 4
    &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td&gt; ...
    &lt;/td&gt;
    &lt;td&gt; ...
    &lt;/td&gt;
    &lt;td&gt; ...
    &lt;/td&gt;
    &lt;td&gt; ...
    &lt;/td&gt;
    &lt;td&gt; ...
    &lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;From these comparisons, we need to compute the rankings of all models. The two most common ranking algorithms are &lt;a href=&quot;https://en.wikipedia.org/wiki/Elo_rating_system&quot;&gt;Elo&lt;/a&gt; (from chess) and &lt;a href=&quot;https://en.wikipedia.org/wiki/TrueSkill&quot;&gt;TrueSkill&lt;/a&gt; (from video games).&lt;/p&gt;

&lt;p&gt;While Chatbot Arena refers to their model scores “Elo scores”, they actually don’t use Elo. In December 2023, they switched to &lt;a href=&quot;https://lmsys.org/blog/2023-12-07-leaderboard/#transition-from-online-elo-rating-system-to-bradley-terry-model&quot;&gt;Bradley-Terry&lt;/a&gt; but scaled the resulting scores to make them look Elo-like (see their &lt;a href=&quot;https://colab.research.google.com/drive/1KdwokPjirkTmpO_P1WByFNFiqxWQquwH#scrollTo=HdZrGr4IcWCl&quot;&gt;notebook&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Given a history of match outcomes, the Bradley-Terry algorithm finds the model scores that maximize the likelihood of these match outcomes, turning model scoring into a maximum likelihood estimation problem. The input, for each training example, is the models that participate in the match. The output is the outcome of the match. Assuming there’s no draw, the outcome of a match is either 0 (a wins) or 1 (b wins).&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Predictive human preference for all LLM model pairs&quot; src=&quot;/assets/pics/predictive-preference/3-bradley-terry.png&quot; style=&quot;float: center; max-width: 90%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h3 id=&quot;correctness_of_chatbot_arena_ranking&quot;&gt;Correctness of Chatbot Arena Ranking&lt;/h3&gt;

&lt;p&gt;Given the same match outcomes, different ranking algorithms can produce different rankings. For example, the ranking computed by Elo might differ from the ranking computed by Bradley-Terry. How do we know that a ranking is correct?&lt;/p&gt;

&lt;p&gt;At its core, model ranking is a predictive problem. We compute a ranking from historical match outcomes and use it to predict future match outcomes. The quality of a ranking is determined by how accurately it can predict future match outcomes.&lt;/p&gt;

&lt;p&gt;Let’s say we have a match between model A and model B. If model A has a higher score, meaning that the ranking algorithm predicts that A wins. If users indeed prefer the higher-ranking model, the ranking algorithm makes a correct prediction.&lt;/p&gt;

&lt;h4 id=&quot;eval_data&quot;&gt;Eval data&lt;/h4&gt;

&lt;p&gt;To compute the accuracy of Chatbot Arena ranking, I used their data published in July 2023, which consists of &lt;a href=&quot;https://drive.google.com/file/d/1ZXiBRtADf9HZ8eEarIFTy-qrPNDUP_H0/view&quot;&gt;33K crowd-sourced comparisons&lt;/a&gt; for matches among 20 models. I used this smaller dataset instead of &lt;a href=&quot;https://drive.google.com/file/d/1ZXiBRtADf9HZ8eEarIFTy-qrPNDUP_H0/view&quot;&gt;their Jan 2024 dataset&lt;/a&gt; because this smaller dataset contains the prompt used for each match, which I need for predictive human preference. Benchmarking on this dataset allows me to compare my model with the Bradley-Terry algorithm later on.&lt;/p&gt;

&lt;p&gt;Here’s an example from their July 2023 dataset.&lt;/p&gt;

&lt;table&gt;
    &lt;style&gt;
        table {
          border-collapse: collapse;
          width: 100%;
        }

        th:not(:last-child), td:not(:last-child) {
          padding: 8px;
          text-align: left;
          border-bottom: 1px solid #ddd;
          border-right: 1px solid #ddd;
        }
    &lt;/style&gt;
  &lt;tr&gt;
   &lt;td&gt;&lt;strong&gt;prompt&lt;/strong&gt;
   &lt;/td&gt;
   &lt;td&gt;&lt;strong&gt;model_a&lt;/strong&gt;
   &lt;/td&gt;
   &lt;td&gt;&lt;strong&gt;model_b&lt;/strong&gt;
   &lt;/td&gt;
   &lt;td&gt;&lt;strong&gt;winner&lt;/strong&gt;
   &lt;/td&gt;
   &lt;td&gt;&lt;strong&gt;model_a&apos;s response&lt;/strong&gt;
   &lt;/td&gt;
   &lt;td&gt;&lt;strong&gt;model_b&apos;s response&lt;/strong&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;who was the last monarch of uk
   &lt;/td&gt;
   &lt;td&gt;koala-13b
   &lt;/td&gt;
   &lt;td&gt;vicuna-13b
   &lt;/td&gt;
   &lt;td&gt;model_a
   &lt;/td&gt;
   &lt;td&gt;The last monarch of the United Kingdom was Queen Elizabeth II, who reigned from 1952 to 2020.
   &lt;/td&gt;
   &lt;td&gt;The current monarch of the United Kingdom is Queen Elizabeth II. She has been the monarch since 1952, and is the longest-reigning monarch in British history.
   &lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;For reference, the Bradley-Terry (BT) scores of the top 7 models in this dataset are as follows.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;GPT-4: 1189&lt;/li&gt;
  &lt;li&gt;Claude-v1: 1150&lt;/li&gt;
  &lt;li&gt;Claude-instant-v1: 1110&lt;/li&gt;
  &lt;li&gt;GPT-3.5-Turbo: 1104&lt;/li&gt;
  &lt;li&gt;WizardLM-13B: 1058&lt;/li&gt;
  &lt;li&gt;Vicuna-13b: 1040&lt;/li&gt;
  &lt;li&gt;Guanaco-33b: 1031&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To create a test set, I randomly select 10% of the data (3300 examples). Each match has three possible outcomes: model_a wins, model_b wins, or tie. This can still be framed as a binary classification problem if we treat a tied match as two matches: one in which model_a wins and one in which model_b wins.&lt;/p&gt;

&lt;h4 id=&quot;results&quot;&gt;Results&lt;/h4&gt;

&lt;p&gt;I found that for all non-tie matches in my test set, the model with the higher Bradley-Terry score is preferred 74.1% of the time. This means that if we always predict the higher-ranked model as the winner for a match, we’d have an accuracy of 74.1%.&lt;/p&gt;

&lt;table&gt;
  &lt;tr&gt;
   &lt;td&gt;&lt;strong&gt;Test data&lt;/strong&gt;
   &lt;/td&gt;
   &lt;td&gt;&lt;strong&gt;Output classes&lt;/strong&gt;
   &lt;/td&gt;
   &lt;td&gt;&lt;strong&gt;# samples&lt;/strong&gt;
   &lt;/td&gt;
   &lt;td&gt;&lt;strong&gt;BT&apos;s accuracy&lt;/strong&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;All matches
   &lt;/td&gt;
   &lt;td&gt;
        &lt;ul&gt;
            &lt;li&gt;model_a wins&lt;/li&gt;
            &lt;li&gt;model_b wins&lt;/li&gt;
            &lt;li&gt;tie&lt;/li&gt;
        &lt;/ul&gt;  
   &lt;/td&gt;
   &lt;td&gt;3,300
   &lt;/td&gt;
   &lt;td&gt;53.33%
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;Non-tie matches
   &lt;/td&gt;
   &lt;td&gt;
&lt;ul&gt;
    &lt;li&gt;model_a wins&lt;/li&gt;
    &lt;li&gt;model_b wins&lt;/li&gt;
&lt;/ul&gt;
   &lt;/td&gt;
   &lt;td&gt;2,367
   &lt;/td&gt;
   &lt;td&gt;74.1%
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;Non-tie matches involving GPT-4
   &lt;/td&gt;
   &lt;td&gt;
        &lt;ul&gt;
            &lt;li&gt;model_a wins&lt;/li&gt;
            &lt;li&gt;model_b wins&lt;/li&gt;
        &lt;/ul&gt;
   &lt;/td&gt;
   &lt;td&gt;355
   &lt;/td&gt;
   &lt;td&gt;85.1% (always pick GPT-4 as winner) &lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Back in July 2023, GPT-4 was considered the strongest model by a long shot (this was before Gemini, Mistral, Claude-v2). Did users always prefer GPT-4 to all other models? They didn’t. In 355 non-tie matches involving GPT-4, GPT-4 wins 85.1%.&lt;/p&gt;

&lt;p&gt;This means that even though GPT-4 is the best model overall, there are prompts for which other models can outperform GPT-4. If we can figure out which prompts these are, and which models work best for them, we can route these prompts to the best-performing models, improving the response quality.&lt;/p&gt;

&lt;h2 id=&quot;predicting_human_preference_for_each_prompt&quot;&gt;Predicting Human Preference For Each Prompt&lt;/h2&gt;

&lt;p&gt;If a ranking algorithm is about figuring out which model is better overall, predictive human preference is about figuring out which model is better for each prompt. If we know in advance that for a particular prompt, GPT-3.5 works just as well as GPT-4, and GPT-3.5 is cheaper, we can route that prompt to GPT-3.5 instead. Or if we know that Mistral-7B works just as well as GPT-4 and Mistral-7B is faster, we can route our query to Mistral-7B instead.&lt;/p&gt;

&lt;p&gt;Model routing can also help with budget planning. Say, you only have enough budget to serve 50% of queries on the strongest model, and the rest to a weaker model, you want to make sure that you send to the weaker model only the queries that you’re confident it can do well on.&lt;/p&gt;

&lt;h3 id=&quot;experiment_setup&quot;&gt;Experiment setup&lt;/h3&gt;

&lt;p&gt;I treat predictive human preference as a binary classification task. Given a match between 2 models, predict which one wins. If the probability of model_a winning is around 0.5, it can be considered a tie. If a Bradley-Terry model takes only &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(model_a, model_b)&lt;/code&gt; as the input, a preference predictor takes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(prompt, model_a, model_b)&lt;/code&gt; as the input.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Predictive human preference for all LLM model pairs&quot; src=&quot;/assets/pics/predictive-preference/4-preference-predictor.png&quot; style=&quot;float: center; max-width: 90%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;The architecture of my preference predictor looks like this. The model encoder and preference predictor are neural networks that can be trained independently or together. I used DistilBERT as my prompt encoder.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Predictive human preference for all LLM model pairs&quot; src=&quot;/assets/pics/predictive-preference/5-predictive-preference-architecture.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;To train my model, I used 90% of LMSYS’s July 2023 dataset. I found that the predictor performed better using only non-tie matches (as opposed to using both tie and non-tie matches). I randomly flipped the order of models in a match 50% of the time.&lt;/p&gt;

&lt;p&gt;To evaluate my model, I used 10% of this data. This is the same test data used to evaluate the correctness of Chatbot Arena’s ranking above.&lt;/p&gt;

&lt;table&gt;
  &lt;tr&gt;
   &lt;td&gt;&lt;strong&gt;Split&lt;/strong&gt;
   &lt;/td&gt;
   &lt;td&gt;&lt;strong&gt;All matches&lt;/strong&gt;
   &lt;/td&gt;
   &lt;td&gt;&lt;strong&gt;Non-tie matches&lt;/strong&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;Train
   &lt;/td&gt;
   &lt;td&gt;29,700
   &lt;/td&gt;
   &lt;td&gt;20,927
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;Test
   &lt;/td&gt;
   &lt;td&gt;3,300
   &lt;/td&gt;
   &lt;td&gt;2,367
   &lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: I should’ve made a separate validation set for hyperparameter tuning. However, given that I didn’t have a lot of data and this is only a proof of concept, I didn’t do it. (I’m also lazy.) The matches are among 20 models, corresponding to 190 model pairs. 20,927 comparisons mean that, on average, there are only 110 comparisons per model pair.&lt;/p&gt;

&lt;h3 id=&quot;experiment_results&quot;&gt;Experiment results&lt;/h3&gt;

&lt;p&gt;I evaluated my preference predictor under two settings:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Using only &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;model_a&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;model_b&lt;/code&gt; as the input. This is to see whether this predictor, using only model names, can make better predictions about match outcomes than Chatbot Arena scores.&lt;/li&gt;
  &lt;li&gt;Using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(prompt, model_a, model_b)&lt;/code&gt; as the input. This is to see whether including prompts helps improve match outcome prediction.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I found that for all non-tie matches, my preference predictor can predict the match outcome accurately 75% of the time if not using prompts, and 76.2% of the time if using prompts. This suggests that human preference for models does change depending on the prompt. While the improvement doesn’t seem much, a 2.1% improvement can be significant at scale.&lt;/p&gt;

&lt;table&gt;
  &lt;tr&gt;
   &lt;td&gt;&lt;strong&gt;Eval data&lt;/strong&gt;
   &lt;/td&gt;
   &lt;td&gt;&lt;strong&gt;# eval samples&lt;/strong&gt;
   &lt;/td&gt;
   &lt;td&gt;&lt;strong&gt;Chatbot Arena&lt;/strong&gt;
   &lt;/td&gt;
   &lt;td&gt;&lt;strong&gt;Preference predictor&lt;/strong&gt;&lt;br /&gt;(without prompts)
   &lt;/td&gt;
   &lt;td&gt;&lt;strong&gt;Preference predictor&lt;/strong&gt;&lt;br /&gt;(with prompts)
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;Non-tie matches
   &lt;/td&gt;
   &lt;td&gt;2,367
   &lt;/td&gt;
   &lt;td&gt;74.1%
   &lt;/td&gt;
   &lt;td&gt;75%
   &lt;/td&gt;
   &lt;td&gt;76.2%
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;Non-tie matches involving GPT-4
   &lt;/td&gt;
   &lt;td&gt;355
   &lt;/td&gt;
   &lt;td&gt;85.1%
   &lt;/td&gt;
   &lt;td&gt;86.2%
   &lt;/td&gt;
   &lt;td&gt;87%
   &lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Keep in mind that this predictor was trained with a small amount of crowd-sourced (e.g. noisy) data. The prompts crowdsourced are also simple. Among &lt;a href=&quot;https://huggingface.co/datasets/lmsys/chatbot_arena_conversations&quot;&gt;33K prompts&lt;/a&gt;, 180 (0.55%) of them are “hello” and “hi”. These simple prompts are insufficient to distinguish strong models from weak ones. I suspect that with more/better data, the performance of this predictor can significantly improve.&lt;/p&gt;

&lt;h3 id=&quot;domain_specific_and_query_specific_leaderboards&quot;&gt;Domain-specific and query-specific leaderboards&lt;/h3&gt;

&lt;p&gt;Recall that 20 models correspond to 190 model pairs. To visualize how the predictor captures human preference, for each evaluation prompt, I generated 190 different inputs, one for each model pair.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Predictive human preference for all LLM model pairs&quot; src=&quot;/assets/pics/predictive-preference/6-model-pairs.png&quot; style=&quot;float: center; max-width: 50%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;I then visualized the 190 predictions for 190 model pairs in a 20 x 20 grid, as shown below for the prompt “Derive the elastic wave equation.” I only included 9 models in the plot to make it readable. The diagonal values refer to comparing a model to itself, so the predicted preference should be 0.5.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Predictive human preference for all LLM model pairs&quot; src=&quot;/assets/pics/predictive-preference/7-derive.png&quot; style=&quot;float: center; max-width: 90%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Given the predicted preference for all model pairs for a prompt, I used a Bradley-Terry model (the same ranking algorithm that LMSYS uses) to create a leaderboard for this prompt. I used the same scaling that LMSYS uses to make the scores look Elo-like. Here’s the ranking of the 9 models shown above for the query “Derive the elastic wave equation.”&lt;/p&gt;

&lt;p&gt;This also means that with this preference predictor, we can create a leaderboard for any arbitrary subset of data. We can have a leaderboard specific to any domain.&lt;/p&gt;

&lt;table&gt;
  &lt;tr&gt;
   &lt;td colspan=&quot;2&quot;&gt;&lt;center&gt;&lt;strong&gt;Model ranking for the prompt&lt;/strong&gt;&lt;em&gt;&quot;Derive the elastic wave equation.&quot;&lt;/em&gt;&lt;/center&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;gpt-4
   &lt;/td&gt;
   &lt;td&gt;1214
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;claude-v1
   &lt;/td&gt;
   &lt;td&gt;1162
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;gpt-3.5-turbo
   &lt;/td&gt;
   &lt;td&gt;1104
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;claude-instant-v1
   &lt;/td&gt;
   &lt;td&gt;1110
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;guanaco-33b
   &lt;/td&gt;
   &lt;td&gt;1023
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;vicuna-13b
   &lt;/td&gt;
   &lt;td&gt;1007
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;vicuna-7b
   &lt;/td&gt;
   &lt;td&gt;985
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;RWKV-4-Raven-14B
   &lt;/td&gt;
   &lt;td&gt;970
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;gpt4all-13b-snoozy
   &lt;/td&gt;
   &lt;td&gt;915
   &lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Despite being a toy predictor, the model seems to be able to capture different models’ performance patterns. One pattern is that for simple prompts, weak models can do (nearly) as well as strong models. For more challenging prompts, however, users are much more likely to prefer stronger models. Here’s a visualization of predicted human preference for an easy prompt (“hello, how are you?”) and a challenging prompt (“Explain why Planc length …”).&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Predictive human preference for all LLM model pairs&quot; src=&quot;/assets/pics/predictive-preference/8-easy-hard-prompts.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Here are the model rankings for these two prompts. The score spread for the simple prompt is much less than the score spread for the challenging prompt. The models that are ranked differently for these two prompts are highlighted in red.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Predictive human preference for all LLM model pairs&quot; src=&quot;/assets/pics/predictive-preference/10-ranking.png&quot; style=&quot;float: center; max-width: 80%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;The predictor is also the most confident that GPT-4 will be preferred for queries in Russian and queries that involve code writing. For example, the average predicted win rate for the following Russian query of GPT-4 against all other models is 91.55%. Notice that for this query, while claude-v1 is predicted to do well on this query, claude-instant-v1 is predicted to do poorly.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Predictive human preference for all LLM model pairs&quot; src=&quot;/assets/pics/predictive-preference/9-russian.png&quot; style=&quot;float: center; max-width: 90%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;My primitive experiment suggests that predictive human preference is feasible using a surprisingly small amount of data. There are many potential use cases for predictive human preference – model routing and interpretability are just two of them.&lt;/p&gt;

&lt;p&gt;Predictive human reference is the first and the most important step in model routing (the other key step is routing strategy). With more and more models being developed, each with different capabilities and a cost structure, model routing has clear economic values.&lt;/p&gt;

&lt;p&gt;I’m aware of four groups (two in stealth) that are working on model routing. One startup is Martian, which announced its &lt;a href=&quot;https://techcrunch.com/2023/11/15/martians-tool-automatically-switches-between-llms-to-reduce-costs/&quot;&gt;$9M seed round&lt;/a&gt;. LMSYS is also working on model routing, which I think is a natural progression from their work in comparative evaluation.&lt;/p&gt;

&lt;p&gt;While my experiment used human-annotated comparisons, LMSYS folks told me that due to the noisiness of crowd-sourced annotations and the costs of expert annotations, they’ve found that using GPT-4 to compare two responses works better. Depending on the complexity of the queries, generating 10,000 comparisons using GPT-4 would cost only $200 - 500, making this very affordable for companies that want to test it out.&lt;/p&gt;

&lt;p&gt;This is the most fun side project I’ve worked on in a while, so I’d love to talk more about it. For those interested, I’ll be hosting a casual 30-minute discussion on predictive human preference on Tuesday, Mar 5, 9.30am PST. Join our &lt;a href=&quot;https://discord.gg/Bgxhua5XVR&quot;&gt;Discord&lt;/a&gt; or email me if you want an invite!&lt;/p&gt;

&lt;h2 id=&quot;acknowledgment&quot;&gt;Acknowledgment&lt;/h2&gt;

&lt;p&gt;Thanks &lt;a href=&quot;https://twitter.com/luke_metz&quot;&gt;Luke Metz&lt;/a&gt; for helping me with the experiments and coercing me into using JAX. While JAX is super cool and makes a lot of things easy, it also caused some of the weirdest bugs I’ve ever seen. I’m glad I used it though. Thanks &lt;a href=&quot;https://www.linkedin.com/in/hanchunglee/&quot;&gt;Han-chung Lee&lt;/a&gt; for feedback on the plots.&lt;/p&gt;
</description>
        <pubDate>Wed, 28 Feb 2024 00:00:00 +0000</pubDate>
        <link>https://huyenchip.com//2024/02/28/predictive-human-preference.html</link>
        <guid isPermaLink="true">https://huyenchip.com/2024/02/28/predictive-human-preference.html</guid>
      </item>
    
      <item>
        <title>Generation configurations: temperature, top-k, top-p, and test time compute</title>
        <description>&lt;p&gt;ML models are probabilistic. Imagine that you want to know what’s the best cuisine in the world. If you ask someone this question twice, a minute apart, their answers both times should be the same. If you ask a model the same question twice, its answer can change. If the model thinks that Vietnamese cuisine has a 70% chance of being the best cuisine and Italian cuisine has a 30% chance, it’ll answer “Vietnamese” 70% of the time, and “Italian” 30%.&lt;/p&gt;

&lt;p&gt;This probabilistic nature makes AI great for creative tasks. What is creativity but the ability to explore beyond the common possibilities, to think outside the box?&lt;/p&gt;

&lt;p&gt;However, this probabilistic nature also causes inconsistency and hallucinations. It’s fatal for tasks that depend on factuality. Recently, I went over 3 months’ worth of customer support requests of an AI startup I advise and found that ⅕ of the questions are because users don’t understand or don’t know how to work with this probabilistic nature.&lt;/p&gt;

&lt;p&gt;To understand why AI’s responses are probabilistic, we need to understand how models generate responses, a process known as sampling (or decoding). This post consists of 3 parts.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Sampling&lt;/strong&gt;: sampling strategies and sampling variables including temperature, top-k, and top-p.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Test time compute&lt;/strong&gt;: increasing the compute allocated to inference, e.g. sampling multiple outputs, to help improve a model’s performance.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Structured outputs&lt;/strong&gt;: how to get models to generate outputs in a certain format.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;sampling&quot;&gt;Sampling&lt;/h2&gt;

&lt;p&gt;Given an input, a neural network produces an output by first computing the probabilities of all possible values. For a classifier, possible values are the available classes. For example, if a model is trained to classify whether an email is spam, there are only two possible values: spam and not spam. The model computes the probability of each of these two values, say being spam is 90% and not spam is 10%.&lt;/p&gt;

&lt;p&gt;To generate the next token, a language model first computes the probability distribution over all tokens in the vocabulary.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Sampling the next token based on token probabilities&quot; src=&quot;/assets/pics/sampling/1-sampling-tokens.png&quot; style=&quot;float: center; max-width: 83%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;For the spam email classification task, it’s okay to output the value with the highest probability. If the email has a 90% chance of being spam, you classify the email as spam. However, for a language model, always picking the most likely token, &lt;em&gt;greedy sampling&lt;/em&gt;, creates boring outputs. Imagine a model that, for whichever question you ask, always responds with the most common words.&lt;/p&gt;

&lt;p&gt;Instead of always picking the next most likely token, we can sample the next token according to the probability distribution over all possible values. Given the context of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;My favorite color is ...&lt;/code&gt;, if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;red&lt;/code&gt; has a 30% chance of being the next token and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;green&lt;/code&gt; has a 50% chance, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;red&lt;/code&gt; will be picked 30% of the time, and “green” 50% of the time.&lt;/p&gt;

&lt;h3 id=&quot;temperature&quot;&gt;Temperature&lt;/h3&gt;

&lt;p&gt;One problem with sampling the next token according to the probability distribution is that the model can be less creative. In the previous example, common words for colors like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;red&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;green&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;purple&lt;/code&gt;, etc. have the highest probabilities. The language model’s answer ends up sounding like that of a five-year-old: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;My favorite color is green.&lt;/code&gt; Because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;the&lt;/code&gt; has a low probability, the model has a low chance of generating a creative sentence such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;My favorite color is the color of a still lake on a spring morning.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Temperature is a technique used to redistribute the probabilities of the possible values. Intuitively, it reduces the probabilities of common tokens, and as a result, increases the probabilities of rarer tokens. This enables models to create more creative responses.&lt;/p&gt;

&lt;p&gt;To understand how temperature works, let’s take a step back to see how a model computes the probabilities. Given an input, a neural network processes this input and outputs a logit vector. Each logit corresponds to one possible. In the case of a language model, each logit corresponds to one token in the model’s vocabulary. The logit vector size is the size of the vocabulary.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Sampling the next token based on token probabilities&quot; src=&quot;/assets/pics/sampling/2-logits.png&quot; style=&quot;float: center; max-width: 57%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;While larger logits correspond to higher probabilities, the logits don’t represent the probabilities. Logits don’t sum up to one. Logits can even be negative, while probabilities have to be non-negative. To convert logits to probabilities, a softmax layer is often used. Let’s say the model has a vocabulary of N and the logit vector is \([x_1, x_2, ..., x_N]\). The probability for the \(i^{th}\) token, \(p_i\), is computed as follows:&lt;/p&gt;

\[p_i = \text{softmax}(x_i) = \frac{e^{x_i}}{\sum_j e^{x_j}}\]

&lt;p&gt;Temperature is a constant used to adjust the logits before the softmax transformation. Logits are divided by temperature. For a given temperature of \(T\), the adjusted logit for the \(i^{th}\) token is \(\frac{x_i}{T}\). Softmax is then applied on this adjusted logit instead of on \(x_i\).&lt;/p&gt;

&lt;p&gt;Let’s walk through a simple example to understand the effect of temperature on probabilities. Imagine that we have a model that has only two possible outputs: A and B. The logits computed from the last layer are &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[1, 3]&lt;/code&gt;. The logit for A is 1 and B is 3.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Without using temperature, equivalent to temperature = 1, the softmax probabilities are &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[0.12, 0.88]&lt;/code&gt;. The model picks B 88% of the time.&lt;/li&gt;
  &lt;li&gt;With temperature = 0.5, the probabilities are &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[0.02, 0.98]&lt;/code&gt;. The model picks B 98% of the time.&lt;/li&gt;
  &lt;li&gt;With temperature = 2, the probabilities are &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[0.27, 0.73]&lt;/code&gt;. The model picks B 73% of the time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The higher the temperature, the less likely the model is going to pick the most obvious value (the value with the highest logit), making the model’s outputs more creative but potentially less coherent. The lower the temperature, the more likely the model is going to pick the most obvious value, making the model’s out more consistent but potentially more boring.&lt;/p&gt;

&lt;p&gt;The graph below shows the softmax probability for token B at different temperatures. As the temperature gets closer to 0, the probability that the model picks token B becomes closer to 1. In our example, for temperature below 0.1, the model almost always outputs B. Model providers typically limit temperature to be between 0 and 2. If you own your model, you can use any non-negative temperature. A temperature of 0.7 is often recommended for creative use cases, as it balances creativity and determinism, but you should experiment and find the temperature that works best for you.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Sampling the next token based on token probabilities using temperature&quot; src=&quot;/assets/pics/sampling/3-temperature.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;It’s common practice to set the temperature to 0 for the model’s outputs to be more consistent. Technically, temperature can never be 0 – logits can’t be divided by 0. In practice, when we set the temperature to 0, the model just picks the token with the value with the largest logit, e.g. performing an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;argmax&lt;/code&gt;, without doing the logit adjustment and softmax calculation.&lt;/p&gt;

&lt;p&gt;A common debugging technique when working with an AI model is looking at the probabilities this model computes for given inputs. For example, if the probabilities look random, the model hasn’t learned much. OpenAI returns probabilities generated by their models as &lt;em&gt;&lt;a href=&quot;https://cookbook.openai.com/examples/using_logprobs&quot;&gt;logprobs&lt;/a&gt;&lt;/em&gt;. Logprobs, short for log probabilities, are probabilities in the log scale. Log scale is preferred when working with a neural network’s probabilities because it helps reduce the underflow problem. A language model can work with a vocabulary size of 100,000, which means the probabilities for many of the tokens can be too small to be represented by a machine. The small numbers might be rounded down to 0. Log scale helps reduce this problem.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Sampling the next token based on token probabilities using logprobs&quot; src=&quot;/assets/pics/sampling/4-logprobs.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h3 id=&quot;top_k&quot;&gt;Top-k&lt;/h3&gt;

&lt;p&gt;Top-k is a sampling strategy to reduce the computation workload without sacrificing too much of the model’s response diversity. Recall that to compute the probability distribution over all possible values, a softmax layer is used. Softmax requires two passes over all possible values: one to perform the exponential sum \(\sum_j e^{x_j}\) and one to perform \(\frac{e^{x_i}}{\sum_j e^{x_j}}\) for each value. For a language model with a large vocabulary, this process is computationally expensive.&lt;/p&gt;

&lt;p&gt;To avoid this problem, after the model has computed the logits, we pick the top k logits and perform softmax over these top k logits only. Depending on how diverse you want your application to be, k can be anywhere from 50 to 500, much smaller than a model’s vocabulary size. The model then samples from these top values. A smaller k value makes the text more predictable but less interesting, as the model is limited to a smaller set of likely words.&lt;/p&gt;

&lt;h3 id=&quot;top_p&quot;&gt;Top-p&lt;/h3&gt;

&lt;p&gt;In top-k sampling, the number of values considered is fixed to k. However, this number should change depending on the situation. For example, given the prompt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Do you like music? Answer with only yes or no.&lt;/code&gt;, the number of values considered should be two: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yes&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;no&lt;/code&gt;. Given the prompt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;What&apos;s the meaning of life?&lt;/code&gt;, the number of values considered should be much larger.&lt;/p&gt;

&lt;p&gt;Top-p, also known as nucleus sampling, allows for a more dynamic selection of values to be sampled from. In top-p sampling, the model sums the probabilities of the most likely next values in descending order and stops when the sum reaches p. Only the values within this cumulative probability are considered. Common values for top-p (nucleus) sampling in language models typically range from 0.9 to 0.95. A top-p value of 0.9, for example, means that the model will consider the smallest set of values whose cumulative probability exceeds 90%.&lt;/p&gt;

&lt;p&gt;Let’s say the probabilities of all tokens are as shown in the image below. If top_p = 90%, only &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yes&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;maybe&lt;/code&gt; will be considered, as their cumulative probability is greater than 90%. If top_p = 99%, then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yes&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;maybe&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;no&lt;/code&gt; are considered.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Sampling the next token based on token probabilities with top-p&quot; src=&quot;/assets/pics/sampling/5-top-p.png&quot; style=&quot;float: center; max-width: 50%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Unlike top-k, top-p doesn’t necessarily reduce the softmax computation load. Its benefit is that because it focuses on only the set of most relevant values for each context, it allows outputs to be more contextually appropriate. In theory, there doesn’t seem to be a lot of benefits to top-p sampling. However, in practice, top-p has proven to work well, causing its popularity to rise.&lt;/p&gt;

&lt;h3 id=&quot;stopping_condition&quot;&gt;Stopping condition&lt;/h3&gt;

&lt;p&gt;An autoregressive language model generates sequences of tokens by generating one token after another. A long output sequence takes more time, costs more compute (money), and can sometimes be annoying to users. We might want to set a condition for the model to stop the sequence.&lt;/p&gt;

&lt;p&gt;One easy method is to ask models to stop generating after a fixed number of tokens. The downside is that the output is likely to be cut off mid-sentence. Another method is to use stop tokens. For example, you can ask models to stop generating when it encounters “&amp;lt;EOS&amp;gt;”. Stopping conditions are helpful to keep the latency and cost down.&lt;/p&gt;

&lt;h2 id=&quot;test_time_compute&quot;&gt;Test Time Compute&lt;/h2&gt;

&lt;p&gt;One simple way to improve a model’s performance is to generate multiple outputs and select the best one. This approach is called &lt;em&gt;test time compute&lt;/em&gt; (or &lt;em&gt;test time sampling&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;You can either show users multiple outputs and let them choose the one that works best for them or devise a method to select the best one. If you want your model’s responses to be consistent, you want to keep all sampling variables fixed. However, if you want to generate multiple outputs and pick the best one, you don’t want to vary your sampling variables.&lt;/p&gt;

&lt;p&gt;One selection method is to pick the output with the highest probability. A language model’s output is a sequence of tokens, each token has a probability computed by the model. The probability of an output is the product of the probabilities of all tokens in the output.&lt;/p&gt;

&lt;p&gt;Consider the sequence of tokens [&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;I&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;love&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;food&lt;/code&gt;] and:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;the probability for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;I&lt;/code&gt; is 0.2&lt;/li&gt;
  &lt;li&gt;the probability for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;love&lt;/code&gt; given &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;I&lt;/code&gt; is 0.1&lt;/li&gt;
  &lt;li&gt;the probability for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;food&lt;/code&gt; given &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;I&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;love&lt;/code&gt; is 0.3&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The sequence’s probability is then: 0.2 * 0.1 * 0.3 = 0.006.&lt;/p&gt;

&lt;p&gt;Mathematically, this can be denoted as follows:&lt;/p&gt;

\[p(\text{I love food}) = p(\text{I}) \times p(\text{love}|\text{I}) \times p(\text{food}|\text{I, love})\]

&lt;p&gt;Remember that it’s easier to work with probabilities on a log scale. The logarithm of a product is equal to a sum of logarithms, so the logprob of a sequence of tokens is the sum of the logprob of all tokens in the sequence.&lt;/p&gt;

\[\text{logprob}(\text{I love food}) = \text{logprob}(\text{I}) + \text{logprob}(\text{love}|\text{I}) + \text{logprob}(\text{food}|\text{I, love})\]

&lt;p&gt;With summing, longer sequences are likely to have to lower total logprob (log(1) = 0, and log of all positive values less than 1 is negative). To avoid biasing towards short sequences, we use the average logprob by dividing the sum by its sequence length. After sampling multiple outputs, we pick the one with the highest average logprob. As of writing, this is what OpenAI API uses. You can set the parameter &lt;em&gt;&lt;a href=&quot;https://platform.openai.com/docs/api-reference/completions/create#completions-create-best_of&quot;&gt;best_of&lt;/a&gt;&lt;/em&gt; to a specific value, say 10, to ask OpenAI models to return the output with the highest average logprob out of 10 different outputs.&lt;/p&gt;

&lt;p&gt;Another method is to use a reward model to score each output, as discussed in the previous section. Recall that both &lt;a href=&quot;https://multithreaded.stitchfix.com/blog/2023/03/06/expert-in-the-loop-generative-ai-at-stitch-fix/&quot;&gt;Stitch Fix&lt;/a&gt; and &lt;a href=&quot;https://engineering.grab.com/llm-powered-data-classification&quot;&gt;Grab&lt;/a&gt; pick the outputs given high scores by their reward models or verifiers. OpenAI also trained verifiers to help their models pick the best solutions to math problems (&lt;a href=&quot;https://arxiv.org/pdf/2110.14168.pdf&quot;&gt;Cobbe et al., 2021&lt;/a&gt;). They found that sampling more outputs led to better performance, but only up to a certain point. In their experiment, this point is 400 outputs. Beyond this point, performance starts to decrease, as shown below. They hypothesized that as the number of sampled outputs increases, the chance of finding adversarial outputs that can fool the verifiers also increases. While this is an interesting experiment, I don’t believe anyone in production samples 400 different outputs for each input. The cost would be astronomical.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Sampling the next token based on token probabilities&quot; src=&quot;/assets/pics/sampling/6-test-time-sampling.png&quot; style=&quot;float: center; max-width: 50%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;You can also choose heuristics based on the needs of your application. For example, if your application benefits from shorter responses, you can pick the shortest one. If your application is to convert from natural language to SQL queries, you can pick the valid SQL query that is the most efficient.&lt;/p&gt;

&lt;p&gt;Sampling multiple outputs can be useful for tasks that expect exact answers. For example, given a math problem, the model can solve it multiple times and pick the most frequent answer as its final solution. Similarly, for a multiple-choice question, a model can pick the most frequently output option. This is what Google did when &lt;a href=&quot;https://storage.googleapis.com/deepmind-media/gemini/gemini_1_report.pdf&quot;&gt;evaluating their model Gemini on MMLU&lt;/a&gt;, a benchmark of multiple-choice questions. They sampled 32 outputs for each question. While this helped Gemini achieve a high score on this benchmark, it’s unclear whether their model is better than another model that gets a lower score by only generating one output for each question.&lt;/p&gt;

&lt;p&gt;The more fickle a model is, the more we can benefit from sampling multiple outputs. The optimal thing to do with a fickle model, however, is to swap it out for another. For one project, we used AI to extract certain information from an image of the product. We found that for the same image, our model could read the information only half of the time. For the other half, the model said that the image was too blurry or the text was too small to read. For each image, we queried the model at most three times, until it could extract the information.&lt;/p&gt;

&lt;p&gt;While we can usually expect some model performance improvement by sampling multiple outputs, it’s expensive. On average, generating two outputs costs approximately twice as much as generating one.&lt;/p&gt;

&lt;h2 id=&quot;structured_outputs&quot;&gt;Structured Outputs&lt;/h2&gt;

&lt;p&gt;Oftentimes, in production, we need models to generate text following certain formats. Having structured outputs is essential for the following two scenarios.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Tasks whose outputs need to follow certain grammar. For example, for text-to-SQL or text-to-regex, outputs have to be valid SQL queries and regexes. For classification, outputs have to be valid classes.&lt;/li&gt;
  &lt;li&gt;Tasks whose outputs are then parsed by downstream applications. For example, if you use an AI model to write product descriptions, you want to extract only the product descriptions without buffer texts like “&lt;em&gt;Here’s the description&lt;/em&gt;” or “&lt;em&gt;As a language model, I can’t …&lt;/em&gt;”. Ideally, for this scenario, models should generate structured outputs, such as JSON with specific keys, that can be parseable.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;OpenAI was the first model provider to introduce &lt;em&gt;&lt;a href=&quot;https://platform.openai.com/docs/guides/text-generation/json-mode&quot;&gt;JSON mode&lt;/a&gt;&lt;/em&gt; in their text generation API. Note that their JSON mode guarantees only that the outputs are valid JSON, not what’s inside the JSON.  As of writing, OpenAI’s JSON mode doesn’t yet work for vision models, but I’m sure it’ll just be a matter of time.&lt;/p&gt;

&lt;p&gt;The generated JSONs can also be truncated due to the model’s stopping condition, such as when it reaches the maximum output token length. If the max token length is set too short, the output JSONs can be truncated and hence not parseable. If it’s set too long, the model’s responses become both too slow and expensive.&lt;/p&gt;

&lt;p&gt;Independent tools like &lt;a href=&quot;https://github.com/guidance-ai/guidance&quot;&gt;guidance&lt;/a&gt; and &lt;a href=&quot;https://github.com/outlines-dev/outlines&quot;&gt;outlines&lt;/a&gt; let you structure the outputs of certain models. Here are two examples of using guidance to generate outputs constrained to a set of options and a regex.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Sampling structured outputs&quot; src=&quot;/assets/pics/sampling/7-guidance.png&quot; style=&quot;float: center; max-width: 81%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h3 id=&quot;how_to_generate_structured_outputs&quot;&gt;How to generate structured outputs&lt;/h3&gt;

&lt;p&gt;You can guide a model to generate constrained outputs at different layers of the AI stack: during prompting, sampling, and finetuning. Prompting is currently the easiest but least effective method. You can instruct a model to output valid JSON following a specific schema. However, there’s no guarantee that the model will always follow this instruction.&lt;/p&gt;

&lt;p&gt;Finetuning is currently the go-to approach to get models to generate outputs in the style and format that you want. You can do finetuning with or without changing the model’s architecture. For example, you can finetune a model on examples with the output format you want. While this still doesn’t guarantee the model will always output the expected format, this is much more reliable than prompting. It also has the added benefit of reducing inference costs, assuming that you no longer have to include instructions and examples of the desirable format in your prompt.&lt;/p&gt;

&lt;p&gt;For certain tasks, you can guarantee the output format with finetuning by modifying the model’s architecture. For example, for classification, you can append a classifier head to the foundation model’s architecture to make sure that the model only outputs one of the pre-specified classes. During finetuing, you can retrain the entire architecture or only this classifier head.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Sampling the next token based on token probabilities&quot; src=&quot;/assets/pics/sampling/8-finetuning-classifier.png&quot; style=&quot;float: center; max-width: 95%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Both sampling and finetuning techniques are needed because of the assumption that the model, by itself, isn’t capable of doing it. As models become more powerful, we can expect them to get better at following instructions. I suspect that in the future, it’ll be easier to get models to output exactly what we need with minimal prompting, and these techniques will become less important.&lt;/p&gt;

&lt;h3 id=&quot;constraint_sampling&quot;&gt;Constraint sampling&lt;/h3&gt;

&lt;p&gt;Constraint sampling is a technique used to guide the generation of text towards certain constraints. The simplest but expensive way to do so is to keep on generating outputs until you find one that fits your constraints, as discussed in the section &lt;a href=&quot;#test_time_compute&quot;&gt; Test Time Compute &lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Constraint sampling can also be done during token sampling. I wasn’t able to find a lot of literature on how companies today are doing it. What’s written below is from my understanding, which can be wrong, so feedback and pointers are welcome!&lt;/p&gt;

&lt;p&gt;At a high level, to generate a token, the model samples among values that meet the constraints. Recall that to generate a token, your model first outputs a logit vector, each logit corresponds to one possible value. With constrained sampling, we filter this logit vector to keep only the values that meet our constraints. Then we sample from these valid values.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Sampling the next token based on token probabilities&quot; src=&quot;/assets/pics/sampling/9-constrained-sampling.png&quot; style=&quot;float: center; max-width: 85%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;In the above example, the constraint is straightforward to filter for. However, in most cases, it’s not that straightforward. We need to have a grammar that specifies what is and isn’t allowed at each step. For example, JSON grammar dictates that after &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{&lt;/code&gt;, we can’t have another &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{&lt;/code&gt; unless it’s part of a string, as in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{&quot;key&quot;: &quot;&quot;}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Building out that grammar and incorporating that grammar into the sampling process is non-trivial. We’d need a separate grammar for every output format we want: JSON, regex, CSV, etc. Some are against constrained sampling because they believe the resources needed for constrained sampling are better invested in training models to become better at following instructions.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;I believe understanding how an AI model samples its outputs is essential for anyone who wishes to leverage AI to solve their problems. Probability is magical but can also be confusing. Writing this post has been a lot of fun as it gave me a chance to dig deeper into many concepts that I’ve been curious about for a long time.&lt;/p&gt;

&lt;p&gt;As always, feedback is much appreciated. Thanks &lt;a href=&quot;https://leehanchung.github.io/&quot;&gt;Han Lee&lt;/a&gt; and &lt;a href=&quot;https://twitter.com/luke_metz&quot;&gt;Luke Metz&lt;/a&gt; for graciously agreeing to be my first readers.&lt;/p&gt;
</description>
        <pubDate>Tue, 16 Jan 2024 00:00:00 +0000</pubDate>
        <link>https://huyenchip.com//2024/01/16/sampling.html</link>
        <guid isPermaLink="true">https://huyenchip.com/2024/01/16/sampling.html</guid>
      </item>
    
      <item>
        <title>Multimodality and Large Multimodal Models (LMMs)</title>
        <description>&lt;p&gt;For a long time, each ML model operated in one data mode – text (translation, language modeling), image (object detection, image classification), or audio (speech recognition).&lt;/p&gt;

&lt;p&gt;However, natural intelligence is not limited to just a single modality. Humans can read, talk, and see. We listen to music to relax and watch out for strange noises to detect danger. Being able to work with multimodal data is essential for us or any AI to operate in the real world.&lt;/p&gt;

&lt;p&gt;OpenAI noted in their &lt;a href=&quot;https://cdn.openai.com/papers/GPTV_System_Card.pdf&quot;&gt;GPT-4V system card&lt;/a&gt; that “&lt;em&gt;incorporating additional modalities (such as image inputs) into LLMs is viewed by some as a key frontier in AI research and development&lt;/em&gt;.”&lt;/p&gt;

&lt;p&gt;Incorporating additional modalities to LLMs (Large Language Models) creates LMMs (Large Multimodal Models). Not all multimodal systems are LMMs. For example, text-to-image models like Midjourney, Stable Diffusion, and Dall-E are multimodal but don’t have a language model component. Multimodal can mean one or more of the following:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Input and output are of different modalities (e.g. text-to-image, image-to-text)&lt;/li&gt;
  &lt;li&gt;Inputs are multimodal (e.g. a system that can process both text and images)&lt;/li&gt;
  &lt;li&gt;Outputs are multimodal (e.g. a system that can generate both text and images)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This post covers multimodal systems in general, including LMMs. It consists of 3 parts.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Part 1 covers the context for multimodality, including why multimodal, different data modalities, and types of multimodal tasks.&lt;/li&gt;
  &lt;li&gt;Part 2 discusses the fundamentals of a multimodal system, using the examples of CLIP, which lays the foundation for many future multimodal systems, and Flamingo, whose impressive performance gave rise to LMMs.&lt;/li&gt;
  &lt;li&gt;Part 3 discusses some active research areas for LMMs, including generating multimodal outputs and adapters for more efficient multimodal training, covering newer multimodal systems such as BLIP-2, LLaVA, LLaMA-Adapter V2, LAVIN, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The post is long. Feel free to skip to the sections most interesting to you.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;⚠ Ambiguous terminology ⚠&lt;/b&gt;&lt;br /&gt;
Multimodal data can also refer to multimodal distributions, e.g. bimodal distribution, which is different from multimodal data in this post.
&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;b&gt;Table of contents&lt;/b&gt;&lt;br /&gt;
&lt;a href=&quot;#part_1_understanding_multimodal&quot;&gt;Part 1. Understanding Multimodal&lt;/a&gt;&lt;br /&gt;
…. &lt;a href=&quot;#why_multimodal&quot;&gt;Why multimodal&lt;/a&gt;&lt;br /&gt;
…. &lt;a href=&quot;#data_modalities&quot;&gt;Data modalities&lt;/a&gt;&lt;br /&gt;
…. &lt;a href=&quot;#multimodal_tasks&quot;&gt;Multimodal tasks&lt;/a&gt;&lt;br /&gt;
…….. &lt;a href=&quot;#generation&quot;&gt;Generation&lt;/a&gt;&lt;br /&gt;
…….. &lt;a href=&quot;#vision_language_understanding&quot;&gt;Vision-language understanding&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;#part_2_multimodal_training&quot;&gt;Part 2. Fundamentals of Multimodal Training&lt;/a&gt;&lt;br /&gt;
…. &lt;a href=&quot;#clip&quot;&gt;CLIP: Contrastive Language-Image Pre-training&lt;/a&gt;&lt;br /&gt;
…….. &lt;a href=&quot;#clip_s_high_level_architecture&quot;&gt;CLIP’s high-level architecture&lt;/a&gt;&lt;br /&gt;
…….. &lt;a href=&quot;#natural_language_supervision&quot;&gt;Natural language supervision&lt;/a&gt;&lt;br /&gt;
…….. &lt;a href=&quot;#contrastive_learning&quot;&gt;Contrastive learning&lt;/a&gt;&lt;br /&gt;
…….. &lt;a href=&quot;#clip_applications&quot;&gt;CLIP applications&lt;/a&gt;&lt;br /&gt;
…. &lt;a href=&quot;#flamingo&quot;&gt;Flamingo: the dawns of LMMs&lt;/a&gt;&lt;br /&gt;
…….. &lt;a href=&quot;#flamingo_s_high_level_architecture&quot;&gt;Flamingo’s high-level architecture&lt;/a&gt;&lt;br /&gt;
…….. &lt;a href=&quot;#data&quot;&gt;Data&lt;/a&gt;&lt;br /&gt;
…….. &lt;a href=&quot;#flamingo_s_vision_encoder&quot;&gt;Flamingo’s vision encoder&lt;/a&gt;&lt;br /&gt;
…….. &lt;a href=&quot;#flamingo_s_language_model&quot;&gt;Flamingo’s language model&lt;/a&gt;&lt;br /&gt;
…. &lt;a href=&quot;#clip_vs_flamingo&quot;&gt;TL;DR: CLIP vs. Flamingo&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;#part_3_research_directions_for_lmms&quot;&gt;Part 3. Research Directions for LMMs&lt;/a&gt;&lt;br /&gt;
…. &lt;a href=&quot;#incorporating_more_data_modalities&quot;&gt;Incorporating more data modalities&lt;/a&gt;&lt;br /&gt;
…. &lt;a href=&quot;#multimodal_systems_for_instruction_following&quot;&gt;Multimodal systems for instruction-following&lt;/a&gt;&lt;br /&gt;
…. &lt;a href=&quot;#adapters_for_more_efficient_multimodal_training&quot;&gt;Adapters for more efficient multimodal training&lt;/a&gt;&lt;br /&gt;
…. &lt;a href=&quot;#generating_multimodal_outputs&quot;&gt;Generating multimodal outputs&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;#conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;#resources&quot;&gt;Resources&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2 id=&quot;part_1_understanding_multimodal&quot;&gt;Part 1. Understanding Multimodal&lt;/h2&gt;

&lt;h2 id=&quot;why_multimodal&quot;&gt;Why multimodal&lt;/h2&gt;

&lt;p&gt;Many use cases are impossible without multimodality, especially those in industries that deal with a mixture of data modalities such as healthcare, robotics, e-commerce, retail, gaming, etc.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Multimodal AI in healthcare&quot; src=&quot;/assets/pics/multimodal/26-healthcare.png&quot; style=&quot;float: center; max-width: 70%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
    An example of how multimodality can be used in healthcare. Image from Multimodal biomedical AI (Acosta et al., Nature Medicine 2022)
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Not only that, incorporating data from other modalities can help boost model performance. Shouldn’t a model that can learn from both text and images perform better than a model that can learn from only text or only image?&lt;/p&gt;

&lt;p&gt;Multimodal systems can provide a more flexible interface, allowing you to interact with them in whichever way works best for you at the moment. Imagine you can ask a question by typing, talking, or just pointing your camera at something.&lt;/p&gt;

&lt;p&gt;One use case that I’m especially excited about, is that multimodality can also enable visually impaired people to browse the Internet and also navigate the real world.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Some cool multimodal use cases from GPT-4V&quot; src=&quot;/assets/pics/multimodal/1-gpt-4v-use-cases.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
    Some cool multimodal use cases from GPT-4V
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2 id=&quot;data_modalities&quot;&gt;Data modalities&lt;/h2&gt;

&lt;p&gt;Different data modes are text, image, audio, tabular data, etc. One data mode can be represented or &lt;em&gt;approximated&lt;/em&gt; in another data mode. For example:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Audio can be represented as images (mel spectrograms).&lt;/li&gt;
  &lt;li&gt;Speech can be transcribed into text, though its text-only representation loses information such as volume, intonation, pauses, etc.&lt;/li&gt;
  &lt;li&gt;An image can be represented as a vector, which, in turn, can be flattened and represented as a sequence of text tokens.&lt;/li&gt;
  &lt;li&gt;A video is a sequence of images plus audio. ML models today mostly treat videos as sequences of images. This is a severe limitation, as sounds have proved to be just as important as visuals for videos. &lt;a href=&quot;https://www.kantar.com/uki/inspiration/advertising-media/the-power-of-tiktok&quot;&gt;88% of TikTok users shared that sound is essential for their TikTok experience&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;A text can be represented as an image if you simply take a picture of it.&lt;/li&gt;
  &lt;li&gt;A data table can be converted into a chart, which is an image.&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;b&gt;How about other data modalities?&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;All digital data formats can be represented using bitstrings (strings of 0 and 1) or bytestrings. A model that can effectively learn from bitstrings or bytestrings will be very powerful, and it can learn from any data mode.&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;There are other data modalities we haven’t touched on, such as graphs and 3D assets. We also haven’t touched on the formats used to represent smell and touch (haptics).&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;br /&gt;
In ML today, audio is still largely treated as a voice-based alternative to text. The most common use cases for audio are still speech recognition (speech-to-text) and speech synthesis (text-to-speech). Non-speech audio use cases, e.g. music generation, are still pretty niche. See the fake Drake &amp;amp; Weeknd song and &lt;a href=&quot;https://huggingface.co/spaces/facebook/MusicGen&quot;&gt;MusicGen model on HuggingFace&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Image is perhaps the most versatile format for model inputs, as it can be used to represent text, tabular data, audio, and to some extent, videos. There’s also so much more visual data than text data. We have phones/webcams that constantly take pictures and videos today.&lt;/p&gt;

&lt;p&gt;Text is a much more powerful mode for model outputs. A model that can generate images can only be used for image generation, whereas a model that can generate text can be used for many tasks: summarization, translation, reasoning, question answering, etc.&lt;/p&gt;

&lt;p&gt;For simplicity, we’ll focus on 2 modalities: images and text. The learnings can be somewhat generalized to other modalities.&lt;/p&gt;

&lt;h2 id=&quot;multimodal_tasks&quot;&gt;Multimodal tasks&lt;/h2&gt;
&lt;p&gt;To understand multimodal systems, it’s helpful to look at the tasks they are built to solve. In literature, I commonly see vision-language tasks divided into two groups: &lt;strong&gt;generation&lt;/strong&gt; and &lt;strong&gt;vision-language understanding&lt;/strong&gt; (VLU), which is the umbrella term for all tasks that don’t require generation. The line between these two groups is blurred, as being able to generate answers requires understanding too.&lt;/p&gt;

&lt;h3 id=&quot;generation&quot;&gt;Generation&lt;/h3&gt;

&lt;p&gt;For generative tasks, the output can be unimodal (e.g. text, image, 3D rendering) or multimodal. While unimodal outputs are common today, multimodal outputs are still shaping up. We’ll discuss multimodal outputs at the end of this post.&lt;/p&gt;

&lt;h4 id=&quot;image-generation-text-to-image-synthesis&quot;&gt;Image generation (text-to-image synthesis)&lt;/h4&gt;

&lt;p&gt;This category is straightforward. Examples: Dall-E, Stable Diffusion, and Midjourney.&lt;/p&gt;

&lt;h4 id=&quot;text-generation&quot;&gt;Text generation&lt;/h4&gt;

&lt;p&gt;A common text generation task is visual question answering. Instead of relying only on text for the context, you can give the model both text and images. Imagine you can point your camera to anything and ask questions like: “My car won’t start. What’s wrong with it?”, “How to make this dish?”, or “What is this meme about?”.&lt;/p&gt;

&lt;p&gt;Another common use case is image captioning, which can be used as part of a text-based image retrieval system. An organization might have millions, if not billions, of images: product images, graphs, designs, team pictures, promotional materials, etc. AI can automatically generate captions and metadata for them, making it easier to find the exact images you want.&lt;/p&gt;

&lt;h3 id=&quot;vision_language_understanding&quot;&gt;Vision-language understanding&lt;/h3&gt;
&lt;p&gt;We’ll zoom into two task types: classification and text-based image retrieval (TBIR).&lt;/p&gt;

&lt;h4 id=&quot;classification&quot;&gt;Classification&lt;/h4&gt;

&lt;p&gt;Classification models can only generate outputs that belong to a pre-determined list of classes. This works when you only care about a fixed number of outcomes. For example, an OCR system only needs to predict if a visual is one of the known characters (e.g. a digit or a letter).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Side note&lt;/strong&gt;: An OCR system processes data at the character level. When used together with a system that can understand the broader context, it can improve use cases such as allowing you to “talk” to any textbook, contract, assembly instructions, etc.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Document processing with GPT-4V&quot; src=&quot;/assets/pics/multimodal/2-gpt-4v-ocr.png&quot; style=&quot;float: center; max-width: 80%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
    Document processing with GPT-4V. The model&apos;s mistake is highlighted in red.
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;One related task to classification is &lt;strong&gt;image-to-text retrieval&lt;/strong&gt;: given an image and a pool of pre-defined texts, find the text that’s most likely to accompany the image. This can be helpful for product image search, i.e. retrieving product reviews from a picture.&lt;/p&gt;

&lt;h4 id=&quot;text-based-image-retrieval-image-search&quot;&gt;Text-based image retrieval (image search)&lt;/h4&gt;

&lt;p&gt;Image search matters not only for search engines but also for enterprises to be able to search through all their internal images and documents. Some people call text-based image retrieval “text-to-image retrieval”.&lt;/p&gt;

&lt;p&gt;There are several approaches to text-based image retrieval. Two of them are:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Generate captions and metadata for each image, either manually or automatically (see image captioning in &lt;strong&gt;Text generation&lt;/strong&gt;). Given a text query, find images whose captions/metadata are closest to this text query.&lt;/li&gt;
  &lt;li&gt;Train a joint embedding space for both images and text. Given a text query, generate an embedding for this query, and find all images whose embeddings are closest to this embedding.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The second approach is more flexible, and I believe will be more widely used. This approach requires having a strong joint embedding space for both vision and language, like the one that OpenAI’s &lt;a href=&quot;https://arxiv.org/abs/2103.00020&quot;&gt;CLIP&lt;/a&gt; developed.&lt;/p&gt;

&lt;h2 id=&quot;part_2_multimodal_training&quot;&gt;Part 2. Fundamentals of Multimodal Training&lt;/h2&gt;

&lt;p&gt;Given the existence of so many amazing multimodal systems, a challenge of writing this post is choosing which systems to focus on. In the end, I decided to focus on two models: &lt;a href=&quot;https://arxiv.org/abs/2103.00020&quot;&gt;CLIP&lt;/a&gt; (2021) and &lt;a href=&quot;https://arxiv.org/abs/2204.14198&quot;&gt;Flamingo&lt;/a&gt; (2022) both for their significance as well as availability and clarity of public details.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;CLIP was the first model that could generalize to multiple &lt;strong&gt;image classification tasks&lt;/strong&gt; with zero- and few-shot learning.&lt;/li&gt;
  &lt;li&gt;Flamingo wasn’t the first large multimodal model that could &lt;strong&gt;generate open-ended responses&lt;/strong&gt; (&lt;a href=&quot;https://arxiv.org/abs/2201.12086&quot;&gt;Salesforce’s BLIP&lt;/a&gt; came out 3 months prior). However, Flamingo’s strong performance prompted some to consider it &lt;a href=&quot;https://arxiv.org/abs/2304.08485&quot;&gt;the GPT-3 moment in the multimodal domain&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even though these two models are older, many techniques they use are still relevant today. I hope they serve as the foundation to understanding newer models. The multimodal space is evolving repaidly, with many new ideas being developed. We’ll go over these newer models in &lt;a href=&quot;#part_3_research_directions_for_lmms&quot;&gt;Part 3&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;At a high level, a multimodal system consists of the following components:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;An &lt;strong&gt;encoder&lt;/strong&gt; for each data modality to generate the embeddings for data of that modality.&lt;/li&gt;
  &lt;li&gt;A way to &lt;strong&gt;align embeddings&lt;/strong&gt; of different modalities into the same &lt;strong&gt;multimodal embedding space&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;[Generative models only] A &lt;strong&gt;language model to generate text responses&lt;/strong&gt;. Since inputs can contain both text and visuals, new techniques need to be developed to allow the language model to condition its responses on not just text, but also visuals.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Ideally, as many of these components should be pretrained and reusable as possible.&lt;/p&gt;

&lt;h2 id=&quot;clip&quot;&gt;CLIP: Contrastive Language-Image Pre-training&lt;/h2&gt;

&lt;p&gt;CLIP’s key contribution is its ability to map data of different modalities, text and images, into a shared embedding space. This shared multimodal embedding space makes text-to-image and image-to-text tasks so much easier.&lt;/p&gt;

&lt;p&gt;Training this multimodal embedding space also produced a strong image encoder, which allows CLIP to achieve &lt;strong&gt;competitive zero-shot performance on many image classification tasks&lt;/strong&gt;. This strong image encoder can be used for many other tasks: image generation, visual question answering, and text-based image retrieval. Flamingo and LLaVa use CLIP as their image encoder. DALL-E uses CLIP to rerank generated images. It’s unclear if GPT-4V uses CLIP.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Zero-shot image classification with CLIP&quot; src=&quot;/assets/pics/multimodal/3-CLIP-image-classification.png&quot; style=&quot;float: center; max-width: 75%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
    Zero-shot image classification with CLIP
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;CLIP leveraged &lt;strong&gt;natural language supervision&lt;/strong&gt; and &lt;strong&gt;contrastive learning&lt;/strong&gt;, which allowed CLIP to both scale up their data and make training more efficient. We’ll go over why/how these two techniques work.&lt;/p&gt;

&lt;h3 id=&quot;clip_s_high_level_architecture&quot;&gt;CLIP&apos;s high-level architecture&lt;/h3&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Architecture of OpenAI&apos;s CLIP&quot; src=&quot;/assets/pics/multimodal/4-CLIP-architecture.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
    CLIP&apos;s architecture. Both encoders and projection matrices are jointly trained together from scratch. The training goal is to maximize the similarity scores of the right (image, text) pairings while minimizing the similarity scores of the wrong pairings (contrastive learning). 
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;For the &lt;strong&gt;image encoder&lt;/strong&gt;, the authors experimented with both ResNet and ViT. Their best-performing model is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ViT-L/14@336px&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Large vision transformer (ViT-L)&lt;/li&gt;
  &lt;li&gt;14 patches (each image is divided into 14x14 pixel patches/sub-images)&lt;/li&gt;
  &lt;li&gt;on 336x336 pixel input&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the &lt;strong&gt;text encoder&lt;/strong&gt;, CLIP uses a Transformer model similar to &lt;a href=&quot;https://openai.com/research/better-language-models&quot;&gt;GPT-2&lt;/a&gt; but smaller. Their base model has only 63M parameters with 8 attention heads. The authors found CLIP’s performance to be less sensitive to the capacity of the text encoder.&lt;/p&gt;

&lt;p&gt;Embeddings generated by the image encoder and text encoder are projected into the same embedding space using two projection matrices \(W_v\) and \(W_l\).&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Given an image embedding \(V_i\), the corresponding multimodal embedding is computed as: \(W_vV_i\).&lt;/li&gt;
  &lt;li&gt;Given a text embedding \(L_i\), the corresponding multimodal embedding is computed as: \(W_lL_i\).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When people say CLIP embeddings, they either refer to these multimodal embeddings or the embeddings generated by CLIP’s image encoder.&lt;/p&gt;

&lt;h3 id=&quot;natural_language_supervision&quot;&gt;Natural language supervision&lt;/h3&gt;

&lt;p&gt;For many years, image models were trained with manually annotated (image, text) datasets (e.g. ImageNet, MS COCO). This isn’t scalable. Manual annotation is time-consuming and expensive.&lt;/p&gt;

&lt;p&gt;The CLIP paper noted that none of the then-available (image, text) datasets was big and high quality enough. They created their own dataset – 400M (image, text) pairs – as follows.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Construct a list of 500,000 queries. Queries are common words, bigrams, and titles of popular Wikipedia articles.&lt;/li&gt;
  &lt;li&gt;Find images matching these queries (string and substring match). The paper mentioned this search did NOT happen on search engines but didn’t specify where. My theory is that since OpenAI already scraped the entire Internet for their GPT models, they probably just queried their internal database.&lt;/li&gt;
  &lt;li&gt;Each image is paired with a text that co-occurs with it (e.g. captions, comments) instead of the query since queries are too short to be descriptive.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Because some queries are more popular than others, to avoid data imbalance, they used at most 20K images for a query.&lt;/p&gt;

&lt;h3 id=&quot;contrastive_learning&quot;&gt;Contrastive learning&lt;/h3&gt;
&lt;p&gt;Pre-CLIP, most vision-language models were trained using a classifier or language model objectives. Contrastive objective is a clever technique that allows CLIP to scale and generalize to multiple tasks.&lt;/p&gt;

&lt;p&gt;We’ll show why the constrastive objective works better for CLIP using an example task of image captioning: given an image, generate a text that describes it.&lt;/p&gt;

&lt;h4 id=&quot;classifier_objective&quot;&gt;Classifier objective&lt;/h4&gt;

&lt;p&gt;A classifier predicts the correct class among a predetermined list of classes. This works when the output space is finite. Previous models that work with (image, text) pair datasets all had this limitation. For example, models working with &lt;a href=&quot;https://www.image-net.org/challenges/LSVRC/2012/&quot;&gt;ILSVRC-2012&lt;/a&gt; limited themselves to 1,000 classes, and &lt;a href=&quot;https://arxiv.org/abs/1707.02968&quot;&gt;JFT-300M&lt;/a&gt; to 18,291 classes.&lt;/p&gt;

&lt;p&gt;This objective limits not only the model’s capacity to output meaningful responses but also its capacity for zero-shot learning. Say, if the model was trained to predict among 10 classes, it won’t work for a task that has 100 classes.&lt;/p&gt;

&lt;h4 id=&quot;lm_objective&quot;&gt;Language model objective&lt;/h4&gt;

&lt;p&gt;If a classifier outputs only one class for each input, a language model outputs a sequence of classes. Each generated class is called a token. Each token is from a predetermined list, the vocabulary, of the language model.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Classifier vs. language model objectives&quot; src=&quot;/assets/pics/multimodal/5-classifier-vs-language-model-objectives.png&quot; style=&quot;float: center; max-width: 90%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
    Classifier vs. language model objectives
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h4 id=&quot;contrastive_objective&quot;&gt;Contrastive objective&lt;/h4&gt;

&lt;p&gt;While the language model objective allows for vastly more flexible outputs, CLIP authors noted this objective made the training difficult. They hypothesized that this is because the model tries to generate &lt;em&gt;exactly&lt;/em&gt; the text accompanying each image, while many possible texts can accompany an image: alt-text, caption, comments, etc.&lt;/p&gt;

&lt;p&gt;For example, in the &lt;a href=&quot;https://arxiv.org/abs/1509.04942&quot;&gt;Flickr30K dataset&lt;/a&gt;, each image has 5 captions provided by human annotators, and the captions for the same image can be very different.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Multiple captions for the same image&quot; src=&quot;/assets/pics/multimodal/6-multiple-captions.png&quot; style=&quot;float: center; max-width: 85%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Contrastive learning is to overcome this challenge. Instead of predicting the exact text of each image, CLIP was trained to predict whether a text is more likely to accompany an image than other texts.&lt;/p&gt;

&lt;p&gt;For each batch of \(N\) (image, text) pairs, the model generates N text embeddings and N image embeddings.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Let \(V_1, V_2, ..., V_n\) be the embeddings for the \(N\) images.&lt;/li&gt;
  &lt;li&gt;Let \(L_1, L_2, ..., L_n\) be the embeddings for the \(N\) texts.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;CLIP computes the cosine similarity scores of the \(N^2\) possible (\(V_i, L_j\)) pairings. The model is trained to maximize the similarity scores of the \(N\) correct pairings while minimizing the scores of the \(N^2 - N\) incorrect pairings. For CLIP, \(N = 32,768\).&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;How CLIP works&quot; src=&quot;/assets/pics/multimodal/7-clip.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Another way to look at this is that each training batch of CLIP is two classification tasks.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;Each image can be paired with N possible texts, and the model tries to predict the correct one. This is the same setup as image-to-text retrieval.&lt;/p&gt;

\[L_{\text{contrastive:txt2im}} = -\frac{1}{N}\sum_i^N\log(\frac{\exp(L_i^TV_i\beta)}{\sum_j^N\exp(L_i^TV_j\beta)})\]
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Each text can be paired with N possible images, and the model tries to predict the correct image. This is the same setup as text-to-image retrieval.&lt;/p&gt;

\[L_{\text{contrastive:im2txt}} = -\frac{1}{N}\sum_i^N\log(\frac{\exp(V_i^TL_i\beta)}{\sum_j^N\exp(V_i^TL_j\beta)})\]
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The sum of these two losses is minimized. 𝛽 is a trainable inverse temperature parameter.&lt;/p&gt;

&lt;p&gt;This is what it all looks like in pseudocode.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;CLIP pseudocode&quot; src=&quot;/assets/pics/multimodal/8-clip-pseudocode.png&quot; style=&quot;float: center; max-width: 60%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;

&lt;p&gt;CLIP authors found that the contrastive objective provided a 12x improvement in efficiency compared to the language model objective baseline while producing higher-quality image embeddings.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;CLIP constrastive learning&quot; src=&quot;/assets/pics/multimodal/9-contrastive-learning-efficiency.png&quot; style=&quot;float: center; max-width: 60%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;

&lt;h3 id=&quot;clip_applications&quot;&gt;CLIP applications&lt;/h3&gt;

&lt;h4 id=&quot;classification-1&quot;&gt;Classification&lt;/h4&gt;

&lt;p&gt;Today, for many image classification tasks, CLIP is still a strong out-of-the-box baseline to be used as-is or fine-tuned.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;CLIP as a strong baseline for image classification&quot; src=&quot;/assets/pics/multimodal/10-clip-perf.png&quot; style=&quot;float: center; max-width: 50%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;

&lt;h4 id=&quot;text-based-image-retrieval&quot;&gt;Text-based image retrieval&lt;/h4&gt;

&lt;p&gt;Since CLIP’s training process was conceptually similar to image-to-text retrieval and text-to-image retrieval, CLIP “&lt;em&gt;displays significant promise for widely-applicable tasks like image retrieval or search&lt;/em&gt;.” However, “&lt;em&gt;on image retrieval, CLIP’s performance relative to the overall state of the art is noticeably lower.&lt;/em&gt;”&lt;/p&gt;

&lt;p&gt;There are attempts to use CLIP for image retrieval. For example, &lt;a href=&quot;https://github.com/rom1504/clip-retrieval&quot;&gt;clip-retrieval&lt;/a&gt; package works as follows:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Generate CLIP embeddings for all your images and store them in a vector database.&lt;/li&gt;
  &lt;li&gt;For each text query, generate a CLIP embedding for this text.&lt;/li&gt;
  &lt;li&gt;Query in the vector database for all images whose embeddings are close to this text query embedding.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4 id=&quot;image-generation&quot;&gt;Image generation&lt;/h4&gt;

&lt;p&gt;CLIP’s joint image-text embeddings are useful for image generation. Given a text prompt, &lt;a href=&quot;https://openai.com/research/dall-e&quot;&gt;DALL-E&lt;/a&gt; (2021) generates many different visuals and uses CLIP to rerank these visuals before showing the top visuals to users.&lt;/p&gt;

&lt;p&gt;In 2022, OpenAI introduced &lt;a href=&quot;https://openai.com/research/hierarchical-text-conditional-image-generation-with-clip-latents&quot;&gt;unCLIP&lt;/a&gt;, a text-to-image synthesis model conditioned on CLIP latents. It consists of two main components:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;CLIP is trained and frozen. The pretrained CLIP model can generate embeddings for both text and images in the same embedding space.&lt;/li&gt;
  &lt;li&gt;Two things happen at image generation:
    &lt;ul&gt;
      &lt;li&gt;Use CLIP to generate embedding for this text.&lt;/li&gt;
      &lt;li&gt;Use a diffusion decoder to generate images conditioned on this embedding.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;unCLIP&quot; src=&quot;/assets/pics/multimodal/11-unCLIP.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h4 id=&quot;text-generation-visual-question-answering-captioning&quot;&gt;Text generation: visual question answering, captioning&lt;/h4&gt;

&lt;p&gt;CLIP authors did attempt to create a model for text generation. One version they experimented with is called LM RN50. Though this model could generate text responses, its performance was consistently around 10% below CLIP’s best-performing model on all the vision-language understanding tasks that CLIP was evaluated on.&lt;/p&gt;

&lt;p&gt;While today CLIP isn’t used directly for text generation, its image encoder is often the backbone for LMMs that can generate texts.&lt;/p&gt;

&lt;h2 id=&quot;flamingo&quot;&gt;Flamingo: the dawns of LMMs&lt;/h2&gt;

&lt;p&gt;Unlike CLIP, Flamingo can generate text responses. In a reductive view, Flamingo is CLIP + a language model, with added techniques to make it possible for the language model to generate text tokens conditioned on both visual and text inputs.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Conversations with Flamingo LMMs&quot; src=&quot;/assets/pics/multimodal/12-flamingo-chatbots.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
    Flamingo can generate text responses conditioned on both text and images
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h3 id=&quot;flamingo_s_high_level_architecture&quot;&gt;Flamingo&apos;s high-level architecture&lt;/h3&gt;

&lt;p&gt;At a high level, Flamingo consists of 2 parts:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Vision encoder&lt;/strong&gt;: a CLIP-like model is trained using contrastive learning. The text encoder of this model is then discarded. The vision encoder is frozen to be used in the main model.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Language model&lt;/strong&gt;: Flamingo finetunes Chinchilla to generate text tokens, conditioned on visuals and text, using language model loss, with two additional components Perceiver Resampler and GATED XATTN-DENSE layers. We’ll discuss them later in this blog.&lt;/li&gt;
&lt;/ol&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Flamingo high level architecture&quot; src=&quot;/assets/pics/multimodal/13-flamingo-architecture.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h3 id=&quot;data&quot;&gt;Data&lt;/h3&gt;

&lt;p&gt;Flamingo used 4 datasets: 2 (image, text) pair datasets, 1 (video, text) pair dataset, and 1 interleaved image and text dataset.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Flamingo&apos;s 4 datasets&quot; src=&quot;/assets/pics/multimodal/14-flamingo-data.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;table&gt;
    &lt;style&gt;
        table {
          border-collapse: collapse;
          width: 100%;
        }

        th, td {
          padding: 8px;
          text-align: left;
          border-bottom: 1px solid #ddd;
        }
    &lt;/style&gt;
  &lt;tr&gt;
   &lt;td&gt;&lt;strong&gt;Dataset&lt;/strong&gt;
   &lt;/td&gt;
   &lt;td&gt;&lt;strong&gt;Type&lt;/strong&gt;
   &lt;/td&gt;
   &lt;td&gt;&lt;strong&gt;Size&lt;/strong&gt;
   &lt;/td&gt;
   &lt;td&gt;&lt;strong&gt;How&lt;/strong&gt;
   &lt;/td&gt;
   &lt;td&gt;&lt;strong&gt;Training weight&lt;/strong&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;M3W
   &lt;/td&gt;
   &lt;td&gt;Interleaved image and text dataset
   &lt;/td&gt;
   &lt;td&gt;43M webpages
   &lt;/td&gt;
   &lt;td&gt;For each webpage, they sample a random subsequence of 256 tokens and take up to the first 5 images included in the sampled sequence.
   &lt;/td&gt;
   &lt;td&gt;1.0
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;ALIGN
   &lt;/td&gt;
   &lt;td&gt;(Image, text) pairs
   &lt;/td&gt;
   &lt;td&gt;1.8B pairs
   &lt;/td&gt;
   &lt;td&gt;Texts are alt-texts, averaging 12 tokens/text.
   &lt;/td&gt;
   &lt;td&gt;0.2
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;LTIP
   &lt;/td&gt;
   &lt;td&gt;(Image, text) pairs
   &lt;/td&gt;
   &lt;td&gt;312M pairs
   &lt;/td&gt;
   &lt;td&gt;Texts are long descriptions, averaging 20.5 tokens/text.
   &lt;/td&gt;
   &lt;td&gt;0.2
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;VTP
   &lt;/td&gt;
   &lt;td&gt;(Video, text) pairs
   &lt;/td&gt;
   &lt;td&gt;27M short videos
   &lt;/td&gt;
   &lt;td&gt;~22 seconds/video on average
   &lt;/td&gt;
   &lt;td&gt;0.03
   &lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h3 id=&quot;flamingo_s_vision_encoder&quot;&gt;Flamingo&apos;s vision encoder&lt;/h3&gt;

&lt;p&gt;Flamingo first trains a CLIP-like model from scratch using contrastive learning. This component only uses the 2 (image, text) pair datasets, ALIGN and LTIP, totaling 2.1B (image, text) pairs. This is 5x larger than the dataset CLIP was trained on.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;For the text encoder, Flamingo uses BERT instead of GPT-2.&lt;/li&gt;
  &lt;li&gt;For the vision encoder, Flamingo uses a NormalizerFree ResNet (NFNet) F6 model.&lt;/li&gt;
  &lt;li&gt;Text and vision embeddings are meanpooled before being projected to the joint embedding space.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;flamingo_s_language_model&quot;&gt;Flamingo&apos;s language model&lt;/h3&gt;

&lt;p&gt;Flamingo uses Chinchilla as their language model. More specifically, they freeze the 9 pretrained Chinchilla LM layers. A traditional language model predicts the next text token based on the preceding text tokens. Flamingo predicts the next text token based on both the preceding text and visual tokens.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Flamingo&apos;s 4 datasets&quot; src=&quot;/assets/pics/multimodal/15-lmm-text-generation.png&quot; style=&quot;float: center; max-width: 53%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
    Next token generation is conditioned on both text and visual tokens. Illustration taken from Chunyuan Li&apos;s CVPR 2023 tutorial: Large Multimodal Models.
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;To be able to generate text conditioned on both text and visual inputs, Flamingo relied on Perceiver Resampler and GATED XATTN-DENSE layers.&lt;/p&gt;

&lt;h4 id=&quot;perceiver-resampler&quot;&gt;Perceiver Resampler&lt;/h4&gt;

&lt;p&gt;As the visual inputs can be both images and videos, the vision encoder can produce a variable number of image or video features. Perceiver Resampler converts these variable features into a consistent 64 visual outputs.&lt;/p&gt;

&lt;p&gt;Interestingly enough, while training the vision encoder, the resolution used was 288 x 288. However, at this phase, visual inputs are resized to 320 × 320. It’s been shown that &lt;a href=&quot;https://arxiv.org/abs/1906.06423&quot;&gt;a higher test-time resolution can lead to improved performance when using CNNs&lt;/a&gt;.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Flamingo&apos;s Perceiver Resampler&quot; src=&quot;/assets/pics/multimodal/16-flamingo-perceiver-resampler.png&quot; style=&quot;float: center; max-width: 90%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h4 id=&quot;gated-xattn-dense-layers&quot;&gt;GATED XATTN-DENSE layers&lt;/h4&gt;

&lt;p&gt;GATED XATTN-DENSE layers are inserted between existing and frozen LM layers to allow the language model to attend more efficiently to the visual tokens when generating text tokens. Without these layers, Flamingo authors noted a drop of 4.2% in the overall score.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Flamingo&apos;s GATED ATTN-DENSE layers&quot; src=&quot;/assets/pics/multimodal/17-gated xattn-dense.png&quot; style=&quot;float: center; max-width: 73%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h4 id=&quot;loss-function&quot;&gt;Loss function&lt;/h4&gt;
&lt;p&gt;Flamingo computes the likelihood of text \(y\) conditioned on the interleaved images and videos \(x\).&lt;/p&gt;

\[p(y|x) = \prod_{l=1}^N p(y_l|y_{&amp;lt;l}, x_{\leq l})\]

&lt;p&gt;The training loss function was a weighted sum of expected negative log-likelihoods of generated text across all 4 datasets, with \(\lambda_m\) being the training weight of dataset \(m\).&lt;/p&gt;

\[\sum_{m=1}^M \lambda_m E_{(x, y)\sim D_m} [ -\sum_{l=1}^L \log p(y|x)]\]

&lt;h4 id=&quot;training&quot;&gt;Training&lt;/h4&gt;

&lt;p&gt;While the Chinchilla LM layers are finetuned and frozen, the additional components are trained from scratch, using all 4 Flamingo datasets, with different weights. &lt;em&gt;Finding the right per-dataset weights was key to performance.&lt;/em&gt; The weight for each dataset is in the &lt;strong&gt;Training weight&lt;/strong&gt; column in the dataset table above.&lt;/p&gt;

&lt;p&gt;VTP’s weight is much smaller than other datasets (0.03 compared to 0.2 and 1), so its contribution to the training should be minimal. However, the authors noted that removing this dataset negatively affects performance on all video tasks.&lt;/p&gt;

&lt;p&gt;While Flamingo isn’t open-sourced, there are many open-source replications of Flamingo.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://huggingface.co/spaces/HuggingFaceM4/idefics_playground&quot;&gt;IDEFICS&lt;/a&gt; (HuggingFace)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/mlfoundations/open_flamingo/issues&quot;&gt;mlfoundations/open_flamingo&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;clip_vs_flamingo&quot;&gt;TL;DR: CLIP vs. Flamingo&lt;/h2&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Flamingo&apos;s 4 datasets&quot; src=&quot;/assets/pics/multimodal/18-clip-flamingo.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2 id=&quot;part_3_research_directions_for_lmms&quot;&gt;Part 3. Research Directions for LMMs&lt;/h2&gt;

&lt;p&gt;CLIP is 3 years old and Flamingo is almost 2. While their architectures serve as a good foundation for us to understand how LMMs are built, there have been many new progresses in the space.&lt;/p&gt;

&lt;p&gt;Here are a few directions that I’m excited about. This is far from an exhaustive list, both because this post has been long and because I’m still learning about the space too. If you have any pointers or suggestions, please let me know!&lt;/p&gt;

&lt;h2 id=&quot;incorporating_more_data_modalities&quot;&gt;Incorporating more data modalities&lt;/h2&gt;

&lt;p&gt;Today, most multimodal systems work with text and images. It’s only a matter of time before we need systems that can incorporate other modalities such as videos, music, and 3D. Wouldn’t it be amazing to have one shared embedding space for ALL data modalities?&lt;/p&gt;

&lt;p&gt;Examples of works in this space:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2212.05171&quot;&gt;ULIP: Learning a Unified Representation of Language, Images, and Point Clouds for 3D Understanding&lt;/a&gt; (Xue et al., Dec 2022)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://browse.arxiv.org/abs/2305.05665&quot;&gt;ImageBind: One Embedding Space To Bind Them All&lt;/a&gt; (Girdhar et al., May 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://next-gpt.github.io/&quot;&gt;NExT-GPT: Any-to-Any Multimodal Large Language Model&lt;/a&gt; (Wu et al., Sep 2023)&lt;/li&gt;
  &lt;li&gt;Jeff Dean’s ambitious &lt;a href=&quot;https://blog.google/technology/ai/introducing-pathways-next-generation-ai-architecture/&quot;&gt;Pathways&lt;/a&gt; project (2021): its vision is to “&lt;em&gt;enable multimodal models that encompass vision, auditory, and language understanding simultaneously&lt;/em&gt;.”&lt;/li&gt;
&lt;/ul&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Imagebind&quot; src=&quot;/assets/pics/multimodal/19-imagebind.png&quot; style=&quot;float: center; max-width:100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;

&lt;h2 id=&quot;multimodal_systems_for_instruction_following&quot;&gt;Multimodal systems for instruction-following&lt;/h2&gt;

&lt;p&gt;Flamingo was trained for completion, but not for dialogue or for following instructions. (If you’re not familiar with completion vs. dialogue, check out my post on &lt;a href=&quot;https://huyenchip.com/2023/05/02/rlhf.html&quot;&gt;RLHF&lt;/a&gt;). Many people are working on building LMMs that can follow instructions and have conversations, such as:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2212.10773&quot;&gt;MultiInstruct: Improving Multi-Modal Zero-Shot Learning via Instruction Tuning&lt;/a&gt; (Xu et al., Dec 2022)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2304.08485&quot;&gt;LLaVA: Visual Instruction Tuning&lt;/a&gt; (Liu et al., Apr 28, 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2305.06500&quot;&gt;InstructBLIP: Towards General-purpose Vision-Language Models with Instruction Tuning&lt;/a&gt; (Salesforce, May 11, 2023)&lt;/li&gt;
  &lt;li&gt;LaVIN: &lt;a href=&quot;https://arxiv.org/abs/2305.15023&quot;&gt;Cheap and Quick: Efficient Vision-Language Instruction Tuning for Large Language Models&lt;/a&gt; (Luo et al., May 24, 2023)&lt;/li&gt;
&lt;/ul&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;LaVIN&quot; src=&quot;/assets/pics/multimodal/20-LaVIN.png&quot; style=&quot;float: center; max-width: 53%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
    Examples of LaVIN&apos;s outputs compared to other LMMs, shown in LaVIN&apos;s paper
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2 id=&quot;adapters_for_more_efficient_multimodal_training&quot;&gt;Adapters for more efficient multimodal training&lt;/h2&gt;

&lt;p&gt;While Flamingo used 9 pretrained and frozen layers from Chinchilla, it had to pretrain its vision encoder, Perceiver Resampler, and GATED XATTN-DENSE layers from scratch. These train-from-scratch modules could be compute-intensive. Many works focus on more efficient ways to bootstrap multimodal systems using less training from scratch.&lt;/p&gt;

&lt;p&gt;Some works are quite promising. BLIP-2, for example, outperformed Flamingo-80B by 8.7% on zero-shot VQA-v2 with 54x fewer trainable parameters.&lt;/p&gt;

&lt;p&gt;Works in this space include:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2301.12597&quot;&gt;BLIP-2: Bootstrapping Language-Image Pre-training with Frozen Image Encoders and Large Language Models&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;[LAVIN] &lt;a href=&quot;https://arxiv.org/abs/2305.15023&quot;&gt;Cheap and Quick: Efficient Vision-Language Instruction Tuning for Large Language Models&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2304.15010&quot;&gt;LLaMA-Adapter V2: Parameter-Efficient Visual Instruction Model&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The two images below are from Chunyuan Li’s &lt;a href=&quot;https://datarelease.blob.core.windows.net/tutorial/vision_foundation_models_2023/slides/Chunyuan_cvpr2023_tutorial_lmm.pdf&quot;&gt;Large Multimodal Models&lt;/a&gt; tutorial at CVPR 2023, which is, btw, an excellent tutorial.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Adapters for LMMs&quot; src=&quot;/assets/pics/multimodal/21-adapters-1.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Adapters for LMMs&quot; src=&quot;/assets/pics/multimodal/22-adapters-2.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2 id=&quot;generating_multimodal_outputs&quot;&gt;Generating multimodal outputs&lt;/h2&gt;

&lt;p&gt;While models that can process multimodal inputs are becoming the norm, multimodal output is still lagging. Many use cases require multimodal outputs. For example, if we ask ChatGPT to explain RLHF, an effective explanation might require graphs, equations, and even simple animations.&lt;/p&gt;

&lt;p&gt;To generate multimodal outputs, a model would first need to generate a shared intermediate output. One key question is what the intermediate output would look like.&lt;/p&gt;

&lt;p&gt;One option for intermediate output is text, which will then be used to generate/synthesize other actions.&lt;/p&gt;

&lt;p&gt;For example, &lt;a href=&quot;https://arxiv.org/abs/2201.07520&quot;&gt;CM3&lt;/a&gt; (Aghajanyan et al., 2022) outputs HTML markup which can be compiled into webpages that contain not only text but also formattings, links, and images. GPT-4V generates Latex code, which can then be reconstructed as data tables.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;CM3&quot; src=&quot;/assets/pics/multimodal/23-cm3.png&quot; style=&quot;float: center; max-width: 80%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
    Sampled outputs from CM3
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;GPT-4V generating LaTeX&quot; src=&quot;/assets/pics/multimodal/24-gpt-4v-latex.png&quot; style=&quot;float: center; max-width: 80%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
    GPT-4V generates Latex code, which can then be reconstructed as a data table
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Another option for intermediate output would be multimodal tokens. This is the option that &lt;a href=&quot;https://www.linkedin.com/in/caiming-xiong-150a1417&quot;&gt;Caiming Xiong&lt;/a&gt;, whose team at Salesforce has done a lot of awesome work on multimodality, told me. Each token will have a tag to denote whether it’s a text token or an image token. Image tokens will then be input into an image model like Diffusion to generate images. Text tokens will then be input into a language model.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/2305.17216&quot;&gt;Generating Images with Multimodal Language Models&lt;/a&gt; (Koh et al., Jun 2023) is an awesome paper that shows how LMMs can generate and retrieve images together with generating texts. See below.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;LMMs generating text and images&quot; src=&quot;/assets/pics/multimodal/27-lmms-generating-images.png&quot; style=&quot;float: center; max-width: 80%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;!-- &lt;h2 id=&quot;bigger_evaluation_benchmarks_for_multimodal&quot;&gt;Bigger evaluation benchmarks for multimodal&lt;/h2&gt;

There have been a lot of complaints about the lack of standardized evaluation benchmarks. Compared to evaluation for standard LLMs, multimodal evaluation is even worse. 

[LLaVA](https://arxiv.org/abs/2304.08485) (Liu et al., 2023) was evaluated on 30 unseen images and 90 new language-image instructions. We need bigger evaluation benchmarks for multimodal!

&lt;center&gt;
    &lt;figure&gt;
    &lt;img
        alt=&quot;LlaVA evaluation&quot;
        src=&quot;/assets/pics/multimodal/25-eval.png&quot;
        style=&quot;float: center; max-width: 90%; margin: 0 0 0em 0em&quot;&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;br&gt;

I&apos;m only aware of one work in this space -- [LAMM: Language-Assisted Multi-Modal Instruction-Tuning Dataset, Framework, and Benchmark](https://arxiv.org/abs/2306.06687) (Yin et al., 2023). I&apos;d appreciate your pointers to similar works! --&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;It’s been a lot of fun going over so many multimodal papers as well as talking to people doing awesome work and trying to summarize the key patterns in one blog post. There’s so much about multimodality that I’m sure there are many things that I’ve missed, but I hope that this post provides the core patterns that will help you develop multimodal systems and apply them to your work.&lt;/p&gt;

&lt;p&gt;As you see in part 3 of this post, we’re still in the early days of multimodal systems (so early that a friend told me he’s not sure if the LMM abbreviation would catch on). Yes, in most of my conversations, there’s little doubt that multimodal systems in general, and LMMs in particular, will be even more impactful than large language models. However, keep in mind that LMMs do not make LLMs obsolete. As LMMs extend upon LLMs, the performance of an LMM relies on the performance of its base LLM. Many labs that work on multimodal systems work on LLMs in parallel.&lt;/p&gt;

&lt;h2 id=&quot;early-reviewers&quot;&gt;Early reviewers&lt;/h2&gt;

&lt;p&gt;I’d like to thank the amazing early reviewers who gave me plenty of pointers and suggestions to make this post better: &lt;a href=&quot;https://www.linkedin.com/in/hanchunglee/&quot;&gt;Han-chung Lee&lt;/a&gt;, &lt;a href=&quot;https://www.linkedin.com/in/samreiswig/&quot;&gt;Sam Reiswig&lt;/a&gt;, and &lt;a href=&quot;https://twitter.com/Luke_Metz&quot;&gt;Luke Metz&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;resources&quot;&gt;Resources&lt;/h2&gt;

&lt;h3 id=&quot;models&quot;&gt;Models&lt;/h3&gt;

&lt;p&gt;An incomplete list of multimodal systems by time to give you a sense of how fast the space is moving!&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/1504.00325&quot;&gt;Microsoft COCO Captions: Data Collection and Evaluation Server&lt;/a&gt; (Apr 2015)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/1505.00468&quot;&gt;VQA: Visual Question Answering&lt;/a&gt; (May 2015)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/1904.01766&quot;&gt;VideoBERT: A Joint Model for Video and Language Representation Learning&lt;/a&gt; (Google, Apr 3, 2019)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/1908.07490&quot;&gt;LXMERT: Learning Cross-Modality Encoder Representations from Transformers&lt;/a&gt; (UNC Chapel Hill, Aug 20, 2019)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2103.00020&quot;&gt;[CLIP] Learning Transferable Visual Models From Natural Language Supervision&lt;/a&gt; (OpenAI, 2021)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2102.02779&quot;&gt;Unifying Vision-and-Language Tasks via Text Generation&lt;/a&gt; (UNC Chapel Hill, May 2021)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2201.12086&quot;&gt;BLIP: Bootstrapping Language-Image Pre-training for Unified Vision-Language Understanding and Generation&lt;/a&gt; (Salesforce, Jan 28, 2022)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2204.14198&quot;&gt;Flamingo: a Visual Language Model for Few-Shot Learning&lt;/a&gt; (DeepMind, April 29, 2022)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2205.14100&quot;&gt;GIT: A Generative Image-to-text Transformer for Vision and Language&lt;/a&gt; (Microsoft, May 2, 2022)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2212.10773&quot;&gt;MultiInstruct: Improving Multi-Modal Zero-Shot Learning via Instruction Tuning&lt;/a&gt; (Xu et al., Dec 2022)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2301.12597&quot;&gt;BLIP-2: Bootstrapping Language-Image Pre-training with Frozen Image Encoders and Large Language Models&lt;/a&gt; (Salesforce, Jan 30, 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2302.05738&quot;&gt;Cross-Modal Fine-Tuning: Align then Refine&lt;/a&gt; (Shen et al., Feb 11, 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2302.14045&quot;&gt;KOSMOS-1: Language Is Not All You Need: Aligning Perception with Language Models&lt;/a&gt; (Microsoft, Feb 27, 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2303.03378&quot;&gt;PaLM-E: An Embodied Multimodal Language Model&lt;/a&gt; (Google, Mar 10, 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2303.16199&quot;&gt;LLaMA-Adapter: Efficient Fine-tuning of Language Models with Zero-init Attention&lt;/a&gt; (Zhang et al., Mar 28, 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2304.14178&quot;&gt;mPLUG-Owl: Modularization Empowers Large Language Models with Multimodality&lt;/a&gt; (Ye et al., Apr 2, 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2304.15010&quot;&gt;LLaMA-Adapter V2: Parameter-Efficient Visual Instruction Model&lt;/a&gt; (Gao et al., Apr 28, 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2304.08485&quot;&gt;LLaVA: Visual Instruction Tuning&lt;/a&gt; (Liu et al., Apr 28, 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2305.04160&quot;&gt;X-LLM: Bootstrapping Advanced Large Language Models by Treating Multi-Modalities as Foreign Languages&lt;/a&gt; (Chen et al., May 7, 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2305.06500&quot;&gt;InstructBLIP: Towards General-purpose Vision-Language Models with Instruction Tuning&lt;/a&gt; (Salesforce, May 11, 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2305.09617&quot;&gt;Towards Expert-Level Medical Question Answering with Large Language Models&lt;/a&gt; (Singhal et al., May 16, 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2305.15023&quot;&gt;Cheap and Quick: Efficient Vision-Language Instruction Tuning for Large Language Models&lt;/a&gt; (Luo et al., May 24, 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2306.15195&quot;&gt;Shikra: Unleashing Multimodal LLM’s Referential Dialogue Magic&lt;/a&gt; (SenseTime, Jun 3, 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2306.09093&quot;&gt;Macaw-LLM: Multi-Modal Language Modeling with Image, Audio, Video, and Text Integration&lt;/a&gt; (Tencent, Jun 15, 2023)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;other-resources&quot;&gt;Other resources&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;[CVPR2023 Tutorial Talk] &lt;a href=&quot;https://www.youtube.com/watch?v=mkI7EPD1vp8&quot;&gt;Large Multimodal Models: Towards Building and Surpassing Multimodal GPT-4&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;Slides: &lt;a href=&quot;https://datarelease.blob.core.windows.net/tutorial/vision_foundation_models_2023/slides/Chunyuan_cvpr2023_tutorial_lmm.pdf&quot;&gt;Large Multimodal Models&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;[CMU course] &lt;a href=&quot;https://cmu-multicomp-lab.github.io/mmml-course/fall2022/&quot;&gt;11-777 MMML&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;[Open source] &lt;a href=&quot;https://github.com/salesforce/LAVIS&quot;&gt;Salesforce’s LAVIS&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Tue, 10 Oct 2023 00:00:00 +0000</pubDate>
        <link>https://huyenchip.com//2023/10/10/multimodal.html</link>
        <guid isPermaLink="true">https://huyenchip.com/2023/10/10/multimodal.html</guid>
      </item>
    
      <item>
        <title>Open challenges in LLM research</title>
        <description>&lt;p&gt;[&lt;em&gt;&lt;a href=&quot;https://www.linkedin.com/posts/chiphuyen_llm-airesearch-generativeai-activity-7097619722363408385-s5Cp&quot;&gt;LinkedIn discussion&lt;/a&gt;, &lt;a href=&quot;https://twitter.com/chipro/status/1691858084824838427&quot;&gt;Twitter thread&lt;/a&gt;&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;Never before in my life had I seen so many smart people working on the same goal: making LLMs better. After talking to many people working in both industry and academia, I noticed the 10 major research directions that emerged. The first two directions, hallucinations and context learning, are probably the most talked about today. I’m the most excited about numbers 3 (multimodality), 5 (new architecture), and 6 (GPU alternatives).&lt;/p&gt;

&lt;h2 id=&quot;1_reduce_and_measure_hallucinations&quot;&gt;1. Reduce and measure hallucinations&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://huyenchip.com/2023/05/02/rlhf.html#rlhf_and_hallucination&quot;&gt;Hallucination&lt;/a&gt; is a heavily discussed topic already so I’ll be quick. Hallucination happens when an AI model makes stuff up. For many creative use cases, hallucination is a feature. However, for most other use cases, hallucination is a bug. I was at a panel on LLM with Dropbox, Langchain, Elastics, and Anthropic recently, and the #1 roadblock they see for companies to adopt LLMs in production is hallucination.&lt;/p&gt;

&lt;p&gt;Mitigating hallucination and developing metrics to measure hallucination is a blossoming research topic, and I’ve seen many startups focus on this problem. There are also ad-hoc tips to reduce hallucination, such as adding more context to the prompt, chain-of-thought, self-consistency, or asking your model to be concise in its response.&lt;/p&gt;

&lt;p&gt;To learn more about hallucination:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2202.03629&quot;&gt;Survey of Hallucination in Natural Language Generation&lt;/a&gt; (Ji et al., 2022)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2305.13534&quot;&gt;How Language Model Hallucinations Can Snowball&lt;/a&gt; (Zhang et al., 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2302.04023&quot;&gt;A Multitask, Multilingual, Multimodal Evaluation of ChatGPT on Reasoning, Hallucination, and Interactivity&lt;/a&gt; (Bang et al., 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2212.10400&quot;&gt;Contrastive Learning Reduces Hallucination in Conversations&lt;/a&gt; (Sun et al., 2022)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2203.11171&quot;&gt;Self-Consistency Improves Chain of Thought Reasoning in Language Models&lt;/a&gt; (Wang et al., 2022)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2303.08896&quot;&gt;SelfCheckGPT: Zero-Resource Black-Box Hallucination Detection for Generative Large Language Models&lt;/a&gt; (​​Manakul et al., 2023)&lt;/li&gt;
  &lt;li&gt;A simple example of fact-checking and hallucination by &lt;a href=&quot;https://github.com/NVIDIA/NeMo-Guardrails/blob/main/examples/grounding_rail/README.md#grounding-fact-checking-and-hallucination&quot;&gt;NVIDIA’s NeMo-Guardrails&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;2_context_learning&quot;&gt;2. Optimize context length and context construction&lt;/h2&gt;

&lt;p&gt;A vast majority of questions require context. For example, if we ask ChatGPT: “What’s the best Vietnamese restaurant?”, the context needed would be “where” because the best Vietnamese restaurant in Vietnam would be different from the best Vietnamese in the US.&lt;/p&gt;

&lt;p&gt;According to this cool paper &lt;a href=&quot;https://arxiv.org/pdf/2109.06157.pdf&quot;&gt;SituatedQA&lt;/a&gt; (Zhang &amp;amp; Choi, 2021), a significant proportion of information-seeking questions have context-dependent answers, e.g. roughly 16.5% of the &lt;a href=&quot;https://ai.google.com/research/NaturalQuestions&quot;&gt;Natural Questions NQ-Open dataset&lt;/a&gt;. Personally, I suspect that this percentage would be even higher for enterprise use cases. For example, say a company builds a chatbot for customer support, for this chatbot to answer any customer question about any product, the context needed might be that customer’s history or that product’s information.&lt;/p&gt;

&lt;p&gt;Because the model “learns” from the context provided to it, this process is also called context learning.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Context needed for a customer support query&quot; src=&quot;/assets/pics/llm-research/2-context.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Context length is especially important for RAG – &lt;a href=&quot;https://arxiv.org/abs/2005.11401&quot;&gt;Retrieval Augmented Generation&lt;/a&gt; (Lewis et al., 2020) – which has emerged to be the predominant pattern for LLM industry use cases. For those not yet swept away in the RAG rage, RAG works in two phases:&lt;/p&gt;

&lt;p&gt;Phase 1: chunking (also known as indexing)&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Gather all the documents you want your LLM to use&lt;/li&gt;
  &lt;li&gt;Divide these documents into chunks that can be fed into your LLM to generate embeddings and store these embeddings in a vector database.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Phase 2: querying&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;When user sends a query, like “&lt;em&gt;Does my insurance policy pay for this drug X&lt;/em&gt;”, your LLM converts this query into an embedding, let’s call it QUERY_EMBEDDING&lt;/li&gt;
  &lt;li&gt;Your vector database fetches the chunks whose embeddings are the most similar to QUERY_EMBEDDING&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Screenshot from &lt;a href=&quot;https://www.youtube.com/watch?v=njzB6fm0U8g&quot;&gt;Jerry Liu’s talk on LlamaIndex&lt;/a&gt; (2023)&lt;/p&gt;
&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Context needed for a customer support query&quot; src=&quot;/assets/pics/llm-research/2-rag.jpg&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;The longer the context length, the more chunks we can squeeze into the context. The more information the model has access to, the better its response will be, right?&lt;/p&gt;

&lt;p&gt;Not always. How much context a model can use and how efficiently that model will use it are two different questions. In parallel with the effort to increase model context length is the effort to make the context more efficient. Some people call it “prompt engineering” or “prompt construction”. For example, a paper that has made the rounds recently is about how models are much better at understanding information at the beginning and the end of the index rather than in the middle of it – &lt;a href=&quot;https://arxiv.org/abs/2307.03172&quot;&gt;Lost in the Middle: How Language Models Use Long Contexts&lt;/a&gt; (Liu et al., 2023).&lt;/p&gt;

&lt;h2 id=&quot;3_incorporate_other_data_modalities&quot;&gt;3. Incorporate other data modalities&lt;/h2&gt;

&lt;p&gt;Multimodality, IMO, is so powerful and yet so underrated. There are many reasons for multimodality.&lt;/p&gt;

&lt;p&gt;First, there are many use cases where multimodal data is required, especially in industries that deal with a mixture of data modalities such as healthcare, robotics, e-commerce, retail, gaming, entertainment, etc. Examples:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Oftentimes, medical predictions require both text (e.g. doctor’s notes, patients’ questionnaires) and images (e.g. CT, X-ray, MRI scans).&lt;/li&gt;
  &lt;li&gt;Product metadata often contains images, videos, descriptions, and even tabular data (e.g. production date, weight, color). You might want to automatically fill in missing product information based on users’ reviews or product photos. You might want to enable users to search for products using visual information, like shape or color.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Second, multimodality promises a big boost in model performance. Shouldn’t a model that can understand both text and images perform better than a model that can only understand text? Text-based models require so much text that there’s a realistic concern that &lt;a href=&quot;https://huyenchip.com/2023/05/02/rlhf.html#data_bottleneck_for_pretraining&quot;&gt;we’ll soon run out of Internet data to train text-based models&lt;/a&gt;. Once we run out of text, we’d need to leverage other data modalities.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Multimodal Flamingo&apos;s architecture&quot; src=&quot;/assets/pics/llm-research/3-flamingo.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
    Flamingo architecture (Alayrac et al., 2022)
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;One use case I’m especially excited about is that multimodality can enable visually impaired people to browse the Internet and navigate the real world.&lt;/p&gt;

&lt;p&gt;Cool multimodal work:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2103.00020&quot;&gt;[CLIP] Learning Transferable Visual Models From Natural Language Supervision&lt;/a&gt; (OpenAI, 2021)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2204.14198&quot;&gt;Flamingo: a Visual Language Model for Few-Shot Learning&lt;/a&gt; (DeepMind, 2022)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2301.12597&quot;&gt;BLIP-2: Bootstrapping Language-Image Pre-training with Frozen Image Encoders and Large Language Models&lt;/a&gt; (Salesforce, 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2302.14045&quot;&gt;KOSMOS-1: Language Is Not All You Need: Aligning Perception with Language Models&lt;/a&gt; (Microsoft, 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://ai.googleblog.com/2023/03/palm-e-embodied-multimodal-language.html&quot;&gt;PaLM-E: An embodied multimodal language model&lt;/a&gt; (Google, 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2304.08485&quot;&gt;LLaVA: Visual Instruction Tuning&lt;/a&gt; (Liu et al., 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://catalog.ngc.nvidia.com/orgs/nvidia/teams/playground/models/neva&quot;&gt;NeVA: NeMo Vision and Language Assistant&lt;/a&gt; (NVIDIA, 2023)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I’ve been working on a post on multimodality that hopefully I can share soon!&lt;/p&gt;

&lt;h2 id=&quot;4_make_llms_faster_and_cheaper&quot;&gt;4. Make LLMs faster and cheaper&lt;/h2&gt;

&lt;p&gt;When GPT-3.5 first came out in late November 2022, many people had concerns about latency and cost of using it in production. However, latency/cost analysis has changed rapidly since then. Within half a year, the community found a way to create a model that came pretty close to GPT-3.5 in terms of performance, yet required just under 2% of GPT-3.5’s memory footprint.&lt;/p&gt;

&lt;p&gt;My takeaway: if you create something good enough, people will figure out a way to make it fast and cheap.&lt;/p&gt;

&lt;table&gt;
&lt;style&gt;
        table {
          border-collapse: collapse;
          width: 100%;
        }

        th, td {
          padding: 8px;
          text-align: left;
          border-bottom: 1px solid #ddd;
        }
    &lt;/style&gt;
  &lt;tr&gt;
   &lt;td&gt;&lt;strong&gt;Date&lt;/strong&gt;
   &lt;/td&gt;
   &lt;td&gt;&lt;strong&gt;Model&lt;/strong&gt;
   &lt;/td&gt;
   &lt;td&gt;&lt;strong&gt;# params&lt;/strong&gt;
   &lt;/td&gt;
   &lt;td&gt;&lt;strong&gt;Quantization&lt;/strong&gt;
   &lt;/td&gt;
   &lt;td&gt;&lt;strong&gt;Memory to finetune&lt;/strong&gt;
   &lt;/td&gt;
   &lt;td&gt;&lt;strong&gt;Can be trained on&lt;/strong&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;Nov 2022
   &lt;/td&gt;
   &lt;td&gt;GPT-3.5
   &lt;/td&gt;
   &lt;td&gt;175B
   &lt;/td&gt;
   &lt;td&gt;16-bit
   &lt;/td&gt;
   &lt;td&gt;375GB
   &lt;/td&gt;
   &lt;td&gt;Many, many machines
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;Mar 2023
   &lt;/td&gt;
   &lt;td&gt;&lt;a href=&quot;https://crfm.stanford.edu/2023/03/13/alpaca.html&quot;&gt;Alpaca 7B&lt;/a&gt;
   &lt;/td&gt;
   &lt;td&gt;7B
   &lt;/td&gt;
   &lt;td&gt;16-bit
   &lt;/td&gt;
   &lt;td&gt;15GB
   &lt;/td&gt;
   &lt;td&gt;Gaming desktop
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;May 2023
   &lt;/td&gt;
   &lt;td&gt;&lt;a href=&quot;https://arxiv.org/abs/2305.14314&quot;&gt;Guanaco 7B&lt;/a&gt;
   &lt;/td&gt;
   &lt;td&gt;7B
   &lt;/td&gt;
   &lt;td&gt;4-bit
   &lt;/td&gt;
   &lt;td&gt;6GB
   &lt;/td&gt;
   &lt;td&gt;Any Macbook
   &lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Below is Guanaco 7B’s performance compared to ChatGPT GPT-3.5 and GPT-4, as reported in the Guanco paper. Caveat: in general, the performance comparison is far from perfect. LLM evaluation is very, very hard.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Guanaco 7B&apos;s performance compared to ChatGPT GPT-3.5 and GPT-4&quot; src=&quot;/assets/pics/llm-research/4-llm-optimization.png&quot; style=&quot;float: center; max-width: 45%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Four years ago, when I started working on the notes that would later become the section &lt;strong&gt;&lt;a href=&quot;https://learning.oreilly.com/library/view/designing-machine-learning/9781098107956/ch07.html#model_compression&quot;&gt;Model Compression&lt;/a&gt;&lt;/strong&gt; for the book &lt;a href=&quot;https://www.amazon.com/Designing-Machine-Learning-Systems-Production-Ready/dp/1098107969&quot;&gt;&lt;strong&gt;Designing Machine Learning Systems&lt;/strong&gt;&lt;/a&gt;, I wrote about four major techniques for model optimization/compression:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Quantization&lt;/strong&gt;: by far the most general model optimization method. Quantization reduces a model’s size by using fewer bits to represent its parameters, e.g. instead of using 32 bits to represent a float, use only 16 bits, or even 4 bits.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Knowledge distillation&lt;/strong&gt;: a method in which a small model (student) is trained to mimic a larger model or ensemble of models (teacher).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Low-rank factorization&lt;/strong&gt;: the key idea here is to replace high-dimensional tensors with lower-dimensional tensors to reduce the number of parameters. For example, you can decompose a 3x3 tensor into the product of a 3x1 and a 1x3 tensor, so that instead of having 9 parameters, you have only 6 parameters.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Pruning&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;All these four techniques are still relevant and popular today. Alpaca was trained using knowledge distillation. QLoRA used a combination of low-rank factorization and quantization.&lt;/p&gt;

&lt;h2 id=&quot;5_design_a_new_model_architecture&quot;&gt;5. Design a new model architecture&lt;/h2&gt;

&lt;p&gt;Since AlexNet in 2012, we’ve seen many architectures go in and out of fashion, including LSTM, seq2seq. Compared to those, Transformer is incredibly sticky. It’s been around since 2017. It’s a big question mark how much longer this architecture will be in vogue.&lt;/p&gt;

&lt;p&gt;Developing a new architecture to outperform Transformer isn’t easy. Transformer has been so heavily optimized over the last 6 years. This new architecture has to be performing at the scale that people care about today, on the hardware that people care about. Side note: &lt;a href=&quot;https://timdettmers.com/2018/10/17/tpus-vs-gpus-for-transformers-bert/&quot;&gt;Transformer was originally designed by Google to run fast on TPUs&lt;/a&gt;, and only later optimized on GPUs.&lt;/p&gt;

&lt;p&gt;There was a lot of excitement in 2021 around S4 from Chris Ré’s lab – see &lt;a href=&quot;https://arxiv.org/abs/2111.00396&quot;&gt;Efficiently Modeling Long Sequences with Structured State Spaces&lt;/a&gt; (Gu et al., 2021). I’m not quite sure what happened to it. Chris Ré’s lab is still very invested in developing new architecture, most recently with their architecture &lt;a href=&quot;https://together.ai/blog/monarch-mixer&quot;&gt;Monarch Mixer&lt;/a&gt; (Fu et al., 2023) in collaboration with the startup &lt;a href=&quot;https://together.ai/blog/monarch-mixer&quot;&gt;Together&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Their key idea is that for the existing Transformer architecture, the complexity of attention is quadratic in sequence length and the complexity of an MLP is quadratic in model dimension. An architecture with subquadratic complexity would be more efficient.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Monarch Mixer architecture&quot; src=&quot;/assets/pics/llm-research/5-monarch-mixer.png&quot; style=&quot;float: center; max-width: 90%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;I’m sure many other labs are working on this idea, though I’m not aware of any attempt that has been made public. If you know of any, please let me know!&lt;/p&gt;

&lt;h2 id=&quot;6_develop_gpu_alternatives&quot;&gt;6. Develop GPU alternatives&lt;/h2&gt;

&lt;p&gt;GPU has been the dominating hardware for deep learning ever since AlexNet in 2012. In fact, one commonly acknowledged reason for AlexNet’s popularity is that it was the first paper to successfully use GPUs to train neural networks. Before GPUs, if you wanted to train a model at AlexNet’s scale, you’d have to use thousands of CPUs, like the one &lt;a href=&quot;https://www.nytimes.com/2012/06/26/technology/in-a-big-network-of-computers-evidence-of-machine-learning.html&quot;&gt;Google released just a few months before AlexNet&lt;/a&gt;. Compared to thousands of CPUs, a couple of GPUs were a lot more accessible to Ph.D. students and researchers, setting off the deep learning research boom.&lt;/p&gt;

&lt;p&gt;In the last decade, many, many companies, both big corporations, and startups, have attempted to create new hardware for AI. The most notable attempts are Google’s &lt;a href=&quot;https://cloud.google.com/tpu/docs/intro-to-tpu&quot;&gt;TPUs&lt;/a&gt;, Graphcore’s &lt;a href=&quot;https://www.graphcore.ai/products/ipu&quot;&gt;IPUs&lt;/a&gt; (what’s happening with IPUs?), and &lt;a href=&quot;https://www.eetimes.com/cerebras-sells-100-million-ai-supercomputer-plans-8-more/&quot;&gt;Cerebras&lt;/a&gt;. SambaNova raised over &lt;a href=&quot;https://spectrum.ieee.org/sambanova-ceo-ai-interview&quot;&gt;a billion dollars to develop new AI chips&lt;/a&gt; but seems to have pivoted to being a generative AI platform.&lt;/p&gt;

&lt;p&gt;For a while, there has been a lot of anticipation around quantum computing, with key players being:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ibm.com/quantum&quot;&gt;IBM’s QPU&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Google’s Quantum computer reported &lt;a href=&quot;https://www.nature.com/articles/d41586-023-00536-w&quot;&gt;a major milestone in quantum error reduction&lt;/a&gt; earlier this year in Nature. Its quantum virtual machine is publicly accessible via &lt;a href=&quot;https://quantumai.google/quantum-virtual-machine&quot;&gt;Google Colab&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Research labs such as &lt;a href=&quot;https://cqe.mit.edu/&quot;&gt;MIT Center for Quantum Engineering&lt;/a&gt;, &lt;a href=&quot;https://www.mpq.mpg.de/en&quot;&gt;Max Planck Institute of Quantum Optics&lt;/a&gt;, &lt;a href=&quot;https://chicagoquantum.org/&quot;&gt;Chicago Quantum Exchange&lt;/a&gt;, &lt;a href=&quot;https://quantum-roadmap.ornl.gov/&quot;&gt;Oak Ridge National Laboratory&lt;/a&gt;, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Another direction that is also super exciting is photonic chips. This is the direciton I know the least about – so please correct me if I’m wrong. Existing chips today use electricity to move data, which consumes a lot of power and also incurs latency. Photonic chips use photons to move data, harnessing the speed of light for faster and more efficient compute. Various startups in this space have raised hundreds of millions of dollars, including &lt;a href=&quot;https://lightmatter.co/&quot;&gt;Lightmatter&lt;/a&gt; ($270M), &lt;a href=&quot;https://ayarlabs.com/&quot;&gt;Ayar Labs&lt;/a&gt; ($220M), &lt;a href=&quot;https://www.lightelligence.ai/&quot;&gt;Lightelligence&lt;/a&gt; ($200M+), and &lt;a href=&quot;https://www.luminous.com/&quot;&gt;Luminous Computing&lt;/a&gt; ($115M).&lt;/p&gt;

&lt;p&gt;Below is the timeline of advances of the three major methods in photonic matrix computation, from the paper &lt;a href=&quot;https://www.nature.com/articles/s41377-022-00717-8&quot;&gt;Photonic matrix multiplication lights up photonic accelerator and beyond&lt;/a&gt; (Zhou et al., Nature 2022). The three different methods are plane light conversion (PLC), Mach–Zehnder interferometer (MZI), and wavelength division multiplexing (WDM).&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Timeline of advances of the three major methods in photonic matrix multiplication&quot; src=&quot;/assets/pics/llm-research/6-photonic-matrix-multiplication.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2 id=&quot;7_make_agents_usable&quot;&gt;7. Make agents usable&lt;/h2&gt;

&lt;p&gt;Agents are LLMs that can take actions, like browsing the Internet, sending emails, making reservations, etc. Compared to other research directions in this post, this might be the youngest direction.&lt;/p&gt;

&lt;p&gt;Because of the novelty and the massive potential, there’s a feverish obsession with agents. &lt;a href=&quot;https://github.com/Significant-Gravitas/Auto-GPT&quot;&gt;Auto-GPT&lt;/a&gt; is now the 25th most popular GitHub repo ever by the number of stars. &lt;a href=&quot;https://github.com/AntonOsika/gpt-engineer&quot;&gt;GPT-Engineering&lt;/a&gt; is another popular repo.&lt;/p&gt;

&lt;p&gt;Despite the excitement, there is still doubt about whether LLMs are reliable and performant enough to be entrusted with the power to act.&lt;/p&gt;

&lt;p&gt;One use case that has emerged though is the use of agents for social studies, like the famous Stanford experiment that shows that a small society of generative agents produces emergent social behaviors: &lt;em&gt;for example, starting with only a single user-specified notion that one agent wants to throw a Valentine’s Day party, the agents autonomously spread invitations to the party over the next two days, make new acquaintances, ask each other out on dates to the party …&lt;/em&gt; (&lt;a href=&quot;https://arxiv.org/abs/2304.03442&quot;&gt;Generative Agents: Interactive Simulacra of Human Behavior&lt;/a&gt;, Park et al., 2023)&lt;/p&gt;

&lt;p&gt;The most notable startup in this area is perhaps Adept, founded by two Transformer co-authors (though &lt;a href=&quot;https://www.theinformation.com/briefings/two-co-founders-of-adept-an-openai-rival-suddenly-left-to-start-another-company&quot;&gt;both already left&lt;/a&gt;) and an ex-OpenAI VP, and has raised almost half a billion dollars to date. Last year, they had a demo showing their agent browsing the Internet and adding a new account to Salesforce. I’m looking forward to seeing their new demos 🙂&lt;/p&gt;

&lt;center&gt;
&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/a7CXIE_Gyy8&quot; title=&quot;YouTube video player&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share&quot; allowfullscreen=&quot;&quot;&gt;
&lt;/iframe&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2 id=&quot;8_improve_learning_from_human_preference&quot;&gt;8. Improve learning from human preference&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://huyenchip.com/2023/05/02/rlhf.html&quot;&gt;RLHF, Reinforcement Learning from Human Preference&lt;/a&gt;, is cool but kinda hacky. I wouldn’t be surprised if people figure out a better way to train LLMs. There are many open questions for RLHF, such as:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. How to mathematically represent human preference?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Currently, human preference is determined by comparison: human labeler determines if response A is better than response B. However, it doesn’t take into account how much better response A is than response B.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. What’s human preference?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Anthropic measured the quality of their model’s responses along the three axes: helpful, honest, and harmless. See &lt;a href=&quot;https://arxiv.org/abs/2212.08073&quot;&gt;Constitutional AI: Harmlessness from AI Feedback&lt;/a&gt; (Bai et al., 2022).&lt;/p&gt;

&lt;p&gt;DeepMind tries to generate responses that please the most people. See &lt;a href=&quot;https://www.deepmind.com/publications/fine-tuning-language-models-to-find-agreement-among-humans-with-diverse-preferences&quot;&gt;Fine-tuning language models to find agreement among humans with diverse preferences&lt;/a&gt;, (Bakker et al., 2022).&lt;/p&gt;

&lt;p&gt;Also, do we want AIs that can take a stand or a vanilla AI that shies away from any potentially controversial topic?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Whose preference is “human” preference, taking into account the differences in cultures, religions, political leanings, etc.?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are a lot of challenges in obtaining training data that can be sufficiently representative of all the potential users.&lt;/p&gt;

&lt;p&gt;For example, for OpenAI’s InstructGPT data, there was no labeler above 65 years old. Labelers are predominantly Filipino and Bangladeshi. See &lt;a href=&quot;https://arxiv.org/abs/2203.02155&quot;&gt;InstructGPT: Training language models to follow instructions with human feedback&lt;/a&gt; (Ouyang et al., 2022).&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Demographics of labelers for InstructGPT&quot; src=&quot;/assets/pics/llm-research/8-instructgpt-demographics.png&quot; style=&quot;float: center; max-width: 55%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Community-led efforts, while admirable in their intention, can lead to biased data. For example, for the OpenAssistant dataset, 201 out of 222 (90.5%) respondents identify as male. &lt;a href=&quot;https://twitter.com/jeremyphoward/status/1647763133665271808/photo/1&quot;&gt;Jeremy Howard has a great Twitter thread on this&lt;/a&gt;.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Self-reported demographics of contributors to OpenAssistant dataset&quot; src=&quot;/assets/pics/llm-research/8-openassistant-demographics.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2 id=&quot;9_improve_the_efficiency_of_the_chat_interface&quot;&gt;9. Improve the efficiency of the chat interface&lt;/h2&gt;

&lt;p&gt;Ever since ChatGPT, there have been multiple discussions on whether chat is a suitable interface for a wide range of tasks.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://austinhenley.com/blog/naturallanguageui.html&quot;&gt;Natural language is the lazy user interface&lt;/a&gt; (Austin Z. Henley, 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://wattenberger.com/thoughts/boo-chatbots&quot;&gt;Why Chatbots Are Not the Future&lt;/a&gt; (Amelia Wattenberger, 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2303.17710&quot;&gt;What Types of Questions Require Conversation to Answer? A Case Study of AskReddit Questions&lt;/a&gt; (Huang et al., 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://idratherbewriting.com/blog/ai-chat-interfaces-are-the-new-user-interface-for-docs&quot;&gt;AI chat interfaces could become the primary user interface to read documentation&lt;/a&gt; (Tom Johnson, 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://eugeneyan.com/writing/llm-ux/&quot;&gt;Interacting with LLMs with Minimal Chat&lt;/a&gt; (Eugene Yan, 2023)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, this is not a new discussion. In many countries, especially in Asia, chat has been used as the interface for super apps for about a decade. &lt;a href=&quot;http://dangrover.com/blog/2014/12/01/chinese-mobile-app-ui-trends.html&quot;&gt;Dan Grover had this discussion back in 2014&lt;/a&gt;.&lt;/p&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Chat has been used as the universal interface for superapps in China for over a decade&quot; src=&quot;/assets/pics/llm-research/9-superapp-chat-interface.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
    Chat as a universal interface for Chinese apps (Dan Grover, 2014)
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;The discussion again got tense in 2016, when many people thought apps were dead and chatbots would be the future.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://acroll.medium.com/on-chat-as-interface-92a68d2bf854&quot;&gt;On chat as interface&lt;/a&gt; (Alistair Croll, 2016)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.technologyreview.com/2016/04/25/8510/is-the-chatbot-trend-one-big-misunderstanding/&quot;&gt;Is the Chatbot Trend One Big Misunderstanding?&lt;/a&gt; (Will Knight, 2016)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://dangrover.com/blog/2016/04/20/bots-wont-replace-apps.html&quot;&gt;Bots won’t replace apps. Better apps will replace apps&lt;/a&gt; (Dan Grover, 2016)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Personally, I love the chat interface because of the following reasons:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Chat is an interface that everyone, even people without previous exposure to computers or the Internet, can learn to use quickly. When I volunteered at a low-income residential neighborhood (are we allowed to say slum?) in Kenya in the early 2010s, I was blown away by how comfortable everyone there was with doing banking on their phone, via texts. No one in that neighborhood had a computer.&lt;/li&gt;
  &lt;li&gt;Chat interface is accessible. You can use voice instead of text if your hands are busy.&lt;/li&gt;
  &lt;li&gt;Chat is also an incredibly robust interface – you can give it any request and it’ll give back a response, even if the response isn’t good.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;However, there are certain areas that I think the chat interface can be improved upon.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;Multiple messages per turn&lt;/p&gt;

    &lt;p&gt;Currently, we pretty much assume one message per turn. This is not how my friends and I text. Often, I need multiple messages to complete my thought, because I need to insert different data (e.g. images, locations, links), I forgot something in the previous messages, or I just don’t feel like putting everything into a massive paragraph.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Multimodal input&lt;/p&gt;

    &lt;p&gt;In the realm of multimodal applications, most energy is spent on building better models, and very little on building better interfaces. Take &lt;a href=&quot;https://catalog.ngc.nvidia.com/orgs/nvidia/teams/playground/models/neva&quot;&gt;Nvidia’s NeVA chatbot&lt;/a&gt;. I’m not a UX expert, but I suspect there might be room for UX improvement here.&lt;/p&gt;

    &lt;p&gt;P.S. Sorry the NeVA team for calling you out. Even with this interface, your work is super cool!&lt;/p&gt;

    &lt;center&gt;
     &lt;figure&gt;
     &lt;img alt=&quot;NVIDIA&apos;s NeVA interface&quot; src=&quot;/assets/pics/llm-research/9-neva.png&quot; style=&quot;float: center; max-width: 80%; margin: 0 0 0em 0em&quot; /&gt;
     &lt;/figure&gt;
 &lt;/center&gt;
    &lt;p&gt;&lt;br /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Incorporating generative AI into your workflows&lt;/p&gt;

    &lt;p&gt;Linus Lee covered this point well in his talk &lt;a href=&quot;https://www.youtube.com/watch?v=rd-J3hmycQs&quot;&gt;Generative AI interface beyond chats&lt;/a&gt;. For example, if you want to ask a question about a column of a chart you’re working on, you should be able just point to that column and ask a question.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Editing and deletion of messages&lt;/p&gt;

    &lt;p&gt;How would editing or deletion of a user input change the conversation flow with the chatbot?&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;10_build_llms_for_non_english_languages&quot;&gt;10. Build LLMs for non-English languages&lt;/h2&gt;

&lt;p&gt;We know that current English-first LLMs don’t work well for many other languages, both in terms of performance, latency, and speed. See:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2304.05613&quot;&gt;ChatGPT Beyond English: Towards a Comprehensive Evaluation of Large Language Models in Multilingual Learning&lt;/a&gt; (Lai et al., 2023)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://blog.yenniejun.com/p/all-languages-are-not-created-tokenized&quot;&gt;All languages are NOT created (tokenized) equal&lt;/a&gt; (Yennie Jun, 2023)&lt;/li&gt;
&lt;/ul&gt;

&lt;center&gt;
    &lt;figure&gt;
    &lt;img alt=&quot;Tokenization for non-English languages&quot; src=&quot;/assets/pics/llm-research/10-non-english-tokens.png&quot; style=&quot;float: center; max-width: 100%; margin: 0 0 0em 0em&quot; /&gt;
    &lt;/figure&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Here are some initiatives that I’m aware of. If you have pointers to others, I’d be happy to include them here.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://aya.for.ai/&quot;&gt;Aya&lt;/a&gt;: An Open Science Initiative to Accelerate Multilingual AI Progress&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://discord.gg/a2PCzB4AdE&quot;&gt;Symato&lt;/a&gt;: Vietnamese ChatGPT&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/22-hours/cabrita&quot;&gt;Cabrita&lt;/a&gt;: Finetuning InstructLLaMA with portuguese data&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/LC1332/Luotuo-Chinese-LLM&quot;&gt;Luotuo-Chinese-LLM&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ymcui/Chinese-LLaMA-Alpaca&quot;&gt;Chinese-LLaMA-Alpaca&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/Facico/Chinese-Vicuna&quot;&gt;Chinese-Vicuna&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Several early readers of this post told me they don’t think I should include this direction for two reasons.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;This is less of a research problem and more of a logistics problem. We already know how to do it. Someone just needs to put money and effort into it. This is not entirely true. Most languages are considered low-resource, e.g. they have far fewer high-quality data compared to English or Chinese, and might require different techniques to train a large language model. See:&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2006.07264&quot;&gt;Low-resource Languages: A Review of Past Work and Future Challenges&lt;/a&gt; (Magueresse et al., 2020)&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://aclanthology.org/P19-1310/&quot;&gt;JW300: A Wide-Coverage Parallel Corpus for Low-Resource Languages&lt;/a&gt; (Agić et al., 2019)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Those more pessimistic think that in the future, many languages will die out, and the Internet will consist of two universes in two languages: English and Mandarin. This school of thought isn’t new – anyone remembers Esperando?&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The impact of AI tools, e.g. machine translation and chatbots, on language learning is still unclear. Will they help people learn new languages faster, or will they eliminate the need of learning new languages altogether?&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Phew, that was a lot of papers to reference, and I have no doubt that I still missed a ton. If there’s something you think I missed, please let me know.&lt;/p&gt;

&lt;p&gt;For another perspective, check out this comprehsive paper &lt;a href=&quot;https://arxiv.org/abs/2307.10169&quot;&gt;Challenges and Applications of Large Language Models&lt;/a&gt; (Kaddour et al., 2023).&lt;/p&gt;

&lt;p&gt;Some of the problems mentioned above are harder than others. For example, I think that number 10, building LLMs for non-English languages, is more straightforward with enough time and resources.&lt;/p&gt;

&lt;p&gt;Number 1, reducing hallucination, will be much harder, since hallucination is just LLMs doing their probabilistic thing.&lt;/p&gt;

&lt;p&gt;Number 4, making LLMs faster and cheaper, will never be completely solved. There is already so much progress in this area, and there will be more, but we will never run out of room for improvement.&lt;/p&gt;

&lt;p&gt;Number 5 and number 6, new architectures and new hardware, are very challenging, but they are inevitable with time. Because of the symbiosis between architecture and hardware – new architecture will need to be optimized for common hardware, and hardware will need to support common architecture – they might be solved by the same company.&lt;/p&gt;

&lt;p&gt;Some of these problems won’t be solved using only technical knowledge. For example, number 8, improving learning from human preference, might be more of a policy problem than a technical problem. Number 9, improving the efficiency of the chat interface, is more of a UX problem. We need more people with non-technical backgrounds to work with us to solve these problems.&lt;/p&gt;

&lt;p&gt;What research direction are you most excited about? What are the most promising solutions you see for these problems? I’d love to hear from you.&lt;/p&gt;
</description>
        <pubDate>Wed, 16 Aug 2023 00:00:00 +0000</pubDate>
        <link>https://huyenchip.com//2023/08/16/llm-research-open-challenges.html</link>
        <guid isPermaLink="true">https://huyenchip.com/2023/08/16/llm-research-open-challenges.html</guid>
      </item>
    
      <item>
        <title>Generative AI Strategy</title>
        <description>&lt;p&gt;I had a lot of fun preparing the talk: &lt;em&gt;“Leadership needs us to do generative AI. What do we do?”&lt;/em&gt; for &lt;a href=&quot;https://fullyconnected.com/&quot;&gt;Fully Connected&lt;/a&gt;. The idea for the talk came from many conversations I’ve had recently with friends who need to figure out their generative AI strategy, but aren’t sure what exactly to do.&lt;/p&gt;

&lt;p&gt;This talk is a simple framework to explore what to do with generative AI. Many ideas are still being fleshed out. I hope to convert this into a proper post when I have more time. In the meantime, I’d love to hear from your experience through this process.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I couldn’t figure out how to make the slides centered on the page. You might want to &lt;a href=&quot;https://huyenchip.com/assets/genai.pdf&quot;&gt;download the slides&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;object data=&quot;/assets/genai.pdf&quot; width=&quot;950&quot; height=&quot;500&quot; type=&quot;application/pdf&quot;&gt;&lt;/object&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Thanks everyone who responded to &lt;a href=&quot;https://www.linkedin.com/feed/update/urn:li:activity:7072230577449439232/&quot;&gt;my post&lt;/a&gt; and shared your thoughts on what I should include in the talk. Thanks &lt;a href=&quot;https://www.linkedin.com/in/kylegallatin/&quot;&gt;Kyle Gallatin&lt;/a&gt;, &lt;a href=&quot;https://www.linkedin.com/in/goku/&quot;&gt;Goku Mohandas&lt;/a&gt;, &lt;a href=&quot;https://www.linkedin.com/in/hanchunglee/&quot;&gt;Han-chung Lee&lt;/a&gt;, and &lt;a href=&quot;https://www.linkedin.com/in/jamiedeguerre/&quot;&gt;Jamie de Guerre&lt;/a&gt; for thoughtful feedback on the talk.&lt;/p&gt;
</description>
        <pubDate>Wed, 07 Jun 2023 00:00:00 +0000</pubDate>
        <link>https://huyenchip.com//2023/06/07/generative-ai-strategy.html</link>
        <guid isPermaLink="true">https://huyenchip.com/2023/06/07/generative-ai-strategy.html</guid>
      </item>
    
  </channel>
</rss>