<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Daniele</title>
    <description>The latest articles on DEV Community by Daniele (@iamdaniele).</description>
    <link>https://dev.to/iamdaniele</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1241957%2Ffe1be71e-af02-42fb-88c5-6e164fa433af.jpg</url>
      <title>DEV Community: Daniele</title>
      <link>https://dev.to/iamdaniele</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/iamdaniele"/>
    <language>en</language>
    <item>
      <title>Advanced prompting techniques: Code Interpreter</title>
      <dc:creator>Daniele</dc:creator>
      <pubDate>Wed, 03 Jan 2024 08:00:00 +0000</pubDate>
      <link>https://dev.to/iamdaniele/advanced-prompting-techniques-code-interpreter-388h</link>
      <guid>https://dev.to/iamdaniele/advanced-prompting-techniques-code-interpreter-388h</guid>
      <description>&lt;p&gt;My company operates in an industry where turnarounds are pretty quick. Sometimes, listings can be gone in a matter of minutes! It's in our best interest to optimize our listing process so that items can be listed on the marketplace very quickly.&lt;/p&gt;

&lt;p&gt;In theory, this should be straightforward – just offer an intuitive flow to sellers, and voila! Well… That doesn't work for us, and here's why:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We work with wholesale quantities, not single items. This means listings can contain tens of thousands of different SKUs. The time it takes to upload a listing increases linearly with the number of items in the listing.&lt;/li&gt;
&lt;li&gt;The industry in which we operate does not have specific standards or well established best practices. No two catalogs we receive are the same. Sometimes the same seller can give us two catalogs in two different formats.&lt;/li&gt;
&lt;li&gt;Most of our sellers do not have the means or the time to convert their format to ours. This is an opportunity for us – our white glove approach is one of reasons sellers like to work with us.&lt;/li&gt;
&lt;li&gt;Even if we had the perfect catalog in the perfect format, our taxonomy needs to align with the seller. Sometimes we receive items in the "Shoes" category, which we'll need to translate to "Footwear", which is our equivalent in our taxonomy. And we'll have to repeat this across hundreds of categories and tens of thousands of lines.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So far, our team has resorted to a combination of traditional prompting techniques and process automation to speed up our listing process. The results were promising but we didn't significantly increase our speed. Besides, these initial attempt was heavy on the code, which didn't make our solution suitable for people with limited coding skills.&lt;/p&gt;

&lt;p&gt;Thankfully, OpenAI introduced &lt;strong&gt;Code Interpreter&lt;/strong&gt; earlier in 2023. In short, Code Interpreter allows you to define assistants that can write their own code, and execute it in a sandboxed, firewalled environment. It works beautifully in our context:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It removes the need to install Python environment and to deal with the command line.&lt;/li&gt;
&lt;li&gt;There is little coding involved, so most of the iteration is at the prompt level.&lt;/li&gt;
&lt;li&gt;We can still pass inputs (such as our taxonomy) into the prompt, keeping our prompt up to date.&lt;/li&gt;
&lt;li&gt;With a detailed prompt, we can automate our team's operation end-to-end, removing the need to run a piecemeal process (which in turn improves our listing turnaround time)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Prompting for Code Interpreter
&lt;/h2&gt;

&lt;p&gt;Prompting for Code Interpreter is not different than prompting for other contexts. In fact, the only difference is in how we explicitly create an Assistant for that purpose:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nb"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;files&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;listing.csv&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;rb&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="n"&gt;purpose&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;assistants&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;instructions&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
You are a data entry operator who is tasked to reorganize the information of a CSV file provided by the user. Do your best to reorganize the information based on the user input, but do not ask for the user&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s help.
&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;

&lt;span class="n"&gt;assistant&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;beta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;assistants&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;instructions&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;instructions&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;gpt-4-1106-preview&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;tools&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;type&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;code_interpreter&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}],&lt;/span&gt;
  &lt;span class="n"&gt;file_ids&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we first uploaded a file containing our listing data, so it can be referenced by the Assistant. We then created a new Assistant, and we gave it code_interpreter tool it can use. This tool comes with no argument specification; since Code Interpreter is built into the model, the Assistant will already know how to invoke it.&lt;/p&gt;

&lt;p&gt;As far as prompting goes, here's how we're going to proceed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We'll first get our updated taxonomy before each run, and feed it to the prompt.&lt;/li&gt;
&lt;li&gt;We'll give the prompt precise instructions about the relationship about items in our taxonomy (so that, for example, the Assistant does not misgender sizes or categories)&lt;/li&gt;
&lt;li&gt;We specify the output columns we want and the output format (an Excel file in this case)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's a simplified version of the initial version of our prompt:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;I have a file I&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ll need to reorganize in a different format. The input file will contains product information, such as:
&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;columns&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;

The file can contain additional information that can be relevant to the task, such as:
&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;file_specific_info&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;

Give then input file, create an output file with the following columns:
&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;output_columns&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;

Do the following for each row:

- Check if each cell in the row contains the above information. If so, start extracting this information and add them in the right column. For example, if the product description contains the item&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s brand, move the brand in the Brand column for that item.

- Check if the product description or any other information in the row contains brand information. For example, the product description may contain a brand name. If you find a brand in that row, add it to the Brand Column.

- Check if a URL is present in the row, and check is formatted in a way that seems to be pointing to an image. If that&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s the case, add it to the Image URL column for that row. If not, leave blank.

- Read the SKU Title or product description carefully, then infer the value for Category Group from the information available in that same row, then choose one of these output values:
&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;taxonomy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;category_groups&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;

If you do not have enough information to determine a Category Group for an item, leave its output value empty.

- For each row, infer the value for Department from the information available in that same row, then choose one of these output values:
&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;taxonomy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;departments&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;

Leave Department empty if Category Group is Apparel or Footwear. Also leave empty if you do not have enough information determine its output value.

- If sizes are provided, translate each size to one of the following values, or leave empty if you cannot determine the right sizing:
&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;taxonomy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;

Note that the input file may list multiple sizes for the same item. If that&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s the case, separate each size into its own row for the same item.

- If two rows end up having the same combination of SKU Title, Size, Price, and MSRP, append one or more additional information (such as Style or SKU) to the Style column so that it creates an unique value.

Some other suggestions for you:
Try to understand what type of data each input column contains, then map it to the relevant output column. When necessary, make assumptions using both conventional knowledge and specific knowledge of the retail industry.

If some values are not available, or if you cannot determine them with confidence, simply leave them blank.

The output should be an Excel file.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Few shots, less flaws
&lt;/h2&gt;

&lt;p&gt;Our basic prompt structure only provides task context, inputs, and the immediate task description. Even so, the prompt is fairly accurate at determining input and output data, as well as applying some heuristics to infer missing information like categories and style descriptions, when missing.&lt;/p&gt;

&lt;p&gt;In our tests, a few-shots variant of the prompt performs significantly better, both in terms of accuracy and latency.&lt;/p&gt;

&lt;h2&gt;
  
  
  It takes time to gain time
&lt;/h2&gt;

&lt;p&gt;It may sound counterintuitive, but it takes time to shave time off our processes. And that's okay: in our case, prompt engineering is a fixed time cost that pays off dividends in a short amount of time. And with the time we saved in making our processes more efficient, we were able to scale our operations, which in turn freed up more bandwidth to optimize our prompts.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>promptengineering</category>
    </item>
    <item>
      <title>Advanced prompting techniques: function calling</title>
      <dc:creator>Daniele</dc:creator>
      <pubDate>Tue, 26 Dec 2023 17:48:42 +0000</pubDate>
      <link>https://dev.to/iamdaniele/advanced-prompting-techniques-function-calling-2866</link>
      <guid>https://dev.to/iamdaniele/advanced-prompting-techniques-function-calling-2866</guid>
      <description>&lt;p&gt;LLMs cannot rely on real-time knowledge. For example, an AI can't tell you what time is it right now, or change their prediction of a winning team given the live score of a football game. Is there a way to overcome this limitation?&lt;/p&gt;

&lt;p&gt;As a user, prompting is probably the most important aspect when dealing with AIs. Writing the right prompt not only can make an LLM more accurate, but it will confer superpowers it can't provide out of the box.&lt;/p&gt;

&lt;p&gt;One of these superpowers is to feed external data to in the prompt. This gives the assistant additional context to provide a more accurate answer. As the model gathers information, we can include these details in the prompt itself (a technique called prompt chaining), so that the model's context grows as it thinks through an accurate answer.&lt;/p&gt;

&lt;p&gt;These techniques work well when we provide data to the prompt at the beginning of the conversation. For example, the model can help us categorize data based on criteria we add to the prompt at the beginning of the conversation. But what if results change during the conversation? And what if the model needs to dynamically access data we couldn't provide?&lt;/p&gt;

&lt;p&gt;We can use a variation of those techniques to let LLMs know they can use tools, so that they won't have to think for themselves when they cannot provide an accurate answer. This is a technique on itself, and it's called function calling (or tool use).&lt;/p&gt;

&lt;p&gt;In tool use, we first tell the model that there are tools available. We describe what are those tools and how they can be invoked. We encourage the model to call those tools whenever it cannot think of an accurate answer, or in situations when it needs access to data it doesn't have. The model will decide what function it needs, and it will output a function call when needed. On the client side, the code will detect a function call, execute it, and return its result in the next prompt. The model can then use those results and decide what to do next (including making another function call).&lt;/p&gt;

&lt;p&gt;Here, I'll use Claude from Anthropic. I choose it for a few reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Anthropic takes AI safety seriously, which makes it easier to deal with harmful responses. Claude is trained using a combination of RLHF and Constitutional AI, which makes it quite resilient out-of-the-box from harmful prompts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Anthropic just released a new &lt;strong&gt;Messages API&lt;/strong&gt; (in beta at the time of writing), and I wanted to try it out!&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What's the weather like?
&lt;/h2&gt;

&lt;p&gt;Now, suppose we want to get the weather in a specific location. The model will first need to convert the user-provided location to a set of coordinates, then get the weather for those coordinates. We will code two functions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;get_lat_long&lt;/code&gt;: converts a location name to a set of coordinates (latitude and longitude)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;get_weather&lt;/code&gt;: gets the weather for that particular set of coordinates.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We'll need to define how those functions will work. In the code, we'll add a specification to describe what the function does, along with its name and arguments. Here's how the specification for get_weather looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;get_weather_description_json&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;get_weather&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;description&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Returns weather data for a given latitude and longitude.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;parameters&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;
      &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;latitude&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;type&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;string&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;description&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;The latitude coordinate as a string&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;longitude&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;type&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;string&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;description&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;The longitude coordinate as a string&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'll likely have a specification like this for each one of the functions we want Claude to use. We will then chain all these descriptions together and encourage Claude to use them. This is done in the prompt:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;In this environment you have access to a set of tools you can use to answer the user's question. You may call them like this:
&amp;lt;function_calls&amp;gt;
{
  "function_calls": [
    {
      "tool_name": "$TOOL_NAME",
      "parameters": {
        "$PARAMETER_NAME": "$PARAMETER_VALUE"
      }
    }
  ]
}
&amp;lt;/function_calls&amp;gt;


Only invoke one function at a time and wait for the results before invoking another function:

&amp;lt;functions&amp;gt;
{
  "function_calls": [
    {
      "name": "get_weather",
      "description": "Returns weather data for a given latitude and longitude.",
      "parameters": [{
        "name": "latitude",
        "type": "string",
        "description": "The latitude coordinate as a string"
      },
      {
        "name": "longitude",
        "type": "string",
        "description": "The longitude coordinate as a string"
      }
    ]
  },
  {
    "name": "get_lat_long",
    "description": "Returns the latitude and longitude for a given place name.",
    "parameters": [{
      "name": "place",
      "type": "string",
      "description": "The place name to geocode and get coordinates for."
  }]
]}
&amp;lt;/functions&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The user will ask something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;What's the weather like in San Francisco?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  JSON meets XML
&lt;/h2&gt;

&lt;p&gt;Anthropic recommends enclosing instruction in XML tags, but one hidden gem about Claude is that it can understand JSON quite simply too. In my test, I tried writing specifications completely in XML (which is Anthropic usually does), and when I converted it into JSON, I found the model was equally good at understanding instructions and making calls. The XML/JSON combination also has the advantage of simplifying the parsing logic in the client, which is nice!&lt;/p&gt;

&lt;h2&gt;
  
  
  AI calls, function responds
&lt;/h2&gt;

&lt;p&gt;When we run this prompt, Claude will output something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;function_calls&amp;gt;
{"function_calls": [{"tool_name": "get_lat_long", "parameters": {"place": "San Francisco"}}]}
&amp;lt;/function_calls&amp;gt;
To get the weather for San Francisco, I first needed to find the latitude and longitude coordinates. I used the get_lat_long tool to geocode "San Francisco" and get its geographic coordinates.
Let's check the response to see if we got the coordinates:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Claude correctly understood how to call a function, and it is in fact asking to call one. We see JSON wrapped in XML tags, but we can't parse it as is. We'll need to get rid of the unwanted output, and then isolate and parse the JSON string. To get rid of the more talkative part, we configure a stop sequence in the API request. Stop sequences will stop any output starting at the location the sequence is encountered. Configuring a stop sequence of &lt;code&gt;&amp;lt;/function_calls&amp;gt;&lt;/code&gt; will automatically skip any output that starts with &lt;code&gt;&amp;lt;/function_calls&amp;gt;&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The output will then look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;function_calls&amp;gt;
{"function_calls": [{"tool_name": "get_lat_long", "parameters": {"place": "San Francisco"}}]}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we just have to remove the initial  tag and parse the JSON. We can do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;&amp;lt;function_calls&amp;gt;&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;tools_string_index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;tools_string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;tools_string_index&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;):]&lt;/span&gt;

&lt;span class="n"&gt;tools_to_invoke&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tools_string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;function_call&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tools_to_invoke&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="c1"&gt;# function call will be {"tool_name": "get_lat_long", "parameters": {"place": "San Francisco"}
&lt;/span&gt;
&lt;span class="nf"&gt;call_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;function_call&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have a dictionary with the name of the function and its parameters. In our code, we have a receiver function that will accept Claude's arguments and return a result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;call_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tool_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parameters&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getattr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tools&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tool_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;parameters&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We take the result, serialize it back into the prompt, and feed it back to Claude.&lt;/p&gt;

&lt;p&gt;Claude will then choose to invoke the get_weather function with the latitude and longitude provided by get_lat_long (which is now part of the prompt). This will return the correct answer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The weather data shows it is currently 12.3°C in San Francisco, with 5.2km/h winds from the west-southwest direction. The weather code indicates cloudy skies.

Let me know if you need any other weather details for San Francisco!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Imagine the possibilities
&lt;/h2&gt;

&lt;p&gt;Function calling is a very powerful technique, and it can be used to go far beyond making API calls.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We could describe a SQL schema, and Claude could perform SQL queries to obtain data from a database&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The model could ask for a specific file in the local filesystem&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The model could execute OS commands&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The model could come up with some search keywords, and the client can search the internet for those. This technique is usually employed as Retrieval Augmented Generation.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With the right prompt, your model can put all its knowledge to great use, and expand the realm of usefulness you can derive from it.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>promptengineering</category>
    </item>
  </channel>
</rss>
