Exploring the Frontiers of AI: The Emergence of LLM-4 Architectures

The relentless advancement of artificial intelligence (AI) technology reshapes our world, with Large Language Models (LLMs) spearheading this transformation. The emergence of the LLM-4 architecture signifies a pivotal moment in AI development, heralding new capabilities in language processing that challenge the boundaries between human and machine intelligence. This article provides a comprehensive exploration of LLM-4 architectures, detailing their innovations, applications, and broader implications for society and technology.

Unveiling LLM-4 Architectures

LLM-4 architectures represent the cutting edge in the evolution of large language models, building upon their predecessors' foundations to achieve new levels of performance and versatility. These models excel in interpreting and generating human language, driven by enhancements in their design and training methodologies.

Empowering Developers With Scalable, Secure, and Customizable Storage Solutions

In today's data-driven world, businesses face numerous challenges when it comes to storing, securing, and analyzing vast amounts of information. As a developer, engineer, or architect, finding the right storage solution that seamlessly integrates with your infrastructure while providing the necessary scalability, security, and performance can be a daunting task. Enter StoneFly, a leading provider of storage area network (SAN) and network-attached storage (NAS) solutions that aim to simplify your life and tackle complex business problems head-on. The 54th IT Press Tour had an opportunity to meet with Stonefly on a recent visit to Silicon Valley. Here's what we learned.

Scalability and Flexibility

One of the key strengths of StoneFly's offerings is its exceptional scalability. Whether you're a small startup or a large enterprise, StoneFly's storage solutions can grow with your business. Starting with a single node, you can easily expand your storage capacity up to 128 nodes, accommodating anywhere from 20TB to a staggering 200PB of data. This flexibility allows you to start small and scale as your needs evolve, eliminating the need for costly upfront investments in storage infrastructure.

Ethical Considerations in AI Development

Artificial Intelligence (AI) has rapidly evolved, empowering us with remarkable capabilities, from predictive analytics to autonomous systems. However, this technological leap also brings forth ethical dilemmas and challenges. As AI development becomes deeply integrated into various aspects of our lives, navigating its development with a keen awareness of ethical considerations is crucial. This article explores the multifaceted ethical considerations in AI development, highlighting the need for responsible and ethical AI deployment.

Ethical Considerations in AI Development

Bias and Fairness

One of the foremost concerns in AI is bias. AI systems learn from historical data, and if this data contains biases, the AI can perpetuate and even amplify those biases. Developers must diligently address biases in datasets and algorithms to ensure fairness, especially in sensitive areas like hiring, lending, and criminal justice.

A Deep Dive Into CDC With Azure Data Factory

Change Data Capture (CDC) in SQL Server is a powerful feature designed to track and capture changes made to data within a database. It provides a reliable and efficient way to identify alterations to tables, allowing for the extraction of valuable insights into data modifications over time. By enabling CDC with Azure Data Factory, SQL Server enables a systematic and automated approach to monitoring and capturing changes, facilitating better data management, auditing, and analysis within the database environment.

Most Common Use-Cases: CDC With Azure Data Factory

Common scenarios where the CDC with Azure Data Factory proves beneficial include:

Accessible Forms with Pseudo Classes

Hey all you wonderful developers out there! In this post, I am going to take you through creating a simple contact form using semantic HTML and an awesome CSS pseudo class known as :focus-within. The :focus-within class allows for great control over focus and letting your user know this is exactly where they are in the experience. Before we jump in, let’s get to the core of what web accessibility is.


Form Accessibility?


You have most likely heard the term “accessibility” everywhere or the numeronym, a11y. What does it mean? That is a great question with so many answers. When we look at the physical world, accessibility means things like having sharps containers in your bathrooms at your business, making sure there are ramps for wheel assisted people, and having peripherals like large print keyboards on hand for anyone that needs it.

The gamut of accessibility doesn’t stop there, we have digital accessibility that we need to be cognizant of as well, not just for external users, but internal colleagues as well. Color contrast is a low hanging fruit that we should be able to nip in the bud. At our workplaces, making sure that if any employee needs assistive tech like a screen reader, we have that installed and available. There are a lot of things that need to be kept into consideration. This article will focus on web accessibility by keeping the WCAG (web content accessibility guidelines) in mind.

MDN (Mozilla Developer Network)

The :focus-within CSS pseudo-class matches an element if the element or any of its descendants are focused. In other words, it represents an element that is itself matched by the :focus pseudo-class or has a descendant that is matched by :focus. (This includes descendants in shadow trees.)

This pseudo class is really great when you want to emphasize that the user is in fact interacting with the element. You can change the background color of the whole form, for example. Or, if focus is moved into an input, you can make the label bold and larger of an input element when focus is moved into that input. What is happening below in the code snippets and examples is what is making the form accessible. :focus-within is just one way we can use CSS to our advantage.

How To Focus


Focus, in regards to accessibility and the web experience, is the visual indicator that something is being interacted with on the page, in the UI, or within a component. CSS can tell when an interactive element is focused.

“The :focus CSS pseudo-class represents an element (such as a form input) that has received focus. It is generally triggered when the user clicks or taps on an element or selects it with the keyboard’s Tab key.”

MDN (Mozilla Developer Network)

Always make sure that the focus indicator or the ring around focusable elements maintains the proper color contrast through the experience.

Focus is written like this and can be styled to match your branding if you choose to style it.

:focus {
  * / INSERT STYLES HERE /*
}

Whatever you do, never set your outline to 0 or none. Doing so will remove a visible focus indicator for everyone across the whole experience. If you need to remove focus, you can, but make sure to add that back in later. When you remove focus from your CSS or set the outline to 0 or none, it removes the focus ring for all your users. This is seen a lot when using a CSS reset. A CSS reset will reset the styles to a blank canvas. This way you are in charge of the empty canvas to style as you wish. If you wish to use a CSS reset, check out Josh Comeau’s reset.

*DO NOT DO what is below!

:focus {
  outline: 0;
}

:focus {
  outline: none;
}


Look Within!


One of the coolest ways to style focus using CSS is what this article is all about. If you haven’t checked out the :focus-within pseudo class, definitely give that a look! There are a lot of hidden gems when it comes to using semantic markup and CSS, and this is one of them. A lot of things that are overlooked are accessible by default, for instance, semantic markup is by default accessible and should be used over div’s at all times.

<header>
  <h1>Semantic Markup</h1>
  <nav>
    <ul>
      <li><a href="/">Home</a></li>
      <li><a href="/about">About</a></li>
    </ul>
  </nav>
</header>

<section><!-- Code goes here --></section>

<section><!-- Code goes here --></section>

<aside><!-- Code goes here --></aside>

<footer><!-- Code goes here --></footer>

The header, nav, main, section, aside, and footer are all semantic elements. The h1 and ul are also semantic and accessible.

Unless there is a custom component that needs to be created, then a div is fine to use, paired with ARIA (Accessible Rich Internet Applications). We can do a deep dive into ARIA in a later post. For now let’s focus…see what I did there…on this CSS pseudo class.

The :focus-within pseudo class allows you to select an element when any descendent element it contains has focus.


:focus-within in Action!

HTML

<form>
  <div>
    <label for="firstName">First Name</label><input id="firstName" type="text">
  </div>
  <div>
    <label for="lastName">Last Name</label><input id="lastName" type="text">
  </div>
  <div>
    <label for="phone">Phone Number</label><input id="phone" type="text">
  </div>
  <div>
    <label for="message">Message</label><textarea id="message"></textarea>
  </div>
</form>

CSS

form:focus-within {
  background: #ff7300;
  color: black;
  padding: 10px;
}

The example code above will add a background color of orange, add some padding, and change the color of the labels to black.

The final product looks something like below. Of course the possibilities are endless to change up the styling, but this should get you on a good track to make the web more accessible for everyone!

First example of focus-within css class highlighting the form background and changing the label text color.

Another use case for using :focus-within would be turning the labels bold, a different color, or enlarging them for users with low vision. The example code for that would look something like below.

HTML

<form>
  <h1>:focus-within part 2!</h1>
  <label for="firstName">First Name: <input name="firstName" type="text" /></label>
  <label for="lastName">Last Name: <input name="lastName" type="text" /></label>
  <label for="phone">Phone number: <input type="tel" id="phone" /></label>
  <label for="message">Message: <textarea name="message" id="message"/></textarea></label>
</form>

CSS

label {
  display: block;
  margin-right: 10px;
  padding-bottom: 15px;
}

label:focus-within {
  font-weight: bold;
  color: red;
  font-size: 1.6em;
}
Showing how to bold, change color and font size of labels in a form using :focus-within.

:focus-within also has great browser support across the board according to Can I use.

Focus within css pseudo class browser support according to the can i use website.

Conclusion

Creating amazing, accessible user experience should always be a top priority when shipping software, not just externally but internally as well. We as developers, all the way up to senior leadership need to be cognizant of the challenges others face and how we can be ambassadors for the web platform to make it a better place.

Using technology like semantic markup and CSS to create inclusive spaces is a crucial part in making the web a better place, let’s continue moving forward and changing lives.

Check out another great resource here on CSS-Tricks on using :focus-within.


Accessible Forms with Pseudo Classes originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

Automated Data Extraction Using ChatGPT AI: Benefits, Examples

Since the release of ChatGPT by OpenAI in 2022, most people in nearly all industries have tried a generative AI tool at least once. The market size for Generative AI is expected to show a CAGR of 24.40%, resulting in a market volume of US $207 billion by 2030. The technology can be useful in multiple ways. One such is extracting data from documents with OpenAI.

Read this post to discover applications and use cases of ChatGPT-based AI to extract data from documents, the challenges and limitations of the technology, and its prospects.

How to Do A/B Split Testing in WordPress (Step by Step)

Do you want to run A/B split tests on your WordPress site to improve conversions?

Split testing helps you understand how small changes in content and design affect user behavior. You can create different versions of a landing page and see which one converts the best.

In this article, we will show you how to easily do A/B split testing in WordPress.

How to do A/B split testing in WordPress

What Is A/B Split Testing?

A/B split testing is a technique that allows you to compare two versions of the same web page against each other so that you can determine which one performs better and produces the best results.

Marketers often use A/B split testing for their landing pages to find out which one gets them more conversions and sales.

Split testing can be used to test any element on the page, including call-to-action buttons, color schemes, layout changes, marketing text, images, and more.

Basically, you show different variations of a page to your audience. After that, you track user engagement and conversions to find out which variation gives you the best results.

Split testing explained

How to Do A/B Split Testing in WordPress

Before we move forward, there are a few things you will need for this tutorial.

Previously, Google Optimize was a free tool that many people used for split testing. However, Google Optimize sunsetted on September 30, 2023. Google announced that it would be investing in A/B testing in Google Analytics 4, which is the latest version of the famous analytics platform.

This means that if you were using Google Optimize before, then you can no longer access your experiments, personalizations, and historical data. When you open your account, you will see a message at the top showing that the tool is now sunset.

Fortunately, there are some other simple ways of A/B testing in WordPress. These are great Google Optimize alternatives, as they are easier to use and help you conduct experiments in WordPress. Simply click the links below to jump ahead to your preferred method:

Method 1: A/B Testing in WordPress Using Thrive Optimize

Thrive Optimize is part of the Thrive Theme suite and lets you run experiments and split tests in WordPress.

Since Google Optimize is no longer available, Thrive Optimize is a great alternative to conduct A/B tests. It offers more features, and you don’t need to enter a code to run a split test. Plus, it will keep your experiment data safe.

Just keep in mind that you will only be able to use Thrive Optimize if you are also using the Thrive Architect page builder plugin.

First, you’ll need to visit the Thrive Themes website and sign up for an account.

Thrive Theme

After creating an account, you can download the Thrive Product Manager plugin from your account area.

Next, you’ll need to install and activate the Thrive Product Manager plugin. For more details, please see our guide on how to install a WordPress plugin.

From here, you can go to the Product Manager page from your WordPress dashboard and click the ‘Log into my account’ button.

Go to Thrive product manager

On the next screen, you’ll see different Thrive Theme products.

From here, you’ll need to install Thrive Optimize and Thrive Architect. Simply check the ‘Install Product’ options and click the ‘Install selected products’ button at the bottom.

Install thrive optimize and thrive architect

If the products don’t automatically activate, then you will need to head back to the Product Manager page from your WordPress dashboard and activate Thrive Architect and Thrive Optimize.

You should now see a success message once the plugin is installed.

View success message

Next, you’ll need to head to Thrive Dashboard » Thrive Optimize from your WordPress admin panel.

After that, you can add a new page by going to Pages » Add New from your WordPress admin panel. Once you are in the content editor, simply click the ‘Launch Thrive Architect’ button.

Edit with Thrive Architect

Next, Thrive Architect will ask you what type of page you would like to create.

You can create a normal page, a blank page with a header and footer, a completely blank page, or select a pre-built landing page. For this tutorial, we will select the ‘Pre-built Landing Page’ option.

Select the type of page you want to create

There are many pre-built landing page templates to choose from. Simply select one and edit your page.

For more details on how to edit your page with Thrive Architect, you can see our guide on how to design landing pages in WordPress.

Select landing page template

In the Thrive Architect page builder, you will see an A/B testing option in the right panel.

Go ahead and click the ‘A/B’ icon.

Click split testing icon

Next, you can create different variants for your page for split testing.

We recommend clicking the ‘Duplicate this variation’ button for your control page. This will create a clone of your main page so you won’t have to create the landing page all over again from scratch.

Create a duplicate variation

You will now see a duplicate variant of your landing page.

Go ahead and click the ‘Edit Variation’ button to make changes.

Edit your variation

Next, you can edit the landing page to split-test different elements.

For example, we will change the color of the call to action (CTA) button. Once you are done, just click the ‘Save Work’ button at the bottom.

Make changes to variation

Now, you can click the ‘A/B’ button from the panel on the left to go back to the A/B testing dashboard.

Next, Thrive Architect also lets you split traffic for each variation. Simply use the toggles at the bottom to allocate the amount of traffic to each page.

Once you are done, go ahead and click the ‘Set Up & Start A/B Test’ option.

Set up and start A/B test

A popup window will now open.

Go ahead and enter a name for your split test and a short description. You can also toggle on the ‘Enable Automatic Winner Settings’ option and set conditions to pick a clear winner, as well as the duration that Thrive Optimize will run the A/B test.

Starting your A/B test

After that, simply click the ‘Next’ button.

Now, you can select a goal for your test. There are 3 options, which include revenue, visit goal page, and subscription. After picking a goal, you can click the ‘Start A/B Test’ button.

Select a goal for the test

For this tutorial, we will select the ‘Subscription’ goal for our test.

Next, you’ll be redirected to the WordPress content editor. You can scroll down to the ‘Thrive Optimize – A/B Test Overview’ meta box and see the 2 variations.

You don’t have to do anything else. The plugin will now test both variations and show the winner.

View Thrive Optimize metabox

To view how the variations are performing, you can click the ‘View Test Details’ option in the meta box.

Here, you can see the conversions over time for each variant and also see which is the control variant. There is also an option to stop the test and choose a winner.

View split test results

Method 2: A/B Testing in WordPress Using Nelio AB Testing (Free)

If you are looking for a free solution for running A/B tests on your WordPress site, then you can use Nelio AB Testing.

While the free version isn’t as powerful as Thrive Optimize, you will get basic features to conduct experiments on your website. For example, you can test headlines, create two different versions of a page to test them, and more.

However, if you are looking for a visual builder and additional options like assigning weights to each variant in the experiment, then we recommend using the first method instead.

To start, you’ll need to install and activate the Nelio AB Testing plugin. If you need help, then please see our guide on how to install a WordPress plugin.

Upon activation, you can head to Nelio A/B Testing from your WordPress dashboard. From here, click the checkbox to accept the terms of service and privacy policy of the plugin, and then click the ‘Continue’ button.

Accept Nelio testing terms of service

After that, you’ll be taken to the Neilo A/B Testing » Tests page in the WordPress admin panel.

Go ahead and click the ‘Add Test’ button.

Add a new test

Next, you’ll see options to test on your website. For example, the plugin lets you split test pages, posts, headlines, widgets, menus, themes, JavaScript, CSS, and more.

For the sake of this tutorial, we will select the ‘Headline’ option and split-test some titles for a blog post.

Choose test type

After that, you can enter a title for your test at the top.

From here, you will see the Control Version and Variants section. First, you can click the dropdown menu and choose a post title. This will be the control version (labeled A).

Enter test title and select post

Next, you can scroll down and enter details of a variant (labeled B).

This will be the new headline you will be testing against the one that’s already live.

Enter variant details for testing

After that, you can scroll down to the Conversion Goals and Actions section. You will notice that the plugin will use the default goal for this experiment type. In this case, it will measure views for your page with a new headline.

However, you can define your own goals for other experiment types. For example, if you want to test a page, then you can select whether you’d like to measure page views, clicks, clicks on external links, YouTube video playback, or choose a custom event.

Select conversion goal and action

The plugin also lets you create segmentation and narrow your test audience. However, this feature is only available for premium users, and you’d need to subscribe to a paid plan.

After setting your conversion goals, you can click the ‘Start’ button at the top to start A/B testing.

Select goal and start the test

Nelio A/B Testing plugin will now conduct the experiment.

To stop the experiment, you can go to the Nelio A/B Testing » Tests page and then click the ‘Stop’ link under the test.

Stop your test

You can also click the ‘View Results’ option to see which variant is performing the best.

The plugin will help you select a winner and show conversions for each variant.

View test results

Bonus Tips for Running A/B Tests in WordPress

When it comes to doing split tests on your website, here are some tips for running your experiments the right way:

  • Decide on a Goal – You’ll need to first decide which feature you want to test and what the end goal is. A good place to start is by studying your audience and their needs. You can also get feedback from users to understand what they are looking for and then split test elements on your site to boost conversions.
  • Keep Your Experiments Simple – It’s generally a good idea to just create 2 versions of your landing pages, ads, text, and other elements. This makes it easier to compare which variant is performing the best.
  • Document Your Split Test Results – After you’ve successfully conducted an experiment, you should document the results. This way, you’ll have valuable data for future reference and easily see what worked on your site.

We hope this article helped you learn how to do A/B split testing in WordPress. You may also want to see our guide to WordPress conversion tracking made simple and how to create an email newsletter the right way.

The post How to Do A/B Split Testing in WordPress (Step by Step) first appeared on WPBeginner.

An Explanation of Jenkins Architecture

In the fast-paced world of software development, efficiency is paramount. Automating repetitive tasks is key to achieving faster delivery cycles and improved quality. This is where Jenkins comes in — a free and open-source automation server that has become synonymous with continuous integration (CI) and continuous delivery (CD).

Jenkins, the open-source automation powerhouse, plays a pivotal role in the DevOps world. But have you ever wondered how it all works under the hood? This blog delves into the intricate architecture of Jenkins, breaking down its core components and how they orchestrate the automation magic.

Installing programs from Github

Hi

I downloaded Tkinter Designer, but theres no exe there.

Ive used Thonny as a "compiler" for running scripts, and I used it to "make" a a simple program, but I dont know how to save as an exe, and I dont know how I would put these files into Thonny.

Am I even using the right program ?

Integration of AI Tools With SAP ABAP Programming

As the landscape of enterprise technology evolves, the marriage of Artificial Intelligence (AI) with SAP ABAP (Advanced Business Application Programming) is reshaping the way businesses approach software development within the SAP ecosystem. This article delves into the groundbreaking integration of AI with SAP ABAP programming, exploring how this fusion is revolutionizing SAP development processes.

SAP ABAP and Its Legacy

The Foundation of SAP Development

SAP ABAP has long been the backbone of SAP development, providing a powerful and versatile language for customizing SAP applications. ABAP's capabilities have driven the customization of SAP systems to meet specific business requirements.

Build Your Own Programming Language

Expanding on "Build Your Own Programming Language" by Clinton Jeffery, this second edition embarks on an ambitious journey to demystify the intricacies of programming language development. Its inception, driven by real-world application and feedback, signifies a pivotal moment in the dialogue between authors and the programming community. This enhanced discussion delves deeper into the book's multifaceted contributions, exploring its role as a beacon for aspiring language designers and its broader implications for the future of software development.

A Responsive Evolution

The evolution from the first to the second edition of Jeffery's work illustrates a responsive adaptation to the needs of its audience. The inclusion of a chapter dedicated to preprocessors and transpilers was more than a mere addition; it was a strategic response to the shifting landscape of programming, where the conversion of legacy code becomes increasingly critical. This adaptation not only broadens the book's utility but also reflects a deep understanding of the ongoing challenges faced by developers in integrating old with new, ensuring the longevity and relevance of programming languages.

How can I upload a tar.bz2 file to openstack swift object storage container

I wrote a Python script that included the python-swiftclient module to connect to the OpenStack Object Storage and upload some files to the OpenStack Object Storage container

It works great if I upload a file that ends with the extension .gz however, Im getting an error regarding the TarFile object having no attribute read after running my script.
when it comes to the compressed file that ends with the extension .tar.bz2.

Ive included the Python script and the errors I got after running it. Please show me where Im wrong, and I would like some assistance in solving this issue. Im a beginner in Python.

from keystoneauth1 import session
from keystoneauth1.identity import v3
from swiftclient.client import Connection
from swiftclient.client import ClientException
import gzip
import tarfile

# Create a password auth plugin
auth = v3.Password(auth_url='https://cloud.company.com:5000/v3/',
                   username='myaccount',
                   password='mypassword',
                   user_domain_name='Default',
                   project_name='myproject',
                   project_domain_name='Default')

# Create session
keystone_session = session.Session(auth=auth)

# Create swiftclient Connection
swift_conn = Connection(session=keystone_session)

# Create a new object with the contents of Netbox database backup
with gzip.open('/var/backup/netbox_backups/netbox_2024-03-16.psql.gz', 'rb') as file:
    swift_conn.put_object(
        container,
        'object_netbox_2024-03-16.psql.gz',
        contents=file,
        content_type='application/gzip'
    )

# Confirm the presence of the object holding the Netbox database backup
obj1 = 'object_netbox_2024-03-16.psql.gz'
container = 'netbox-backups'
try:
    resp_headers = swift_conn.head_object(container, obj1)
    print("The object " + obj1 + " was successfully created")
except ClientException as e:
    if e.http_status == '404':
        print("The object " + obj1 + " was not found!")
    else:
        print("An error occurred checking for the existence of the object " + obj1)

# Create a new object with the contents of the compressed Netbox media backup
with tarfile.open('/var/backup/netbox_backups/netbox_media_2024-03-20.tar.bz2', mode='r:bz2') as file_tar_bz2:

    # Read the contents of the compressed Netbox media backup file
    file_contents = file_tar_bz2.read()

    # Create a file-like object from the contents of the compressed Netbox media backup file
    my_file_like_object = io.BytesIO(file_contents)

    # Upload the returned contents to the OpenStack Object Storage container
    swift_conn.put_object(
        container,
        'object_netbox_media_2024-03-20.tar.bz2',
        contents=file_tar_bz2,
        content_type='application/x-tar'
    )

# Confirm the presence of the object holding the compressed Netbox media backup
obj2 = 'object_netbox_media_2024-03-16.tar.bz2'
container = 'netbox-backups'
try:
    resp_headers = swift_conn.head_object(container, obj2)
    print("The object " + obj2 + " was successfully created")
except ClientException as e:
    if e.http_status == '404':
        print("The object " + obj2 + " was not found!")
    else:
        print("An error occurred checking for the existence of the object " + obj2)

Below is the error I got after running the script.

Python
Traceback (most recent call last):
File "/opt/scripts/netbox_backups_transfer.py", line 57, in <module>
file_contents = file_tar_bz2.read()
AttributeError: 'TarFile' object has no attribute 'read'

Paris Olympics Chatbot- Get Ticket Information Using Chat-GPT and LangChain

I was searching for Paris Olympics ticket prices for tennis games recently. The official website directs you to a PDF document containing ticket prices and venues for all the games. However, I found the PDF document to be very hard to navigate. To make things easier, I developed a chatbot to search this PDF document and answer my queries in natural language. And this is what I am going to share in this article.

I used the OpenAI API to create document embeddings (convert documents to numeric values) and the Python LangChain library as the orchestration framework to develop this chatbot.

So, let's begin without ado.

Installing and Importing Required Libraries

The following script installs the libraries required to run scripts in this article.


!pip install -U langchain
!pip install langchain-openai
!pip install pypdf
!pip install faiss-cpu

The script below imports required libraries.


from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_community.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain.chains import create_retrieval_chain
from langchain_core.documents import Document
import os

Generate Default Responses from Chat-GPT

Let's first generate some responses from Chat-GPT without augmenting its knowledge base with information about the Paris Olympics ticket price.

In a Python application, you will use the OpenAI API key to generate Chat-GPT responses. You can retrieve your API key by signing up for OpenAI API.

You can save your API key in an environment variable and retrieve it in your Python application via the following script.


openai_key = os.environ.get('OPENAI_KEY2')

Next, you must create an object of the ChatOpenAI class, pass it your API key, the model name (gpt-4 in this case), and the temperature value (between 0 and 1). A higher temperature value allows the model to be more creative.


llm = ChatOpenAI(
    openai_api_key = openai_key ,
    model = 'gpt-4',
    temperature = 0.5
)

To generate a response, call the invoke() method using the model object and pass it your input query.

result = llm.invoke("You are a comedian, tell a Joke about maths.")
print(result.content)

Output:


Why was the math book sad?

Because it had too many problems!

You can use the LangChain ChatPromptTemplate class to create a chatbot. The from_messages() method in the following script tells LangChain that we want to execute the conversation in message format. In this case, you must specify the value for the user attribute. The system attribute is optional.


prompt = ChatPromptTemplate.from_messages([
    ("system", '{assistant}'),
    ("user", "{input}")
])

Next, you must create a chain combining your prompt with the LLM. You can use the pipe operator (|) to create a chain.

Finally, you can execute the message chain using the invoke() method. You must pass values for the attributes defined in your prompt (system and user in our case).

You can print LLM's response using the content attribute of the response object.


chain = prompt | llm

result = chain.invoke(
    {"assistant": "You are a comedian",
     "input": "Tell a joke about mathematics"}
)

print(result.content)

Output:


Why was the math book sad?

Because it had too many problems!

The LangChain also provides a StrOutputParser object that you can use to directly retrieve string responses without using the content attribute, as shown in the script below.


output_parser = StrOutputParser()

chain = prompt | llm | output_parser

result = chain.invoke(
    {"assistant": "You are a comedian",
     "input": "Tell a joke about mathematics"}
)
print(result)

Output:


Why was the math book sad?

Because it had too many problems!

Chat-GPT can only generate correct responses if it contains the answer in its knowledge base. Since the latest model of GPT-4 was trained on the data until December 2023, it will not be able to return factual information about the events of 2024. Let's verify this.

In the following prompt, I asked Chat-GPT to tell me the ticket prices for tennis games in the Paris Olympics 2024.


result = chain.invoke(
    {"assistant": "You are a ticket receptionist",
     "input": "What is the lowest ticket price for tennis games in Paris Olympics 2024?"})
print(result)

Output:

image1.png

The above response shows that Chat-GPT does not know the Paris Olympics ticket prices.

In the next section, we will provide Chat-GPT with the ticket information using a retrieval augmented generation technique (RAG), and you will see that it generates correct responses.

RAG for Augmented Response Generation from Chat-GPT

Retrieval augmented generation (RAG) works in three steps.

  1. Split and create embeddings for the documents containing the knowledge base you want to search.
  2. Based on the user query, use similarity search to find documents most likely to contain a response for the query.
  3. Pass the user query and the matched document in a prompt to an LLM (Chat-GPT in our case) to generate a response.

Let's see how we can do this.

Splitting and Embedding Ticket Price Information Document

The following script imports the PDF document containing ticket price information and splits it into multiple pages.


loader = PyPDFLoader("https://tickets.paris2024.org/obj/media/FR-Paris2024/ticket-prices.pdf")
docs = loader.load_and_split()

Next, you can use any embedding technique to convert documents to numeric format. We convert documents to numeric embeddings since matching numbers is easier than documents.

Many advanced embedding techniques exist. However, OpenAI embeddings are the most accurate. The following script creates a vector database storing the vector embeddings for all the pages in the input PDF document.

embeddings = OpenAIEmbeddings(openai_api_key = openai_key)

text_splitter = RecursiveCharacterTextSplitter()
documents = text_splitter.split_documents(docs)
vector = FAISS.from_documents(documents, embeddings)
Question Answering with Chatbot

To implement question answering, we will create a ChatPromptTemplate object, passing the input query along with the context information retrieved from the vector database. The context information contains the answer to our query.

We will create a create_stuff_documents_chain chain that can generate LLM responses based on the context retrieved from documents.


from langchain.chains.combine_documents import create_stuff_documents_chain

prompt = ChatPromptTemplate.from_template("""Answer the following question based only on the provided context:

Question: {input}

Context: {context}
"""
)

document_chain = create_stuff_documents_chain(llm, prompt)

Let's first run our chain with the hard-coded context. In the following script, we ask a question and pass the context manually.

document_chain.invoke({
    "input": "What is the lowest ticket price for tennis games in Paris Olympics 2024?",
    "context": [Document(page_content = "The ticket prices for tennis games are 15, 10, 25 euros")]
})

Output:

'The lowest ticket price for tennis games in Paris Olympics 2024 is 10 euros.'

The Chat-GPT was intelligent enough to infer from the context that the lowest price is 10 euros.

However, we do not want to pass the context information manually. This kills the whole purpose of a chatbot. Instead, we want to provide the context information by searching the vector database containing the knowledge base. Then, we pass the searched context along with the input query to an LLM.

To do so, you can use your vector database's retriever() class and pass it along with the document_chain object to the create_retrieval_chain() class object.


retriever = vector.as_retriever()
retrieval_chain = create_retrieval_chain(retriever, document_chain)

Next, you can call the invoke() method from your retrieval chain to generate the final response. This method will pass the query to the vector database, which returns the most similar document and stores it in the context variable. This context variable, along with the input from the retrieval chain, is passed to the document_chain, which returns the final response.

The following script implements the method to generate the final response.


def generate_response(query):
    response = retrieval_chain.invoke({"input": query})
    print(response["answer"])

Let's now ask some questions about the prices of tickets for the tennis games. The page for the tennis games' ticket prices looks like this.

image2.png

Let's first ask about the lowest-priced ticket.

query = "What is the lowest ticket price for tennis games?"
generate_response(query)

Output:

The lowest ticket price for tennis games is 30.

The above response is correct, as shown in the price table.

Let's see another example. The following output shows that the model correctly returns the category for the lowest-priced ticket.

query = "What is the category for the lowest ticket price for tennis games?"
generate_response(query)

Output:

The category for the lowest ticket price for tennis games is D.

You can also ask a more complicated question such as the following, and you will see that the model will generate a correct response.

query = "What is the maximum price for category B for men's single tennis games for non-medal games?"
generate_response(query)

Output:

The maximum price for category B for men's single tennis games for non-medal games is 185.

I tried asking further questions about other sports, game venues, etc., and the model always returned the correct response.

Conclusion

The retrieval augmented generation (RAG) technique has made it easy to develop customized chatbots on your data. In this tutorial, you saw how to develop a chatbot that can provide ticket information for all the games in the Paris Olympics. You can use the same approach to develop chatbots that query other data types such as PDFs, websites, text documents, etc.

Maximizing Developer Efficiency and Productivity in 2024: A Personal Toolkit

Numerous developers embark on their tech journey only to find themselves disoriented, intimidated by coding sessions, and wrestling with the notion that they might not possess the quintessential programmer's mindset. The path they tread is fraught with challenges, stemming not only from a lack of proper experience but also from the absence of essential tools.

Crafting exceptional software is no small feat. It demands an extensive repertoire of knowledge, an eye for detail, astute logical reasoning, relentless research, and, most crucially, time. 

Understanding Escape Analysis in Go

Go uses escape analysis to determine the dynamic scope of Go values. Typically, go tries to store all the Go values in the function stack frame. The go compiler can predetermine which memory needs to be freed and emits machine instructions to clean it up. This way it becomes easy to clean up memory without the intervention of the Go Garbage Collector. This way of allocating memory is typically called stack allocation.

But when the compiler cannot determine the lifetime of a Go value it escapes to the heap. A value may also escape to the heap when the compiler does not know the size of the variable, or it’s too large to fit into the stack, or if the compiler cannot determine whether the variable is used after the function ends or the function stack frame is not used anymore.