<?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 Jeremy Heffner on Medium]]></title>
        <description><![CDATA[Stories by Jeremy Heffner on Medium]]></description>
        <link>https://medium.com/@jeremyheffner?source=rss-da9c22637308------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*mqEw4AeDGilXiv85TRvYyA.png</url>
            <title>Stories by Jeremy Heffner on Medium</title>
            <link>https://medium.com/@jeremyheffner?source=rss-da9c22637308------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 04:37:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jeremyheffner/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[Enhancing the Reliability of GPT Powered Solutions with Function Callbacks]]></title>
            <link>https://jeremyheffner.medium.com/enhancing-the-reliability-of-gpt-powered-solutions-with-function-callbacks-e85634742d8f?source=rss-da9c22637308------2</link>
            <guid isPermaLink="false">https://medium.com/p/e85634742d8f</guid>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[gpt]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[openai]]></category>
            <category><![CDATA[language-model]]></category>
            <dc:creator><![CDATA[Jeremy Heffner]]></dc:creator>
            <pubDate>Fri, 21 Jul 2023 11:53:45 GMT</pubDate>
            <atom:updated>2023-07-24T08:54:16.518Z</atom:updated>
            <content:encoded><![CDATA[<p>Artificial intelligence is evolving at a rapid pace, with OpenAI’s <a href="https://openai.com/gpt-4">GPT-4</a> model paving the way for rapid advancements. Large language models like GPT-4 generate human-like text and exhibit a wide array of skills. Recently, OpenAI introduced an exciting feature — <a href="https://platform.openai.com/docs/guides/gpt/function-calling">function calling</a>, opening up a new horizon of capabilities.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mW-e37JxNIKZRDaRqLeTLQ.png" /></figure><p>Function calls, available in the <em>gpt-4–0613</em> and <em>gpt-3.5-turbo-0613</em> models, offer developers the power to describe functions to the model. The model then intelligently outputs a JSON object with arguments to invoke these functions. This powerful capability enables developers to bridge the gap between the model’s capabilities and external tools or APIs, boosting the model’s ability to execute precise logic and fetch fresh data from web APIs.</p><p>Besides leveraging this capability to call out to third party tools, this feature can improve the reliability of returning structured data from the model to power your solution. You can think of this like a callback function that you pass to the model and then instruct the model to invoke the callback to return your desired outputs.</p><h3>Defining Functions</h3><p>A <a href="https://json-schema.org/understanding-json-schema/index.html">JSON Schema</a> format allows developers to specify the parameter names, types, and descriptions for the available function(s). The choice of whether to invoke a function can be left to the model based on the context, or can be forced by the developer.</p><p>A crucial aspect of this feature is the power of using descriptions in your API definition. These descriptions help the model to understand the function’s use, minimizing the number of examples required for training and enabling zero-shot use cases.</p><p>Some best practices for defining your functions are to:</p><ul><li>Use semantically meaningful parameter names and return values rather than opaque special values.</li><li>Use <em>description</em> keys to provide simple explanations of the expected data and format.</li><li>Use the capabilities of JSON Schema to tightly define the expected data; for example use <a href="https://json-schema.org/understanding-json-schema/reference/generic.html#enumerated-values">enumerations</a> for discrete choice parameters</li></ul><h3>Sentiment Analysis: A Practical Example</h3><p>Let’s consider a real-world example: returning the sentiment of a given text. Here, we define a function “return_sentiment”. This function is designed to take an enumerated parameter representing the sentiment of the given text, which can be “Positive”, “Negative”, or “Neutral”.</p><p>First we need to describe the callback function schema:</p><pre>gpt_functions = [<br>    {<br>        &quot;name&quot;: &quot;return_sentiment&quot;,<br>        &quot;description&quot;: &quot;Returns the sentiment of the provided text&quot;,<br>        &quot;parameters&quot;: {<br>            &quot;type&quot;: &quot;object&quot;,<br>            &quot;properties&quot;: {<br>                &quot;sentiment&quot;: {<br>                    &quot;type&quot;: &quot;string&quot;,<br>                    &quot;description&quot;: &quot;The sentiment of the text&quot;,<br>                    &quot;enum&quot;: [&quot;Positive&quot;, &quot;Negative&quot;, &quot;Neutral&quot;],<br>                },<br>            },<br>            &quot;required&quot;: [&quot;sentiment&quot;],<br>            &quot;additionalProperties&quot;: False,<br>        },<br>    },<br>]</pre><p>Then, we can invoke the chat completion API instructing the model to invoke the callback function as follows:</p><pre>text_to_analyze = sys.argv[1]  # get text from command line<br>messages = [<br>    {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: &quot;You are a helpful assistant.&quot;},<br>    {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: text_to_analyze},<br>]<br><br>completion = await openai.ChatCompletion.acreate(<br>    model=&quot;gpt-3.5-turbo&quot;,<br>    messages=messages,<br>    functions=gpt_functions,<br>    function_call={&quot;name&quot;: &quot;return_sentiment&quot;},<br>)<br><br>response = json.loads(<br>    completion[&quot;choices&quot;][0][&quot;message&quot;][&quot;function_call&quot;][&quot;arguments&quot;]<br>)<br>print(response)</pre><p>Note that the only guidance the model has in fulfilling our request in this example is the function schema. This example showcases the remarkable reliability and efficiency of this function callback pattern, even in a zero-shot setting with no other instructions provided to the model.</p><p><a href="https://gist.github.com/jerheff/254718528237c9a5327b541af9fb8cba">Full example code</a></p><h3>Leverage LLMs with FlexDS</h3><p>Language models provide powerful capabilities for developers, but navigating this landscape and harnessing their potential can be a challenging endeavor. This is where <a href="https://www.nyumbics.com/">Nyumbics</a>’ <a href="https://www.nyumbics.com/flexds">FlexDS</a> comes in. Our team of seasoned data scientists and software developers excel in solving your business problems leveraging AI technologies as well as more traditional data science and analysis techniques.</p><p>Learn more about our <a href="https://www.nyumbics.com/flexds">managed monthly subscription for data science consulting</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e85634742d8f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Encoding Cyclic Variables for Machine Learning]]></title>
            <link>https://levelup.gitconnected.com/encoding-cyclic-variables-for-machine-learning-780f61131288?source=rss-da9c22637308------2</link>
            <guid isPermaLink="false">https://medium.com/p/780f61131288</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Jeremy Heffner]]></dc:creator>
            <pubDate>Tue, 25 Feb 2020 02:40:15 GMT</pubDate>
            <atom:updated>2021-04-10T18:58:41.683Z</atom:updated>
            <content:encoded><![CDATA[<p>Feature engineering is a key activity for building accurate machine learning models. By transforming the representation of information, we make it easier for a machine learning model to map our input variables to our target values. One common class of features that crop up are cyclic variables.</p><p>Cyclic variables often arise when representing temporal features such as the day of the week or hour of the day. Take the day of the week as an example. A common representation of the day of week assigns integers such that Sunday is 0, Monday is 1, Tuesday is 2, and so forth. For some classes of models, this representation is quite useless. For example, a <a href="https://en.wikipedia.org/wiki/Logistic_regression">logistic regression</a> model would have a difficult time extracting much information from such a representation as the target is likely not linearly related to this representation. A common solution is to employ <a href="https://en.wikipedia.org/wiki/One-hot">one-hot-encoding</a> to cast the 7 days into 7 binary variables. But what do we lose with this approach?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*h45QKnJMaHKcxTPgyRMOmQ.png" /></figure><p>Purely categorical features have no distance from one another. Encoding such variables with a one-hot-encoding doesn’t lose information since there is no innate proximity. For example, one-hot-encoding the colors red, yellow, and orange implies that red is no ‘closer’ to orange than it is yellow. Encoding days of the week in a similar matter throws out information that Wednesday is closer to Thursday than Friday. If the cardinality of the values is low enough and enough examples exist across the training data, losing the similarity information may not matter too much, but doing so makes the model work harder to learn generalizable patterns since the effect of each day of the week is independently learned.</p><p>Considering how the algorithm you are employing learns when designing your feature encoding is a good exercise.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*yIGKNWczOILMRj3I" /><figcaption>Photo by <a href="https://unsplash.com/@haleywayphotography?utm_source=medium&amp;utm_medium=referral">Haley Hamilton</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>For a logistic regression model, we learn a coefficient per input feature. If we assume that when we learn the effect of a Tuesday we gain some information about the effect of a Wednesday we can evolve our one-hot-encoding. Instead of encoding Tuesday as [0, 0, 1, 0, 0, 0, 0] we could instead encode it as [0, 0.5, 1, 0.5, 0, 0, 0] or as [0.25, 0.5, 1, 0.5, 0.25, 0, 0] tying the effects together with diminishing strength as the distance increases. This smoothing of effects is similar to the <a href="https://adeshpande3.github.io/A-Beginner%27s-Guide-To-Understanding-Convolutional-Neural-Networks/">convolution filters</a> used in image processing where the kernel weights are often freely learned. In a spatial analysis, the kernel is often a closed-form such as the <a href="https://en.wikipedia.org/wiki/Mat%C3%A9rn_covariance_function">Matérn</a> or <a href="https://en.wikipedia.org/wiki/Gaussian_process">Gaussian</a> covariance functions where the degree of smoothing is controlled via a kernel bandwidth value. In our case, the spreading distance and form could be controlled as hyperparameters that are optimized through cross-validation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*rvQPLtFYN-lILLe5" /><figcaption>Photo by <a href="https://unsplash.com/@fabulu75?utm_source=medium&amp;utm_medium=referral">Fabrice Villard</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>If we switch our consideration to decision tree-based models such as <a href="https://xgboost.readthedocs.io/en/latest/">XGBoost</a> or random forests, we arrive at different encodings. A decision tree recursively splits variables at specific cut points. Consider our day of week encoding which starts numbering Sundays as 0. If our data includes a weekday versus weekend effect, then splitting Sunday (0) and Saturday (6) off from the weekdays requires two splits. If instead, we start the day of week encoding with Mondays being 0 then only one split is necessary.</p><p>Unlike the regression model we first examined, the decision tree naturally preserves proximity of the weekdays in our 0 to 6 encoding aside from the discontinuity from 6 to 0. How can we address this?</p><p>One approach would be to simply provide multiple 0 to 6 encodings rotated around the days of the week; perhaps one encoding with Monday as 0 and one with Friday as 0. This redundancy provides the model with more options.</p><p>Still, to pick out say Tuesday and Wednesday requires two cut points. If instead, we believe that clusters of days are likely to have an impact, we could change the encoding. One hot encoding is frequently used with XGBoost to represent categoricals. In the day of week case, this would add 7 features to the model. Using the same number of features, we could instead generate features that represent the offset from each day. For instance, the Sunday feature would be 0 on Sundays, 1 on Mondays or Saturdays, 2 on Tuesdays or Fridays, and so forth. For a training record from a Sunday, the 7 features would have values [0, 1, 2, 3, 3, 2, 1]. With this representation, the model can pull out any number of days symmetrically centered on Sunday with just one cut.</p><p>Other models such as neural networks, might benefit from still other encodings such as the <a href="https://medium.com/life-at-hopper/ai-in-travel-part-2-representing-cyclic-and-geographic-features-4ada33dd0b22">spatial sign transformation</a>.</p><p>There is no hard and fast rule about what is best to use as it will depend on the dynamics of the effects in your use case, but understanding how your model learns when deciding what to try first is a good starting point.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=780f61131288" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/encoding-cyclic-variables-for-machine-learning-780f61131288">Encoding Cyclic Variables for Machine Learning</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>