<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Amin Zamani on Medium]]></title>
        <description><![CDATA[Stories by Amin Zamani on Medium]]></description>
        <link>https://medium.com/@AminZamani?source=rss-fd252084a846------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*RLd4xlRWNU_4n0nsumJgcw.png</url>
            <title>Stories by Amin Zamani on Medium</title>
            <link>https://medium.com/@AminZamani?source=rss-fd252084a846------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 22:07:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@AminZamani/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The Pyramid of Knowledge — or How to Make Better Decisions]]></title>
            <link>https://medium.com/@AminZamani/someone-once-wrote-that-the-problem-with-life-is-that-it-only-happens-once-cf251a533bd4?source=rss-fd252084a846------2</link>
            <guid isPermaLink="false">https://medium.com/p/cf251a533bd4</guid>
            <dc:creator><![CDATA[Amin Zamani]]></dc:creator>
            <pubDate>Wed, 28 May 2025 16:38:55 GMT</pubDate>
            <atom:updated>2025-05-28T16:44:26.832Z</atom:updated>
            <content:encoded><![CDATA[<h3>The Pyramid of Knowledge — or How to Make Better Decisions</h3><p>Someone once wrote that the problem with life is that it only happens once. It’s like an improvised theater.</p><p>The sentence is about regret — the regret we feel after making decisions. We make a choice, only to find out later that there was a better solution. But we didn’t know it at the time. The challenge of life, for every person, is: <em>how can I find the optimal solution, so I regret less later?</em></p><p>In life, there are things we know, and things we know we don’t know. But there are also things we <em>don’t know that we don’t know</em>. There’s a widely accepted belief that to make better decisions, we need to expand what we <em>know</em>. But I think what really matters more is expanding the set of things we <em>know we don’t know</em>.</p><p>Here’s the pyramid of knowledge (drawn with ChatGPT’s help):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vEztLzcF-X7d3P4DSIOQEQ.jpeg" /><figcaption>AI Generated Image about Pyramid of Knowledge</figcaption></figure><p>The things we <em>know</em> make up a small part of all the knowledge available to us. The second layer might be a little bigger, and the third — the things we don’t even know <em>exist</em> — is by far the largest. Every layer of the pyramid feeds from the one beneath it. The things we know today were once things we knew we didn’t know. And before that, we didn’t even know they existed.</p><p>For simplicity (and because writing and reading this paragraph wasn’t exactly easy), let’s abbreviate the three levels as follows:</p><ul><li>Level 1: K (knowns)</li><li>Level 2: KK (known unknowns)</li><li>Level 3: UK (unknown unknowns)</li></ul><p><strong>The problem:</strong> My hypothetical roommate and I are facing a critical challenge: <em>Who should wash the dishes today?</em></p><h3>Level 1 — Things We Know (K)</h3><p>These are the things we’re good at. Our areas of expertise. The stuff we’ve practiced. I, for example, know a few things about software engineering. I know certain programming languages and use them regularly. I can ride a bike without thinking. But this knowledge isn’t permanent. It requires maintenance.</p><p>If I step away from the tech world for a while, maybe I can still write a simple program five years later — but I’ll definitely have lost a good portion of my knowledge. If I don’t use English for a while, as a non-native speaker, I’ll likely forget many words.</p><p>This first level of the pyramid requires energy to maintain. But it also sits at the top of our mental stack — it’s the fastest to access. It’s tiny. And humans seem to have a limited capacity to expand it. Adding new knowledge and keeping the old stuff alive is hard work.</p><p>So, my roommate and I agree that everyone should wash their own dishes — that way, we both suffer equally.</p><h3>Level 2 — Things We Know We Don’t Know (KK)</h3><p>There are technologies I’ve heard about, and I know a little about them, but I’ve never worked with them. I know I don’t <em>know</em> them. For instance, I know a few things about graph databases. I’ve never used them in a real-world project, but I have a sense of what they are and what they’re good for. Or I know a bit about game theory — I know it’s useful for analyzing human, corporate, and political behavior.</p><p>This kind of knowledge is cool. It’s resilient over time, and it doesn’t need much upkeep. I can choose to dive deeper and move something from this level up to K, or just leave it here until I need it.</p><p>Someone once said, “When you have a hammer, everything looks like a nail.” The things in K are our hammers.</p><p>As level 2 expands, so does our toolbox. We know what we don’t know — and we have a rough sense of what those unknowns are. Now I might think: <em>Maybe I should model the dishwashing problem using game theory?</em> Or: <em>Maybe a graph database is a better choice for this social-media-like project I’m working on?</em></p><p>Level 2 helps us pick <em>how</em> to solve a problem. The actual solving? That happens with Level 1 knowledge.</p><p>So I decide to study game theory. I pull it up from KK to K and solve my dishwashing dilemma.</p><p>The major difference between experienced and inexperienced people lies in the size of their level 2 knowledge. People might be equally skilled (K), but a more experienced person has a much broader KK — and that makes them better at solving problems and giving advice.</p><p>In software engineering, this is the core difference between a mid-level and a senior engineer. The senior has access to a wider set of tools, built up through years of exposure. The path from junior to senior is about growing that second level.</p><h3>Level 3 — Things We Don’t Know We Don’t Know (UK)</h3><p>This is where all the knowledge of the world lives. The world is full of things we don’t even know <em>could</em> exist — and knowing about them could unlock new ways of solving our problems.</p><p>Most of the challenges people face — in life, relationships, work, even gaming — have probably been encountered and solved by someone else over the last 10,000 years. But we don’t know those solutions exist. And if we <em>did</em>, we’d probably handle life better.</p><p>Maybe if my roommate and I had known that dishwashers were a thing, we’d both be much happier now.</p><h3>So, how do we bring things up from UK to KK?</h3><p>Searching won’t help. You don’t know what to search for. You have to expose yourself — put yourself <em>in the path</em> of unknown knowledge.</p><p>Listen to podcasts. Go to events where you don’t know what the title even means. Talk to the people you meet there. Read blogs by people who seem to solve problems better than you. Subscribe to newsletters.</p><p>We’ll never know everything. But we <em>can</em> grow our mental toolbox.</p><p>If we know what we don’t know, we increase our chances of making better decisions.</p><p>And if we put ourselves in front of what we <em>don’t know we don’t know</em>, that’s when real discovery happens.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cf251a533bd4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Build a Custom Conversational Agent with LangChain?]]></title>
            <link>https://medium.com/@AminZamani/how-to-build-a-custom-conversational-agent-with-langchain-fbe6ad99998f?source=rss-fd252084a846------2</link>
            <guid isPermaLink="false">https://medium.com/p/fbe6ad99998f</guid>
            <category><![CDATA[langchain-agents]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[large-language-models]]></category>
            <dc:creator><![CDATA[Amin Zamani]]></dc:creator>
            <pubDate>Wed, 26 Jul 2023 09:46:13 GMT</pubDate>
            <atom:updated>2023-07-26T09:46:59.325Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FIwVC-xbX5yWVrUF.png" /></figure><p>If you are building a product on top of LLMs, you may have heard of LangChain. LangChain is an open-source, opinionated framework for working with a variety of large language models.</p><blockquote><em>LangChain is a framework for developing applications powered by language models. It enables applications that are:</em></blockquote><blockquote><strong><em>Data-aware</em></strong><em>: connect a language model to other sources of data</em></blockquote><blockquote><strong><em>Agentic</em></strong><em>: allow a language model to interact with its environment</em></blockquote><blockquote><em>The main value propositions of LangChain are:</em></blockquote><blockquote><strong><em>Components</em></strong><em>: abstractions for working with language models, along with a collection of implementations for each abstraction. Components are modular and easy-to-use, whether you are using the rest of the LangChain framework or not.</em></blockquote><blockquote><strong><em>Off-the-shelf chains</em></strong><em>: a structured assembly of components for accomplishing specific higher-level tasks.</em></blockquote><blockquote><em>Off-the-shelf chains make it easy to get started. For more complex applications and nuanced use cases, components make it easy to customize existing chains or build new ones.</em></blockquote><p>You can find more details about how LangChain works in their documentation, which is excellent and covers most of the essential parts of the framework.</p><p><a href="https://blog.langchain.dev/custom-agents">https://blog.langchain.dev/custom-agents</a></p><p>In this post, I will explain how to build a custom conversational agent in LangChain. The documentation pyonly talks about custom LLM agents that use the React framework and tools to answer, and the default LangChain conversational agent may not be suitable for all use cases. So, what if your agent needs to talk to the user in a specific way and perform some of its tasks with tools?</p><p>A LangChain agent has three parts:</p><ol><li>PromptTemplate: the prompt that tells the LLM how it should behave.</li><li>OutputParser: this parses the output of the LLM and decides if any tools should be called or not.</li><li>AgentClass: a Python class that inherits from the LangChain Agent class to inform LangChain that our class is an agent.</li></ol><p>First, let’s create the prompt template.</p><pre>PREFIX = &quot;&quot;&quot;You are an AI agent that does its best to assist the user. You may need use the following tools:<br>Your Tools:<br>{tools}<br><br>----<br>Continuously review and analyze your actions to ensure you are performing to the best of your abilities.<br>Constructively self-criticize your big-picture behavior constantly.<br>Reflect on past decisions and strategies to refine your approach.<br><br>You should only respond in JSON format as described below:<br>Response Format:<br>{format_instructions}<br>Ensure the response can be parsed by Python json.loads<br>&quot;&quot;&quot;<br><br>FORMAT_INSTRUCTIONS = &quot;&quot;&quot;{<br>    &quot;thoughts&quot;: {<br>                &quot;text&quot;: &quot;thought&quot;,<br>                &quot;speak&quot;: &quot;thoughts summary to say to user&quot;,<br>            },<br>            &quot;tool&quot;: {&quot;name&quot;: &quot;tool name&quot;, &quot;input&quot;: &quot;value&quot;},<br>        }<br>&quot;&quot;&quot;<br>SUFFIX = &quot;&quot;&quot;<br>USER&#39;S INPUT<br>--------------------<br>Talks friendly, short. NOTE to the Response Format is mandatory.<br>USER INPUT: {input}<br>&quot;&quot;&quot;</pre><p>Variables:</p><ol><li>tools: This section lists the tools that the agent has accessed and can use. Each tool has a name and description, and the agent classifies them based on these attributes to decide which tool to call based on user input.</li><li>format_instructions: This variable tells the LLM how to respond. The LLM&#39;s responses should follow a format that we can parse.</li><li>input: This variable contains general input from the user.</li></ol><p>We now need to implement the output parser.</p><blockquote><em>The output parser is responsible for parsing the LLM output into </em><em>AgentAction and </em><em>AgentFinish. This usually depends heavily on the prompt used.<br>This is where you can change the parsing to do retries, handle whitespace, etc</em></blockquote><pre>from langchain.agents.agent import Agent, AgentOutputParser<br>from typing import Union<br>from langchain.agents import AgentOutputParser<br>from langchain.schema import AgentAction, AgentFinish<br>import re<br>import json<br>def preprocess_json_input(input_str: str) -&gt; str:<br>    &quot;&quot;&quot;Preprocesses a string to be parsed as json.<br><br>    Replace single backslashes with double backslashes,<br>    while leaving already escaped ones intact.<br><br>    Args:<br>        input_str: String to be preprocessed<br><br>    Returns:<br>        Preprocessed string<br>    &quot;&quot;&quot;<br>    corrected_str = re.sub(<br>        r&#39;(?&lt;!\\\\)\\\\(?![&quot;\\\\/bfnrt]|u[0-9a-fA-F]{4})&#39;, r&quot;\\\\\\\\&quot;, input_str<br>    )<br>    return corrected_str<br><br>class CustomOutputParser(AgentOutputParser):<br>    def parse(self, llm_output: str) -&gt; Union[AgentAction, AgentFinish]:<br>        try:<br>            parsed = json.loads(llm_output, strict=False)<br>        except json.JSONDecodeError:<br>            preprocessed_text = preprocess_json_input(llm_output)<br>            try:<br>                parsed = json.loads(preprocessed_text, strict=False)<br>            except Exception:<br>                raise ValueError(f&quot;Could not parse LLM output: `{llm_output}`&quot;)<br>        if &quot;tool&quot; in parsed and &quot;name&quot; in parsed[&quot;tool&quot;]:<br>            return AgentAction(tool=parsed[&quot;tool&quot;][&quot;name&quot;], tool_input=parsed[&quot;tool&quot;][&quot;input&quot;], log=llm_output)<br>        return AgentFinish(<br>            return_values={&quot;output&quot;: llm_output},<br>            log=llm_output,<br>        )</pre><p>Okay, we have the prompt and parser, and now we need to build the final part.</p><pre>from __future__ import annotations<br>from typing import Any, List, Optional, Sequence, Tuple<br>from pydantic import Field<br>from langchain.agents.utils import validate_tools_single_input<br>from langchain.base_language import BaseLanguageModel<br>from langchain.callbacks.base import BaseCallbackManager<br>from langchain.chains import LLMChain<br>from langchain.prompts.base import BasePromptTemplate<br>from langchain.prompts.chat import (<br>    ChatPromptTemplate,<br>    HumanMessagePromptTemplate,<br>    MessagesPlaceholder,<br>)<br>from langchain.schema import (<br>    AgentAction,<br>    AIMessage,<br>    BaseMessage,<br>    BaseOutputParser,<br>    HumanMessage,<br>    SystemMessage,<br>)<br>from langchain.tools.base import BaseTool<br>from langchain.callbacks.base import BaseCallbackManager<br><br>class CustomChatAgent(Agent):<br>    output_parser: AgentOutputParser = Field(<br>        default_factory=CustomOutputParser)<br><br>    @classmethod<br>    def _get_default_output_parser(cls, **kwargs: Any) -&gt; AgentOutputParser:<br>        return CustomOutputParser()<br><br>    @property<br>    def _agent_type(self) -&gt; str:<br>        raise NotImplementedError<br><br>    @property<br>    def observation_prefix(self) -&gt; str:<br>        &quot;&quot;&quot;Prefix to append the observation with.&quot;&quot;&quot;<br>        return &quot;Observe: &quot;<br><br>    @property<br>    def llm_prefix(self) -&gt; str:<br>        &quot;&quot;&quot;Prefix to append the llm call with.&quot;&quot;&quot;<br>        return &quot;&quot;<br><br>    @classmethod<br>    def _validate_tools(cls, tools: Sequence[BaseTool]) -&gt; None:<br>        super()._validate_tools(tools)<br>        validate_tools_single_input(cls.__name__, tools)<br><br>    @classmethod<br>    def create_prompt(<br>        cls,<br>        tools: Sequence[BaseTool],<br>        system_message: str = PREFIX,<br>        human_message: str = SUFFIX,<br>        formats: str = FORMAT_INSTRUCTIONS,<br>        input_variables: Optional[List[str]] = None,<br>        output_parser: Optional[BaseOutputParser] = None,<br>    ) -&gt; BasePromptTemplate:<br>        tool_strings = &quot;\\n&quot;.join(<br>            [f&quot;&gt; {tool.name}: {tool.description}&quot; for tool in tools]<br>        )<br>        _output_parser = output_parser or cls._get_default_output_parser()<br>        system_message = system_message.format(<br>            format_instructions=formats,<br>            tools=tool_strings,<br>        )<br>        if input_variables is None:<br>            input_variables = [&quot;input&quot;, &quot;chat_history&quot;, &quot;agent_scratchpad&quot;]<br>        messages = [<br>            SystemMessage(content=system_message),<br>            MessagesPlaceholder(variable_name=&quot;chat_history&quot;),<br>            HumanMessagePromptTemplate.from_template(human_message),<br>            MessagesPlaceholder(variable_name=&quot;agent_scratchpad&quot;)<br>        ]<br>        return ChatPromptTemplate(input_variables=input_variables, messages=messages)<br><br>    def _construct_scratchpad(<br>        self, intermediate_steps: List[Tuple[AgentAction, str]]<br>    ) -&gt; List[BaseMessage]:<br>        &quot;&quot;&quot;Construct the scratchpad that lets the agent continue its thought process.&quot;&quot;&quot;<br>        thoughts: List[BaseMessage] = []<br>        for action, observation in intermediate_steps:<br>            thoughts.append(AIMessage(content=action.log))<br>            human_message = HumanMessage(<br>                content=f&quot;Observe: {observation}&quot;<br>            )<br>            thoughts.append(human_message)<br>        return thoughts<br><br>    @classmethod<br>    def from_llm_and_tools(<br>        cls,<br>        llm: BaseLanguageModel,<br>        tools: Sequence[BaseTool],<br>        callback_manager: Optional[BaseCallbackManager] = None,<br>        output_parser: Optional[AgentOutputParser] = None,<br>        system_message: str = PREFIX,<br>        human_message: str = SUFFIX,<br>        formats: str = FORMAT_INSTRUCTIONS,<br>        input_variables: Optional[List[str]] = None,<br>        **kwargs: Any,<br>    ) -&gt; Agent:<br>        &quot;&quot;&quot;Construct an agent from an LLM and tools.&quot;&quot;&quot;<br>        cls._validate_tools(tools)<br>        _output_parser = output_parser or cls._get_default_output_parser()<br>        prompt = cls.create_prompt(<br>            tools,<br>            system_message=system_message,<br>            human_message=human_message,<br>            formats=formats,<br>            input_variables=input_variables,<br>            output_parser=_output_parser,<br>        )<br>        llm_chain = LLMChain(<br>            llm=llm,<br>            prompt=prompt,<br>            callback_manager=callback_manager,<br>        )<br>        tool_names = [tool.name for tool in tools]<br>        return cls(<br>            llm_chain=llm_chain,<br>            allowed_tools=tool_names,<br>            output_parser=_output_parser,<br>            **kwargs,<br>        )</pre><p>So, let’s put all the parts together:</p><pre>from langchain.agents import Tool, AgentExecutor<br>from langchain.agents import load_tools<br>from langchain.chat_models import ChatOpenAI<br>from langchain.llms import OpenAI</pre><pre>math_llm = OpenAI(temperature=0.0)<br>tools = load_tools(<br>    [&quot;human&quot;, &quot;llm-math&quot;],<br>    llm=math_llm,<br>)</pre><p>We need memory for our agent to remember the conversation.</p><pre>from langchain.memory import ConversationBufferMemory<br>memory = ConversationBufferMemory(memory_key=&quot;chat_history&quot;)</pre><pre>llm = ChatOpenAI(openai_api_key=OPENAI_API_KEY, temperature=0)</pre><pre>agent = CustomChatAgent.from_llm_and_tools(<br>            llm=llm, tools=tools)</pre><pre>chain = AgentExecutor.from_agent_and_tools(<br>            agent=agent, tools=tools, verbose=True, memory=memory, stop=[&quot;Observe:&quot;])</pre><pre>chain.run(&quot;hello&quot;)</pre><pre>&gt; Entering new AgentExecutor chain...<br>{<br>    &quot;thoughts&quot;: {<br>        &quot;text&quot;: &quot;I should greet the user back.&quot;,<br>        &quot;speak&quot;: &quot;Hello! How can I assist you today?&quot;<br>    }<br>}<br><br>&gt; Finished chain.</pre><pre>chain.run(&quot;What&#39;s my friend Eric&#39;s surname?&quot;)</pre><pre>&gt; Entering new AgentExecutor chain...<br>{<br>    &quot;thoughts&quot;: {<br>        &quot;text&quot;: &quot;The user is asking for their friend&#39;s surname. I need to find out Eric&#39;s surname.&quot;,<br>        &quot;speak&quot;: &quot;Let me find out Eric&#39;s surname for you.&quot;<br>    },<br>    &quot;tool&quot;: {<br>        &quot;name&quot;: &quot;human&quot;,<br>        &quot;input&quot;: &quot;What is Eric&#39;s surname?&quot;<br>    }<br>}<br><br>What is Eric&#39;s surname?<br><br>Observe: Zhu<br>{<br>    &quot;thoughts&quot;: {<br>        &quot;text&quot;: &quot;The user provided the surname &#39;Zhu&#39; for their friend Eric.&quot;,<br>        &quot;speak&quot;: &quot;Eric&#39;s surname is Zhu.&quot;<br>    }<br>}<br><br>&gt; Finished chain.</pre><p>Now, our agent can determine when to speak to the user and when to perform an action using tools.</p><p>This prompt and structure were inspired by the LangChain source code.</p><p><a href="https://github.com/hwchase17/langchain">https://github.com/hwchase17/langchain</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fbe6ad99998f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>