<?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 Iryna Kondrashchenko on Medium]]></title>
        <description><![CDATA[Stories by Iryna Kondrashchenko on Medium]]></description>
        <link>https://medium.com/@iryna230520?source=rss-aaa3985be912------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dXzJ63aPs4BwAfahw6WYMg.jpeg</url>
            <title>Stories by Iryna Kondrashchenko on Medium</title>
            <link>https://medium.com/@iryna230520?source=rss-aaa3985be912------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 04:13:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@iryna230520/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[ONNX: Bridging the Gap Between AI Frameworks and Real-World Deployments]]></title>
            <link>https://medium.com/@iryna230520/onnx-bridging-the-gap-between-ai-frameworks-and-real-world-deployments-2540cd8d6d92?source=rss-aaa3985be912------2</link>
            <guid isPermaLink="false">https://medium.com/p/2540cd8d6d92</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Iryna Kondrashchenko]]></dc:creator>
            <pubDate>Sun, 30 Jul 2023 19:32:22 GMT</pubDate>
            <atom:updated>2023-07-30T19:32:22.704Z</atom:updated>
            <content:encoded><![CDATA[<p>Beginners Guide to ONNX and ONNXRuntime.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/821/1*DDtWOU2MFVJuSX0cX02PfA.png" /></figure><p>The <a href="https://onnx.ai/">Open Neural Network Exchange (ONNX)</a> is an open-source project developed in a cooperation between Facebook, Microsoft, and other major companies, that defines a standard format for machine learning models. The core goal of ONNX is to ensure the interoperability between different frameworks by expressing the models in a unified way. Despite the recent surge in popularity, which has led many data scientists to become familiar with it, ONNX still generates considerable confusion about its functionality, and practical applications. In this article we will attempt to dispel this confusion by providing a breakdown of ONNX’s structure, its core functionalities, and the benefits it offers.</p><h4><strong>What exactly is ONNX ?</strong></h4><p>ONNX is a format that allows expressing a model as a computational graph, where the nodes correspond to the standardized operations, and the edges define the flow of the data. To make an ONNX graph the following components have to be included:</p><ul><li><strong>Nodes:</strong> each node contains the information about the operation to be executed (each operation is identified by a unique name in the ONNX standard, i.e. “Add”), the inputs to the node (an input can be either a model input, model initializer or the output of one of the previous nodes), the node outputs, and (optionally) additional operation-specific attributes.</li><li><strong>Inputs: </strong>the inputs are the tensors that are passed to the model at the beginning of each iteration (usually the features).</li><li><strong>Initializers:</strong> the tensors with predefined values that do not change across iterations. Initializers are usually used to store the weights of the model.</li><li><strong>Outputs:</strong> a subset of the node outputs to be used as global model outputs.</li><li><strong>Opsets:</strong> ONNX uses opsets to manage the operators and their versions. An operator can belong to a certain opset defined by a reversed domain name (in onnx standard there are two opsets: <em>ai.onnx</em> and <em>ai.onnx.ml</em>). In addition, the opsets have different versions to maintain backward compatibility. When a model is exported, it’s tagged with an opset version to ensure that the model will continue to be interpretable by the runtime, even as operators get updated or added.</li></ul><p>By expressing models as computational graphs, ONNX can represent complex, multi-step computations. These computations can range from simple linear regressions to complex deep learning models. Because of this flexibility, and because it’s based on widely accepted principles, ONNX can represent models from a wide range of machine learning frameworks. As a simple example let’s see how a Logistic Regression could look like as an ONNX model.</p><p>Logistic regression can be defined as follows:</p><pre>y = sigmoid(wx + b)</pre><p>or equivalently</p><pre>y = Sigmoid(Add(Mul(w, x), b)</pre><p>Now let’s breakdown this into ONNX components:</p><ul><li>Nodes/Operators: [Sigmoid, Mul, Add]</li><li>Inputs (tensors that change between iterations): [x]</li><li>Initializers (tensors that remain static between iterations): [w, b]</li><li>Outputs: [y]</li></ul><p>The final computational graph looks as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RYOfZSIEnDv2bSCjXCDDMw.png" /></figure><h4><strong>What are the differences between ONNX and ONNXRuntime ?</strong></h4><p>The terms ONNX and ONNXRuntime are often used interchangeably. However, these are two independent projects with different purposes. ONNX itself only defines the format of the models, but does not provide the functionality of running the models.</p><p>On the other hand, <a href="https://onnxruntime.ai/">ONNX Runtime</a> is a performance-focused engine for deploying ONNX models. It is developed and maintained by Microsoft and provides an inference engine that delivers low-latency and high-efficiency computations for ONNX models. It supports a wide range of hardware and platforms, including CPUs, GPUs, and edge devices. Furthermore, while ONNX Runtime fully supports all operators in the ONNX specification, it extends this support to include additional, non-standard ops.</p><p>In other words, ONNX defines a universal “language” for expressing the models, while ONNXRuntime executes the model expressed in that language.</p><h4><strong>What are the advantages of ONNX/ONNXRuntime ?</strong></h4><p>The main advantages of using ONNX and ONNXRuntime are as follows:</p><ul><li>Fewer dependencies: ONNX mitigates the challenges presented by dependencies. Each deep learning framework requires specific versions of the libraries to function correctly, which can potentially complicate the deployment process. ONNX addresses this issue by simplifying the model representation, removing the need for most framework-specific libraries.</li><li>Backwards Compatibility: ONNX is designed to be backwards compatible. This means that a model saved with older versions can still be used with newer versions even if some of the operators have changed.</li></ul><h4><strong>How to create an ONNX model ?</strong></h4><p>In this section we will reuse the logistic regression example to see how the ONNX model can be constructed from scratch.</p><pre><br># ! pip install onnx <br><br>import onnx<br>from onnx import helper, TensorProto, OperatorSetIdProto<br><br># Define the input and output tensors<br>X = helper.make_tensor_value_info(&#39;X&#39;, TensorProto.FLOAT, [1])<br>Y = helper.make_tensor_value_info(&#39;Y&#39;, TensorProto.FLOAT, [1])<br><br># Create initializers for weight (W) and bias (b) with specific values<br>W_init = helper.make_tensor(&#39;W&#39;, TensorProto.FLOAT, [1], [0.5])  # Weight<br>b_init = helper.make_tensor(&#39;b&#39;, TensorProto.FLOAT, [1], [-0.25])  # Bias<br><br># Create nodes for multiplication, addition, and sigmoid activation<br>node_mul = helper.make_node(&#39;Mul&#39;, [&#39;X&#39;, &#39;W&#39;], [&#39;mul_result&#39;], name=&#39;mul_node&#39;)  # Wx<br>node_add = helper.make_node(&#39;Add&#39;, [&#39;mul_result&#39;, &#39;b&#39;], [&#39;add_result&#39;], name=&#39;add_node&#39;)  # Wx + b<br>node_sigmoid = helper.make_node(&#39;Sigmoid&#39;, [&#39;add_result&#39;], [&#39;Y&#39;], name=&#39;sigmoid_node&#39;)  # sigmoid(Wx + b)<br><br># Create a graph from the nodes and initializers<br>graph_def = helper.make_graph(<br>    [node_mul, node_add, node_sigmoid],<br>    &#39;logistic_regression_model&#39;,<br>    [X],<br>    [Y],<br>    [W_init, b_init]<br>)<br><br># Specify the opset and its version<br>opset = OperatorSetIdProto(domain=&quot;ai.onnx&quot;, version=15)<br><br># Create a model from the graph with specified opset<br>model_def = helper.make_model(graph_def, producer_name=&#39;onnx-example&#39;, opset_imports=[opset])<br><br># Print the model<br>print(&#39;The model is:\n{}&#39;.format(model_def))<br><br># Save the model<br>onnx.save_model(model_def, &#39;logistic_regression_model.onnx&#39;)</pre><p>Note: The saved model can be visualized using the <a href="https://netron.app/">netron.app</a> visualization tool.</p><p>It is worth noting that the provided example demonstrates the usage of relatively low-level functions to construct the ONNX graph. In practice, this is rarely required, as ONNX integrations are available for most of the major frameworks, which makes the conversion process easier. For example, <a href="https://github.com/OKUA1/falcon"><strong>Falcon library</strong></a><strong> </strong>allows to train the models and export them into a deployable ONNX model in just a single line of code.</p><pre># ! pip install falcon-ml<br>from falcon import AutoML<br>from falcon.datasets import load_churn_dataset<br><br>df = load_churn_dataset()<br><br># this will automatically train the model and save it as ONNX<br>AutoML(task = &#39;tabular_classification&#39;, train_data = df)</pre><p>You can check the original Falcon article to learn more about the library or possible ONNX deployment process.</p><p><a href="https://medium.com/@o.kostromin/effortless-model-training-and-deployment-using-falcon-ml-onnx-and-fastapi-338a0984e0b8">Effortless model training and deployment using Falcon ML, ONNX and FastAPI</a></p><h4><strong>Conclusion</strong></h4><p>In conclusion, ONNX serves as a bridge between different machine learning frameworks, reducing dependencies, ensuring backwards compatibility, and simplifying model deployment. While it may require some learning curve to fully utilize ONNX, its increasing popularity results in a growing number of integrations which form a comprehensive ecosystem to create, visualize, and run ML models across various platforms. Therefore, using the ONNX in the ML workflow can yield significant benefits.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2540cd8d6d92" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Fine-tune Google PaLM 2 with Scikit-LLM]]></title>
            <link>https://medium.com/@iryna230520/fine-tune-google-palm-2-with-scikit-llm-d41b0aa673a5?source=rss-aaa3985be912------2</link>
            <guid isPermaLink="false">https://medium.com/p/d41b0aa673a5</guid>
            <category><![CDATA[scikit-learn]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Iryna Kondrashchenko]]></dc:creator>
            <pubDate>Tue, 04 Jul 2023 20:24:49 GMT</pubDate>
            <atom:updated>2024-06-18T20:43:36.276Z</atom:updated>
            <content:encoded><![CDATA[<p>The latest update of Scikit-LLM brings the possibility of fine-tuning PaLM 2 — the most advanced LLM from Google.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UGt6bdt0Zdw6SaeEJSIdjw.png" /></figure><h3>Introduction</h3><p>Recently, Google opened the access to <a href="https://ai.google/discover/palm2/">PaLM 2</a>, their most advanced large language model. Similarly to other models, a pre-trained PaLM 2 can be used for a variety of language tasks: classification, summarization, question answering, etc. In addition to that, it can be fine-tuned on custom data right in the Google Cloud for enhanced predictive power.</p><p>In this article we will provide a detailed guide on how to fine-tune Google PaLM 2 with <a href="https://github.com/iryna-kondr/scikit-llm"><strong>Scikit-LLM</strong></a><strong> </strong>using<strong> </strong>the newly added estimators. This is arguably the easiest method as it provides a familiar scikit-learn compatible API.</p><p>If you are not familiar with Scikit-LLM, check out my previous article about it:</p><p><a href="https://medium.com/@iryna230520/scikit-llm-nlp-with-chatgpt-in-scikit-learn-733b92ab74b1">Scikit-LLM: NLP with ChatGPT in Scikit-Learn</a></p><h3>Google Cloud Set Up</h3><p>Currently, the only way to use PaLM 2 is via Vertex AI platform. Therefore, it is needed to create a Google account, log in to <a href="https://console.cloud.google.com/">Google Cloud Console</a> and <a href="https://developers.google.com/workspace/guides/create-project">create a Google Cloud project</a>. After the project is created, select this project from a list of projects next to the Google Cloud logo (upper left corner). Then search for and select Vertex AI in the search bar.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/654/1*U_yR8-S7HloqhEoo3F1JWg.png" /></figure><p>Once you are on the Vertex AI main page, enable all recommended APIs. Please note that it can take several minutes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/930/1*OVQZjqhnviwrWABBHz0xBg.png" /></figure><p>Finally, we need to install a Google Cloud CLI on the local machine by following the <a href="https://cloud.google.com/sdk/docs/install">steps from the official documentation</a>, and <a href="https://cloud.google.com/docs/authentication/application-default-credentials#personal">set the application default credentials</a> by running the following command:</p><pre>gcloud auth application-default login</pre><p>If everything worked properly, you should see the following page in the browser:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2vCew_m5_ShcZSB4WZ3S9A.png" /></figure><h3>Using PaLM 2 with Scikit-LLM</h3><p>In this section we will see how to use the available Scikit-LLM estimators to interact with PaLM 2 model in Vertex AI.</p><ol><li>Install Scikit-LLM</li></ol><pre>pip install scikit-llm</pre><p>2. Set the project id, which can be found in a list of the projects.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/684/1*ATSvee9KaPx5iP8J1Rze2A.png" /></figure><pre>from skllm.config import SKLLMConfig<br><br>SKLLMConfig.set_google_project(&quot;YOUR_PROJECT_ID&quot;)</pre><p>3. To verify that Scikit-LLM is properly connected to the Google Cloud, let’s try to run a simple zero-shot classification task. In this setting we are not yet fine-tuning the model, so it should take just a few seconds to run.</p><pre>from skllm.models.vertex.classification import ZeroShotVertexClassifier<br>from skllm.datasets import get_classification_dataset<br><br>X, y = get_classification_dataset()<br>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5)<br><br>clf = ZeroShotVertexClassifier()<br>clf.fit(X_train, y_train)<br>labels = clf.predict(X_test)<br><br>print(labels)</pre><p>If everything worked properly, you will see a list of predicted labels (positive/neutral/negative).</p><h3>PaLM 2 Fine-Tuning with Scikit-LLM</h3><p>In order to fine-tune the model we need to have 64 cores of the TPU v3 pod training resource. By default this quota is set to 0 cores and has to be increased. For that <a href="https://cloud.google.com/docs/quota_detail/view_manage#requesting_higher_quota">Go to Quotas </a>-&gt; filter quotas for <em>“Restricted image training TPU V3 pod cores per region” </em>and select “<em>europe-west4”</em> region (at the moment only this region is available for PaLM tuning).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*idLxHiXH8BuyVYEs3FGkmg.png" /></figure><p>Then click on <em>“Edit Quotas”, </em>set the limit to 64 and submit the request. Since all requests are processed manually by Google Support, it may take several days for a quota to be increased.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hhJdZhfd2IZlrtU7cUUtXQ.png" /></figure><blockquote>In principle, PaLM 2 tuning on TPU is subject to <a href="https://cloud.google.com/vertex-ai/pricing">regular pricing</a> for custom model training. However, at the time of writing this article, PaLM 2 tuning is still in the preview stage, so all charges are discounted by 100%. Please check the current status and pricing before training the model to avoid unexpected costs.</blockquote><p>As soon as the quota was increased, it is possible to proceed with fine-tuning right away. Scikit-LLM provides two models: VertexClassifier and TunableVertexText2Text. The difference between them is that VertexClassifier is optimized for text classification tasks, whereas TunableVertexText2Text can be trained on the arbitrary input-output pairs.</p><h4>Example 1: Classification</h4><p>To use a VertexClassifier, we can reuse our zero-shot example. The only thing that we need to change is to replace ZeroShotVertexClassifier with VertexClassifier. The number of the update steps can be selected following <a href="https://cloud.google.com/vertex-ai/docs/generative-ai/models/tune-models#recommended_configurations">the official recommendations from Google</a>.</p><pre>from skllm.models.vertex.classification.tunable import VertexClassifier<br>from skllm.datasets import get_classification_dataset<br><br>X, y = get_classification_dataset()<br>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5)<br><br>clf = VertexClassifier(n_update_steps=100)<br>clf.fit(X_train, y_train)<br>labels = clf.predict(X_test)<br><br>print(labels)</pre><h4>Example 2: General Tuning</h4><pre>from skllm.models.vertex.text2text.tunable import TunableVertexText2Text<br><br>X = [&quot;Tell us something about Scikit-LLM&quot; for _ in range(20)]<br>y = [&quot;Scikit-LLM is awesome&quot; for _ in range(20)]<br><br>model = TunableVertexText2Text(n_update_steps=100)<br>model.fit(X, y)<br>labels = model.predict([&quot;Tell us something about Scikit-LLM&quot;])<br><br>print(labels[0])<br><br># &gt; Scikit-LLM is awesome</pre><p>Behind the scenes, Scikit-LLM will automatically create a tuning pipeline in Vertex AI. As soon as the training starts, the link to the pipeline will be displayed in the terminal. You can use it for monitoring the state of the job.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7K_DWCa40Du-HJM8vQoriQ.png" /><figcaption>Exemplary fine-tuning pipeline</figcaption></figure><h3>Conclusion</h3><p>In this article we explored one of the easiest options to fine-tune Google PaLM 2. By leveraging the power of Scikit-LLM, this can be done in just a few lines of code following the familiar scikit-learn interface.</p><p>If you want to learn more about LLM-related Python tools, check out my previous article about <a href="https://github.com/OKUA1/agent_dingo">Dingo</a> — a microframework for creating simple AI agents.</p><p><a href="https://medium.com/@iryna230520/dingo-seamlessly-integrate-python-functions-into-chatgpt-6e3237f09824">Dingo: Seamlessly Integrate Python Functions into ChatGPT</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d41b0aa673a5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dingo: Seamlessly Integrate Python Functions into ChatGPT]]></title>
            <link>https://medium.com/@iryna230520/dingo-seamlessly-integrate-python-functions-into-chatgpt-6e3237f09824?source=rss-aaa3985be912------2</link>
            <guid isPermaLink="false">https://medium.com/p/6e3237f09824</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[chatgpt]]></category>
            <dc:creator><![CDATA[Iryna Kondrashchenko]]></dc:creator>
            <pubDate>Sun, 25 Jun 2023 20:08:59 GMT</pubDate>
            <atom:updated>2023-06-25T20:10:54.159Z</atom:updated>
            <content:encoded><![CDATA[<p>Integrate any Python function into ChatGPT in a single line of code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5On9yVP7KV9GEC5jRBOxdA.png" /></figure><h3>Introduction</h3><p>Recents advancements in Large Language Models led to the emergence of AI Agents. In plain terms, an agent is a mechanism that equips LLMs with a set of external tools (functions). These tools can be invoked by the model (once or multiple times in a row) based on the user’s input.</p><p>Originally, in order to integrate a function into an LLM, it was needed to carefully construct the prompt in order to describe the function. Additionally, the output had to be parsed to detect whether the model intended to use this function.</p><p>The latest release of GPT introduced function calling capabilities that simplify the process of integrating the functions. First of all, it defines a clear and fixed structure for describing the functions. Moreover, the models themselves have been further fine-tuned to both detect when a function should be called (based on the input) and to respond with JSON that adheres to the function signature. However, despite these improvements, the integration process still remains tedious and requires a lot of overhead.</p><p>In this article we will explore how to build a simple conversational AI agent using the <a href="https://github.com/OKUA1/agent_dingo"><strong>Agent Dingo</strong></a><strong> </strong>library.</p><p><strong>The key feature of Dingo is that it allows to integrate an external function into ChatGPT by adding just a single line of code.</strong></p><h3>Quick Start</h3><p>To give a quick glimpse into a core functionality of Dingo, we will build a simple agent that is able to provide the information about the current weather by querying the <a href="https://openweathermap.org/api">OpenWeather</a> API.</p><p>As the first step, the library has to be installed.</p><pre>pip install agent-dingo</pre><p>Next, we need to set the OpenAI API key. You can check how to create the key in my <a href="https://medium.com/@iryna230520/scikit-llm-nlp-with-chatgpt-in-scikit-learn-733b92ab74b1">previous post</a>.</p><pre>export OPENAI_API_KEY=&lt;YOUR_KEY&gt;</pre><p>Then, our Dingo agent can be instantiated as follows.</p><pre>from agent_dingo import AgentDingo<br><br>agent = AgentDingo()</pre><p>Let’s say that for getting the current weather via OpenWeather API we have a get_temperature function. In order to integrate it into ChatGPT we need to add <strong>a single line of code</strong> — the decorator @agent.function.</p><pre>@agent.function<br>def get_temperature(city: str) -&gt; str:<br>    base_url = &quot;https://api.openweathermap.org/data/2.5/weather&quot;<br>    params = {&quot;q&quot;: city, &quot;appid&quot;: openweathermap_api_key, &quot;units&quot;: &quot;metric&quot;}<br>    response = requests.get(base_url, params=params)<br>    data = response.json()<br>    print(&quot;get_temperature function output :&quot;, data[&quot;main&quot;])<br>    return str(data)</pre><p>To get the output we need to call thechat method.</p><pre>agent.chat(&quot;What is the current weather in Linz?&quot;)</pre><p>As you can see the process of function integration is very intuitive and simple. Behind the scenes we are asking ChatGPT for a current weather in Linz. The model has access to the function (get_temperature) that was integrated by using agent.function decorator. The model decides to call get_temperature function and passes city Linz as an argument. The response is parsed and we receive the following output.</p><pre>The current temperature in Linz is 30°C.</pre><p>As you might have noticed, we did not have to provide any descriptions of the function. This is achieved by the automatic docstring generation during the registration process (when agent.function decorator is called). However, this might be suboptimal for complex functions. Therefore, instead of relying on automatic generation we encourage you to always provide a docstring of the function. It is possible to use either google or numpy docstring styles. In our get_temperature example it would look as follows.</p><pre>@agent.function<br>def get_temperature(city: str) -&gt; str:<br>    &quot;&quot;&quot;Retrieves the current temperature in a city.<br>    Parameters<br>    ----------<br>    city : str<br>        The city to get the temperature for.<br>    Returns<br>    -------<br>    float<br>        str representation of the json response from the weather api.<br>    &quot;&quot;&quot;<br>    base_url = &quot;https://api.openweathermap.org/data/2.5/weather&quot;<br>    params = {&quot;q&quot;: city, &quot;appid&quot;: openweathermap_api_key, &quot;units&quot;: &quot;metric&quot;}<br>    response = requests.get(base_url, params=params)<br>    data = response.json()<br>    print(&quot;get_temperature function output :&quot;, data[&quot;main&quot;])<br>    return str(data)</pre><p>You can find the code used in this article (with extended examples) <a href="https://github.com/OKUA1/code_examples/tree/main/202306_dingo_medium">here</a>.</p><h3>Conclusion</h3><p>In this article we explored how <a href="https://github.com/OKUA1/agent_dingo"><strong>Agent Dingo</strong></a> streamlines the process of integrating Python functions into ChatGPT in order to build a simple conversational AI agent. With Dingo, you no longer have to worry about manually integrating the functions or dealing with intermediate function calls. The framework is crafted to automate these tasks, allowing you to focus on writing the core functionality of your app.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e3237f09824" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[First Steps in LangChain: The Ultimate Guide for Beginners (part 2)]]></title>
            <link>https://medium.com/@iryna230520/first-steps-in-langchain-the-ultimate-guide-for-beginners-part-2-d17a2f057f43?source=rss-aaa3985be912------2</link>
            <guid isPermaLink="false">https://medium.com/p/d17a2f057f43</guid>
            <category><![CDATA[langchain]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[nlp]]></category>
            <dc:creator><![CDATA[Iryna Kondrashchenko]]></dc:creator>
            <pubDate>Wed, 21 Jun 2023 20:49:55 GMT</pubDate>
            <atom:updated>2023-06-21T21:43:31.882Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*q74YvSb41qQCPUne5V1mfA.png" /></figure><p>In the <a href="https://medium.com/@iryna230520/first-steps-in-langchain-the-ultimate-guide-for-beginners-part-1-2baf5a4e1b81">previous article</a>, we covered three modules of the LangChain: Prompts, Models and Memory. In this article, I would like to continue the explanation and cover the next two modules, specifically the Data connection (former Indexes module) and Chains.</p><h4>Data Connection</h4><p>This module is designed to handle user-specific data, that is not part of the model’s training set. This module provides various building blocks to load, transform, and query data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f61UNlx02BD3Wi-fEU7Xkg.png" /></figure><p>There are 5 components of the Data connection module:</p><ul><li>Document Loaders are used to load data from a variety of sources as Documents. There are lots of integrations available, including CSV, HTML, JSON, PDF, Markdown, AWS S3 Directory, etc. You can check all of them <a href="https://python.langchain.com/docs/modules/data_connection/document_loaders/">here</a>.</li></ul><p>In the example below a CSV file is loaded using aCSVLoader:</p><pre>from langchain.document_loaders.csv_loader import CSVLoader<br><br>loader = CSVLoader(file_path=&#39;data/example_dataset.csv&#39;)<br>data = loader.load()<br>print(data)</pre><ul><li>Document Transformers are used to transform the loaded documents to better suit your application. This can include splitting a long document into smaller chunks, that can fit into your model’s context window. The default text splitter is the RecursiveCharacterTextSplitter, which creates chunks based on splitting on certain characters and ensures that semantically related pieces of text are kept together. There are many other text splitters available, check them <a href="https://python.langchain.com/docs/modules/data_connection/document_transformers/">here</a>.</li></ul><p>In the example below some arbitrary long text is split into chunks of the (maximum) size 100 characters, and with the overlap of 20 characters between the chunks (to maintain continuity).</p><pre>from langchain.text_splitter import RecursiveCharacterTextSplitter<br><br>with open(&#39;data/long_text.txt&#39;) as f:<br>    long_text = f.read()<br><br>text_splitter = RecursiveCharacterTextSplitter(<br>    chunk_size=100,<br>    chunk_overlap=20<br>)<br><br>texts = text_splitter.create_documents([long_text])<br>print(texts[0].page_content, &quot;\n&quot;)<br>print(texts[1].page_content)</pre><ul><li>Text Embedding Models allow creating a vector representation of a single query or a list of texts. Various <a href="https://python.langchain.com/docs/modules/data_connection/text_embedding/">embedding model providers </a>are already integrated in a LangChain.</li></ul><p>In the example below we use text-embedding-ada-002 embedding model from OpenAI. For that it is needed to install and import openai package, as well as generate OpenAI API key. Please refer to <a href="https://medium.com/@iryna230520/scikit-llm-nlp-with-chatgpt-in-scikit-learn-733b92ab74b1">my previous post</a> to check how the API key can be generated.</p><pre>!pip install openai<br><br>import openai<br>from langchain.embeddings import OpenAIEmbeddings<br><br>embeddings_model = OpenAIEmbeddings(<br>    openai_api_key=OPENAI_API_KEY, model=&quot;text-embedding-ada-002&quot;)<br>simple_document = [<br>    &quot;The aroma of freshly baked bread wafted through the cozy bakery.&quot;,<br>    &quot;The city lights shimmered like a sea of diamonds in the night.&quot;,<br>    &quot;The scent of pine filled the air as hikers ventured deeper into the forest.&quot;]<br>embeddings = embeddings_model.embed_documents(simple_document)<br>print(embeddings)</pre><ul><li>Vector Stores allow searching over unstructured data. If you are not familiar with Vector Indices and Vector Databases, I encourage you to read the excellent <a href="https://www.pinecone.io/learn/vector-database/">article from Pinecone</a> about them. Firstly, vector embeddings of the data are created and stored together with some metadata/reference to the original content the embeddings were created from. Afterwards, this data is used to retrieve the most similar vector embeddings to the embedded query. <a href="https://python.langchain.com/docs/modules/data_connection/vectorstores/">There are many providers</a> of vector indices and databases, such as Pinecone, FAISS, Milvus, Weaviate, etc. available in LangChain.</li></ul><p>In the example below we use text-embedding-ada-002 embedding model for creating vector representation of the text, and Facebook AI Similarity Search (FAISS) library for finding the most similar texts to a query.</p><pre>!pip install faiss-cpu<br><br>from langchain.embeddings.openai import OpenAIEmbeddings<br>from langchain.text_splitter import CharacterTextSplitter<br>from langchain.vectorstores import FAISS<br>from langchain.document_loaders import TextLoader<br><br># load the document<br>loader = TextLoader(&quot;sample_text.txt&quot;)<br>documents = loader.load()<br># split the text<br>text_splitter = CharacterTextSplitter(chunk_size=100, chunk_overlap=0)<br>docs = text_splitter.split_documents(documents)<br># initialize OpenAI embeddings model<br>embeddings_model = OpenAIEmbeddings(<br>    openai_api_key=OPENAI_API_KEY, model=&quot;text-embedding-ada-002&quot;)<br># initialize FAISS library<br>db = FAISS.from_documents(docs, embeddings_model)<br># query for which similarity search will be done<br>query = &quot;Taxis and buses weaved through the maze of traffic, carrying commuters to their destinations.&quot;<br># find the most similar docs to the query<br>similar_docs = db.similarity_search(query)<br>print(similar_docs[0].page_content)</pre><ul><li>Retrievers module serves to fetch or retrieve relevant documents based on a given query. Different classes within the module act as wrappers for <a href="https://python.langchain.com/docs/modules/data_connection/retrievers/">various sources,</a> such as the ArxivRetriever class, which is effectively a wrapper for ArxivAPIWrapper. Each retriever class has a method named get_relevant_documents() which is used to retrieve the documents relevant to a specific query. The retrieved documents are returned as a list of Document objects.</li></ul><p>In the example below we use Arxiv Retriever Plugin, thus it is needed to install and import arxiv and pymupdf packages:</p><pre>!pip install arxiv<br>!pip install pymupdf<br><br>from langchain.retrievers import ArxivRetriever<br><br># initialize Arxiv retriever<br>retriever = ArxivRetriever(load_max_docs=2)<br># get documents similar to &quot;GPT-4 Technical Report paper&quot; (2303.08774)<br>docs = retriever.get_relevant_documents(query=&quot;2303.08774&quot;)<br># get meta-information of the first document found<br>print(docs[0].metadata)</pre><h4>Chains</h4><p>Chains is the core module of the LangChain. It allows combining multiple components together to create a single application. There are 4 types of the chains available: LLM, Router, Sequential, and Transformation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KVg8KyzKsijcWh2asxPNXg.png" /></figure><ul><li>The most basic type of chain is a LLMChain. It takes user input, formats it with a PromptTemplate, and passes it to the language model (either LLM or chat model).</li></ul><p>In the example below we use the PromptTemplate defined in <a href="https://medium.com/@iryna230520/first-steps-in-langchain-the-ultimate-guide-for-beginners-part-1-2baf5a4e1b81">my last article</a> for creating a simple LLMChain:</p><pre>from langchain.llms import OpenAI<br>from langchain.chains import LLMChain<br>from langchain.prompts import PromptTemplate<br><br>llm = OpenAI(model_name=&quot;gpt-3.5-turbo-0613&quot;, temperature=0,<br>             openai_api_key=OPENAI_API_KEY)<br>template = &quot;&quot;&quot;You will provided with the sample text. \<br>Your task is to rewrite the text to be gramatically correct. \<br>Sample text: ```{sample_text}``` \<br>Output: <br>&quot;&quot;&quot;<br>prompt_template = PromptTemplate.from_template(<br>    template=template<br>)<br>sample_text = &quot;Me likes cats not dogs. They jumps high so much!&quot;<br><br># initialize LLMChain by passing LLM and prompt template<br>llm_chain = LLMChain(<br>    llm=llm,<br>    prompt=prompt_template<br>)<br>llm_output = llm_chain.run(sample_text)<br>print(llm_output)</pre><p>Similarly, the chain can be created using chat model (the PromptTemplate is taken from <a href="https://medium.com/@iryna230520/first-steps-in-langchain-the-ultimate-guide-for-beginners-part-1-2baf5a4e1b81">my last article</a>):</p><pre>from langchain.chat_models import ChatOpenAI<br>from langchain.prompts import SystemMessagePromptTemplate, HumanMessagePromptTemplate, ChatPromptTemplate<br><br># initialize ChatGPT model<br>chat = ChatOpenAI(model_name=&quot;gpt-3.5-turbo-0613&quot;, temperature=0,<br>                  openai_api_key=OPENAI_API_KEY)<br>system_template = &quot;&quot;&quot;You will provided with the sample text. \<br>Your task is to translate the text into {output_language} language \<br>and summarize the translated text in at most {max_words} words. \ <br>&quot;&quot;&quot;<br>system_message_prompt_template = SystemMessagePromptTemplate.from_template(<br>    system_template)<br>human_template = &quot;{sample_text}&quot;<br>human_message_prompt_template = HumanMessagePromptTemplate.from_template(<br>    human_template)<br>chat_prompt_template = ChatPromptTemplate.from_messages(<br>    [system_message_prompt_template, human_message_prompt_template])<br><br># initialize LLMChain by passing LLM and prompt template<br>llm_chain = LLMChain(llm=chat, prompt=chat_prompt_template)<br>llm_output = llm_chain.run(output_language=&quot;English&quot;, max_words=15,<br>                           sample_text=&quot;Estoy deseando que llegue el fin de semana.&quot;)<br>print(llm_output)</pre><ul><li>A RouterChain in LangChain is a construct that dynamically selects the next chain to be used for a given input. RouterChain itself is responsible for this selection, while destination_chains are the chains to which the router chain can direct its input. Essentially, the purpose of a RouterChain is to determine the most appropriate chain to use for handling a given input. There are different types of RouterChains available, such as LLMRouterChainand EmbeddingRouterChain. LLMRouterChain uses a language model to determine how to route things, whileEmbeddingRouterChain uses embeddings and similarity principle to route between destination chains.</li><li>SequentialChains allow connecting multiple chains, in particular it makes possible to take the output from one chain and use it as an input to the next chain. There are 2 types of SequentialChains: SimpleSequentialChain, that allows only for a single input/output at each step, and SequentialChain, that allows for multiple inputs/outputs.</li></ul><p>In the example below we create a SimpleSequentialChain that first translates a text into French, and then creates a summary of the text.</p><pre>from langchain.llms import OpenAI<br>from langchain.chains import LLMChain, SimpleSequentialChain<br>from langchain.prompts import PromptTemplate<br><br>llm = OpenAI(model_name=&quot;gpt-3.5-turbo-0613&quot;, temperature=0,<br>             openai_api_key=OPENAI_API_KEY)<br>template_1 = &quot;&quot;&quot;You will provided with the sample text. \<br>Your task is to translate the sample text to French. \<br>Sample text: ```{sample_text}``` \<br>Transalation: <br>&quot;&quot;&quot;<br># create a prompt for a translation task<br>prompt_template_1 = PromptTemplate.from_template(<br>    template=template_1<br>)<br># initialize transaltion chain<br>chain_1 = LLMChain(<br>    llm=llm,<br>    prompt=prompt_template_1<br>)<br><br>template_2 = &quot;&quot;&quot;You will provided with the sample text. \<br>Your task is to summarize the sample text to be 1 sentence long.\<br>The summary needs to be provided in the original text language.\<br>Sample text: ```{transalted_text}``` \<br>Summary: <br>&quot;&quot;&quot;<br># create a prompt for a summarization task<br>prompt_template_2 = PromptTemplate.from_template(<br>    template=template_2<br>)<br># initialize summarization chain<br>chain_2 = LLMChain(<br>    llm=llm,<br>    prompt=prompt_template_2<br>)<br># create the final chain<br>overall_chain = SimpleSequentialChain(chains=[chain_1, chain_2], verbose=True)<br>input_text = &quot;A myriad of flowers bloom in the meadow, painting a vibrant spectrum of colors against the emerald backdrop. The gentle hum of bees is heard as they move from blossom to blossom, engaged in their endless dance of pollination. As the day turns to dusk, the meadow&#39;s inhabitants retreat, leaving behind only the soft whispers of the wind.&quot;<br># generate output<br>output = overall_chain.run(input_text)<br>print(output)</pre><ul><li>TransformationChain allows applying some transformations (by using preliminary created transformation functions) tot the output of the chain before passing it further to the next chain in the sequence.</li></ul><p>The example below was taken from langChain documentation. Here, the transformation function (transform_func) is created. It takes the text from the input dictionary, shortens it to the first three paragraphs, and returns a new dictionary with the shortened text. After the transformation is done, the transformed text is passed to the LLMChain, which uses a language model to further process the text, in this case, to summarize it. Finally, the TransformationChain and the LLMChain are combined into a SimpleSequentialChain, which runs the chains in sequence: first the transformation, then the summarization​.</p><pre>from langchain.chains import TransformChain, LLMChain, SimpleSequentialChain<br>from langchain.llms import OpenAI<br>from langchain.prompts import PromptTemplate<br><br>with open(&quot;../../state_of_the_union.txt&quot;) as f:<br>    state_of_the_union = f.read()<br># create transformation function<br><br><br>def transform_func(inputs: dict) -&gt; dict:<br>    text = inputs[&quot;text&quot;]<br>    shortened_text = &quot;\n\n&quot;.join(text.split(&quot;\n\n&quot;)[:3])<br>    return {&quot;output_text&quot;: shortened_text}<br><br><br># initializre transformation chain<br>transform_chain = TransformChain(<br>    input_variables=[&quot;text&quot;], output_variables=[&quot;output_text&quot;], transform=transform_func<br>)<br>template = &quot;&quot;&quot;Summarize this text:<br><br>{output_text}<br><br>Summary:&quot;&quot;&quot;<br>prompt = PromptTemplate(input_variables=[&quot;output_text&quot;], template=template)<br># initialize LLM chain for a summarization task<br>llm_chain = LLMChain(llm=OpenAI(), prompt=prompt)<br># create a sequential chain<br>sequential_chain = SimpleSequentialChain(chains=[transform_chain, llm_chain])<br># generate output<br>output = sequential_chain.run(state_of_the_union)<br>print(output)</pre><h3>Conclusion</h3><p>LangChain is a powerful framework that streamlines the development of AI applications. In this article, we have provided an overview of two important LangChain modules: DataConnection and Chains. In the next article, we will delve into the remaining modules, expanding our understanding of the full capabilities of LangChain.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d17a2f057f43" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Exciting OpenAI Updates Bring New Capabilities and Cost Reductions]]></title>
            <link>https://medium.com/@iryna230520/exciting-openai-updates-bring-new-capabilities-and-cost-reductions-ecef681fbf2d?source=rss-aaa3985be912------2</link>
            <guid isPermaLink="false">https://medium.com/p/ecef681fbf2d</guid>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[chatgpt]]></category>
            <category><![CDATA[large-language-models]]></category>
            <category><![CDATA[openai]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Iryna Kondrashchenko]]></dc:creator>
            <pubDate>Wed, 14 Jun 2023 07:45:10 GMT</pubDate>
            <atom:updated>2023-06-14T12:19:15.834Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a7h4fEKX7v7rdYYEKDeZUw.png" /></figure><h3>Introduction</h3><p>On June 14th OpenAI has launched several exciting updates to their language models. Developers now have a new <em>function calling </em>capability in the Chat Completions API, alongside <em>more adaptable versions</em> of `gpt-4` and `gpt-3.5-turbo`. They’ve also <em>extended the context version </em>of `gpt-3.5-turbo` to 16k and <em>reduced costs </em>for their embeddings model and `gpt-3.5-turbo` input tokens by 75% and 25% respectively.</p><h3>Function Calling</h3><p>The most interesting feature is that the newest models, `gpt-3.5-turbo-0613` and `gpt-4–0613`, are now equipped with an exciting new feature: they can generate JSON objects to call predefined functions in your code. It’s like instructing an AI assistant to understand the steps to carry out tasks and let it provide the recipe for executing those tasks.</p><p>It is needed to describe the functions to the models during an API call. The functions are specified using the following parameters: 1) name of the function; 2) description of the function that will be used by the model to decide when to call the function; 3) parameters the function accepts, described as a JSON Schema object. The model decides when a function needs to be invoked based on the conversation context and then produces the necessary JSON structure. Remember, the Chat Completions API itself doesn’t call the function, instead the model provides you the instruction set, which you use to call the function in your code.</p><p>This function calling capability is pretty powerful. It opens up an array of potential applications like creating chatbots that call external APIs to answer questions, converting natural language into API calls, or extracting structured data from text. But with great power comes great responsibility. OpenAI recommends incorporating user confirmation flows before executing tasks with real-world impacts, such as sending emails or making purchases.</p><p>Since the functions are put into the system message, they count towards the model’s context limit and are billed as input tokens, so manage these carefully in order not to run into context limits.</p><p>To use this feature, you include the user query and a set of functions in the API call. The model then can choose to call a function and if so, it will provide a response structured as a stringified JSON object. You parse this JSON in your code and call your function with the given arguments. Then you call the model again, with the function response as a new message, and the model summarizes the results back to the user. This back-and-forth lets the model act as a dynamic, interactive assistant to help carry out a wide range of tasks.</p><p>Lets look at the example from the official OpenAI docs:</p><pre>import openai<br>import json<br><br># Example dummy function hard coded to return the same weather<br># In production, this could be your backend API or an external API<br>def get_current_weather(location, unit=&quot;fahrenheit&quot;):<br>    &quot;&quot;&quot;Get the current weather in a given location&quot;&quot;&quot;<br>    weather_info = {<br>        &quot;location&quot;: location,<br>        &quot;temperature&quot;: &quot;72&quot;,<br>        &quot;unit&quot;: unit,<br>        &quot;forecast&quot;: [&quot;sunny&quot;, &quot;windy&quot;],<br>    }<br>    return json.dumps(weather_info)<br><br># Step 1, send model the user query and what functions it has access to<br>def run_conversation():<br>    response = openai.ChatCompletion.create(<br>        model=&quot;gpt-3.5-turbo-0613&quot;,<br>        messages=[{&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &quot;What&#39;s the weather like in Boston?&quot;}],<br>        functions=[<br>            {<br>                &quot;name&quot;: &quot;get_current_weather&quot;,<br>                &quot;description&quot;: &quot;Get the current weather in a given location&quot;,<br>                &quot;parameters&quot;: {<br>                    &quot;type&quot;: &quot;object&quot;,<br>                    &quot;properties&quot;: {<br>                        &quot;location&quot;: {<br>                            &quot;type&quot;: &quot;string&quot;,<br>                            &quot;description&quot;: &quot;The city and state, e.g. San Francisco, CA&quot;,<br>                        },<br>                        &quot;unit&quot;: {&quot;type&quot;: &quot;string&quot;, &quot;enum&quot;: [&quot;celsius&quot;, &quot;fahrenheit&quot;]},<br>                    },<br>                    &quot;required&quot;: [&quot;location&quot;],<br>                },<br>            }<br>        ],<br>        function_call=&quot;auto&quot;,<br>    )<br><br>    message = response[&quot;choices&quot;][0][&quot;message&quot;]<br><br>    # Step 2, check if the model wants to call a function<br>    if message.get(&quot;function_call&quot;):<br>        function_name = message[&quot;function_call&quot;][&quot;name&quot;]<br><br>        # Step 3, call the function<br>        # Note: the JSON response from the model may not be valid JSON<br>        function_response = get_current_weather(<br>            location=message.get(&quot;location&quot;),<br>            unit=message.get(&quot;unit&quot;),<br>        )<br><br>        # Step 4, send model the info on the function call and function response<br>        second_response = openai.ChatCompletion.create(<br>            model=&quot;gpt-3.5-turbo-0613&quot;,<br>            messages=[<br>                {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &quot;What is the weather like in boston?&quot;},<br>                message,<br>                {<br>                    &quot;role&quot;: &quot;function&quot;,<br>                    &quot;name&quot;: function_name,<br>                    &quot;content&quot;: function_response,<br>                },<br>            ],<br>        )<br>        return second_response<br><br>print(run_conversation())</pre><p>In this example, we first provided the model with the ability to call the `get_weather` function, which it did in its initial response. In a subsequent message, we relayed the response of the function back to the model so that it could generate the final output.</p><h3>Conclusion</h3><p>To wrap up, OpenAI’s recent updates have brought in versatile features, improved controllability, and significant cost reductions.</p><p>The updated models will go live on June 27th, but you can already test them today by adding the `-0613` suffix to the model name.</p><p>If you want to learn more about exciting LLMs based projects, check out my previous articles:</p><ul><li><a href="https://medium.com/@iryna230520/scikit-llm-nlp-with-chatgpt-in-scikit-learn-733b92ab74b1">Scikit-LLM: NLP with ChatGPT in Scikit-Learn</a></li><li><a href="https://medium.com/@iryna230520/dynamic-few-shot-prompting-overcoming-context-limit-for-chatgpt-text-classification-2f70c3bd86f9">Dynamic Few-Shot Prompting: Overcoming Context Limit for ChatGPT Text Classification</a></li><li><a href="https://medium.com/@iryna230520/first-steps-in-langchain-the-ultimate-guide-for-beginners-part-1-2baf5a4e1b81">First Steps in LangChain: The Ultimate Guide for Beginners (part 1)</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ecef681fbf2d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dynamic Few-Shot Prompting: Overcoming Context Limit for ChatGPT Text Classification]]></title>
            <link>https://medium.com/@iryna230520/dynamic-few-shot-prompting-overcoming-context-limit-for-chatgpt-text-classification-2f70c3bd86f9?source=rss-aaa3985be912------2</link>
            <guid isPermaLink="false">https://medium.com/p/2f70c3bd86f9</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[chatgpt]]></category>
            <category><![CDATA[scikit-learn]]></category>
            <dc:creator><![CDATA[Iryna Kondrashchenko]]></dc:creator>
            <pubDate>Mon, 12 Jun 2023 07:01:25 GMT</pubDate>
            <atom:updated>2024-06-18T20:27:42.131Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*efHbRd6rrxr8TGs0vLW5kw.png" /></figure><p>Recent explosion in the popularity of large language models like ChatGPT has led to their increased usage in classical NLP tasks like language classification. This involves providing a context (sample) and candidate labels to the model to reason about. More precisely, this approach is called zero-shot classification, which implies that the model does not have to be retrained to generalize to new/unseen categories. Naturally, not having to fine-tune a model for a specific task can be highly beneficial from a business perspective, as it significantly reduces development time as well as the additional burden of maintaining custom models.</p><p>However, one downside of the zero-shot approach is its limited ability to leverage existing labeled samples (training data). In this article, we will see how to solve this problem using dynamic few-shot prompting, which includes only a relevant subset of the training data in the prompt.</p><p>All code examples in this article use the <a href="https://github.com/iryna-kondr/scikit-llm">Scikit-LLM</a> library. Please check <a href="https://medium.com/@iryna230520/scikit-llm-nlp-with-chatgpt-in-scikit-learn-733b92ab74b1">my previous post</a> for more information.</p><h3>Recap: Zero-Shot vs Few-Shot prompting</h3><p>Before diving into dynamic few-shot prompting, let’s briefly recap the concepts of zero-shot and few-shot prompting.</p><p>Zero-shot prompting is the method where ChatGPT, or any other language model, is used to classify text without any additional task-specific training. It involves framing a question or task for the model and providing it with options to choose from. Essentially, the model uses the knowledge it was trained with to complete the task.</p><p>A very simple zero-shot prompt can look like this:</p><pre>Input: &quot;The rover landed on Mars after a seven-month journey.&quot;<br>Prompt: &quot;Is this text about Science, Sports, or Politics?&quot;</pre><p>Under the hood, a `ZeroShotGPTClassifier` from Scikit-LLM also uses zero-shot prompting and allows allows building an estimator in just three lines of code:</p><pre>from skllm.models.gpt.classification.zero_shot import ZeroShotGPTClassifier<br><br>clf = ZeroShotGPTClassifier(model = &quot;gpt-3.5-turbo&quot;)<br>clf.fit(X, y)<br>labels = clf.predict(X)</pre><p>On the other hand, few-shot prompting gives ChatGPT several examples alongside the input. This serves to contextualize the task and offer direction for the model’s responses. Seeing these examples, the model understands the expected output, which improves its performance across a range of tasks.</p><p>Example prompt:</p><pre>Example 1:<br>Input: &quot;The athlete won a gold medal at the Olympics.&quot;<br>Output: &quot;Sports&quot;<br><br>Example 2:<br>Input: &quot;The legislation was passed after a long debate in the Senate.&quot;<br>Output: &quot;Politics&quot;<br><br>Example 3:<br>Input: &quot;The discovery of the Higgs boson at CERN marked a milestone in particle physics.&quot;<br>Output: &quot;Science&quot;<br><br>Task:<br>Input: &quot;The rover landed on Mars after a seven-month journey.&quot;<br>Prompt: &quot;Is this text about Science, Sports, or Politics?&quot;</pre><p>Example code:</p><pre>from skllm.models.gpt.classification.few_shot import FewShotGPTClassifier<br><br>clf = FewShotGPTClassifier(model=&quot;gpt-3.5-turbo&quot;)<br>clf.fit(X, y)<br>labels = clf.predict(X)</pre><h3>Dynamic Few-Shot prompting</h3><p>While few-shot prompting looks great on paper, as it allows the use of information from the training dataset to make predictions, it has significant scalability issues.</p><p>To understand the problem, let’s take a sample from the previous few-shot prompt.</p><pre>Input: &quot;The athlete won a gold medal at the Olympics.&quot;<br>Output: &quot;Sports&quot;</pre><p>If we pass this through the <a href="https://platform.openai.com/tokenizer">web interface of the OpenAI tokenizer</a>, we can see that the text corresponds to 19 tokens.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bDOsb2aEYxhACa77BHvScw.png" /></figure><p><strong><em>Why is this important?</em></strong></p><p>Firstly, modern LLMs have limited context length. For example, <em>gpt-3.5-turbo</em>, the most popular OpenAI model, has a context limit of 4096 tokens, while most of the current generation open-source models are limited to 2048 tokens. Given that our sample was 19 tokens long, we could provide at most 215 samples to <em>gpt-3.5-turbo</em> or 107 samples to an open-source LLM like <em>LLaMA</em>. In real scenarios, this number will be even lower since we do not account for additional tokens consumed by the prompt itself. Additionally, the text samples that need to be classified are often much longer.</p><p>Even if the context size was unlimited, processing longer prompts would require more computational resources, which is usually associated with higher financial costs.</p><p>A very natural solution to the problem is to use only a subset of the training data for the prompt itself.</p><p>This is exactly what `DynamicFewShotGPTClassifier` does. During inference, for each unlabeled datapoint, it dynamically selects N training examples from each class to be used in the prompt.</p><pre>from skllm.models.gpt.classification.few_shot import DynamicFewShotGPTClassifier<br><br>clf = DynamicFewShotGPTClassifier(n_examples=3)<br>clf.fit(X, y)<br>labels = clf.predict(X)</pre><p>To better understand how it works let’s consider a toy example, where the goal is to figure out whether the person is talking about the books or movies.</p><pre>from skllm.models.gpt.classification.few_shot import DynamicFewShotGPTClassifier<br><br>X = [<br>    &quot;I love reading science fiction novels, they transport me to other worlds.&quot;,<br>    &quot;A good mystery novel keeps me guessing until the very end.&quot;,<br>    &quot;Historical novels give me a sense of different times and places.&quot;,<br>    &quot;I love watching science fiction movies, they transport me to other galaxies.&quot;,<br>    &quot;A good mystery movie keeps me on the edge of my seat.&quot;,<br>    &quot;Historical movies offer a glimpse into the past.&quot;,<br>]<br><br>y = [&quot;books&quot;, &quot;books&quot;, &quot;books&quot;, &quot;movies&quot;, &quot;movies&quot;, &quot;movies&quot;]<br><br>query = &quot;I have fallen deeply in love with this sci-fi book; its unique blend of science and fiction has me spellbound.&quot;<br><br>clf = DynamicFewShotGPTClassifier(n_examples=1).fit(X, y)<br><br>prompt = clf._get_prompt(query)<br>print(prompt)</pre><p>These are the examples that were automatically picked by the classifier to be included in the prompt:</p><pre>Sample input:<br>```I love reading science fiction novels, they transport me to other worlds.```<br><br>Sample target: books<br><br><br>Sample input:<br>```I love watching science fiction movies, they transport me to other galaxies.```<br><br>Sample target: movies</pre><p>Notice how both of the examples are clearly similar to the query as the person is talking about the science-fiction genre in all of the cases.</p><p><strong><em>But how exactly does it select examples dynamically based on the new input?</em></strong></p><p>This is achieved by adding a classical KNN-like algorithm as an additional preprocessor. If we assume that the most relevant examples are the most similar ones, then the problem reduces to a nearest neighbors search and can be tackled in three steps:</p><p>(1) Vectorization</p><p>Before doing the nearest neighbors search, the training set must be embedded into fixed-dimensional vectors. This can easily be achieved using the OpenAI embedding API (or any other alternative).</p><p>(2) Index construction</p><p>While any nearest neighbor algorithm can be used for this task, even the brute force option from scikit-learn, it is important to keep in mind the scalability aspects as we are dealing with very high-dimensional data and potentially a high number of samples as well. Luckily, there are plenty of tools that handle these scenarios extremely well. For example, <a href="https://github.com/spotify/annoy">Annoy</a>, a library for fast approximate nearest neighbors search developed by Spotify. By constructing the index once during training, it is possible to perform fast neighbors searches during inference.</p><p>(3) Balanced sampling</p><p>The last thing to be accounted for is class balancing. If only N nearest neighbors are selected for a few-shot prompting, there is a very high risk that some of the classes will be underrepresented or missing completely. To mitigate this issue, instead of creating a single index, the training data is partitioned by class. In this way, we are able to sample N examples from each class, ensuring the equal representation of each class.</p><h3><strong>Conclusion</strong></h3><p>In this article, we explored the concept of dynamic few-shot prompting, an enhancement of the zero-shot and few-shot prompting approaches for text classification with large language models like ChatGPT. We learned how it utilizes the existing labeled data to improve classification accuracy by dynamically selecting relevant examples to include in the prompt. By leveraging a KNN-like algorithm, the dynamic few-shot prompting process creates a balance between using the inherent capacity of language models to generalize from limited examples, and the need to process large-scale data for practical applications.</p><p>Furthermore, it is an efficient method that adapts to the constraints of context length in current language models and optimizes the usage of computational resources.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2f70c3bd86f9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[First Steps in LangChain: The Ultimate Guide for Beginners (part 1)]]></title>
            <link>https://medium.com/@iryna230520/first-steps-in-langchain-the-ultimate-guide-for-beginners-part-1-2baf5a4e1b81?source=rss-aaa3985be912------2</link>
            <guid isPermaLink="false">https://medium.com/p/2baf5a4e1b81</guid>
            <category><![CDATA[langchain]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[openai]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Iryna Kondrashchenko]]></dc:creator>
            <pubDate>Sun, 04 Jun 2023 15:58:20 GMT</pubDate>
            <atom:updated>2023-06-18T20:46:46.325Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/886/1*RbYFKFD8CBPE_fl90bwSxg.png" /></figure><h3><strong>What is LangChain?</strong></h3><p><a href="https://python.langchain.com/en/latest/index.html">LangChain</a> is a framework built around large language models (LLMs). It includes integrations with a wide range of systems and tools. LangChain allows chaining of various modular components, such as a PromptTemplate and LLM, to create a sequence or Chain that takes an input, processes it, and generates a response. This makes it a versatile tool that can be used in a variety of different contexts and applications. Being an open-source project, LangChain benefits from the input and contributions of a large community of developers.</p><p>LangChain incorporates seven modules: Prompts, Models, Memory, Indexes, Chains, Agents, and Callbacks. In this article I would like to describe three of them: prompts, models, and memory.</p><blockquote>Note: In order to use LangChain with OpenAI wrapper, we will need to install and import LangChain and OpenAI packages, as well as generate OpenAI API key. Please refer to <a href="https://medium.com/@iryna230520/scikit-llm-nlp-with-chatgpt-in-scikit-learn-733b92ab74b1">my latest post </a>to check how the API key can be generated.</blockquote><pre>#install the LangChain package<br>pip install langchain<br># install the OpenAI package<br>pip install openai</pre><pre># import langchain<br>import langchain<br>#import openai<br>import openai<br><br># set openai_api_key as an environment variable <br>import os<br>os.environ[&quot;OPENAI_API_KEY&quot;] = &quot;&quot;</pre><h3>Prompts</h3><p>A prompt refers to the statement or question provided to the LLM to request information. Typically, it is a short text snippet or a few sentences that convey the user’s intent or query.</p><p>The Prompts module is responsible for the creation and management of prompts.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6q8_6ZjOWb58z3hhprzwpA.png" /></figure><p>LangChain simplifies the creation of prompts by providing a PromptTemplate. This template can be thought of as a format or pattern for the prompts, with placeholders that can be filled with specific details or examples. This approach allows reusing of prompts, which becomes especially important as the prompt length increases. <br>There are two types of prompt templates: text prompt templates and chat prompt templates.</p><p><strong>Text Prompt Templates</strong> take a string text as an input.</p><pre>from langchain.prompts import PromptTemplate<br><br># create a string template with `sample_text` input variable<br>template = &quot;&quot;&quot;You will provided with the sample text. \<br>Your task is to rewrite the text to be gramatically correct. \<br>Sample text: ```{sample_text}``` \<br>Output: <br>&quot;&quot;&quot;<br># create a prompt template using above-defined template string <br>prompt_template = PromptTemplate.from_template(<br>    template=template<br>)<br># specify the `sample_text` variable<br>sample_text = &quot;Me likes cats not dogs. They jumps high so much!&quot;<br># generate a final prompt by passing `sample_text` variable<br>final_prompt = prompt_template.format(<br>    sample_text=sample_text<br>)<br>print(final_prompt)</pre><p><strong>Chat prompt templates</strong> take a list of chat messages as an input. Each chat message is associated with a role (e.g, <a href="https://platform.openai.com/docs/guides/chat/introduction">AI, Human or System</a>).</p><pre>from langchain.prompts import SystemMessagePromptTemplate, HumanMessagePromptTemplate, ChatPromptTemplate<br><br># create a string template for a System role with two input variable: `output_language` and `max_words`<br>system_template = &quot;&quot;&quot;You will provided with the sample text. \<br>Your task is to translate the text into {output_language} language \<br>and summarize the translated text in at most {max_words} words. \ <br>&quot;&quot;&quot;<br># create a prompt template for a System role<br>system_message_prompt_template = SystemMessagePromptTemplate.from_template(<br>    system_template)<br># create a string template for a System role with `sample_text` input variable<br>human_template = &quot;{sample_text}&quot;<br># create a prompt template for a Human role<br>human_message_prompt_template = HumanMessagePromptTemplate.from_template(human_template)<br># create chat prompt template out of one or several message prompt templates<br>chat_prompt_template = ChatPromptTemplate.from_messages(<br>    [system_message_prompt_template, human_message_prompt_template])<br># generate a final prompt by passing all three variables (`output_language`,  `max_words`, `sample_text`)<br>final_prompt = chat_prompt_template.format_prompt(output_language=&quot;English&quot;, max_words=15,<br>                          sample_text=&quot;Estoy deseando que llegue el fin de semana.&quot;).to_messages()<br>print(final_prompt)</pre><h3>Models</h3><p>The Models module is a core component of LangChain. It consists of two types of models: Language Models and Text Embedding Models.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/615/1*2ZVRlCJMvg6HmM_OwL8P_Q.png" /></figure><h4>Language Models</h4><p><strong>Language Models</strong> are particularly well-suited for text generation tasks. There are two variations of language models: LLMs and Chat Models. LLMs take a text string as input and output a text string as well.</p><pre>from langchain.llms import OpenAI<br><br># initialize GPT-3.5 model, remember that temperature parameter defines randomness of the response<br>llm = OpenAI(model_name=&quot;gpt-3.5-turbo&quot;, temperature=0)<br>template = &quot;&quot;&quot;You will provided with the sample text. \<br>Your task is to rewrite the text to be gramatically correct. \<br>Sample text: ```{sample_text}``` \<br>Output: <br>&quot;&quot;&quot;<br>prompt_template = PromptTemplate.from_template(<br>    template=template<br>)<br>sample_text = &quot;Me likes cats not dogs. They jumps high so much!&quot;<br>final_prompt = prompt_template.format(<br>    sample_text=sample_text<br>)<br># generate the output by calling GPT model and passing the prompt<br>completion = llm(final_prompt)<br>print(completion)</pre><h4>Chat Models</h4><p><strong>Chat Models</strong> are backed by language models. They take chat messages as input and return chat messages as output. This makes them particularly suitable for conversational AI applications.</p><pre>from langchain.chat_models import ChatOpenAI<br>from langchain.prompts import SystemMessagePromptTemplate, HumanMessagePromptTemplate, ChatPromptTemplate<br><br># initialize ChatGPT model<br>chat = ChatOpenAI(temperature=0)<br>system_template = &quot;&quot;&quot;You will provided with the sample text. \<br>Your task is to translate the text into {output_language} language \<br>and summarize the translated text in at most {max_words} words. \ <br>&quot;&quot;&quot;<br>system_message_prompt_template = SystemMessagePromptTemplate.from_template(<br>    system_template)<br>human_template = &quot;{sample_text}&quot;<br>human_message_prompt_template = HumanMessagePromptTemplate.from_template(human_template)<br>chat_prompt_template = ChatPromptTemplate.from_messages(<br>    [system_message_prompt_template, human_message_prompt_template])<br>final_prompt = chat_prompt_template.format_prompt(output_language=&quot;English&quot;, max_words=15,<br>                          sample_text=&quot;Estoy deseando que llegue el fin de semana.&quot;).to_messages()<br># generate the output by calling ChatGPT model and passing the prompt<br>completion = chat(final_prompt)<br>print(completion)</pre><h4>Text Embedding Models</h4><p>Text embedding is used to represent text data in a numerical format that can be understood and processed by ML models. It is used in many NLP tasks, such as similarity measurement and text classification.</p><p><strong>Text Embedding Models</strong> take text as input and return a list of floats. In the example below we will use a wrapper aroung OpenAI​ embedding model.</p><pre>from langchain.embeddings import OpenAIEmbeddings<br><br># initialize OpenAI embedding model<br>embeddings = OpenAIEmbeddings(model = &quot;text-embedding-ada-002&quot;)<br># create a text to be embedded<br>text = &quot;It is imperative that we work towards sustainable practices, reducing waste and conserving resources.&quot;<br># generate embedding by calling OpenAI embedding model and passing the text<br>embedded_text = embeddings.embed_query(text)<br>print(embedded_text)</pre><h3>Memory</h3><p>The Memory module in LangChain is designed to retain a concept of the <em>state</em> throughout a user’s interactions with a language model. Statelessness means that language model treats each incoming query independently. However, in certain applications like chatbots, it is crucial to remember previous interactions at both short-term and long-term levels. Passing the entire conversation as a context to the language model is not an optimal solution, as the number of tokens can quickly increase and potentially exceed the allowed limit. The Memory module facilitates the storage of conversation history, addressing these challenges.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/991/1*rzrU35NvfzbmZpWhKirBlQ.png" /></figure><p>One of the simplest memory types is ConversationBufferMemory, that simply stores messages and then extracts them in a variable.</p><pre>from langchain.memory import ConversationBufferMemory<br><br>memory = ConversationBufferMemory()<br># saving conversation<br>memory.save_context({&quot;input&quot;: &quot;Describe LSTM&quot;}, {<br>                    &quot;output&quot;: &quot;LSTM is a type of recurrent neural network architecture that is widely used for sequential and time series data processing.&quot;})<br># retrieving conversation from a memory<br>memory.load_memory_variables({})</pre><p>Apart from ConversationBufferMemory, LangChain offers various other types of Memory. Please refer to the <a href="https://python.langchain.com/en/latest/modules/memory/how_to_guides.html">LangChain documentation</a> to explore and determine which memory type best suits your specific requirements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/733/1*0MCopUF4PSnXbTnncNq_5w.png" /></figure><p>If you are not familiar with Vector Databases, I encourage you to read the excellent <a href="https://www.pinecone.io/learn/vector-database/">article from Pinecone</a> about them.</p><p>It is worth mentioning that multiple types of memory can be used simultaneously. Additionally, it is possible to save the conversation in a database for long-term storage and retrieval.</p><h3>Conclusion</h3><p>LangChain is a powerful framework that streamlines the development of AI applications. In this article, we have provided an overview of three key LangChain modules: Prompt, Model, and Memory. In the next article, we will delve into the remaining modules, expanding our understanding of the full capabilities of LangChain.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2baf5a4e1b81" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Scikit-LLM: NLP with ChatGPT in Scikit-Learn]]></title>
            <link>https://medium.com/@iryna230520/scikit-llm-nlp-with-chatgpt-in-scikit-learn-733b92ab74b1?source=rss-aaa3985be912------2</link>
            <guid isPermaLink="false">https://medium.com/p/733b92ab74b1</guid>
            <category><![CDATA[chatgpt]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[scikit-learn]]></category>
            <dc:creator><![CDATA[Iryna Kondrashchenko]]></dc:creator>
            <pubDate>Sun, 14 May 2023 13:35:00 GMT</pubDate>
            <atom:updated>2024-06-18T20:31:23.989Z</atom:updated>
            <content:encoded><![CDATA[<p>Seamlessly integrate powerful language models like ChatGPT into scikit-learn for enhanced text analysis tasks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CZGBhKnqebek2LhRyj4jCQ.png" /></figure><h3>Introduction</h3><p>Classification and labelling are common tasks in natural language processing (NLP). In traditional machine learning workflows these tasks would involve collecting labeled data, training a model, deploying it in the cloud, and making inferences. However, this process can be time-consuming, requiring separate models for each task, and not always yielding optimal results.</p><p>With recent advancements in the area of large language models, such as ChatGPT, we now have a new way to approach NLP tasks. Rather than training and deploying separate models for each task, we can use a single model to perform a wide range of NLP tasks simply by providing it with a prompt.</p><p>In this article we will explore how to build the models for multiclass and multi-label text classification using ChatGPT as a backbone. To achieve this, we will use the <a href="https://github.com/iryna-kondr/scikit-llm"><strong>scikit-LLM library</strong></a><strong>,</strong> which provides a scikit-learn compatible wrapper around OpenAI REST API. Hence, allowing to build the model in the same way as you would do with any other scikit-learn model.</p><h3>Preparing the environment</h3><p>As the first step we need to install scikit-LLM python package.</p><pre>pip install scikit-llm</pre><p>Next we need to prepare our OpenAI API keys. In order to create a key please follow these steps:</p><ol><li>Go to <a href="https://platform.openai.com/account/api-keys">OpenAI platform</a> and sign in with your account.</li><li>Click “Create New Secret Key” to generate a new key. Make sure to store the key, since as soon the window with the key closes, you will not be able to reopen it anymore.</li><li>Additionally, you will need an organization ID that can be found <a href="https://platform.openai.com/account/org-settings">here</a>.</li></ol><p>Now we can configure scikit-LLM to use the generated key.</p><pre>from skllm.config import SKLLMConfig<br>SKLLMConfig.set_openai_key(&quot;&lt;YOUR_KEY&gt;&quot;)<br>SKLLMConfig.set_openai_org(&quot;&lt;YOUR_ORGANISATION&gt;&quot;)</pre><h3>Use Case #1: Multiclass Reviews Classification</h3><p>We will have a look at a very common task: text sentiment prediction. The dataset consists of movie reviews. The possible sentiments are positive, neutral or negative. The sample of the dataset can be seen below:</p><pre>+-----------------------------------------------------------------------------+----------+<br>| Review                                                                      |  Label   |<br>+-----------------------------------------------------------------------------+----------+<br>| I was absolutely blown away by the performances in &#39;Summer&#39;s End&#39;.          |          |<br>| The acting was top-notch, and the plot had me gripped from start to finish. | Positive |<br>| A truly captivating cinematic experience that I would highly recommend.     |          |<br>+-----------------------------------------------------------------------------+----------+<br>| I was thoroughly disappointed with &#39;Silver Shadows&#39;.                        |          |<br>| The plot was confusing and the performances were lackluster.                | Negative |<br>| I wouldn&#39;t recommend wasting your time on this one.                         |          |<br>+-----------------------------------------------------------------------------+----------+<br>| &#39;The Last Frontier&#39; was simply okay.                                        |          |<br>| The plot was decent and the performances were acceptable.                   | Neutral  |<br>| However, it lacked a certain spark to make it truly memorable.              |          |<br>+-----------------------------------------------------------------------------+----------+</pre><p>We would need to initialize ZeroShotGPTClassifier<em> </em>that takes model name as a parameter. In our example we will use <em>gpt-3.5-turbo</em> model (default ChatGPT). The list of the possible models can be found <a href="https://platform.openai.com/docs/models/overview">here</a>. Afterwards, we train the classifier using fit()method and predict the labels by calling predict()method. Scikit-LLM will automatically query the OpenAI API and transform the response into a regular list of labels. Additionally, Scikit-LLM will ensure that the obtained response contains a valid label. If this is not the case, a label will be assigned randomly (with label probabilities being proportional to label occurrences in the training set).</p><blockquote>Note: as we are using zero-shot text classification, where the model does not see any prior training examples, it is crucial that the labels are expressed in natural language, are descriptive, and self-explanatory.</blockquote><pre>from skllm.models.gpt.classification.zero_shot import ZeroShotGPTClassifier<br>from skllm.datasets import get_classification_dataset<br><br># demo sentiment analysis dataset<br>X, y = get_classification_dataset() <br><br>clf = ZeroShotGPTClassifier(model = &quot;gpt-3.5-turbo&quot;)<br>clf.fit(X, y)<br>labels = clf.predict(X)</pre><p>In the example above we passed labelled training dataset to the classifier. This is done solely for making the API scikit-learn compatible. In fact, X is not used during training at all. Moreover, for y it is sufficient to provide candidate labels in an arbitrary order. Therefore, even if no labelled training data is available, the model can still be built (as shown below).</p><pre>from skllm.models.gpt.classification.zero_shot import ZeroShotGPTClassifier<br>from skllm.datasets import get_classification_dataset<br><br>X, _ = get_classification_dataset()<br><br>clf = ZeroShotGPTClassifier(model = &quot;gpt-3.5-turbo&quot;)<br>clf.fit(None, [&#39;positive&#39;, &#39;negative&#39;, &#39;neutral&#39;])<br>labels = clf.predict(X)</pre><h3>Use Case #2: Multi-Label Reviews Classification</h3><p>Another common NLP task is multi-label classification, meaning each sample might be assigned to one or several distinct classes.</p><pre>+----------------------------------------------------------------+-------------------------+<br>| Review                                                         | Labels                  |<br>+----------------------------------------------------------------+-------------------------+<br>| The food was delicious and the service was excellent.          | Food, Service           |<br>| The hotel room was clean and comfortable.                      | Accommodation           |<br>| I loved the friendly staff and the beautiful decor.            | Service, Ambiance       |<br>| The movie was entertaining but the ending was disappointing.   | Entertainment           |<br>| The product arrived on time and was of great quality.          | Delivery, Quality       |<br>| The concert was electrifying and the band was energetic.       | Entertainment, Music    |<br>| The customer support was helpful and quick.                    | Service, Support        |<br>| The book had an engaging plot and well-developed characters.   | Literature, Storytelling|<br>| The hiking trail offered breathtaking views.                   | Outdoor, Adventure      |<br>| The museum had a wide collection of art and artifacts.         | Culture, Art            |<br>+----------------------------------------------------------------+-------------------------+</pre><p>For this task we can useMultiLabelZeroShotGPTClassifier. The structure of the code remains the same with the only difference that each label in y is a list.</p><pre>from skllm.models.gpt.classification.zero_shot import MultiLabelZeroShotGPTClassifier<br>from skllm.datasets import get_multilabel_classification_dataset<br><br># demo dataset for multi-label classification<br>X, y = get_multilabel_classification_dataset()<br><br>clf = MultiLabelZeroShotGPTClassifier(max_labels=3)<br>clf.fit(X, y)<br>labels = clf.predict(X)</pre><p>Similarly to the ZeroShotGPTClassifier, it is sufficient if only candidate labels are provided. However, this time the classifier expects y of a type List[List[str]]. Since the actual structure or ordering of the labels is irrelevant, we can simply wrap our list of candidate labels into an additional outer list.</p><pre>from skllm.models.gpt.classification.zero_shot import MultiLabelZeroShotGPTClassifier<br>from skllm.datasets import get_multilabel_classification_dataset<br><br>X, _ = get_multilabel_classification_dataset()<br>candidate_labels = [<br>    &quot;Quality&quot;, <br>    &quot;Price&quot;, <br>    &quot;Delivery&quot;, <br>    &quot;Service&quot;, <br>    &quot;Product Variety&quot;, <br>    &quot;Customer Support&quot;, <br>    &quot;Packaging&quot;, <br>    &quot;User Experience&quot;, <br>    &quot;Return Policy&quot;, <br>    &quot;Product Information&quot;<br>]<br>clf = MultiLabelZeroShotGPTClassifier(max_labels=3)<br>clf.fit(None, [candidate_labels])<br>labels = clf.predict(X)</pre><h3>Conclusion</h3><p>Scikit-LLM is an easy and efficient way to build ChatGPT-based text classification models using conventional scikit-learn compatible estimators without having to manually interact with OpenAI APIs.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=733b92ab74b1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>