<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Openai on Generative Storytelling</title>
    <link>https://www.generativestorytelling.ai/blog/tags/openai/</link>
    <description>Recent content in Openai on Generative Storytelling</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <managingEditor>devlin.bentley@gmail.com (Devlin)</managingEditor>
    <webMaster>devlin.bentley@gmail.com (Devlin)</webMaster>
    <lastBuildDate>Thu, 05 Sep 2024 06:42:07 +0000</lastBuildDate><atom:link href="https://www.generativestorytelling.ai/blog/tags/openai/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Using OpenAI Tool Call Helpers in Typescript</title>
      <link>https://www.generativestorytelling.ai/blog/posts/using-openai-tool-call-helpers-in-typescript/</link>
      <pubDate>Thu, 05 Sep 2024 06:42:07 +0000</pubDate>
      <author>devlin.bentley@gmail.com (Devlin)</author>
      <guid>https://www.generativestorytelling.ai/blog/posts/using-openai-tool-call-helpers-in-typescript/</guid>
      <description>&lt;p&gt;The documentation for how to use the new beta tool call helpers API is pretty lackluster, so here is a very brief example.&lt;/p&gt;
&lt;p&gt;Overview and Assumptions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;I tend to store all my tools+prompts in a separate file, while some of my tool handlers are private class methods that I cannot just import and assign when defining my tools. To work around this, I create functions that take in the tool and assign it to the &lt;code&gt;function&lt;/code&gt; field of the tool. (Update: Now with full type safety!)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;I know that type inference can in theory do this, but it is &lt;em&gt;much&lt;/em&gt; easier to have errors at the place of declaration of the variable, rather than have them at the call site that takes the variable in. This is especially true with how these runnable functions are typed in TS, I ran into a 30 minute &amp;ldquo;what is happening&amp;rdquo; debugging session when the TS compiler gave me an absolutely horrible error&lt;/p&gt;
&lt;p&gt;message when I was passing in an array of multiple tools to &lt;a href=&#34;http://openai.beta.chat&#34;&gt;&lt;code&gt;openai.beta.chat&lt;/code&gt;&lt;/a&gt;&lt;code&gt;.completions.runTools&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;</description>
      <content>&lt;p&gt;The documentation for how to use the new beta tool call helpers API is pretty lackluster, so here is a very brief example.&lt;/p&gt;
&lt;p&gt;Overview and Assumptions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;I tend to store all my tools+prompts in a separate file, while some of my tool handlers are private class methods that I cannot just import and assign when defining my tools. To work around this, I create functions that take in the tool and assign it to the &lt;code&gt;function&lt;/code&gt; field of the tool. (Update: Now with full type safety!)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;I know that type inference can in theory do this, but it is &lt;em&gt;much&lt;/em&gt; easier to have errors at the place of declaration of the variable, rather than have them at the call site that takes the variable in. This is especially true with how these runnable functions are typed in TS, I ran into a 30 minute &amp;ldquo;what is happening&amp;rdquo; debugging session when the TS compiler gave me an absolutely horrible error&lt;/p&gt;
&lt;p&gt;message when I was passing in an array of multiple tools to &lt;a href=&#34;http://openai.beta.chat&#34;&gt;&lt;code&gt;openai.beta.chat&lt;/code&gt;&lt;/a&gt;&lt;code&gt;.completions.runTools&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-typescript&#34; data-lang=&#34;typescript&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;// businessLogic.ts
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;export&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;type&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;ToolCb&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; (&lt;span style=&#34;color:#a6e22e&#34;&gt;args&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; {&lt;span style=&#34;color:#a6e22e&#34;&gt;cbNumberParam&lt;/span&gt;: &lt;span style=&#34;color:#66d9ef&#34;&gt;number&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;cbStringParam&lt;/span&gt;: &lt;span style=&#34;color:#66d9ef&#34;&gt;string&lt;/span&gt;}) &lt;span style=&#34;color:#f92672&#34;&gt;=&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-typescript&#34; data-lang=&#34;typescript&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;// tools.ts
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;import&lt;/span&gt; { &lt;span style=&#34;color:#a6e22e&#34;&gt;RunnableToolFunctionWithParse&lt;/span&gt; } &lt;span style=&#34;color:#66d9ef&#34;&gt;from&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;openai/lib/RunnableFunction&amp;#34;&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;import&lt;/span&gt; { &lt;span style=&#34;color:#a6e22e&#34;&gt;ToolCb&lt;/span&gt; } &lt;span style=&#34;color:#66d9ef&#34;&gt;from&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;./businessLogic&amp;#34;&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;export&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;const&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;createMyFancyTool&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; (&lt;span style=&#34;color:#a6e22e&#34;&gt;functionCallback&lt;/span&gt;: &lt;span style=&#34;color:#66d9ef&#34;&gt;ToolCb&lt;/span&gt;)&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;RunnableToolFunctionWithParse&lt;/span&gt;&amp;lt;&lt;span style=&#34;color:#f92672&#34;&gt;Parameters&lt;/span&gt;&amp;lt;&lt;span style=&#34;color:#f92672&#34;&gt;ToolCb&lt;/span&gt;&amp;gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;0&lt;/span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;]&lt;/span&gt;&amp;gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&amp;gt;&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;type&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;function&amp;#39;&lt;/span&gt;, &lt;span style=&#34;color:#75715e&#34;&gt;// if you don&amp;#39;t declare as RunnableToolFunctionWithParse up above you&amp;#39;ll need to write &amp;#39;function&amp;#39; as &amp;#39;function&amp;#39; here!
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;function&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#66d9ef&#34;&gt;function&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;functionCallback&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#a6e22e&#34;&gt;parse&lt;/span&gt;: &lt;span style=&#34;color:#66d9ef&#34;&gt;JSON.parse&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#a6e22e&#34;&gt;name&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;My fancy tool&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#75715e&#34;&gt;// naming tools is actually really important for getting ChatGPT to accurately call them!
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#a6e22e&#34;&gt;description&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;A fancy tool you should call when needed&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#75715e&#34;&gt;// descriptions are also super important and should be considered part of prompt engineering
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#a6e22e&#34;&gt;parameters&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                &lt;span style=&#34;color:#66d9ef&#34;&gt;type&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;object&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                &lt;span style=&#34;color:#a6e22e&#34;&gt;properties&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                    &lt;span style=&#34;color:#a6e22e&#34;&gt;cbNumberParam&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                        &lt;span style=&#34;color:#66d9ef&#34;&gt;type&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;number&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                        &lt;span style=&#34;color:#a6e22e&#34;&gt;description&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;the numeric data&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                    },
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                    &lt;span style=&#34;color:#a6e22e&#34;&gt;cbStringParam&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                        &lt;span style=&#34;color:#66d9ef&#34;&gt;type&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;string&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                        &lt;span style=&#34;color:#a6e22e&#34;&gt;description&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;the string data&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                    },
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                    &lt;span style=&#34;color:#a6e22e&#34;&gt;required&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; [&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;cbNumberParam&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;cbStringParam&amp;#34;&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Astute readers will notice that this is only 2 fields different than what the regular tool definition is. As mentioned above, I ran into an issue where after I added the function and parse fields, I got some really gnarly errors, which where the result of me not having written &lt;code&gt;type: &#39;function&#39; as &#39;function&#39;&lt;/code&gt; which you&amp;rsquo;ll likely need to do if you don&amp;rsquo;t explicitly type everything. OpenAI&amp;rsquo;s example code neglects this. However after fully typing everything, I no longer needed to cast the string.&lt;/p&gt;
&lt;p&gt;Finally, if you don&amp;rsquo;t like using the parameters built in, you can create a separate type for the arguments and use that directly.&lt;/p&gt;
</content>
    </item>
    
  </channel>
</rss>
