<?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 Ahsan Umar on Medium]]></title>
        <description><![CDATA[Stories by Ahsan Umar on Medium]]></description>
        <link>https://medium.com/@codewithdark?source=rss-e8e49f81bfa9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*BuMG2NCZByg6srDUbDpkOw.png</url>
            <title>Stories by Ahsan Umar on Medium</title>
            <link>https://medium.com/@codewithdark?source=rss-e8e49f81bfa9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 25 May 2026 04:30:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@codewithdark/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[batch.md]]></title>
            <link>https://medium.com/@codewithdark/batch-md-69183112f567?source=rss-e8e49f81bfa9------2</link>
            <guid isPermaLink="false">https://medium.com/p/69183112f567</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[gradient-descent]]></category>
            <category><![CDATA[ml-so-good]]></category>
            <category><![CDATA[llm]]></category>
            <dc:creator><![CDATA[Ahsan Umar]]></dc:creator>
            <pubDate>Tue, 24 Dec 2024 09:32:47 GMT</pubDate>
            <atom:updated>2024-12-24T09:32:47.555Z</atom:updated>
            <content:encoded><![CDATA[<p>Gradient descent is the workhorse of modern machine learning, powering everything from simple linear regression to complex neural networks. In this comprehensive guide, we’ll dive deep into batch gradient descent, understanding how it works, its advantages and limitations, and when to use it.</p><p>Batch gradient descent is an optimization algorithm used to find the parameters (weights and biases) that minimize the cost function of a machine learning model. The term “batch” refers to the fact that it uses the entire training dataset to compute the gradient in each iteration.</p><p>At its core, batch gradient descent follows a simple yet powerful principle: iteratively adjust the parameters in the direction that reduces the cost function the most. This direction is given by the negative gradient of the cost function.</p><p>The update rule for batch gradient descent is:</p><p>$$ \theta_{j} = \theta_{j} — \alpha \frac{\partial}{\partial \theta_{j}} J(\theta) $$</p><p>Where:</p><ul><li>$\theta_{j}$ is the j-th parameter</li><li>$\alpha$ is the learning rate</li><li>$J(\theta)$ is the cost function</li><li>$\frac{\partial}{\partial \theta_{j}} J(\theta)$ is the partial derivative of the cost function with respect to $\theta_{j}$</li></ul><p>For linear regression, the cost function is typically the Mean Squared Error (MSE):</p><p>$$ J(\theta) = \frac{1}{2m} \sum_{i=1}^{m} (h_\theta(x^{(i)}) — y^{(i)})² $$</p><p>Where:</p><ul><li>$m$ is the number of training examples</li><li>$h_\theta(x)$ is the hypothesis function</li><li>$x^{(i)}$ and $y^{(i)}$ are the i-th input feature and target value respectively</li></ul><p>Let’s break down how batch gradient descent works step by step:</p><ol><li><strong>Initialization</strong>: Start with random parameter values</li><li><strong>Forward Pass</strong>: Compute predictions for all training examples</li><li><strong>Cost Calculation</strong>: Calculate the cost using all training examples</li><li><strong>Gradient Computation</strong>: Calculate the gradient of the cost function</li><li><strong>Parameter Update</strong>: Update all parameters using the computed gradient</li><li><strong>Repeat</strong>: Steps 2–5 until convergence</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*AYMouuzkuiwP72n3" /></figure><ol><li><strong>Stable Convergence</strong>: By using the entire dataset, batch gradient descent provides stable updates and a smooth convergence path.</li><li><strong>Guaranteed Convergence</strong>: For convex problems, it will always converge to the global minimum (with proper learning rate).</li><li><strong>Vectorization</strong>: Efficient implementation using matrix operations, especially on modern hardware.</li><li><strong>Memory Requirements</strong>: Needs to store the entire dataset in memory.</li><li><strong>Computational Cost</strong>: Each iteration requires processing the entire dataset.</li><li><strong>Redundancy</strong>: May perform redundant computations when data points are similar.</li><li><strong>Local Minima</strong>: Can get stuck in local minima for non-convex problems.</li></ol><p>The learning rate $\alpha$ is crucial for successful optimization. Here are some guidelines:</p><ul><li>Start with a small value (e.g., 0.01)</li><li>If convergence is too slow, increase by a factor of 10</li><li>If diverging, decrease by a factor of 10</li><li>Consider learning rate schedules for better convergence</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*n43Hdq2lJiWq4CIT" /></figure><p>Always normalize your features before applying gradient descent:</p><p>Monitor the change in cost function and stop when:</p><ul><li>The change is below a threshold</li><li>A maximum number of iterations is reached</li><li>The gradient magnitude is sufficiently small</li></ul><p>Batch gradient descent is most suitable when:</p><ol><li>Dataset fits in memory</li><li>Computing power is not a constraint</li><li>Need for stable convergence is paramount</li><li>Problem is convex or nearly convex</li></ol><p>For larger datasets or non-convex problems, consider alternatives like:</p><ul><li>Stochastic Gradient Descent (SGD)</li><li>Mini-batch Gradient Descent</li><li>Advanced optimizers (Adam, RMSprop, etc.)</li></ul><p>Batch gradient descent remains a fundamental algorithm in machine learning, providing a solid foundation for understanding more advanced optimization techniques. While it may not always be the most practical choice for modern large-scale problems, its principles form the basis for more sophisticated approaches.</p><p>Remember these key points:</p><ul><li>Always normalize your features</li><li>Choose learning rate carefully</li><li>Monitor convergence</li><li>Consider the trade-offs with other optimization methods</li></ul><p>By mastering batch gradient descent, you’ll better understand the optimization landscape of machine learning and make informed decisions about which algorithm to use for your specific problems.</p><p><em>This article is part of our Machine Learning Fundamentals series. For more in-depth tutorials and guides, follow us on Medium.</em></p><p>links:</p><p><em>Originally published at </em><a href="https://gist.github.com/codewithdark-git/beecfd065d14176ee43e8e88a4c6145a"><em>http://github.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=69183112f567" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[️ Crawl4AI: The Open-Source LLM-Friendly Web Crawler & Scraper You’ve Been Waiting For]]></title>
            <link>https://medium.com/@codewithdark/%EF%B8%8F-crawl4ai-the-open-source-llm-friendly-web-crawler-scraper-youve-been-waiting-for-2cb50c2ce6f9?source=rss-e8e49f81bfa9------2</link>
            <guid isPermaLink="false">https://medium.com/p/2cb50c2ce6f9</guid>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[crawling]]></category>
            <category><![CDATA[web-scraping]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Ahsan Umar]]></dc:creator>
            <pubDate>Sun, 15 Dec 2024 10:59:13 GMT</pubDate>
            <atom:updated>2024-12-15T10:59:13.485Z</atom:updated>
            <content:encoded><![CDATA[<p>Crawl4AI is the ultimate tool for asynchronous web crawling and data extraction, purpose-built for LLM (Large Language Model) and AI-driven applications. Whether you’re handling dynamic websites, cleaning up content, or performing advanced link and media analysis, Crawl4AI is here to simplify it all.</p><p>Let’s dive into how you can use Crawl4AI and explore its features through this comprehensive guide.</p><h3>🚀 Getting Started with Crawl4AI</h3><h4>1. Installation</h4><p>To begin, install the necessary dependencies:</p><pre>pip install -U crawl4ai<br>pip install nest_asyncio<br>playwright install</pre><p>You’re now ready to crawl the web!</p><h3>2. Basic Setup: Crawl Your First Website</h3><p>Start simple by crawling a web page and retrieving its content:</p><pre>import asyncio<br>from crawl4ai import AsyncWebCrawler, CacheMode<br>import nest_asyncio</pre><pre>nest_asyncio.apply()</pre><pre>async def simple_crawl():<br>    async with AsyncWebCrawler(verbose=True) as crawler:<br>        result = await crawler.arun(<br>            url=&quot;https://www.kidocode.com/degrees/technology&quot;,<br>            cach_mode=CacheMode.ENABLED  # Cache is enabled by default<br>        )<br>        print(result.markdown_v2.raw_markdown[:500].replace(&quot;\n&quot;, &quot; -- &quot;))  # Print the first 500 characters</pre><pre>asyncio.run(simple_crawl())</pre><p>In just a few lines of code, Crawl4AI fetches the page content in Markdown format!</p><h3>3. Handling Dynamic Websites with JavaScript</h3><p>Dynamic websites often load content using JavaScript. Crawl4AI handles such scenarios with ease:</p><pre>async def crawl_dynamic_content():<br>    async with AsyncWebCrawler(verbose=True) as crawler:<br>        js_code = [<br>            &quot;const loadMoreButton = Array.from(document.querySelectorAll(&#39;button&#39;)).find(button =&gt; button.textContent.includes(&#39;Load More&#39;)); loadMoreButton &amp;&amp; loadMoreButton.click();&quot;<br>        ]<br>        result = await crawler.arun(<br>            url=&quot;https://www.nbcnews.com/business&quot;,<br>            js_code=js_code,<br>            cach_mode=CacheMode.ENABLED<br>        )<br>        print(result.markdown_v2.raw_markdown[:500].replace(&quot;\n&quot;, &quot; -- &quot;))<br>        <br>asyncio.run(crawl_dynamic_content())</pre><p>With support for custom JavaScript execution, you can effortlessly interact with buttons, scrollbars, and more.</p><h3>4. Content Cleaning for Structured Results</h3><p>Crawl4AI offers advanced content filtering, letting you extract only what’s needed:</p><pre>from crawl4ai.content_filter_strategy import PruningContentFilter<br>from crawl4ai.markdown_generation_strategy import DefaultMarkdownGenerator</pre><pre>async def clean_content():<br>    async with AsyncWebCrawler(verbose=True) as crawler:<br>        result = await crawler.arun(<br>            url=&quot;https://en.wikipedia.org/wiki/Apple&quot;,<br>            excluded_tags=[&#39;nav&#39;, &#39;footer&#39;, &#39;aside&#39;],<br>            remove_overlay_elements=True,<br>            markdown_generator=DefaultMarkdownGenerator(<br>                content_filter=PruningContentFilter(threshold=0.48),<br>                options={&quot;ignore_links&quot;: True}<br>            )<br>        )<br>        print(f&quot;Markdown Length: {len(result.markdown_v2.raw_markdown)}&quot;)</pre><pre>asyncio.run(clean_content())</pre><p>This makes Crawl4AI perfect for AI applications requiring clean, contextually relevant data.</p><h3>5. Advanced Crawling Features</h3><h4>Link Analysis</h4><p>Want to analyze and filter links? Crawl4AI’s link analysis lets you manage both internal and external links:</p><pre>async def link_analysis():<br>    async with AsyncWebCrawler() as crawler:<br>        result = await crawler.arun(<br>            url=&quot;https://www.nbcnews.com/business&quot;,<br>            exclude_external_links=True,<br>            exclude_social_media_links=True<br>        )<br>        print(f&quot;Found {len(result.links[&#39;internal&#39;])} internal links&quot;)<br>        for link in result.links[&#39;internal&#39;][:5]:<br>            print(f&quot;Link: {link[&#39;href&#39;]}, Text: {link[&#39;text&#39;]}&quot;)<br>            <br>asyncio.run(link_analysis())</pre><h4>Media Handling</h4><p>Extract images and media data with ease:</p><pre>async def media_handling():<br>    async with AsyncWebCrawler() as crawler:<br>        result = await crawler.arun(<br>            url=&quot;https://www.nbcnews.com/business&quot;,<br>            exclude_external_images=False<br>        )<br>        for img in result.media[&#39;images&#39;][:5]:<br>            print(f&quot;Image URL: {img[&#39;src&#39;]}, Alt: {img[&#39;alt&#39;]}&quot;)<br>            <br>asyncio.run(media_handling())</pre><h3>6. Hooks for Custom Workflows</h3><p>Crawl4AI allows you to customize workflows using hooks. For example, you can modify headers before navigation:</p><pre>async def custom_hook_workflow():<br>    async with AsyncWebCrawler() as crawler:<br>        crawler.crawler_strategy.set_hook(&quot;before_goto&quot;, lambda page, _: print(&quot;[Hook] Preparing to navigate...&quot;))<br>        result = await crawler.arun(url=&quot;https://crawl4ai.com&quot;, cach_mode=CacheMode.ENABLED)<br>        print(result.markdown_v2.raw_markdown[:500].replace(&quot;\n&quot;, &quot; -- &quot;))</pre><pre>asyncio.run(custom_hook_workflow())</pre><h3>7. Multi-Page Crawling with Sessions</h3><p>Session-based crawling is ideal for navigating multi-page content while maintaining browser state:</p><pre>async def multi_page_crawling():<br>    async with AsyncWebCrawler() as crawler:<br>        for page in range(3):<br>            result = await crawler.arun(<br>                url=&quot;https://github.com/microsoft/TypeScript/commits/main&quot;,<br>                session_id=&quot;typescript_session&quot;,<br>                js_code=&quot;document.querySelector(&#39;a[data-testid=\&quot;pagination-next-button\&quot;]&#39;).click();&quot;,<br>                js_only=page &gt; 0,<br>                cache_mode=CacheMode.BYPASS<br>            )<br>            print(f&quot;Page {page + 1}: {result.success}&quot;)<br>            <br>asyncio.run(multi_page_crawling())</pre><h3>8. Structured Data Extraction</h3><p>Extract structured data using JSON schemas or even LLM-based strategies:</p><pre>from crawl4ai.extraction_strategy import JsonCssExtractionStrategy</pre><pre>async def extract_data():<br>    schema = {<br>        &quot;name&quot;: &quot;Courses&quot;,<br>        &quot;baseSelector&quot;: &quot;.course-section&quot;,<br>        &quot;fields&quot;: [<br>            {&quot;name&quot;: &quot;title&quot;, &quot;selector&quot;: &quot;h2&quot;, &quot;type&quot;: &quot;text&quot;},<br>            {&quot;name&quot;: &quot;description&quot;, &quot;selector&quot;: &quot;p&quot;, &quot;type&quot;: &quot;text&quot;}<br>        ]<br>    }<br>    async with AsyncWebCrawler() as crawler:<br>        result = await crawler.arun(<br>            url=&quot;https://www.example.com&quot;,<br>            extraction_strategy=JsonCssExtractionStrategy(schema, verbose=True)<br>        )<br>        print(result.extracted_content)<br>        <br>asyncio.run(extract_data())</pre><p>For even more advanced cases, integrate LLMs like OpenAI for schema-based data extraction.</p><h3>9. Semantic Content Extraction</h3><p>Need contextually relevant sections? Crawl4AI leverages semantic clustering for better results:</p><pre>from crawl4ai.extraction_strategy import CosineStrategy</pre><pre>async def semantic_extraction():<br>    strategy = CosineStrategy(<br>        semantic_filter=&quot;AI trends, future technology&quot;,<br>        sim_threshold=0.3,<br>        verbose=True<br>    )<br>    async with AsyncWebCrawler() as crawler:<br>        result = await crawler.arun(<br>            url=&quot;https://example.com/ai-trends&quot;,<br>            extraction_strategy=strategy<br>        )<br>        print(result.extracted_content)<br>        <br>asyncio.run(semantic_extraction())</pre><h3>10. The Future of Web Crawling</h3><p>Crawl4AI empowers developers with the tools to tackle modern web scraping challenges. Its robust features — from handling dynamic content to advanced extraction strategies — make it an essential companion for building LLM applications.</p><ul><li><strong>GitHub</strong>: <a href="https://github.com/unclecode/crawl4ai">Crawl4AI Repository</a></li><li><strong>Website</strong>: <a href="https://crawl4ai.com/">https://crawl4ai.com</a></li></ul><p>Happy crawling! 🕷️</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2cb50c2ce6f9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implementing and Comparing Polynomial Regression from Scratch in Python.]]></title>
            <link>https://medium.com/@codewithdark/implementing-and-comparing-polynomial-regression-from-scratch-in-python-a072af3e63f8?source=rss-e8e49f81bfa9------2</link>
            <guid isPermaLink="false">https://medium.com/p/a072af3e63f8</guid>
            <category><![CDATA[gradient-descent]]></category>
            <category><![CDATA[polynomial-regression]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Ahsan Umar]]></dc:creator>
            <pubDate>Fri, 13 Dec 2024 16:04:38 GMT</pubDate>
            <atom:updated>2024-12-13T16:06:50.654Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*im4GcLTV9Zom_CEa" /></figure><p>Polynomial regression extends linear regression by modeling nonlinear relationships using polynomial terms. In this comprehensive guide, we’ll implement polynomial regression from scratch, compare it with scikit-learn’s implementation, and explore optimization techniques.</p><p>Basic linear regression is expressed as:</p><p>$$y = β₀ + β₁x + ε$$</p><p>where:</p><ul><li>y is the dependent variable</li><li>x is the independent variable</li><li>β₀ is the intercept</li><li>β₁ is the slope</li><li>ε is the error term</li></ul><p>Polynomial regression extends this to:</p><p>$$y = β₀ + β₁x + β₂x² + … + βₙxⁿ + ε$$</p><p>The Mean Squared Error (MSE) cost function:</p><p>$$J(β) = \frac{1}{2m} \sum_{i=1}^m (h_β(x^{(i)}) — y^{(i)})²$$</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*E0UZoDvC9SJ9a3nh" /></figure><p>First, let’s create synthetic nonlinear data:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*hlQn38wi7rLpsf4S" /></figure><p>Our enhanced CustomPolynomialEstimator class:</p><p>Key Features:</p><ol><li><strong>Feature Scaling</strong>: Normalizes features using StandardScaler</li><li><strong>Polynomial Transformation</strong>: Creates polynomial terms up to specified degree</li><li><strong>Interaction Terms</strong>: Optional interaction terms between features</li><li><strong>Feature Selection</strong>: Variance-based feature selection</li><li><strong>Memory Optimization</strong>: Efficient matrix operations</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*CxOmkjOwTHc3W3pT" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*6d0GQh_wkx8rHo6J" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*6W2PDo6Qvk1GNwUn" /></figure><p>The gradient descent update rule:</p><p>$$β = β — α\frac{\partial}{\partial β}J(β)$$</p><p>where α is the learning rate.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*psF0B6pMwk8DROAV" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*EZ8K9VRDKRH9P9Ot" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*fjygz_TNXeetrmYf" /></figure><p>$$Variance_{threshold} = \frac{1}{n}\sum_{i=1}^n (x_i — \bar{x})²J(β) = MSE + λ\sum|β_i|$$$$J(β) = MSE + λ\sum β_i²$$</p><p>Complete implementation: <a href="https://github.com/codewithdark-git/ML-Algorithms-From-Scratch.git">GitHub Repository Link</a></p><ol><li>Cross-validation implementation</li><li>Advanced regularization techniques</li><li>Sparse matrix support</li><li>GPU acceleration</li><li><a href="https://scikit-learn.org/">Scikit-learn Documentation</a></li><li><a href="https://en.wikipedia.org/wiki/Statistical_learning_theory">Statistical Learning Theory</a></li><li><a href="https://en.wikipedia.org/wiki/Mathematical_optimization">Numerical Optimization Techniques</a></li></ol><p>Follow me on social media:<br>- [LinkedIn](<a href="https://linkedin.com/in/codewithdark">https://linkedin.com/in/codewithdark</a>)<br>- [NoteBook](<a href="https://www.kaggle.com/code/codewithdark/polynomial-regression-from-scratch">https://www.kaggle.com/code/codewithdark/polynomial-regression-from-scratch</a>)<br>- [GitHub](<a href="https://github.com/codewithdark-git">https://github.com/codewithdark-git</a>)</p><p><em>Originally published at </em><a href="https://gist.github.com/codewithdark-git/755c61a16a6ded2e855c162169bf63a4"><em>http://github.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a072af3e63f8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implementing Multiple Linear Regression from Scratch]]></title>
            <link>https://medium.com/@codewithdark/implementing-multiple-linear-regression-from-scratch-4722d8c47889?source=rss-e8e49f81bfa9------2</link>
            <guid isPermaLink="false">https://medium.com/p/4722d8c47889</guid>
            <category><![CDATA[ml-so-good]]></category>
            <category><![CDATA[linear-regression]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[numpy]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Ahsan Umar]]></dc:creator>
            <pubDate>Wed, 11 Dec 2024 16:49:06 GMT</pubDate>
            <atom:updated>2024-12-11T16:53:01.064Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*fiO9exqNdnuxT96_lRdvKg.gif" /></figure><h3>Overview</h3><p>Multiple Linear Regression (MLR) is a statistical method that models the relationship between a dependent variable and multiple independent variables. It extends simple linear regression, which uses a single feature to predict the outcome, to handle multiple features. The general form of the MLR equation is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-Z_HyjkHj0VF749jIbS6RQ.png" /></figure><h3>Approach</h3><p>We’ll implement multiple linear regression from scratch using two different methods:</p><ol><li><strong>Gradient Descent</strong>: An iterative approach to optimize the model parameters.</li><li><strong>Normal Equation</strong>: A direct mathematical solution to compute the optimal parameters.</li></ol><h3>1. Data Generation and Preprocessing</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*WVqjYMxVINH-ynYr.png" /></figure><p>To demonstrate the MLR model, we first generate synthetic data. For this example, we will create a dataset with three features, where the true relationship is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/837/1*b4121V3aO2UOaw_QBPTmuw.png" /></figure><p>The noise is added to simulate real-world data variability, making it more challenging for the model to fit the data perfectly.</p><h3>2. Building the Multiple Linear Regression Class</h3><p>We implement the model using two methods: <strong>Gradient Descent</strong> and <strong>Normal Equation</strong>.</p><h4>Gradient Descent Method</h4><p>Gradient descent is an optimization algorithm used to minimize the loss function. In the case of linear regression, the loss function is the Mean Squared Error (MSE). Gradient descent iteratively adjusts the model’s parameters to find the values that minimize the loss.</p><pre>import numpy as np<br><br>class MultipleLinearRegression:<br>    def __init__(self, learning_rate=0.01, n_iterations=1000):<br>        self.learning_rate = learning_rate<br>        self.n_iterations = n_iterations<br>        self.weights = None<br>        self.bias = None<br>        self.history = {&#39;loss&#39;: [], &#39;weights&#39;: [], &#39;bias&#39;: []}<br><br>    def predict(self, X):<br>        return np.dot(X, self.weights) + self.bias<br><br>    def fit(self, X, y):<br>        n_samples, n_features = X.shape<br>        self.weights = np.zeros(n_features)<br>        self.bias = 0<br>        for i in range(self.n_iterations):<br>            y_predicted = self.predict(X)<br><br>            # Compute gradients<br>            dw = (1/n_samples) * np.dot(X.T, (y_predicted - y))<br>            db = (1/n_samples) * np.sum(y_predicted - y)<br><br>            # Update parameters<br>            self.weights -= self.learning_rate * dw<br>            self.bias -= self.learning_rate * db<br><br>            # Track loss and parameters<br>            loss = np.mean((y_predicted - y) ** 2)<br>            self.history[&#39;loss&#39;].append(loss)<br>            self.history[&#39;weights&#39;].append(self.weights.copy())<br>            self.history[&#39;bias&#39;].append(self.bias)</pre><h4>Normal Equation Method</h4><p>The normal equation provides a closed-form solution to calculate the optimal weights directly. It avoids the need for iterative optimization but can be computationally expensive for large datasets.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7B8G6teksHPVO3j08qKG1Q.png" /></figure><pre>class MultipleLinearRegressionNormal:<br>    def __init__(self):<br>        self.weights = None<br>        self.bias = None<br>    <br>    def predict(self, X):<br>        return np.dot(X, self.weights) + self.bias<br><br>    def fit(self, X, y):<br>        # Add bias term (column of ones) to the feature matrix<br>        X_b = np.c_[np.ones((X.shape[0], 1)), X]<br><br>        # Compute the optimal weights using the normal equation<br>        betas = np.linalg.inv(X_b.T @ X_b) @ X_b.T @ y<br><br>        # Separate bias and weights<br>        self.bias = betas[0]<br>        self.weights = betas[1:</pre><h3>3. Training the Model</h3><p>After defining the models, we proceed to train them using the generated synthetic data. The dataset is split into training and testing sets, and the models are fitted to the training data.</p><pre># Generate synthetic data<br>X, y = generate_data()<br>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)<br><br># Scale the features (important for gradient descent)<br>scaler = StandardScaler()<br>X_train_scaled = scaler.fit_transform(X_train)<br>X_test_scaled = scaler.transform(X_test)<br># Train Gradient Descent Model<br>gd_model = MultipleLinearRegression(learning_rate=0.06, n_iterations=700)<br>gd_model.fit(X_train_scaled, y_train)<br># Train Normal Equation Model<br>normal_model = MultipleLinearRegressionNormal()<br>normal_model.fit(X_train_scaled, y_train)</pre><h3>4. Evaluating the Model</h3><p>After training, we evaluate both models using the <strong>R² score</strong>, which measures the proportion of variance explained by the model. A higher R² score indicates a better model fit.</p><p>The <strong>R² (R-squared)</strong>, or <strong>coefficient of determination</strong>, is a statistical measure that indicates how well the independent variables explain the variance in the dependent variable. The formula for R² is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QnqCTp_JIILSDaq5EGoP3g.png" /></figure><pre>from sklearn.metrics import r2_score<br><br># Evaluate the models<br>gd_predictions = gd_model.predict(X_test_scaled)<br>normal_predictions = normal_model.predict(X_test_scaled)<br><br>print(&quot;Gradient Descent Model R² Score:&quot;, r2_score(y_test, gd_predictions))<br>print(&quot;Normal Equation Model R² Score:&quot;, r2_score(y_test, normal_predictions))</pre><h3>5. Visualization</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Q2slDwK6yZ9u1w_V.gif" /></figure><p>Visualizing the training process can help us understand the model’s convergence. We plot the loss progression over iterations and track how the weights change during training.</p><pre>import matplotlib.pyplot as plt<br><br># Plot loss progression<br>plt.figure(figsize=(10, 5))<br>plt.subplot(1, 2, 1)<br>plt.plot(gd_model.history[&#39;loss&#39;])<br>plt.title(&#39;Loss Progression&#39;)<br>plt.xlabel(&#39;Iterations&#39;)<br>plt.ylabel(&#39;Mean Squared Error&#39;)<br># Plot weight changes<br>plt.subplot(1, 2, 2)<br>weights_history = np.array(gd_model.history[&#39;weights&#39;])<br>for i in range(weights_history.shape[1]):<br>    plt.plot(weights_history[:, i], label=f&#39;Weight {i+1}&#39;)<br>plt.title(&#39;Weight Progression&#39;)<br>plt.xlabel(&#39;Iterations&#39;)<br>plt.ylabel(&#39;Weight Value&#39;)<br>plt.legend()<br>plt.tight_layout()<br>plt.show()</pre><h3>Complete Example Code</h3><p>Here’s the complete code combining all steps:</p><pre>import numpy as np<br>import matplotlib.pyplot as plt<br>from sklearn.model_selection import train_test_split<br>from sklearn.metrics import r2_score<br>from sklearn.preprocessing import StandardScaler</pre><h4>Generate synthetic data</h4><pre># Set random seed for reproducibility<br>np.random.seed(42)<br><br>def generate_data(n_samples=100, n_features=3):<br>    X = np.random.randn(n_samples, n_features)<br>    true_coefficients = np.array([3, 1.5, -2])<br>    noise = np.random.normal(0, 0.5, n_samples)<br>    y = 2 + np.dot(X, true_coefficients) + noise<br>    return X, y</pre><h4>Generate and prepare data</h4><pre>X, y = generate_data()<br>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)</pre><h4>Scale the features</h4><pre>scaler = StandardScaler()<br>X_train_scaled = scaler.fit_transform(X_train)<br>X_test_scaled = scaler.transform(X_test)</pre><h4>Train Gradient Descent Model</h4><pre>gd_model = MultipleLinearRegression(learning_rate=0.06, n_iterations=700)<br>gd_model.fit(X_train_scaled, y_train)</pre><h4>Train Normal Equation Model</h4><pre><br>normal_model = MultipleLinearRegressionNormal()<br>normal_model.fit(X_train_scaled, y_train)</pre><h4>Evaluate models</h4><pre><br>gd_predictions = gd_model.predict(X_test_scaled)<br>normal_predictions = normal_model.predict(X_test_scaled)<br>print(&quot;Gradient Descent Model R² Score:&quot;, r2_score(y_test, gd_predictions))<br>print(&quot;Normal Equation Model R² Score:&quot;, r2_score(y_test, normal_predictions))</pre><h4>Visualization</h4><pre>plt.figure(figsize=(12, 5))<br>plt.subplot(1, 2, 1)<br>plt.plot(gd_model.history[&#39;loss&#39;])<br>plt.title(&#39;Loss Progression&#39;)<br>plt.xlabel(&#39;Iterations&#39;)<br>plt.ylabel(&#39;Mean Squared Error&#39;)<br>plt.subplot(1, 2, 2)<br>weights_history = np.array(gd_model.history[&#39;weights&#39;])<br>for i in range(weights_history.shape[1]):<br>    plt.plot(weights_history[:, i], label=f&#39;Weight {i+1}&#39;)<br>plt.title(&#39;Weight Progression&#39;)<br>plt.xlabel(&#39;Iterations&#39;)<br>plt.ylabel(&#39;Weight Value&#39;)<br>plt.legend()<br>plt.tight_layout()<br>plt.show()</pre><h3>Conclusion</h3><p>By implementing multiple linear regression from scratch, we gain a deeper understanding of the underlying mechanics of regression models. The two methods — <strong>Gradient Descent</strong> and <strong>Normal Equation</strong> — offer different trade-offs in terms of complexity and computational efficiency. Gradient Descent is more flexible and scales well with large datasets, while the Normal Equation provides a direct solution, making it faster for smaller datasets.</p><h3>Key Takeaways:</h3><ul><li>Multiple Linear Regression models the relationship between multiple features and a target variable.</li><li>Gradient Descent and Normal Equation are two common methods for solving MLR problems.</li><li>Feature scaling is important for</li><li>Gradient Descent to converge efficiently.</li><li>Evaluating the model using metrics like R² helps assess its performance.</li></ul><h3>Potential Improvements:</h3><ul><li>Implement regularization (e.g., Lasso, Ridge) to prevent overfitting.</li><li>Use cross-validation to get a better estimate of model performance.</li><li>Explore more advanced optimization algorithms (e.g., Stochastic Gradient Descent).</li></ul><p>If you’re looking to dive deeper into linear regression and machine learning, here are some great resources:</p><ul><li><a href="https://www.kaggle.com/code/codewithdark/notebook5bcf21db04">Kaggle: Explore the Notebook</a></li><li><a href="https://www.linkedin.com/in/codewithdark">LinkedIn: Connect with me</a></li><li><a href="https://github.com/codewithdark-git/ML-Algorithms-From-Scratch.git">GitHub: Explore Repo</a></li><li><a href="https://scikit-learn.org/stable/modules/linear_model.html#ordinary-least-squares">scikit-learn Documentation on Linear Regression</a></li></ul><p>Feel free to reach out with any questions or thoughts. Happy learning and coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4722d8c47889" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building an Intelligent Search Agent with LangChain]]></title>
            <link>https://medium.com/@codewithdark/building-an-intelligent-search-agent-with-langchain-e6a5d71f16d3?source=rss-e8e49f81bfa9------2</link>
            <guid isPermaLink="false">https://medium.com/p/e6a5d71f16d3</guid>
            <category><![CDATA[agents]]></category>
            <category><![CDATA[groq]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[langchain]]></category>
            <category><![CDATA[genai]]></category>
            <dc:creator><![CDATA[Ahsan Umar]]></dc:creator>
            <pubDate>Thu, 05 Dec 2024 03:00:57 GMT</pubDate>
            <atom:updated>2024-12-05T03:00:57.182Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VAqBET8aMfTZ66H0My362w.png" /><figcaption>Search Agent with LangChain</figcaption></figure><h4><strong>Introduction</strong></h4><p>In this blog post, we’ll explore how to create a powerful search agent using LangChain. This agent can perform web searches and provide informative responses to our queries. We’ll use the DuckDuckGo search API and OpenAI’s language model to create an intelligent agent to understand questions and provide relevant answers.</p><p><strong>Prerequisites</strong></p><p>Before we begin, ensure you have the following:</p><ul><li>Python 3.8+</li><li>LangChain installed (pip install langchain)</li><li>GROQ API key (or another compatible language model)</li><li>Basic understanding of Python and AI concepts</li></ul><h4>1. Setting Up the Environment</h4><p>First, let’s set up our project and install the necessary dependencies:</p><pre>%pip install python-dotenv langchain langchain-community langchain_groq</pre><h4>2. Importing Required Libraries</h4><pre>import os<br>from dotenv import load_dotenv<br>from langchain.agents import Tool, AgentExecutor, create_react_agent<br>from langchain.tools import DuckDuckGoSearchRun<br>from langchain_groq import ChatGroq<br>from langchain.prompts import PromptTemplate<br>from langchain.memory import ConversationBufferMemory<br><br># Load environment variables<br>load_dotenv()</pre><h4><strong>2. Creating the Search Tool</strong></h4><p>We’ll create a search tool using DuckDuckGo’s search API. This will allow our agent to search the internet for information.</p><pre># Initialize the search tool<br>search = DuckDuckGoSearchRun()<br><br>tools = [<br>    Tool(<br>        name=&quot;Search&quot;,<br>        func=search.run,<br>        description=&quot;Useful for searching information on the internet. Use this when you need to find current or factual information.&quot;<br>    )<br>]</pre><h4><strong>3. Setting Up the Language Model</strong></h4><p>We will use a Large Language Model (OpenAI, Claude, Gemini) as the brain of our agent. This will aid in processing and understanding user queries and search results. In this app, we utilized the Llama model provided by Groq.</p><pre># Initialize the language model<br>llm = ChatGroq(<br>    temperature=0.7,<br>    model=&quot;llama3-8b-8192&quot;,<br>    api_key=os.getenv(&quot;Groq_API_KEY&quot;),<br>)</pre><h4><strong>4. Creating the Agent</strong></h4><p>Now we’ll create our agent by combining the search tool with the language model.</p><pre># Define the prompt template for the agent<br>template = &quot;&quot;&quot;Answer the following questions as best you can. You have access to the following tools:<br><br>{tools}<br><br>Use the following format:<br><br>Question: the input question you must answer<br>Thought: you should always think about what to do<br>Action: the action to take, should be one of [{tool_names}]<br>Action Input: the input to the action<br>Observation: the result of the action<br>... (this Thought/Action/Action Input/Observation can repeat N times)<br>Thought: I now know the final answer<br>Final Answer: the final answer to the original input question<br><br>Begin!<br><br>Question: {input}<br>Thought: {agent_scratchpad}&quot;&quot;&quot;<br><br># Create the agent<br>agent = create_react_agent(<br>    llm=llm,<br>    tools=tools,<br>    prompt=PromptTemplate.from_template(template)<br>)<br><br># Create the agent executor<br>agent_executor = AgentExecutor.from_agent_and_tools(<br>    agent=agent,<br>    tools=tools,<br>    verbose=True,<br>    memory=ConversationBufferMemory(memory_key=&quot;chat_history&quot;)<br>)</pre><h4><strong>5. Testing the Search Agent</strong></h4><p>Let’s test our agent with some example queries to see how it performs.</p><pre>def ask_question(query):<br>    &quot;&quot;&quot;<br>    Function to ask a question to the agent and get a response<br>    &quot;&quot;&quot;<br>    try:<br>        response = agent_executor.invoke({&quot;input&quot;: query})<br>        return response.get(&quot;output&quot;, &quot;Sorry, I couldn&#39;t generate a response.&quot;)<br>    except Exception as e:<br>        return f&quot;An error occurred: {str(e)}&quot;</pre><p><em>Test the agent with a query</em></p><pre>query = &quot;What are the latest developments in quantum computing?&quot;<br>print(ask_question(query))</pre><blockquote><strong>Understanding How It Works</strong></blockquote><p>Our search agent works through several key components:</p><p>1. <strong>Tools</strong>: The DuckDuckGo search tool allows the agent to search the internet for information.</p><p>2. <strong>Language Model</strong>: The ChatOpenAI model processes queries and search results intelligently.</p><p>3. <strong>Agent</strong>: The ReAct agent follows a thought process of:</p><ul><li>Understanding the question</li><li>Deciding what information is needed</li><li>Using tools to gather information</li><li>Formulating a response</li></ul><p>4. <strong>Memory</strong>: The ConversationBufferMemory allows the agent to maintain context across multiple interactions.</p><blockquote><strong>Best Practices and Tips</strong></blockquote><p>1. <strong>API Key Security</strong>: Always use environment variables for API keys</p><p>2. <strong>Temperature Setting</strong>: Adjust the temperature parameter to control response creativity</p><p>3. <strong>Error Handling</strong>: Implement proper error handling for API calls</p><p>4. <strong>Rate Limiting</strong>: Be mindful of API rate limits</p><blockquote><strong>Conclusion</strong></blockquote><p>We’ve successfully built a powerful search agent using LangChain that can:</p><ul><li>Perform web searches</li><li>Process and understand search results</li><li>Provide informative responses</li><li>Maintain conversation context</li></ul><p>GitHub:: <a href="https://shorturl.at/ofO3O">https://shorturl.at/ofO3O</a></p><p>LinkedIn:: <a href="https://shorturl.at/ZmxDv">https://shorturl.at/ZmxDv</a></p><p>Notebook:: <a href="https://shorturl.at/1ATxC">https://shorturl.at/1ATxC</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e6a5d71f16d3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Linear Regression: A Visual Journey from Theory to Implementation]]></title>
            <link>https://medium.com/@codewithdark/mastering-linear-regression-a-visual-journey-from-theory-to-implementation-2640e4d8c25f?source=rss-e8e49f81bfa9------2</link>
            <guid isPermaLink="false">https://medium.com/p/2640e4d8c25f</guid>
            <category><![CDATA[ml-so-good]]></category>
            <category><![CDATA[regression]]></category>
            <category><![CDATA[mlops]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[linear-regression]]></category>
            <dc:creator><![CDATA[Ahsan Umar]]></dc:creator>
            <pubDate>Tue, 03 Dec 2024 12:34:04 GMT</pubDate>
            <atom:updated>2024-12-03T12:34:04.304Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*1PRWnCWO8M05-fmj5C0jFA.gif" /><figcaption>Data</figcaption></figure><p>Linear Regression is a fundamental algorithm in machine learning that serves as a stepping stone to understanding more complex models. In this comprehensive guide, we’ll explore the algorithm through interactive visualizations and practical implementation.</p><p><strong>1. Understanding Linear Regression</strong></p><p><strong>The Core Concept</strong></p><p>Linear Regression models the relationship between variables by fitting a linear equation to observed data. In its simplest form:</p><pre>y = wx + b</pre><p>Where:</p><ul><li>y is the predicted value (dependent variable)</li><li>x is the input feature (independent variable)</li><li>w is the weight (slope)</li><li>b is the bias (y-intercept)</li></ul><p><strong>The Learning Process</strong></p><p>The model learns by minimizing the Mean Squared Error (MSE):</p><pre>MSE = (1/n) * Σ(y_true - y_predicted)²</pre><p><strong>2. Implementation from Scratch</strong></p><p>Let’s implement Linear Regression using NumPy. Our implementation includes:</p><pre>class LinearRegression:<br>    def __init__(self, learning_rate=0.01, n_iterations=1000):<br>        self.learning_rate = learning_rate<br>        self.n_iterations = n_iterations<br>        self.weights = None<br>        self.bias = None<br>        self.history = {&#39;loss&#39;: [], &#39;weights&#39;: [], &#39;bias&#39;: []}<br>    <br>    def fit(self, X, y):<br>        # Initialize parameters<br>        n_samples, n_features = X.shape<br>        self.weights = np.zeros(n_features)<br>        self.bias = 0<br>        <br>        # Gradient descent<br>        for _ in range(self.n_iterations):<br>            # Forward pass<br>            y_predicted = np.dot(X, self.weights) + self.bias<br>            <br>            # Compute gradients<br>            dw = (1/n_samples) * np.dot(X.T, (y_predicted - y))<br>            db = (1/n_samples) * np.sum(y_predicted - y)<br>            <br>            # Update parameters<br>            self.weights -= self.learning_rate * dw<br>            self.bias -= self.learning_rate * db<br>            <br>            # Track history<br>            loss = self._compute_loss(y, y_predicted)<br>            self.history[&#39;loss&#39;].append(loss)</pre><p><strong>Visualizing the Learning Process</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*fiO9exqNdnuxT96_lRdvKg.gif" /><figcaption><strong>Learning Process</strong></figcaption></figure><p>The animation above shows how our model iteratively learns to fit the data. Watch as the red line (our model’s predictions) adjusts to better fit the blue data points.</p><p><strong>Training Progress</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*I1eWDrn2xUYCMPyKRsKKFg.png" /><figcaption>Loss History</figcaption></figure><p>The loss history plot demonstrates how our model’s error decreases over time, indicating successful learning.</p><p><strong>Parameter Evolution</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ve-9rglhPyufZb5oCoStMQ.png" /><figcaption><strong>Parameter Evolution</strong></figcaption></figure><p>This visualization shows how our model’s parameters (weight and bias) converge to their optimal values. The dashed red lines indicate the true parameters used to generate our synthetic data.</p><p><strong>Final Model Performance</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MgL5XPznVNn7JHd85GIThA.png" /><figcaption><strong>Final Model Performance</strong></figcaption></figure><p>The final model achieves excellent fit to the data, with predictions (red line) closely following the underlying pattern in the data points (blue dots).</p><p><strong>3. Key Implementation Details</strong></p><p><strong>Gradient Descent</strong></p><p>The model uses gradient descent to minimize the loss function:</p><p><strong>1. Calculate predictions: `y_pred = X * w + b`</strong></p><p><strong>2. Compute gradients:</strong></p><pre>dw = (1/n) * X.T * (y_pred - y)<br>db = (1/n) * sum(y_pred - y)</pre><p><strong>3. Update parameters:</strong></p><pre>w = w - learning_rate * dw<br>b = b - learning_rate * db</pre><p><strong>Hyperparameters</strong></p><ul><li>Learning Rate: Controls step size during optimization</li><li>Number of Iterations: Determines training duration</li><li>Initial Parameters: Starting points for weights and bias</li></ul><p><strong>4. Best Practices and Tips</strong></p><p>1. <strong>Data Preprocessing.</strong></p><ul><li>Scale features to similar ranges</li><li>Remove outliers</li><li>Handle missing values</li></ul><p>2. <strong>Model Training.</strong></p><ul><li>Start with small learning rates</li><li>Monitor loss for convergence</li><li>Use early stopping if needed</li></ul><p>3. <strong>Evaluation.</strong></p><ul><li>Split data into train/test sets</li><li>Use multiple metrics (MSE, R², MAE)</li><li>Validate assumptions</li></ul><p><strong>6. Advanced Topics</strong></p><p>1. <strong>Regularization.</strong></p><ul><li>L1 (Lasso)</li><li>L2 (Ridge)</li><li>Elastic Net</li></ul><p>2. <strong>Extensions.</strong></p><ul><li>Polynomial Regression</li><li>Multiple Linear Regression</li><li>Weighted Linear Regression</li></ul><p><strong>Conclusion</strong></p><p>Linear Regression, despite its simplicity, provides:</p><ol><li>Strong foundation for machine learning concepts</li><li>Practical utility in many real-world applications</li><li>Insights into model training and optimization</li></ol><p>The visual approach we’ve taken helps us understand:</p><ul><li>How the model learns over time</li><li>The role of different parameters</li><li>The importance of proper training</li></ul><p><strong>Resources</strong></p><p><a href="https://github.com/your-username/ML-Algorithms-From-Scratch">https://github.com/codewithdark-git/ML-Algorithms-From-Scratch</a></p><p><a href="https://www.kaggle.com/code/codewithdark/linear-regression-implementation-from-scratch">https://www.kaggle.com/code/codewithdark/linear-regression-implementation-from-scratch</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2640e4d8c25f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DocsGPT: A Technical Deep Dive into Modern Documentation Search]]></title>
            <link>https://medium.com/@codewithdark/docsgpt-a-technical-deep-dive-into-modern-documentation-search-30810d7593e6?source=rss-e8e49f81bfa9------2</link>
            <guid isPermaLink="false">https://medium.com/p/30810d7593e6</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[genai]]></category>
            <category><![CDATA[gpt]]></category>
            <category><![CDATA[langchain]]></category>
            <dc:creator><![CDATA[Ahsan Umar]]></dc:creator>
            <pubDate>Mon, 02 Dec 2024 15:35:47 GMT</pubDate>
            <atom:updated>2024-12-02T15:35:47.157Z</atom:updated>
            <content:encoded><![CDATA[<h4><strong>System Architecture Overview</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fHYXT9c7tMaNDivkBudKTg.png" /><figcaption><strong>System Architecture Overview</strong></figcaption></figure><p><strong>Technical Architecture</strong></p><p>DocsGPT is built on a sophisticated stack that combines vector search, large language models, and modern web technologies. Let’s break down each component and understand how they work together.</p><p><strong>Core Components:</strong></p><p><strong>Vector Search Engine.</strong></p><pre># Key technologies:<br>- FAISS (Facebook AI Similarity Search)<br>- HuggingFace Embeddings<br>- Groq LLM Integration</pre><p><strong>Content Processing Pipeline.</strong></p><pre># Features:<br>- Recursive text splitting<br>- Metadata enrichment<br>- Chunk optimization<br>- Vector store management</pre><p><strong>Search Orchestration.</strong></p><pre># Capabilities:<br>- Multi-source retrieval<br>- Context-aware querying<br>- Response enhancement<br>- Source attribution</pre><h4><strong>Technical Implementation Details</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BekKjzdyKPrZ8S0FA0h-wg.png" /><figcaption><strong>Vector Store Implementation</strong></figcaption></figure><p><strong>1. Vector Store Implementation</strong></p><p>The system uses FAISS for efficient similarity search:</p><pre>class ContentIngestionPipeline:<br>    def __init__(self):<br>        self.vector_store = None<br>        self.embeddings = HuggingFaceEmbeddings()<br>        self._load_vector_store()</pre><p>Key features:</p><ul><li>Persistent vector storage</li><li>Optimized similarity search</li><li>Configurable search parameters</li><li>Automatic store management</li></ul><p><strong>2. Content Processing</strong></p><p>The content processing pipeline is sophisticated:</p><pre>splitter = RecursiveCharacterTextSplitter(<br>    chunk_size=1000,<br>    chunk_overlap=200,<br>    length_function=len,<br>    separators=[&quot;\n\n&quot;, &quot;\n&quot;, &quot;. &quot;, &quot; &quot;, &quot;&quot;]<br>)</pre><p>Processing steps:</p><ol><li>URL content extraction</li><li>Text chunking with overlap</li><li>Metadata enrichment</li><li>Quality filtering</li><li>Vector embedding generation</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/809/1*BB0AwjI4FUwn6HueSnhwCg.png" /><figcaption><strong>Search Orchestration</strong></figcaption></figure><p><strong>3. Search Orchestration</strong></p><p>The search flow includes:</p><pre>async def search(self, query: str, k: int = 3):<br>    # 1. Vector DB check<br>    # 2. Google search fallback<br>    # 3. Content processing<br>    # 4. AI enhancement</pre><p>Features:</p><ul><li>Asynchronous processing</li><li>Multi-stage search pipeline</li><li>Error handling and logging</li><li>Result refinement</li></ul><p><strong>4. AI Enhancement</strong></p><p>The system uses Groq for response generation:</p><pre>self.llm = ChatGroq(<br>    api_key=os.getenv(&#39;GROQ_API_KEY&#39;),<br>    model_name=os.getenv(&#39;MODEL_NAME&#39;),<br>    max_tokens=2000,<br>    temperature=0.3<br>)</pre><p>Capabilities:</p><ul><li>Context-aware responses</li><li>Temperature-controlled output</li><li>Token optimization</li><li>Source attribution</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6pvi-k4Y428_mUhX8N-Xzg.png" /><figcaption><strong>Performance Optimizations.</strong></figcaption></figure><p><strong>Performance Optimizations.</strong></p><ol><li><strong>Vector Search.</strong></li></ol><ul><li>Similarity score thresholding</li><li>Configurable k-nearest neighbors</li><li>Fetch optimization</li></ul><pre>search_kwargs={<br>    &quot;k&quot;: 3,<br>    &quot;score_threshold&quot;: 0.5,<br>    &quot;fetch_k&quot;: 10<br>}</pre><p>2. <strong>Content Processing.</strong></p><ul><li>Chunk size optimization</li><li>Overlap management</li><li>Small chunk filtering</li><li>Metadata enrichment</li></ul><p>3. <strong>Response Generation.</strong></p><ul><li>Async processing</li><li>Error handling</li><li>Logging integration</li><li>Result caching</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O73WIxAWvHmPpjYmtuLuzg.png" /><figcaption><em>roadmap visualization</em></figcaption></figure><h4><strong>Future Technical Enhancements:</strong></h4><ol><li><strong>Scalability Improvements.</strong></li></ol><ul><li>Distributed vector storage</li><li>Load balancing</li><li>Caching optimization</li></ul><p>2. <strong>Search Enhancement.</strong></p><ul><li>Cross-language support</li><li>Semantic search improvements</li><li>Custom embedding models</li></ul><p>3. <strong>Content Processing.</strong></p><ul><li>Advanced text extraction</li><li>Multi-format support</li><li>Real-time updates</li></ul><h4><strong>Conclusion</strong></h4><p>DocsGPT represents a sophisticated implementation of modern search technologies, combining vector search, large language models, and efficient content processing. Its modular architecture and thoughtful optimizations make it a powerful tool for documentation search and retrieval.</p><p>The system’s technical design prioritizes:</p><ul><li>Search accuracy</li><li>Processing efficiency</li><li>Response quality</li><li>Scalability</li><li>Maintainability</li></ul><p>This technical implementation ensures that DocsGPT can handle complex documentation queries while providing accurate, context-aware responses in a timely manner.</p><p><a href="https://github.com/codewithdark-git/DocsGPT.git">https://github.com/codewithdark-git/DocsGPT.git</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=30810d7593e6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>