<?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 Lakshan Wijesekara on Medium]]></title>
        <description><![CDATA[Stories by Lakshan Wijesekara on Medium]]></description>
        <link>https://medium.com/@lwijesekara149?source=rss-3f14c104cc80------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*c9QTvAJm2mw29Prx.jpg</url>
            <title>Stories by Lakshan Wijesekara on Medium</title>
            <link>https://medium.com/@lwijesekara149?source=rss-3f14c104cc80------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 16:24:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@lwijesekara149/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[How I Created an AI Chatbot and Trained on Application Data for Free]]></title>
            <link>https://medium.com/@lwijesekara149/how-i-created-an-ai-chatbot-and-trained-on-application-data-for-free-7e1cd1cc753d?source=rss-3f14c104cc80------2</link>
            <guid isPermaLink="false">https://medium.com/p/7e1cd1cc753d</guid>
            <category><![CDATA[free-chatbot-for-website]]></category>
            <category><![CDATA[chatbot-with-ai]]></category>
            <dc:creator><![CDATA[Lakshan Wijesekara]]></dc:creator>
            <pubDate>Wed, 11 Jun 2025 11:30:39 GMT</pubDate>
            <atom:updated>2025-06-11T11:35:57.848Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently I needed to create a feature for users to chat with an AI assistant and get needed assistance and automate the process.</p><p>I found a tutorial to implement an AI chatbot using OpenAI, but it was a hassle due to their credit limits. Following is the solution I’ve found to implement an AI chatbot with industry leading LLM for zero cost.</p><p><strong>Step 01: Visit OpenRouter.ai to access a free LLM and create an API key</strong></p><p>First you need to head on to <a href="https://openrouter.ai/">OpenRouter</a> site and create an API key for your project. <strong>Make sure to copy the key to somewhere safe</strong>, because you can’t find it again and the site will not show the full key again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zxR6jyWUXMGEwrDWFinC2g.png" /></figure><p>To access a free LLM, you need to turn down the prompt pricing to free and find models which have (free) next to their name.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z9tWPAj3hWHSIoiCltRpJw.png" /></figure><p>You can choose any model you need from here, and our work here is almost finished. Final step for you to do is the following,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1011/1*BGhbrcH3nE-AvxXvIVzq-A.png" /></figure><p>Go inside your preferred model and copy the highlighted name for later use. Now you’re all done with the setup in this site.</p><p><strong>Note: The free versions come with a limit of 20 requests per minute (Pretty usable amount)</strong></p><p><strong>Step 02: Configure your API Key and the model in your class</strong></p><p>Now, we need to head back into our python code to specify the API key and the model we chose. You can specify this as a global variable or inside the method as you want. For security reasons, keep your API keys in a separate env file.</p><pre># API key of Open router<br>OPENROUTER_API_KEY = os.getenv(&quot;OPEN_ROUTER_API_KEY&quot;)<br># Open router AI model<br>OPENROUTER_MODEL = os.getenv(&quot;OPEN_ROUTER_MODEL&quot;)</pre><p><strong>Step 03: Create a PDF file with all the training data needed for the model</strong></p><p>We can use the LLM for general queries if we’re not training the model, but we need to train this model to answer specific questions regarding our application.</p><p>To do that, I have created a PDF file with training data and extracted it to pass onto the LLM. You can extract a website even if you don’t want to use a PDF to train your model.</p><p>You can create an editable training file with the help of Chat GPT or any other AI you can use. Then create the training pdf.</p><p>The code uses fitz from PyMuPDF library to extract text page by page, you may have to install the PyMuPDF library to import the fitz module.</p><pre>import fitz<br><br>def extract_text_from_pdf(self):<br>        try:<br>            pdf_path = &quot;./Chatbot_Training_Data_Property_Management.pdf&quot;<br>            doc = fitz.open(pdf_path)<br>            pdf_text = &quot;&quot;<br>            for page_num in range(doc.page_count):<br>                page = doc.load_page(page_num)<br>                pdf_text += page.get_text(&quot;text&quot;)<br>            doc.close()<br>            return pdf_text<br><br>        except Exception as e:<br>            return f&quot;Error extracting text: {e}&quot;</pre><p>After running the code, it will return the pdf_text, then we will write it to a text file as shown below,</p><pre>extracted_text = extract_text_from_pdf()<br>file = open(&quot;training_data.txt&quot;, &quot;w&quot;, encoding=&#39;utf-8&#39;)<br>file.write(extracted_text)</pre><p><strong>Step 04: Define the logic to generate responses from the AI with the extracted training data.</strong></p><p>Now, the interesting part, let’s get some responses from the model we’re using.</p><p>Let’s create an endpoint to receive the user message and run the logic.</p><pre>@cross_origin(supports_credentials=True)<br>@user_api_blueprint.route(&#39;/users/chat-ai&#39;, methods=[&#39;POST&#39;, &#39;OPTIONS&#39;])<br>def chat_with_ai():<br>    if request.method == &#39;OPTIONS&#39;:<br>#Respond to preflight request<br>        response = make_response()<br>        return response, 204<br>    try:<br>        user_message = request.json.get(&quot;message&quot;)<br>        headers = {<br>            &quot;Authorization&quot;: f&quot;Bearer {OPENROUTER_API_KEY}&quot;, #Key we defined before<br>            &quot;HTTP-Referer&quot;: &quot;http://localhost:4200&quot;,<br>            &quot;X-Title&quot;: &quot;PropertyRental&quot;<br>        }<br>    with open(&quot;./extracted_pdf.txt&quot;) as f:<br>        training_data = f.read() #Read and pass the training data at the content below<br>    data = {<br>        &quot;model&quot;: OPENROUTER_MODEL, #Model we defined before<br>        &quot;messages&quot;: [{&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: <br>(&quot;You are a friendly, professional assistant for our Property Management System.&quot;<br>&quot;Here is the application spec and chatbot training data you should refer <br>to:\n\n&quot; + training_data)},{&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: user_message} #Added training data<br>    try:<br>        response = httpx.post(<br>        &quot;https://openrouter.ai/api/v1/chat/completions&quot;,<br>        headers = headers,<br>        json = data,<br>        timeout = 30<br>        )<br>        response.raise_for_status()<br>        content = response.json()[&quot;choices&quot;][0][&quot;message&quot;][&quot;content&quot;]<br>        return content<br><br>    except Exception:<br>        return InvalidResponse(&quot;The chat service is currently unavailable!&quot;)    </pre><p>This will return the response generated for the user message and will refer the training data for the response. Because we are doing the response generation like this, the AI will answer questions regarding the application with accuracy, and you can always improve the training dataset.</p><p>Finally, you just have to send the user message from frontend as you wish, and the response will be generated in a friendly and informative manner and the assistance is automated for good.</p><p>Potential issues, you can face are, corporate networks might block the following request from backend to the Open router,</p><pre> response = httpx.post(<br>            &quot;https://openrouter.ai/api/v1/chat/completions&quot;,<br>            headers = headers,<br>            json = data,<br>            timeout = 30<br>            )</pre><p>If it happens try mobile hotspot and it will work or you may have to attach the proxy of the corporate network.</p><p>Happy Coding…!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7e1cd1cc753d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to read data from a JSON file and use the data globally in your Angular application]]></title>
            <link>https://medium.com/@lwijesekara149/how-to-read-data-of-a-json-file-and-use-it-globally-in-your-angular-application-d35665960c31?source=rss-3f14c104cc80------2</link>
            <guid isPermaLink="false">https://medium.com/p/d35665960c31</guid>
            <dc:creator><![CDATA[Lakshan Wijesekara]]></dc:creator>
            <pubDate>Mon, 21 Oct 2024 04:42:53 GMT</pubDate>
            <atom:updated>2024-10-21T04:44:13.253Z</atom:updated>
            <content:encoded><![CDATA[<p>When I started programming, I did not know about backend processes and started with simple front-end programming. Then I wanted to keep a simple data source to carry out the project. The obvious choice was to use a JSON file and read the data from it and use it globally throughout my application.</p><p>Without further ado, I will share the steps I followed to do exactly that.</p><ol><li><strong>Create a JSON file in your application (I usually create it under an assets folder).</strong></li></ol><figure><img alt="Example of some of my JSON files" src="https://cdn-images-1.medium.com/max/323/1*Bxz1z2dQB7jhPctazUJ0Ag.png" /><figcaption>Example of some of my JSON files</figcaption></figure><p><strong>2. Populate the JSON file with necessary data.</strong></p><pre>[{ &quot;id&quot;: 1, &quot;selectedCity&quot;: &quot;Colombo&quot;, &quot;propertyName&quot;: &quot;Prime Lands&quot;, &quot;propertyArea&quot;: 2000, &quot;monthlyRental&quot;: &quot;50000&quot;, &quot;latitude&quot;: 6.9271, &quot;longtitude&quot;: 79.8612}]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/393/1*WCNztXvExHSgL1Qt44EBEg.png" /><figcaption>Example from my application</figcaption></figure><p>The JSON file should be formatted as shown above, and you can add many objects by separating the object by a comma. [ { object 1 } , { Object 2 },…].</p><p>Keeping an id for each record is very useful when you need to call a specific record in future.</p><p><strong>3. Create a Service file to read this JSON file. (Important if you want to use this data throughout your application)</strong></p><p>To create a service file in angular, the recommended method is to use the CLI to create the required service file in your application.</p><pre>ng generate service property</pre><p>This will create a service named, <strong>property.service.ts. </strong>In next steps, we are going to use this file to subscribe to this JSON as an observable.</p><p><strong>4. Fetching data from the JSON file</strong></p><p>Now that you have created the property service file, we are going to retrieve the data stream from JSON file.</p><p>First, you need to import these two modules to your particular service file, or it will not work as intended,</p><pre>import { HttpClient } from &#39;@angular/common/http&#39;;<br>import { Observable } from &#39;rxjs&#39;;</pre><p>After importing the modules, you should define the httpClient in your constructor to use it,</p><pre>constructor(private http: HttpClient) {}</pre><p>After above steps, we will define a method to retrieve the data stream from the JSON file,</p><pre> fetchData(): Observable&lt;Property[]&gt; {<br>    return this.http.get&lt;Property[]&gt;(&#39;assets/properties.json&#39;);<br>  }</pre><p>Now, as you can see, I have created this fetchData method to retrieve the data from our JSON file. I used fetchData as the method name, but you can use any name you want.</p><p>Quick note, the &lt;Property[]&gt; after get is an interface. I have defined an interface named Property as a best practice and it will tell the app, way of data coming from the JSON file. Following is my interface for property.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/401/1*_c-S9k8DMvqVA9dSi5GRtA.png" /><figcaption>Property Interface</figcaption></figure><p><strong>5. How to use this in a component? Let’s see…</strong></p><p>Now you have done almost everything to get the data from your JSON file. Let’s use it in one of our components, shall we?</p><p>To use this in a component, first you should have a component. My example component is properties.component.ts. As I mentioned earlier, you can use the CLI command to create a component. (ng generate component (componentName))</p><p>First, import your service module and interface to your component,</p><pre>import { PropertydataService } from &#39;../../services/propertydata.service&#39;;<br>import { Property } from &#39;../../interfaces/property&#39;;</pre><p>Second step is to load it into the constructor of your properties component,</p><pre> constructor(<br>    private propertyDataService: PropertydataService<br>  ) {}</pre><p>Now, you can write a method to subscribe to the data stream coming from the properties JSON file in this component.</p><pre>getProperties(): void {<br>    this.propertyDataService.fetchData().subscribe({<br>      next: (propertyData) =&gt; {<br>          this.propertyData= property!;(property can be an array variable declared in this class)<br>        } <br>      error: (error) =&gt; {<br>        this.messageService.add({<br>          severity: &#39;error&#39;,<br>          summary: &#39;Error&#39;,<br>          detail: &#39;Error occurred&#39; + error,<br>        });<br>      },<br>    });<br>  }</pre><p>This is the way of using your JSON file data inside a component in a simple manner. We were able to use this throughout our application because the use of service class and the ability to call the service class when needed inside any component.</p><p>Hope you got an idea on this topic and thank you for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d35665960c31" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>