{"id":190,"date":"2023-10-07T06:24:36","date_gmt":"2023-10-07T00:54:36","guid":{"rendered":"https:\/\/farrukhnaveed.co\/blogs\/?p=190"},"modified":"2023-10-08T07:19:18","modified_gmt":"2023-10-08T01:49:18","slug":"fine-tuning-gpt-3s-llm-model-with-peft-on-elasticsearch-data-using-python","status":"publish","type":"post","link":"https:\/\/farrukhnaveed.co\/blogs\/fine-tuning-gpt-3s-llm-model-with-peft-on-elasticsearch-data-using-python\/","title":{"rendered":"Fine-tuning GPT-3&#8217;s LLM Model with PEFT on Elasticsearch Data using Python"},"content":{"rendered":"\n<p>OpenAI&#8217;s GPT-3, particularly its LLM (Language Model) variant, has been a game-changer in the world of natural language processing. Many companies and individual developers are leveraging its capabilities to enhance their applications, chatbots, and other services. One such enhancement is the idea of fine-tuning GPT-3 using your own custom data to make the model more tailored to specific tasks or domains.<\/p>\n\n\n\n<p>PEFT, which stands for Parallel Elastic Fine-Tuning, provides a method to fine-tune these massive models in a more efficient manner by parallelizing the training process across multiple machines and leveraging data stored in Elasticsearch. In this article, we will discuss how to get started with fine-tuning the LLM variant of GPT-3 using PEFT on Elasticsearch data with Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites:<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>GPT-3&#8217;s LLM model<\/strong>: You should have access to the model. Visit OpenAI&#8217;s official website for more details.<\/li>\n\n\n\n<li><strong>Elasticsearch<\/strong>: Have it installed and running with your data indexed.<\/li>\n\n\n\n<li><strong>Python<\/strong>: Ensure you have Python 3.6 or later installed.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Fine-tune GPT-3&#8217;s LLM using PEFT:<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Set up your Elasticsearch Cluster:<\/h3>\n\n\n\n<p>Before you can use the data stored in Elasticsearch, you need to ensure it&#8217;s correctly set up. Index your data into Elasticsearch if it&#8217;s not already there. This will be the source from which PEFT will pull the data for training.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Install Necessary Python Libraries:<\/h3>\n\n\n\n<p>Make sure to install the necessary libraries. This will typically include the OpenAI library and Elasticsearch library for Python.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"pip install openai elasticsearch\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #88C0D0\">pip<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">install<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">openai<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">elasticsearch<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">3. Connect to your Elasticsearch Cluster:<\/h3>\n\n\n\n<p>Using the Elasticsearch library for Python, establish a connection to your cluster.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"from elasticsearch import Elasticsearch\n\nes = Elasticsearch(['http:\/\/localhost:9200'])\n\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #88C0D0\">from<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">elasticsearch<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">import<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">Elasticsearch<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">es<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">Elasticsearch<\/span><span style=\"color: #ECEFF4\">([<\/span><span style=\"color: #ECEFF4\">&#39;<\/span><span style=\"color: #A3BE8C\">http:\/\/localhost:9200<\/span><span style=\"color: #ECEFF4\">&#39;<\/span><span style=\"color: #ECEFF4\">])<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Replace &#8216;localhost&#8217; and &#8216;9200&#8217; with your Elasticsearch host and port if they are different.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. Prepare your Data:<\/h3>\n\n\n\n<p>The training data should be in a specific format for GPT-3&#8217;s fine-tuning. Each data point will be a dictionary with &#8220;prompt&#8221; and &#8220;completion&#8221; keys. Fetch your data from Elasticsearch and transform it into this format.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"def fetch_data_from_es(index_name, size=10000):\n    results = es.search(index=index_name, size=size)\n    training_data = []\n    for hit in results['hits']['hits']:\n        data_point = {\n            &quot;prompt&quot;: hit['_source']['prompt'],\n            &quot;completion&quot;: hit['_source']['completion']\n        }\n        training_data.append(data_point)\n    return training_data\n\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #88C0D0\">def<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">fetch_data_from_es<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #88C0D0\">index_name,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">size=<\/span><span style=\"color: #B48EAD\">10000<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #A3BE8C\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #88C0D0\">results<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">es.search<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9\">index<\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #A3BE8C\">index_name,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">size<\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #A3BE8C\">size<\/span><span style=\"color: #D8DEE9FF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #88C0D0\">training_data<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">=<\/span><span style=\"color: #D8DEE9FF\"> []<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">for<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">hit<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">in<\/span><span style=\"color: #D8DEE9FF\"> results<\/span><span style=\"color: #ECEFF4\">[<\/span><span style=\"color: #ECEFF4\">&#39;<\/span><span style=\"color: #A3BE8C\">hits<\/span><span style=\"color: #ECEFF4\">&#39;<\/span><span style=\"color: #ECEFF4\">][<\/span><span style=\"color: #ECEFF4\">&#39;<\/span><span style=\"color: #A3BE8C\">hits<\/span><span style=\"color: #ECEFF4\">&#39;<\/span><span style=\"color: #ECEFF4\">]<\/span><span style=\"color: #D8DEE9FF\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #88C0D0\">data_point<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #88C0D0\">&quot;prompt&quot;<\/span><span style=\"color: #88C0D0\">:<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">hit[<\/span><span style=\"color: #ECEFF4\">&#39;<\/span><span style=\"color: #A3BE8C\">_source<\/span><span style=\"color: #ECEFF4\">&#39;<\/span><span style=\"color: #A3BE8C\">][<\/span><span style=\"color: #ECEFF4\">&#39;<\/span><span style=\"color: #A3BE8C\">prompt<\/span><span style=\"color: #ECEFF4\">&#39;<\/span><span style=\"color: #A3BE8C\">],<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #88C0D0\">&quot;completion&quot;<\/span><span style=\"color: #88C0D0\">:<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">hit[<\/span><span style=\"color: #ECEFF4\">&#39;<\/span><span style=\"color: #A3BE8C\">_source<\/span><span style=\"color: #ECEFF4\">&#39;<\/span><span style=\"color: #A3BE8C\">][<\/span><span style=\"color: #ECEFF4\">&#39;<\/span><span style=\"color: #A3BE8C\">completion<\/span><span style=\"color: #ECEFF4\">&#39;<\/span><span style=\"color: #A3BE8C\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #88C0D0\">training_data.append(data_point<\/span><span style=\"color: #D8DEE9FF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">return<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">training_data<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">5. Fine-Tune using PEFT:<\/h3>\n\n\n\n<p>This is a conceptual step. As of my last update, OpenAI&#8217;s official libraries did not support PEFT out of the box. But let&#8217;s assume there&#8217;s a hypothetical API to which you pass the data, and it handles the fine-tuning using PEFT.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"import openai\n\ndef fine_tune_with_peft(training_data):\n    openai.FineTuning.create(\n        model=&quot;gpt-3.5-turbo&quot;,\n        training_data=training_data,\n        fine_tuning_method=&quot;PEFT&quot;,\n        callback=&quot;http:\/\/your.callback.url&quot; # Hypothetical callback for asynchronous tasks\n    )\n\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #88C0D0\">import<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">openai<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">def<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">fine_tune_with_peft<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #88C0D0\">training_data<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #A3BE8C\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #88C0D0\">openai.FineTuning.create(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #D8DEE9\">model<\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">gpt-3.5-turbo<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #D8DEE9\">training_data<\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #A3BE8C\">training_data,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #D8DEE9\">fine_tuning_method<\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">PEFT<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #D8DEE9\">callback<\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">http:\/\/your.callback.url<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #616E88\"># Hypothetical callback for asynchronous tasks<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    )<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Here, the function would send your training data to the fine-tuning API, which would then handle the process using PEFT, potentially distributing the training task over multiple nodes if your Elasticsearch cluster spans several machines.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. Monitor and Validate:<\/h3>\n\n\n\n<p>Once the fine-tuning process starts, monitor its progress. Once complete, it&#8217;s crucial to validate the model to ensure it&#8217;s learning correctly from your data. Use a subset of your data or create a new set of prompts to test the model&#8217;s responses. Compare these to expected outputs, and iterate on your training data or fine-tuning process as necessary.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion:<\/h2>\n\n\n\n<p>Fine-tuning GPT-3, especially its LLM variant, offers the potential to make this already powerful model even more tailored to specific tasks or datasets. While PEFT provides a mechanism for efficiently conducting this fine-tuning, the integration with Elasticsearch can further streamline the process, especially for businesses that already leverage Elasticsearch for storing and managing their data.<\/p>\n\n\n\n<p>Always remember, while fine-tuning can make the model more domain-specific, it&#8217;s essential to validate and test to ensure the nuances of your specific data are captured without introducing biases or errors.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>OpenAI&#8217;s GPT-3, particularly its LLM (Language Model) variant, has been a game-changer in the world of natural language processing. Many companies and individual developers are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":191,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,20],"tags":[15,21],"class_list":["post-190","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ai","category-gpt","tag-elasticsearch","tag-llm"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Fine-tuning GPT-3&#039;s LLM Model with PEFT on Elasticsearch Data using Python - Farrukh&#039;s Tech Space<\/title>\n<meta name=\"description\" content=\"Fine-tuning GPT-3&#039;s LLM model with Parallel Elastic Fine-Tuning (PEFT) on Elasticsearch data can tailor the model to specific tasks. To start, set up and index data in Elasticsearch, then install required Python libraries. Connect to the Elasticsearch cluster and format data into &quot;prompt&quot; and &quot;completion&quot; pairs. Although direct PEFT support wasn&#039;t available in OpenAI&#039;s libraries as of the last update, the conceptual steps involve passing training data for fine-tuning. After the process, it&#039;s essential to monitor and validate the model&#039;s performance, ensuring it captures specific data nuances without introducing biases.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/farrukhnaveed.co\/blogs\/fine-tuning-gpt-3s-llm-model-with-peft-on-elasticsearch-data-using-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Fine-tuning GPT-3&#039;s LLM Model with PEFT on Elasticsearch Data using Python\" \/>\n<meta property=\"og:description\" content=\"Fine-tuning GPT-3&#039;s LLM model with Parallel Elastic Fine-Tuning (PEFT) on Elasticsearch data can tailor the model to specific tasks. To start, set up and index data in Elasticsearch, then install required Python libraries. Connect to the Elasticsearch cluster and format data into &quot;prompt&quot; and &quot;completion&quot; pairs. Although direct PEFT support wasn&#039;t available in OpenAI&#039;s libraries as of the last update, the conceptual steps involve passing training data for fine-tuning. After the process, it&#039;s essential to monitor and validate the model&#039;s performance, ensuring it captures specific data nuances without introducing biases.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/farrukhnaveed.co\/blogs\/fine-tuning-gpt-3s-llm-model-with-peft-on-elasticsearch-data-using-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Farrukh&#039;s Tech Space\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-07T00:54:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-10-08T01:49:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/farrukhnaveed.co\/blogs\/wp-content\/uploads\/2023\/10\/LLMs-with-ES-and-GPT.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1281\" \/>\n\t<meta property=\"og:image:height\" content=\"627\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Farrukh Naveed Anjum\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"Fine-tuning GPT-3&#039;s LLM Model with PEFT on Elasticsearch Data using Python\" \/>\n<meta name=\"twitter:description\" content=\"Fine-tuning GPT-3&#039;s LLM model with Parallel Elastic Fine-Tuning (PEFT) on Elasticsearch data can tailor the model to specific tasks. To start, set up and index data in Elasticsearch, then install required Python libraries. Connect to the Elasticsearch cluster and format data into &quot;prompt&quot; and &quot;completion&quot; pairs. Although direct PEFT support wasn&#039;t available in OpenAI&#039;s libraries as of the last update, the conceptual steps involve passing training data for fine-tuning. After the process, it&#039;s essential to monitor and validate the model&#039;s performance, ensuring it captures specific data nuances without introducing biases.\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/farrukhnaveed.co\/blogs\/wp-content\/uploads\/2023\/10\/LLMs-with-ES-and-GPT.png\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Farrukh Naveed Anjum\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/farrukhnaveed.co\/blogs\/fine-tuning-gpt-3s-llm-model-with-peft-on-elasticsearch-data-using-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/farrukhnaveed.co\/blogs\/fine-tuning-gpt-3s-llm-model-with-peft-on-elasticsearch-data-using-python\/\"},\"author\":{\"name\":\"Farrukh Naveed Anjum\",\"@id\":\"https:\/\/farrukhnaveed.co\/blogs\/#\/schema\/person\/ce7d07e6a917b9b73aa79007a2357d29\"},\"headline\":\"Fine-tuning GPT-3&#8217;s LLM Model with PEFT on Elasticsearch Data using Python\",\"datePublished\":\"2023-10-07T00:54:36+00:00\",\"dateModified\":\"2023-10-08T01:49:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/farrukhnaveed.co\/blogs\/fine-tuning-gpt-3s-llm-model-with-peft-on-elasticsearch-data-using-python\/\"},\"wordCount\":552,\"publisher\":{\"@id\":\"https:\/\/farrukhnaveed.co\/blogs\/#organization\"},\"keywords\":[\"Elasticsearch\",\"LLM\"],\"articleSection\":[\"AI\",\"GPT\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/farrukhnaveed.co\/blogs\/fine-tuning-gpt-3s-llm-model-with-peft-on-elasticsearch-data-using-python\/\",\"url\":\"https:\/\/farrukhnaveed.co\/blogs\/fine-tuning-gpt-3s-llm-model-with-peft-on-elasticsearch-data-using-python\/\",\"name\":\"Fine-tuning GPT-3's LLM Model with PEFT on Elasticsearch Data using Python - Farrukh&#039;s Tech Space\",\"isPartOf\":{\"@id\":\"https:\/\/farrukhnaveed.co\/blogs\/#website\"},\"datePublished\":\"2023-10-07T00:54:36+00:00\",\"dateModified\":\"2023-10-08T01:49:18+00:00\",\"description\":\"Fine-tuning GPT-3's LLM model with Parallel Elastic Fine-Tuning (PEFT) on Elasticsearch data can tailor the model to specific tasks. To start, set up and index data in Elasticsearch, then install required Python libraries. Connect to the Elasticsearch cluster and format data into \\\"prompt\\\" and \\\"completion\\\" pairs. Although direct PEFT support wasn't available in OpenAI's libraries as of the last update, the conceptual steps involve passing training data for fine-tuning. After the process, it's essential to monitor and validate the model's performance, ensuring it captures specific data nuances without introducing biases.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/farrukhnaveed.co\/blogs\/fine-tuning-gpt-3s-llm-model-with-peft-on-elasticsearch-data-using-python\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/farrukhnaveed.co\/blogs\/#website\",\"url\":\"https:\/\/farrukhnaveed.co\/blogs\/\",\"name\":\"Farrukh Naveed Anjum Blogs\",\"description\":\"Empowering Software Architects with Knowledge on Big Data and AI\",\"publisher\":{\"@id\":\"https:\/\/farrukhnaveed.co\/blogs\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/farrukhnaveed.co\/blogs\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/farrukhnaveed.co\/blogs\/#organization\",\"name\":\"Farrukh Naveed Anjum Blogs\",\"url\":\"https:\/\/farrukhnaveed.co\/blogs\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/farrukhnaveed.co\/blogs\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/farrukhnaveed.co\/blogs\/wp-content\/uploads\/2023\/06\/IMG_5018-scaled.jpg\",\"contentUrl\":\"https:\/\/farrukhnaveed.co\/blogs\/wp-content\/uploads\/2023\/06\/IMG_5018-scaled.jpg\",\"width\":1707,\"height\":2560,\"caption\":\"Farrukh Naveed Anjum Blogs\"},\"image\":{\"@id\":\"https:\/\/farrukhnaveed.co\/blogs\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/farrukhnaveed.co\/blogs\/#\/schema\/person\/ce7d07e6a917b9b73aa79007a2357d29\",\"name\":\"Farrukh Naveed Anjum\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/farrukhnaveed.co\/blogs\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/bdf1af0d569259df562434e6dc99415a377c6fc053f9e1507aa34a6522561bb8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/bdf1af0d569259df562434e6dc99415a377c6fc053f9e1507aa34a6522561bb8?s=96&d=mm&r=g\",\"caption\":\"Farrukh Naveed Anjum\"},\"description\":\"Full Stack Developer and Software Architect with 14 years of experience in various domains, including Enterprise Resource Planning, Data Retrieval, Web Scraping, Real-Time Analytics, Cybersecurity, NLP, ED-Tech, and B2B Price Comparison\",\"sameAs\":[\"https:\/\/farrukhnaveed.co\/blog\"],\"url\":\"https:\/\/farrukhnaveed.co\/blogs\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Fine-tuning GPT-3's LLM Model with PEFT on Elasticsearch Data using Python - Farrukh&#039;s Tech Space","description":"Fine-tuning GPT-3's LLM model with Parallel Elastic Fine-Tuning (PEFT) on Elasticsearch data can tailor the model to specific tasks. To start, set up and index data in Elasticsearch, then install required Python libraries. Connect to the Elasticsearch cluster and format data into \"prompt\" and \"completion\" pairs. Although direct PEFT support wasn't available in OpenAI's libraries as of the last update, the conceptual steps involve passing training data for fine-tuning. After the process, it's essential to monitor and validate the model's performance, ensuring it captures specific data nuances without introducing biases.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/farrukhnaveed.co\/blogs\/fine-tuning-gpt-3s-llm-model-with-peft-on-elasticsearch-data-using-python\/","og_locale":"en_US","og_type":"article","og_title":"Fine-tuning GPT-3's LLM Model with PEFT on Elasticsearch Data using Python","og_description":"Fine-tuning GPT-3's LLM model with Parallel Elastic Fine-Tuning (PEFT) on Elasticsearch data can tailor the model to specific tasks. To start, set up and index data in Elasticsearch, then install required Python libraries. Connect to the Elasticsearch cluster and format data into \"prompt\" and \"completion\" pairs. Although direct PEFT support wasn't available in OpenAI's libraries as of the last update, the conceptual steps involve passing training data for fine-tuning. After the process, it's essential to monitor and validate the model's performance, ensuring it captures specific data nuances without introducing biases.","og_url":"https:\/\/farrukhnaveed.co\/blogs\/fine-tuning-gpt-3s-llm-model-with-peft-on-elasticsearch-data-using-python\/","og_site_name":"Farrukh&#039;s Tech Space","article_published_time":"2023-10-07T00:54:36+00:00","article_modified_time":"2023-10-08T01:49:18+00:00","og_image":[{"width":1281,"height":627,"url":"https:\/\/farrukhnaveed.co\/blogs\/wp-content\/uploads\/2023\/10\/LLMs-with-ES-and-GPT.png","type":"image\/png"}],"author":"Farrukh Naveed Anjum","twitter_card":"summary_large_image","twitter_title":"Fine-tuning GPT-3's LLM Model with PEFT on Elasticsearch Data using Python","twitter_description":"Fine-tuning GPT-3's LLM model with Parallel Elastic Fine-Tuning (PEFT) on Elasticsearch data can tailor the model to specific tasks. To start, set up and index data in Elasticsearch, then install required Python libraries. Connect to the Elasticsearch cluster and format data into \"prompt\" and \"completion\" pairs. Although direct PEFT support wasn't available in OpenAI's libraries as of the last update, the conceptual steps involve passing training data for fine-tuning. After the process, it's essential to monitor and validate the model's performance, ensuring it captures specific data nuances without introducing biases.","twitter_image":"https:\/\/farrukhnaveed.co\/blogs\/wp-content\/uploads\/2023\/10\/LLMs-with-ES-and-GPT.png","twitter_misc":{"Written by":"Farrukh Naveed Anjum","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/farrukhnaveed.co\/blogs\/fine-tuning-gpt-3s-llm-model-with-peft-on-elasticsearch-data-using-python\/#article","isPartOf":{"@id":"https:\/\/farrukhnaveed.co\/blogs\/fine-tuning-gpt-3s-llm-model-with-peft-on-elasticsearch-data-using-python\/"},"author":{"name":"Farrukh Naveed Anjum","@id":"https:\/\/farrukhnaveed.co\/blogs\/#\/schema\/person\/ce7d07e6a917b9b73aa79007a2357d29"},"headline":"Fine-tuning GPT-3&#8217;s LLM Model with PEFT on Elasticsearch Data using Python","datePublished":"2023-10-07T00:54:36+00:00","dateModified":"2023-10-08T01:49:18+00:00","mainEntityOfPage":{"@id":"https:\/\/farrukhnaveed.co\/blogs\/fine-tuning-gpt-3s-llm-model-with-peft-on-elasticsearch-data-using-python\/"},"wordCount":552,"publisher":{"@id":"https:\/\/farrukhnaveed.co\/blogs\/#organization"},"keywords":["Elasticsearch","LLM"],"articleSection":["AI","GPT"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/farrukhnaveed.co\/blogs\/fine-tuning-gpt-3s-llm-model-with-peft-on-elasticsearch-data-using-python\/","url":"https:\/\/farrukhnaveed.co\/blogs\/fine-tuning-gpt-3s-llm-model-with-peft-on-elasticsearch-data-using-python\/","name":"Fine-tuning GPT-3's LLM Model with PEFT on Elasticsearch Data using Python - Farrukh&#039;s Tech Space","isPartOf":{"@id":"https:\/\/farrukhnaveed.co\/blogs\/#website"},"datePublished":"2023-10-07T00:54:36+00:00","dateModified":"2023-10-08T01:49:18+00:00","description":"Fine-tuning GPT-3's LLM model with Parallel Elastic Fine-Tuning (PEFT) on Elasticsearch data can tailor the model to specific tasks. To start, set up and index data in Elasticsearch, then install required Python libraries. Connect to the Elasticsearch cluster and format data into \"prompt\" and \"completion\" pairs. Although direct PEFT support wasn't available in OpenAI's libraries as of the last update, the conceptual steps involve passing training data for fine-tuning. After the process, it's essential to monitor and validate the model's performance, ensuring it captures specific data nuances without introducing biases.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/farrukhnaveed.co\/blogs\/fine-tuning-gpt-3s-llm-model-with-peft-on-elasticsearch-data-using-python\/"]}]},{"@type":"WebSite","@id":"https:\/\/farrukhnaveed.co\/blogs\/#website","url":"https:\/\/farrukhnaveed.co\/blogs\/","name":"Farrukh Naveed Anjum Blogs","description":"Empowering Software Architects with Knowledge on Big Data and AI","publisher":{"@id":"https:\/\/farrukhnaveed.co\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/farrukhnaveed.co\/blogs\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/farrukhnaveed.co\/blogs\/#organization","name":"Farrukh Naveed Anjum Blogs","url":"https:\/\/farrukhnaveed.co\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/farrukhnaveed.co\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/farrukhnaveed.co\/blogs\/wp-content\/uploads\/2023\/06\/IMG_5018-scaled.jpg","contentUrl":"https:\/\/farrukhnaveed.co\/blogs\/wp-content\/uploads\/2023\/06\/IMG_5018-scaled.jpg","width":1707,"height":2560,"caption":"Farrukh Naveed Anjum Blogs"},"image":{"@id":"https:\/\/farrukhnaveed.co\/blogs\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/farrukhnaveed.co\/blogs\/#\/schema\/person\/ce7d07e6a917b9b73aa79007a2357d29","name":"Farrukh Naveed Anjum","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/farrukhnaveed.co\/blogs\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/bdf1af0d569259df562434e6dc99415a377c6fc053f9e1507aa34a6522561bb8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/bdf1af0d569259df562434e6dc99415a377c6fc053f9e1507aa34a6522561bb8?s=96&d=mm&r=g","caption":"Farrukh Naveed Anjum"},"description":"Full Stack Developer and Software Architect with 14 years of experience in various domains, including Enterprise Resource Planning, Data Retrieval, Web Scraping, Real-Time Analytics, Cybersecurity, NLP, ED-Tech, and B2B Price Comparison","sameAs":["https:\/\/farrukhnaveed.co\/blog"],"url":"https:\/\/farrukhnaveed.co\/blogs\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/farrukhnaveed.co\/blogs\/wp-json\/wp\/v2\/posts\/190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/farrukhnaveed.co\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/farrukhnaveed.co\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/farrukhnaveed.co\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/farrukhnaveed.co\/blogs\/wp-json\/wp\/v2\/comments?post=190"}],"version-history":[{"count":1,"href":"https:\/\/farrukhnaveed.co\/blogs\/wp-json\/wp\/v2\/posts\/190\/revisions"}],"predecessor-version":[{"id":192,"href":"https:\/\/farrukhnaveed.co\/blogs\/wp-json\/wp\/v2\/posts\/190\/revisions\/192"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/farrukhnaveed.co\/blogs\/wp-json\/wp\/v2\/media\/191"}],"wp:attachment":[{"href":"https:\/\/farrukhnaveed.co\/blogs\/wp-json\/wp\/v2\/media?parent=190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/farrukhnaveed.co\/blogs\/wp-json\/wp\/v2\/categories?post=190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/farrukhnaveed.co\/blogs\/wp-json\/wp\/v2\/tags?post=190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}