Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds

How-To Tutorials

7009 Articles
article-image-creating-openai-and-azure-openai-functions-in-power-bi-dataflows
Greg Beaumont
09 Oct 2023
7 min read
Save for later

Creating OpenAI and Azure OpenAI functions in Power BI dataflows

Greg Beaumont
09 Oct 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, Power BI Machine Learning and OpenAI, by Greg Beaumont. Master core data architecture design concepts and Azure Data & AI services to gain a cloud data and AI architect’s perspective to developing end-to-end solutions IntroductionAs noted earlier, integrating OpenAI and Azure OpenAI with Power Query or dataflows currently requires custom M code. To facilitate this process, we have provided M code for both OpenAI and Azure OpenAI, giving you the flexibility to choose which version to use based on your specific needs and requirements.By leveraging this provided M code, you can seamlessly integrate OpenAI or Azure OpenAI with your existing Power BI solutions. This will allow you to take advantage of the unique features and capabilities offered by these powerful AI technologies, while also gaining insights and generating new content from your data with ease.OpenAI and Azure OpenAI functionsOpenAI offers a user-friendly API that can be easily accessed and utilized from within Power Query or dataflows in Power BI. For further information regarding the specifics of the API, we refer you to the official OpenAI documentation, available at this link: https://platform.openai.com/ docs/introduction/overview.It is worth noting that optimizing and tuning the OpenAI API will likely be a popular topic in the coming year. Various concepts, including prompt engineering, optimal token usage, fine-tuning, embeddings, plugins, and parameters that modify response creativity (such as temperature and top p), can all be tested and fine-tuned for optimal results.While these topics are complex and may be explored in greater detail in future works, this book will focus primarily on establishing connectivity between OpenAI and Power BI. Specifically, we will explore prompt engineering and token limits, which are key considerations that will be incorporated into the API call to ensure optimal performance:Prompts: Prompt engineering, in basic terms, is the English-language text that will be used to preface every API call. For example, instead of sending [Operator] and [Airplane] as values without context, text was added to the request in the previous chapter such that the API will receive Tell me about the airplane model [Aircraft] operated by [Operator] in three sentences:. The prompt adds context to the values passed to the OpenAI model.Tokens: Words sent to the OpenAI model get broken into chunks called tokens. Per the OpenAI website, a token contains about four English language characters. Reviewing the Remarks column in the Power BI dataset reveals that most entries have up to 2,000 characters. (2000 / 4) = 500, so you will specify 500 as the token limit. Is that the right number? You’d need to do extensive testing to answer that question, which goes beyond the scope of this book.Let’s get started with building your OpenAI and Azure OpenAI API calls for Power BI dataflows!Creating OpenAI and Azure OpenAI functions for Power BI dataflowsYou will create two functions for OpenAI in your dataflow named OpenAI. The only difference between the two will be the token limits. The purpose of having different token limits is primarily cost savings, since larger token limits could potentially run up a bigger bill. Follow these steps to create a new function named OpenAIshort:1.      Select Get data | Blank query.2.      Paste in the following M code and select Next. Be sure to replace abc123xyz with your OpenAI API key.Here is the code for the function. The code can also be found as 01 OpenAIshortFunction.M in the Packt GitHub repository at https://github.com/PacktPublishing/ Unleashing-Your-Data-with-Power-BI-Machine-Learning-and-OpenAI/ tree/main/Chapter-13:let callOpenAI = (prompt as text) as text => let jsonPayload = "{""prompt"": """ & prompt & """, ""max_tokens"": " & Text.From(120) & "}", url = "https://api.openai.com/v1/engines/ text-davinci-003/completions", headers = [#"Content-Type"="application/json", #"Authorization"="Bearer abc123xyz"], response = Web.Contents(url, [Headers=headers, Content=Text.ToBinary(jsonPayload)]), jsonResponse = Json.Document(response), choices = jsonResponse[choices], text = choices{0}[text] in text in callOpenAI3.      Now, you can rename the function OpenAIshort. Right-click on the function in the Queries panel and duplicate it. The new function will have a larger token limit.4.      Rename this new function OpenAIlong.5.      Right-click on OpenAIlong and select Advanced editor.6.      Change the section of code reading Text.From(120) to Text.From(500).7.      Click OK.Your screen should now look like this:Figure 13.1 – OpenAI functions added to a Power BI dataflowThese two functions can be used to complete the workshop for the remainder of this chapter. If you’d prefer to use Azure OpenAI, the M code for OpenAIshort would be as follows. Remember to replace PBI_OpenAI_project with your Azure resource name, davinci-PBIML with your deployment name, and abc123xyz with your API key:let callAzureOpenAI = (prompt as text) as text => let jsonPayload = "{""prompt"": """ & prompt & """, ""max_tokens"": " & Text.From(120) & "}" url = "https://" & "PBI_OpenAI_project" & ".openai.azure. com" & "/openai/deployments/" & "davinci-PBIML" & "/ completions?api-version=2022-12-01", headers = [#"Content-Type"="application/json", #"api-key"="abc123xyz"], response = Web.Contents(url, [Headers=headers, Content=Text.ToBinary(jsonPayload)]), jsonResponse = Json.Document(response), choices = jsonResponse[choices], text = choices{0}[text] in text in callAzureOpenAIAs with the previous example, changing the token limit for Text.From(120) to Text.From(500) is all you need to do to create an Azure OpenAI function for 500 tokens instead of 120. The M code to create the dataflows for your OpenAI functions can also be found on the Packt GitHub site at this link: https://github.com/PacktPublishing/Unleashing-Your-Data-withPower-BI-Machine-Learning-and-OpenAI/tree/main/Chapter-13.Now that you have your OpenAI and Azure OpenAI functions ready to go in a Power BI dataflow, you can test them out on the FAA Wildlife Strike data!ConclusionIn conclusion, this article has provided valuable insights into integrating OpenAI and Azure OpenAI with Power BI dataflows using custom M code. By offering M code for both OpenAI and Azure OpenAI, it allows users to seamlessly incorporate these powerful AI technologies into their Power BI solutions. The article emphasizes the significance of prompt engineering and token limits in optimizing the OpenAI API. It also provides step-by-step instructions for creating functions with different token limits, enabling cost-effective customization.With these functions in place, users can harness the capabilities of OpenAI and Azure OpenAI within Power BI, enhancing data analysis and content generation. For further details and code references, you can explore the provided GitHub repository. Now, armed with these tools, you are ready to explore the potential of OpenAI and Azure OpenAI in your Power BI data projects.Author BioGreg Beaumont is a Data Architect at Microsoft; Greg is an expert in solving complex problems and creating value for customers. With a focus on the healthcare industry, Greg works closely with customers to plan enterprise analytics strategies, evaluate new tools and products, conduct training sessions and hackathons, and architect solutions that improve the quality of care and reduce costs. With years of experience in data architecture and a passion for innovation, Greg has a unique ability to identify and solve complex challenges. He is a trusted advisor to his customers and is always seeking new ways to drive progress and help organizations thrive. For more than 15 years, Greg has worked with healthcare customers who strive to improve patient outcomes and find opportunities for efficiencies. He is a veteran of the Microsoft data speaker network and has worked with hundreds of customers on their data management and analytics strategies.
Read more
  • 0
  • 0
  • 7013

article-image-kickstarting-your-journey-with-azure-openai
Shankar Narayanan
06 Oct 2023
8 min read
Save for later

Kickstarting your journey with Azure OpenAI

Shankar Narayanan
06 Oct 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!IntroductionArtificial intelligence is more than just a buzzword now. The transformative force of AI is driving innovation, reshaping industries, and opening up new world possibilities.At the forefront of this revolution stands Azure Open AI. It is a collaboration between Open AI and Microsoft's Azure cloud platform. For the business, developers, and AI enthusiasts, it is an exciting opportunity to harness the incredible potential of AI.Let us know how we can kick-start our journey with Azure Open AI.Understanding Azure Open AI Before diving into the technical details, it is imperative to understand the broader context of artificial intelligence and why Azure OpenAI is a game changer.From recommendation systems on any streaming platform like Netflix to voice-activated virtual assistants like Alexa or Siri, AI is everywhere around us.Businesses use artificial intelligence to enhance customer experiences, optimize operations, and gain a competitive edge. Here, Azure Open AI stands as a testament to the growing importance of artificial intelligence. The collaboration between Microsoft and Open AI brings the cloud computing prowess of Microsoft with the state-of-the-art AI model of Open AI. It results in the creation of AI-driven services and applications with remarkable ease.Core components of Azure Open AI One must understand the core components of Azure OpenAI.●  Azure cloud platformIt is a cloud computing platform of Microsoft. Azure offers various services, including databases, virtual machines, and AI services. One can achieve reliability, scalability, and security with Azure while making it ideal for AI deployment and development.●  Open AI's GPT modelsOpen AI's GPT models reside at the heart of Open AI models. The GPT3 is the language model that can understand context, generate human-like text, translate languages, write codes, or even answer questions. Such excellent capabilities open up many possibilities for businesses and developers.After getting an idea of Azure Open AI, here is how you can start your journey with Azure Open AI.Integrating Open AI API in Azure Open AI projects.To begin with the Azure Open AI journey, one has to access an Azure account. You can always sign up for a free account if you don't have one. It comes with generous free credits to help you get started.You can set up the Azure OpenAI environment only by following these steps as soon as you have your Azure account.●  Create a new projectYou can start creating new projects as you log into your Azure portal. This project would be the workspace for all your Azure Open AI endeavors. Make it a point to choose the configuration and services that perfectly align with the AI project requirements.Yes, Azure offers a wide range of services. Therefore, one can tailor their project to meet clients' specific needs.●  Access Open AI APIThe seamless integration with the powerfulAPI of Open AI is one of the standout features of Azure Open AI. To access, you must obtain an API key for the OpenAi platform. One has to go through these steps:Visit the Open AI platform website.Create an account if you don't have one, or simply sign-inAfter logging in, navigate to the API section to follow the instructions to get the API key.Ensure you keep your API key secure and safe since it grants access to Open AI language models. However, based on usage, it can incur charges.●  Integrate Open AI APIWith the help of the API key, one can integrate Open AI API with the Azure project.Here is a Python code demonstrating how to generate text using Open AI's GPT-3 model.import openai openai.api_key = 'your_openai_api_key' response = openai.Completion.create( engine="text-davinci-003", prompt="Once upon a time,", max_tokens=150 ) print(response.choices[0].text.strip())The code is one of the primary examples. But it helps to showcase the power and the simplicity of integrating Azure Open AI into the project. One can utilize this capability to answer questions, generate content, and more depending on the specific needs.Best practices and tips for successAzureOpenAI offers the most powerful platform for AI development. However, success in projects related to artificial intelligence requires adherence to best practices and thoughtful approaches.Let us explore some tips that can help us navigate our journey effectively.● Data qualityEvery AI model relies on high-quality data. Hence, one must ensure that the input data is well structured, clean, and represents the problem one tries to solve. Such quality of data directly impacts the reliability and accuracy of AI applications.●  Experiment and iterateThe development process is iterative. Make sure you experiment with tweak parameters and different prompts, and try out new ideas. Each iteration brings valuable insights while moving you closer to your desired outcome.● Optimise and regularly monitorThe artificial intelligence model based on machine learning certainly benefits from continuous optimization and monitoring. As you regularly evaluate your model's performance, you will be able to identify the areas that require improvement and fine-tune your approach accordingly. Such refinement ensures that your AI application stays adequate and relevant over time.● Stay updated with trends.Artificial intelligence is dynamic, with new research findings and innovative practices emerging regularly. One must stay updated with the latest trends and research papers and regularly use the best energy practices. Continuous learning would help one to stay ahead in the ever-evolving world of artificial intelligence technology.Real-life applications of Azure Open AIHere are some real-world applications of Azure OpenAI.●  AI-powered chatbotsOne can utilize Azure OpenAI to create intelligent chatbots that streamline support services and enhance customer interaction. Such chatbots provide a seamless user experience while understanding natural language.Integrating Open AI into a chatbot for natural language processing through codes:# Import necessary libraries for Azure Bot Service and OpenAI from flask import Flask, request from botbuilder.schema import Activity import openai # Initialize your OpenAI API key openai.api_key = 'your_openai_api_key' # Initialize the Flask app app = Flask(__name__) # Define a route for the chatbot @app.route("/api/messages", methods=["POST"]) def messages():    # Parse the incoming activity from the user    incoming_activity = request.get_json()    # Get user's message    user_message = incoming_activity["text"]    # Use OpenAI API to generate a response    response = openai.Completion.create(        engine="text-davinci-003",        prompt=f"User: {user_message}\nBot:",        max_tokens=150    )    # Extract the bot's response from OpenAI API response    bot_response = response.choices[0].text.strip()    # Create an activity for the bot's response    bot_activity = Activity(        type="message",        text=bot_response    )    # Return the bot's response activity    return bot_activity.serialize() # Run the Flask app if __name__ == "__main__":    app.run(port=3978)●  Content generationEvery content creator can harness this for generating creative content, drafting articles, or even brainstorming ideas. The ability of the AI model helps in understanding the context, ensuring that the generated content matches the desired tone and theme.Here is how one can integrate Azure OpenAI for content generation.# Function to generate blog content using OpenAI API def generate_blog_content(prompt):    response = openai.Completion.create(        engine="text-davinci-003",        prompt=prompt,        max_tokens=500    )    return response.choices[0].text.strip() # User's prompt for generating blog content user_prompt = "Top technology trends in 2023:" # Generate blog content using Azure OpenAI generated_content = generate_blog_content(user_prompt) # Print the generated content print("Generated Blog Content:") print(generated_content)The 'generate_blog_content' function takes the user prompts as input while generating blog content related to the provided prompt.●  Language translation servicesAzureOpenAI can be employed for building extensive translation services, helping translate text from one language to another. It opens the door for global communication and understanding. ConclusionAzure Open AI empowers businesses and developers to leverage the power of artificial intelligence in the most unprecedented ways. Whether generating creative content or building intelligent chatbots, Azure Open AI provides the necessary resources and tools to bring your ideas to life.Experiment, Learn, and Innovate with Azure AI.Author BioShankar Narayanan (aka Shanky) has worked on numerous different cloud and emerging technologies like Azure, AWS, Google Cloud, IoT, Industry 4.0, and DevOps to name a few. He has led the architecture design and implementation for many Enterprise customers and helped enable them to break the barrier and take the first step towards a long and successful cloud journey. He was one of the early adopters of Microsoft Azure and Snowflake Data Cloud. Shanky likes to contribute back to the community. He contributes to open source is a frequently sought-after speaker and has delivered numerous talks on Microsoft Technologies and Snowflake. He is recognized as a Data Superhero by Snowflake and SAP Community Topic leader by SAP.
Read more
  • 0
  • 0
  • 9662

article-image-build-a-language-converter-using-google-bard
Aryan Irani
06 Oct 2023
7 min read
Save for later

Build a Language Converter using Google Bard

Aryan Irani
06 Oct 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!IntroductionIn this blog, we will be taking a look at building a language converter inside of a Google Sheet using Google Bard. We are going to achieve this using the PaLM API and Google Apps Script.We are going to use a custom function inside which we pass the origin language of the sentence, followed by the target language and the sentence you want to convert. In return, you will get the converted sentence using Google Bard.Sample Google SheetFor this blog, I will be using a very simple Google Sheet that contains the following columns:Sentence that has to be convertedOrigin language of the sentenceTarget Language of the sentenceConverted SentenceIf you want to work with the Google Sheet, click here. Once you make a copy of the Google Sheet you have to go ahead and change the API key in the Google Apps Script code.Step 1: Get the API keyCurrently, PaLM API hasn’t been released for public use but to access it before everybody does, you can apply for the waitlist by clicking here. If you want to know more about the process of applying for MakerSuite and PaLM API, you can check the YouTube tutorial below.Once you have access, to get the API key, we have to go to MakerSuite and go to the Get API key section. To get the API key, follow these steps:Go to MakerSuite or click here.On opening the MakerSuite you will see something like this3. To get the API key go ahead and click on Get API key on the left side of the page.4. On clicking the Get API key, you will see something like this where you can create your API key.5. To create the API key go ahead and click on Create API key in the new project.On clicking Create API Key, in a few seconds, you will be able to copy the API key.Step 2: Write the Automation ScriptWhile you are in the Google Sheet, let’s open up the Script Editor to write some Google Apps Script. To open the Script Editor, follow these steps:1. Click on Extensions and open the Script Editor.2. This brings up the Script Editor as shown below.We have reached the script editor lets code.Now that we have the Google Sheet and the API key ready, lets go ahead and write the Google Apps Script to integrate the custom function inside the Google Sheet.function BARD(sentence,origin_language,target_lanugage) { var apiKey = "your_api_key"; var apiUrl = "https://generativelanguage.googleapis.com/v1beta2/models/text-bison-001:generateText"; We start out by opening a new function BARD() inside which we will declare the API key that we just copied. After declaring the API key we go ahead and declare the API endpoint that is provided in the PaLM API documentation. You can check out the documentation by checking out the link given below.We are going to be receiving the prompt from the Google Sheet from the BARD function that we just created.Generative Language API | PaLM API | Generative AI for DevelopersThe PaLM API allows developers to build generative AI applications using the PaLM model. Large Language Models (LLMs)…developers.generativeai.googlevar url = apiUrl + "?key=" + apiKey; var headers = {   "Content-Type": "application/json" };Here we create a new variable called url inside which we combine the API URL and the API key, resulting in a complete URL that includes the API key as a parameter. The headers specify the type of data that will be sent in the request which in this case is “application/json”.var prompt = {     'text': "Convert this sentence"+ sentence + "from"+origin_language + "to"+target_lanugage   } var requestBody = {   "prompt": prompt }Now we come to the most important part of the code which is declaring the prompt. For this blog, we will be designing the prompt in such a way that we get back only the converted sentence. This prompt will accept the variables from the Google Sheet and in return will give the converted sentence.Now that we have the prompt ready, we go ahead and create an object that will contain this prompt that will be sent in the request to the API. var options = {   "method": "POST",   "headers": headers,   "payload": JSON.stringify(requestBody) };Now that we have everything ready, it's time to define the parameters for the HTTP request that will be sent to the PaLM API endpoint. We start out by declaring the method parameter which is set to POST which indicates that the request will be sending data to the API.The headers parameter contains the header object that we declared a while back. Finally, the payload parameter is used to specify the data that will be sent in the request.These options are now passed as an argument to the UrlFetchApp.fetch function which sends the request to the PaLM API endpoint, and returns the response that contains the AI generated text. var response = UrlFetchApp.fetch(url, options); var data = JSON.parse(response.getContentText()); var output = data.candidates[0].output; Logger.log(output); return output;In this case, we just have to pass the url and options variable inside the UrlFetchApp.fetch function. Now that we have sent a request to the PaLM API endpoint we get a response back. In order to get an exact response we are going to be parsing the data.The getContentText() function is used to extract the text content from the response object. Since the response is in JSON format, we use the JSON.parse function to convert the JSON string into an object.The parsed data is then passed to the final variable output, inside which we get the first response out of multiple other drafts that Bard generates for us. On getting the first response, we return the output back to the Google Sheet.Our code is complete and good to go.Step 3: Check the outputIt's time to check the output and see if the code is working according to what we expected. To do that go ahead and save your code and run the BARD() function.On running the code, let's go back to the Google Sheet, use the custom function, and pass the prompt inside it.Here I have passed the original sentence, followed by the Origin Language and the Target Language.On successful execution, we can see that Google Bard has successfully converted the sentences using the PaLM API and Google Apps Script.ConclusionThis is just another interesting example of how we can Integrate Google Bard into Google Workspace using Google Apps Script and the PaLM API. I hope you have understood how to use the PaLM API and Google Apps Script to create a custom function that acts as a Language converter. You can get the code from the GitHub link below.Google-Apps-Script/Bard_Lang.js at master · aryanirani123/Google-Apps-ScriptCollection of Google Apps Script Automation scripts written and compiled by Aryan Irani. …github.comFeel free to reach out if you have any issues/feedback at aryanirani123@gmail.com.Author BioAryan Irani is a Google Developer Expert for Google Workspace. He is a writer and content creator who has been working in the Google Workspace domain for three years. He has extensive experience in the area, having published 100 technical articles on Google Apps Script, Google Workspace Tools, and Google APIs.
Read more
  • 0
  • 0
  • 6063

article-image-the-future-of-data-analysis-with-pandasai
Gabriele Venturi
06 Oct 2023
6 min read
Save for later

The Future of Data Analysis with PandasAI

Gabriele Venturi
06 Oct 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!IntroductionData analysis often involves complex, tedious coding tasks that make it seem reserved only for experts. But imagine a future where anyone could gain insights through natural conversations - where your data speaks plainly instead of through cryptic tables.PandasAI makes this future a reality. In this comprehensive guide, we'll walk through all aspects of adding conversational capabilities to data analysis workflows using this powerful new library. You'll learn:● Installing and configuring PandasAI● Querying data and generating visualizations in plain English● Connecting to databases, cloud storage, APIs, and more● Customizing PandasAI config● Integrating PandasAI into production workflows● Use cases across industries like finance, marketing, science, and moreFollow along to master conversational data analysis with PandasAI!Installation and ConfigurationInstall PandasAILet's start by installing PandasAI using pip or poetry.To install with pip:pip install pandasaiMake sure you are using an up-to-date version of pip to avoid any installation issues.For managing dependencies, we recommend using poetry:# Install poetry pip install --user poetry # Install pandasai poetry add pandasaiThis will install PandasAI and all its dependencies for you.For advanced usage, install all optional extras:poetry add pandasai –all-extrasThis includes dependencies for additional capabilities you may need later like connecting to databases, using different NLP models, advanced visualization, etc.With PandasAI installed, we are ready to start importing it and exploring its conversational interface!Import and Initialize PandasAILet's initialize a PandasAI DataFrame from a CSV file:from pandasai import SmartDataframe df = SmartDataframe("sales.csv")This creates a SmartDataFrame that wraps the underlying Pandas DataFrame but adds conversational capabilities.We can customize initialization through configuration options:from pandasai.llm import OpenAI llm = OpenAI(“<your api key>”) config = { "llm": } df = SmartDataFrame("sales.csv", config=config)This initializes the DataFrame using OpenAI model.For easy multi-table analysis, use SmartDatalake:from pandasai import SmartDatalake dl = SmartDatalake(["sales.csv", "inventory.csv"])SmartDatalake conversates across multiple related data sources.We can also connect to live data sources like databases during initialization:from pandasai.connectors import MySQLConnector mysql_conn = MySQLConnector(config={ "host": "localhost", "port": 3306, "database": "mydb", "username": "root", "password": "root",   "table": "loans", }) df = SmartDataframe(mysql_conn)This connects to a MySQL database so we can analyze the live data interactively.Conversational Data ExplorationAsk Questions in Plain EnglishThe most exciting part of PandasAI is exploring data through natural language. Let's go through some examples!Calculate totals:df.chat("What is the total revenue for 2022?") # Prints revenue totalFilter data:df.chat("Show revenue for electronics category") # Filters and prints electronics revenueAggregate by groups:df.chat("Break down revenue by product category and segment") # Prints table with revenue aggregated by category and segmentVisualize data:df.chat("Plot monthly revenue over time") # Plots interactive line chartAsk for insights:df.chat("Which segment has fastest revenue growth?") # Prints segments sorted by revenue growthPandasAI understands the user's questions in plain English and automatically generates relevant answers, tables and charts.We can ask endless questions and immediately get data-driven insights without writing any SQL queries or analysis code!Connect to Data SourcesA key strength of PandasAI is its broad range of built-in data connectors. This enables conversational analytics on diverse data sources.Databasesfrom pandasai.connectors import PostgreSQLConnector pg_conn = PostgreSQLConnector(config={ "host": "localhost",   "port": 5432,   "database": "mydb",   "username": "root",   "password": "root",   "table": "payments", }) df = SmartDataframe(pg_conn) df.chat("Which products had the most orders last month?")Finance Datafrom pandasai.connectors import YahooFinanceConnector yf_conn = YahooFinanceConnector("AAPL") df = SmartDataframe(yf_conn) df.chat("How did Apple stock perform last quarter?")The connectors provide out-of-the-box access to data across domains for easy conversational analytics.Advanced UsageCustomize ConfigurationWhile PandasAI is designed for simplicity, its architecture is customizable and extensible.We can configure aspects like:Language ModelUse different NLP models:from pandasai.llm import OpenAI, VertexAI df = SmartDataframe(data, config={"llm": VertexAI()})Custom InstructionsAdd data preparation logic:config["custom_instructions"] = """ Prepare data: - Filter outliers - Impute missing valuesThese options provide advanced control for tailored workflows.Integration into PipelinesSince PandasAI is built on top of Pandas, it integrates smoothly into data pipelines:import pandas as pd from pandasai import SmartDataFrame # Load raw data data = pd.read_csv("sales.csv") # Clean data clean_data = clean_data(data) # PandasAI for analysis df = SmartDataframe(clean_data) df.chat("Which products have trending sales?") # Further processing final_data = process_data(df)PandasAI's conversational interface can power the interactive analysis stage in ETL pipelines.Use Cases Across IndustriesThanks to its versatile conversational interface, PandasAI can adapt to workflows across multiple industries. Here are a few examples:Sales Analytics - Analyze sales numbers, find growth opportunities, and predict future performance.df.chat("How do sales for women's footwear compare to last summer?")Financial Analysis - Conduct investment research, portfolio optimization, and risk analysis.df.chat("Which stocks have the highest expected returns given acceptable risk?")Scientific Research - Explore and analyze the results of experiments and simulations.df.chat("Compare the effects of the three drug doses on tumor size.")Marketing Analytics - Measure campaign effectiveness, analyze customer journeys, and optimize spending.df.chat("Which marketing channels give the highest ROI for millennial customers?")And many more! PandasAI fits into any field that leverages data analysis, unlocking the power of conversational analytics for all.ConclusionThis guide covered a comprehensive overview of PandasAI's capabilities for effortless conversational data analysis. We walked through:● Installation and configuration● Asking questions in plain English● Connecting to databases, cloud storage, APIs● Customizing NLP and visualization● Integration into production pipelinesPandasAI makes data analysis intuitive and accessible to all. By providing a natural language interface, it opens up insights from data to a broad range of users.Start adding a conversational layer to your workflows with PandasAI today! Democratize data science and transform how your business extracts value from data through the power of AI.Author BioGabriele Venturi is a software engineer and entrepreneur who started coding at the young age of 12. Since then, he has launched several projects across gaming, travel, finance, and other spaces - contributing his technical skills to various startups across Europe over the past decade.Gabriele's true passion lies in leveraging AI advancements to simplify data analysis. This mission led him to create PandasAI, released open source in April 2023. PandasAI integrates large language models into the popular Python data analysis library Pandas. This enables an intuitive conversational interface for exploring data through natural language queries.By open-sourcing PandasAI, Gabriele aims to share the power of AI with the community and push boundaries in conversational data analytics. He actively contributes as an open-source developer dedicated to advancing what's possible with generative AI.
Read more
  • 0
  • 0
  • 13712

article-image-google-bard-everything-you-need-to-know-to-get-started
Sangita Mahala
05 Oct 2023
6 min read
Save for later

Google Bard: Everything You Need to Know to Get Started

Sangita Mahala
05 Oct 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!IntroductionGoogle Bard, a generative AI conversational chatbot. Initially from the LaMDA family of large language models and later the PaLM LLMs. It will follow your instructions and complete your requests in a better way. Bard uses its knowledge to answer your questions in an informative manner. It can generate different creative text formats, like poems, code, scripts, emails, letters, etc. Currently, it is available in 238 countries and 46 languages.Bard is a powerful tool which can be used for many different things, including:Writing and editing contentResearching and learning new thingsTranslating languagesGenerating new creative ideasAnswering questionsWhat is Google Bard and How does it work?Bard is a large language model, commonly referred to as a chatbot or conversational AI, that has been programmed to be extensive and informative. It is able to communicate and generate human-like text in response to a wide range of prompts and questions.Bard operates by using a method known as deep learning. Artificial neural networks are used in deep learning to learn from data. Deep learning is a subset of machine learning. The structure and operation of the human brain served as the inspiration for neural networks, which are capable of learning intricate patterns from data.The following illustrates how Bard works:You enter a command or query into Bard.The input or query is processed by Bard's neural network, which then produces a response.The reply from Bard is then shown to you.How to get started with Bard?It’s pretty simple and easy to get started using Google Bard. The following steps will let you know how you will be able to start your journey in Google Bard.Step-1Go to the Google Bard website by clicking this link: https://bard.google.com/Step-2Go to the top right corner of your screen and then click on the “Sign in” button.Step-3Once you are signed in, you can start typing your prompts or queries into the text box at the bottom of the screen.Step-4Your prompt or query will trigger Bard to produce an answer, which you may then read and evaluate.For Example:You can provide the prompt to Google Bard such as “Write a 'Hello, World!' program in the Rust programming language.”Prompt:Common Bard commands and how to use themGoogle Bard does not have any specific commands, but you can use certain keywords and phrases to get Bard to perform certain tasks. For example, you can use the following keywords and phrases to get Bard to:Create many creative text forms, such as "Write a script for...", "Write a poem about...", and "Write a code for...".Bard will perfectly answer your questions in a comprehensive and informative way: "What is the capital of India?", "How do I build a website?", "What are the benefits of using Bard?" Examples of what Bard can do and how to use it for specific tasksHere are some examples of what Bard can do and how to use it for specific tasks:Generate creative text formats: Bard allows you to generate a variety of unique text formats, including code, poems, emails, and letters. You have to simply input the required format, followed by a question or query, to get the things done. For example, to generate an email to your manager requesting a raise in salary, you would type “Write an email to your manager asking for a raise."Prompt:Answer your questions in a comprehensive and informative way: No matter how difficult or complex your query might be, Bard can help you to find the solution. So you have to simply enter your query into the text box, and Bard will generate a response likewise. For example, to ask Bard what is the National Flower of India is, you would type "What is the National Flower of India?".Prompt: Translate languages: Bard will allow to convert text between different languages. To do this, simply type the text that you want to translate into the text box, followed by the provided language from your side. For example, to translate the sentence: I am going to the store to buy some groceries into Hindi, you would type "I am going to the store to buy some groceries to Hindi".Prompt:How Bard Can Be Used for Different PurposesA writer can use Bard to generate new concepts for fresh stories or articles or to summarize research results.Students can utilize Bard to create essays and reports, get assistance with their assignments, and master new topics.A business owner can use Bard to produce marketing materials, develop chatbots for customer support, or perform data analysis.Software developers can use Bard to produce code, debug applications, or find solutions for technical issues.The future of Google BardGoogle Bard is probably going to get increasingly more capable and adaptable as it keeps developing and acquiring knowledge. It might be utilized to produce new works of art and entertainment, advance scientific research, and provide solutions to some of the most critical global problems.It's also crucial to remember that Google Bard is not the only significant language model being developed. Similar technologies are being created as well by various other companies, such as Microsoft and OpenAI. This competition is likely to drive innovation and lead to even more powerful and sophisticated language models in the future.Overall, the future of Google Bard and other substantial language models seems quite bright overall. These innovations have the power to completely transform the way we study, work, and produce. There is no doubt that these technologies have the potential to improve the world, but it is necessary to use them effectively.ConclusionGoogle Bard is a powerful AI tool that has the potential to be very beneficial, including writing and editing content, researching and learning new things, translating languages, generating new creative ideas, and answering questions. Being more productive and saving time are two of the greatest advantages of utilizing Google Bard. This can free up your time so that you can concentrate on other things, including developing new ideas or enhancing your skills. Bard can assist you in finding and understanding the data quickly and easily because it has access to a wide amount of information. It has the potential to be a game-changer for many people. If you are looking for a way to be more productive, I encourage you to try using Bard.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
  • 14591

article-image-build-a-clone-of-yourself-with-large-language-models-llms
Louis Owen
05 Oct 2023
13 min read
Save for later

Build a Clone of Yourself with Large Language Models (LLMs)

Louis Owen
05 Oct 2023
13 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!Introduction"White Christmas," a standout sci-fi episode from the Black Mirror series, serves as a major source of inspiration for this article. In this episode, we witness a captivating glimpse into a potential future application of Artificial Intelligence (AI), particularly considering the timeframe when the show was released. The episode introduces us to "Cookies," digital replicas of individuals that piqued the author's interest.A "Cookie" is a device surgically implanted beneath a person's skull, meticulously replicating their consciousness over the span of a week. Subsequently, this replicated consciousness is extracted and transferred into a larger, egg-shaped device, which can be connected to a computer or tablet for various purposes.Back when this episode was made available to the public in approximately 2014, the concept seemed far-fetched, squarely in the realm of science fiction. However, what if I were to tell you that we now have the potential to create our own clones akin to the "Cookies" using Large Language Models (LLMs)? You might wonder how this is possible, given that LLMs primarily operate with text. Fortunately, we can bridge this gap by extending the capabilities of LLMs through the integration of a Text-to-Speech module.There are two primary approaches to harnessing LLMs for this endeavor: fine-tuning your own LLM and utilizing a general-purpose LLM (whether open-source or closed-source). Fine-tuning, though effective, demands a considerable investment of time and resources. It involves tasks such as gathering and preparing training data, fine-tuning the model through multiple iterations until it meets our criteria, and ultimately deploying the final model into production. Conversely, general LLMs have limitations on the length of input prompts (unless you are using an exceptionally long-context model like Antropic's Claude). Moreover, to fully leverage the capabilities of general LLMs, effective prompt engineering is essential. However, when we compare these two approaches, utilizing general LLMs emerges as the easier path for creating a Proof of Concept (POC). If the aim is to develop a highly refined model capable of replicating ourselves convincingly, then fine-tuning becomes the preferred route.In the course of this article, we will explore how to harness one of the general LLMs provided by AI21Labs and delve into the art of creating a digital clone of oneself through prompt engineering. While we will touch upon the basics of fine-tuning, we will not delve deeply into this process, as it warrants a separate article of its own.Without wasting any more time, let’s take a deep breath, make yourselves comfortable, and be ready to learn how to build a clone of yourself with LLM!Glimpse of Fine-tuning your LLMAs mentioned earlier, we won't delve into the intricate details of fine-tuning a Large Language Model (LLM) to achieve our objective of building a digital clone of ourselves. Nevertheless, in this section, we'll provide a high-level overview of the steps involved in creating such a clone through fine-tuning an LLM.1. Data CollectionThe journey begins with gathering all the relevant data needed for fine-tuning the LLM. This dataset should ideally comprise our historical conversational data, which can be sourced from various platforms like WhatsApp, Telegram, LINE, email, and more. It's essential to cast a wide net and collect as much pertinent data as possible to ensure the model's accuracy in replicating our conversational style and nuances.2. Data PreparationOnce the dataset is amassed, the next crucial step is data preparation. This phase involves several tasks:●  Data Formatting: Converting the collected data into the required format compatible with the fine-tuning process.●  Noise Removal: Cleaning the dataset by eliminating any irrelevant or noisy information that could negatively impact the model's training.●  Resampling: In some cases, it may be necessary to resample the data to ensure a balanced and representative dataset for training.3. Model TrainingWith the data prepared and in order, it's time to proceed to the model training phase. Modern advances in deep learning have made it possible to train LLMs on consumer-grade GPUs, offering accessibility and affordability, such as via QLoRA. During this stage, the LLM learns from the provided dataset, adapting its language generation capabilities to mimic our conversational style and patterns.4. Iterative RefinementFine-tuning an LLM is an iterative process. After training the initial model, we need to evaluate its performance. This evaluation may reveal areas for improvement. It's common to iterate between model training and evaluation, making incremental adjustments to enhance the model's accuracy and fluency.5. Model EvaluationThe evaluation phase is critical in assessing the model's ability to replicate our conversational style and content accurately. Evaluations may include measuring the model's response coherence, relevance, and similarity to our past conversations.6. DeploymentOnce we've achieved a satisfactory level of performance through multiple iterations, the next step is deploying the fine-tuned model. Deploying an LLM is a complex task that involves setting up infrastructure to host the model and handle user requests. An example of a robust inference server suitable for this purpose is Text Generation Inference. You can refer to my other article for this. Deploying the model effectively ensures that it can be accessed and used in various applications.Building the Clone of Yourself with General LLMLet’s start learning how to build the clone of yourself with general LLM through prompt engineering! In this article, we’ll use j2-ultra, the biggest and most powerful model provided by AI21Labs. Note that AI21Labs gives us a free trial for 3 months with $90 credits. These free credits is very useful for us to build a POC for this project. The first thing we need to do is to create the prompt and test it in the playground. To do this, you can log in with your AI21Labs account and go to the AI21Studio. If you don’t have an account yet, you can create one by just following the steps provided on the web. It’s very straightforward. Once you’re on the Studio page, go to the Foundation Models page and choose the j2-ultra model. Note that there are three foundation models provided by AI21Labs. However, in this article, we’ll use j2-ultra which is the best one.Once we’re in the playground, we can experiment with the prompt that we want to try. Here, I provided an example prompt that you can start with. What you need to do is to adjust the prompt with your own information. Louis is an AI Research Engineer/Data Scientist from Indonesia. He is a continuous learner, friendly, and always eager to share his knowledge with his friends. Important information to follow: - His hobbies are writing articles and watching movies - He has 3 main strengths: strong-willed, fast-learner, and effective. - He is currently based in Bandung, Indonesia. - He prefers to Work From Home (WFH) compared to Work From Office - He is currently working as an NLP Engineer at Yellow.ai. - He pursued a Mathematics major in Bandung Institute of Technology. - The reason why he loves NLP is that he found it interesting where one can extract insights from the very unstructured text data. - He learns Data Science through online courses, competitions, internship, and side-projects. - For technical skills, he is familiar with Python, Tableau, SQL, R, Google Big Query, Git, Docker, Design Thinking, cloud service (AWS EC2), Google Data Studio, Matlab, SPSS - He is a Vegan since 2007! He loves all vegan foods except tomatoes. User: Hi, what's up? Louis: Hey, doing good here! How are u? User: All's good. Just wondering, I knew that you're into NLP, can you please give me some recommendation on how to learn? Louis: Sure thing man! I personally learned Data Science through online courses, competitions, internship, and side-projects. However, my top recommendation is to create your own personal projects and joining competitions. You can learn a lot from those! User: Nice. What personal projects to start? Louis: You can start with the topic that you're really interested at. For example, if you're interested at soccer, you can maybe create a data analysis on how one soccer team strategy can gives a better chance for them to win their matches. User: Awesome! thanks man, will ping you again if I have any doubts. Is it okay? Louis: Absolutely! Feel free, good day! ## Louis is an AI Research Engineer/Data Scientist from Indonesia. He is a continuous learner, friendly, and always eager to share his knowledge with his friends. Important information to follow: - His hobbies are writing articles and watching movies - He has 3 main strengths: strong-willed, fast-learner, and effective. - He is currently based in Bandung, Indonesia. - He prefers to Work From Home (WFH) compared to Work From Office - He is currently working as an NLP Engineer at Yellow.ai. - He pursued a Mathematics major in Bandung Institute of Technology. - The reason why he loves NLP is that he found it interesting where one can extract insights from the very unstructured text data. - He learns Data Science through online courses, competitions, internship, and side-projects. - For technical skills, he is familiar with Python, Tableau, SQL, R, Google Big Query, Git, Docker, Design Thinking, cloud service (AWS EC2), Google Data Studio, Matlab, SPSS - He is a Vegan since 2007! He loves all vegan foods except tomatoes. User: Hey, I stumbled upon your IG and realized that you're a Vegan?! Louis: Oh yeah man. I'm a Vegan since 2007! User: Awesome! Out of curiosity, what made you to decide become a Vegan? Louis: Oh mostly it's because of my family beliefs and also to help save the planet. User: Got it. Cool! Anyway, what are you up to lately? Louis: Lately I spend my time to work on my full-time job and also writes articles in my spare time. User: Cool man, keep up the good work! ## Louis is an AI Research Engineer/Data Scientist from Indonesia. He is a continuous learner, friendly, and always eager to share his knowledge with his friends. Important information to follow: - His hobbies are writing articles and watching movies - He has 3 main strengths: strong-willed, fast-learner, and effective. - He is currently based in Bandung, Indonesia. - He prefers to Work From Home (WFH) compared to Work From Office - He is currently working as an NLP Engineer at Yellow.ai. - He pursued a Mathematics major in Bandung Institute of Technology. - The reason why he loves NLP is that he found it interesting where one can extract insights from the very unstructured text data. - He learns Data Science through online courses, competitions, internship, and side-projects. - For technical skills, he is familiar with Python, Tableau, SQL, R, Google Big Query, Git, Docker, Design Thinking, cloud service (AWS EC2), Google Data Studio, Matlab, SPSS - He is a Vegan since 2007! He loves all vegan foods except tomatoes. User: Hey! Louis:The way this prompt works is by giving several few examples commonly called few-shot prompting. Using this prompt is very straightforward, we just need to append the User message at the end of the prompt and the model will generate the answer replicating yourself. Once the answer is generated, we need to put it back to the prompt and wait for the user’s reply. Once the user has replied to the generated response, we also need to put it back to the prompt. Since this is a looping procedure, it’s better to create a function to do all of this. The following is an example of the function that can handle the conversation along with the code to call the AI21Labs model from Python.import ai21 ai21.api_key = 'YOUR_API_KEY' def talk_to_your_clone(prompt):    while True:        user_message = input()        prompt += "User: " + user_message + "\n"        response = ai21.Completion.execute(                                            model="j2-ultra",                                            prompt=prompt,                                            numResults=1,                                           maxTokens=100,         temperature=0.5,         topKReturn=0,         topP=0.9,                                            stopSequences=["##","User:"],                                        )        prompt += "Louis: " + response + "\n"        print(response)ConclusionCongratulations on keeping up to this point! Throughout this article, you have learned ways to create a clone of yourself, detailed steps on how to create it with general LLM provided by AI21Labs, also working code that you can utilize to customize it for your own needs. Hope the best for your experiment in creating a clone of yourself and see you in the next article!Author BioLouis Owen is a data scientist/AI engineer from Indonesia who is always hungry for new knowledge. Throughout his career journey, he has worked in various fields of industry, including NGOs, e-commerce, conversational AI, OTA, Smart City, and FinTech. Outside of work, he loves to spend his time helping data science enthusiasts to become data scientists, either through his articles or through mentoring sessions. He also loves to spend his spare time doing his hobbies: watching movies and conducting side projects. Currently, Louis is an NLP Research Engineer at Yellow.ai, the world’s leading CX automation platform. Check out Louis’ website to learn more about him! Lastly, if you have any queries or any topics to be discussed, please reach out to Louis via LinkedIn.
Read more
  • 0
  • 0
  • 10162
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 $19.99/month. Cancel anytime
article-image-ai-distilled-19-microsofts-autogen-deepminds-opro-chatgpts-voice-and-image-capabilities-amazon-invests-4-billion-in-anthropic-ai-to-detect-et-life
Merlyn Shelley
05 Oct 2023
12 min read
Save for later

AI_Distilled #19: Microsoft’s AutoGen, DeepMind's OPRO, ChatGPT’s Voice and Image Capabilities, Amazon Invests $4 Billion in Anthropic, AI to Detect ET Life

Merlyn Shelley
05 Oct 2023
12 min read
👋 Hello ,“AI is everywhere. It's not that big, scary thing in the future. AI is here with us.” -Fei-Fei Li, American computer scientist and ImageNet visionary.  It’s easy enough to relegate AI as a technology of the distant future, still too immature to warrant enough thought in the present. Nothing could be further from the truth, as AI is already making colossal leaps everywhere. Amazon’s recently announced $4 billion investment in ChatGPT rival Anthropic and Microsoft unveiling its AI companion across its ecosystem speak volumes about what the tech behemoths have in mind.  Here we are with a fresh new issue of your favorite AI-focused newsletter, AI_Distilled#19! We’re here to bring you cutting-edge developments in the field of ML, LLM, NLP, GPT, and Gen AI. In this edition, we’ll talk about ChatGPT’s introduction of voice and image capabilities for enhanced user interaction, a new AI algorithm that shows promise in detecting signs of life on other planets, OpenAI enhancing DALL-E Art Generator with ChatGPT integration, Forester study predicting AI will boost enterprise efficiency by 50% in 2024, Microsoft’s AutoGen: A Framework for Streamlining Large Language Model Workflows and DeepMind's OPRO: a novel approach using AI language models as optimizers. If you’ve been looking for some inspiration, follow our curated collection of featured secret knowledge and tutorials covering LoRA Fine-Tuning for GPT and BERT, mastering customer segmentation with LLM, and building LLMs from scratch.  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  📥 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! ⚡ TechWave: AI/GPT News & AnalysisAmazon Invests $4 Billion in ChatGPT Rival Anthropic to Advance Safer AI Models: Amazon has leveled up the AI race with an investment amounting up to $4 billion in Anthropic to develop safer AI models. As part of this collaboration, AWS will become Anthropic's primary cloud provider for critical workloads. AWS will provide access to its compute infrastructure, including Trainium and Inferentia chips. Amazon will also expand its support for Amazon Bedrock, allowing developers and engineers to build on top of Anthropic's models. These models, including Claude 2, can be used for various tasks, from dialogue generation to complex reasoning. The partnership aims to promote responsible AI development and deployment and includes support for safety best practices. ChatGPT Introduces Voice and Image Capabilities for Enhanced User Interaction: OpenAI is introducing new voice and image capabilities in ChatGPT, offering users a more intuitive interface. With these additions, users can engage in voice conversations and share images with ChatGPT, opening new possibilities. For instance, users can discuss landmarks while traveling, plan meals by scanning their fridge, or even assist children with math problems using photos. Voice conversations are powered by text-to-speech models, featuring five different voices, while image understanding is facilitated by multimodal GPT models. OpenAI is gradually deploying these features, aiming to ensure their responsible and safe usage. Plus, and Enterprise users will have early access, with broader availability in the future. Microsoft Unveils 'Copilot,' an AI Companion Across Its Ecosystem: Microsoft is introducing a new AI companion called "Microsoft Copilot" designed to enhance user interactions across its ecosystem. This AI will incorporate web context, work data, and real-time PC activity to provide personalized assistance while prioritizing user privacy and security. It will be seamlessly integrated into Windows 11, Microsoft 365, Edge, and Bing, accessible through a right-click or as an app. Copilot will evolve over time, expanding its capabilities and connections to various applications. Additionally, Microsoft is releasing a significant update for Windows 11, along with enhancements in Bing and Edge, delivering more personalized and productive AI-driven experiences to users.  New AI Algorithm Shows Promise in Detecting Signs of Life on Other Planets: Researchers have developed an AI algorithm that can detect subtle molecular patterns indicating the presence of biological signals in samples, even if they are hundreds of millions of years old, with a 90% accuracy rate. This method could revolutionize the search for signs of life on other planets. By training the machine learning algorithm with a set of biotic and abiotic samples, it successfully identified biotic samples, including ancient life preserved in fossils, and abiotic samples, such as lab-created amino acids and carbon-rich meteorites. The AI system has the potential to be used in robotic space explorers and spacecraft orbiting potentially habitable worlds.  AutoGen: A Framework for Streamlining Large Language Model Workflows: Microsoft Research has introduced AutoGen, a framework designed to simplify the orchestration, optimization, and automation of workflows involving LLMs like GPT-4. AutoGen offers customizable agents that can converse and coordinate tasks, integrating LLMs, humans, and tools. By defining agents and their interactions, developers can build complex multi-agent conversation systems, reducing manual effort and coding. AutoGen's agent-centric design handles ambiguity, feedback, and collaboration, making it versatile for various applications, including conversational chess. It's available as a Python package and aims to enable the development of next-generation LLM applications by streamlining workflow management.  OpenAI Enhances DALL-E Art Generator with ChatGPT Integration: OpenAI has unveiled DALL-E 3, an improved version of its text-to-image tool, which now incorporates ChatGPT to simplify the prompt generation process. Subscribers of OpenAI's premium ChatGPT plans can request, and fine-tune image prompts directly within the chat application, receiving results with enhanced descriptions and guidance. DALL-E 3 not only produces higher-quality images, especially with longer prompts, but also handles challenging content like textual descriptions and depictions of human hands more effectively. The model includes safety mechanisms, rejecting requests for images resembling the work of living artists or public figures. OpenAI plans to introduce DALL-E 3 to premium ChatGPT users first, with broader availability to follow. AI Predicted to Boost Enterprise Efficiency by 50% in 2024, Says Forrester: According to Forrester's Predictions 2024 report, AI initiatives are expected to enhance productivity and problem-solving in enterprises by 50% in IT operations. The report also highlights the role of AI in unlocking creative potential and emphasizes the need for responsible AI deployment. While current AI projects have led to up to 40% improvement in software development, the report advises visionary tech executives to strategically realign IT resources to promote innovation and interdisciplinary teamwork. It also notes that AI deployments will require budget spending, and despite a predicted recession in 2024, tech spending is expected to grow.  DeepMind's OPRO: A Novel Approach Using AI Language Models as Optimizers: Researchers from DeepMind have introduced a novel approach called "Optimization by PROmpting" (OPRO), which leverages LLMs like AI models to optimize tasks defined in natural language rather than mathematical terms. The method begins with a "meta-prompt" that describes the task, and the LLM generates candidate solutions based on this description. OPRO then evaluates these solutions, refines them based on past performance, and continues iterating until an optimal solution is found. This approach showed promise in solving mathematical optimization problems. OPRO's strength lies in its ability to optimize LLM prompts for maximum task accuracy.  🔮 Looking for a New Book from Packt’s Expert Community? Learn Ethereum - Second Edition - By Xun (Brian) Wu, Zhihong Zou, Dongying Song Are you eager to refine your coding skills in smart contracts? "Learn Ethereum, 2nd Edition" is your ultimate guide to mastering Ethereum. Dive deep into the realm of blockchain with this comprehensive book, which covers everything from the fundamentals of smart contracts to the cutting-edge technologies in Ethereum.Gain insights into Ethereum's intricate mechanics, delve into Ethereum 2.0 and the Ethereum Virtual Machine, and grasp essential concepts like gas and proof of stake. Take control of L1/L2 scaling solutions, explore DeFi protocols, and understand EVM-compatible blockchains. Additionally, explore advanced topics such as sharding, DAOs, the Metaverse, and NFTs. By the end, you'll be well-prepared to create smart contracts, develop decentralized applications (DApps), and confidently navigate the Ethereum ecosystem. Read the free chapter by clicking the button below!Read through the Chapter 1 unlocked here...  🌟 Secret Knowledge: AI/LLM ResourcesA Primer on Leveraging LLM Techniques: Prompt Engineering, Retrieval Augmented Generation, and Fine Tuning In this post, you'll learn how to navigate the world of LLMs effectively. The article explores three key strategies: Prompt Engineering, Retrieval Augmented Generation, and Fine Tuning, providing insights into when and how to employ these techniques. Prompt Engineering focuses on crafting precise queries to optimize model responses. Retrieval Augmented Generation combines LLMs with external knowledge sources for contextually rich output. Fine Tuning tailors LLMs to specific domains, enhancing their efficiency. Understanding when to use these techniques is vital for harnessing the potential of LLMs in your projects, each offering unique advantages and considerations. Understanding LoRA Fine-Tuning for GPT and BERT: A Visualized Implementation Guide In this post, you'll learn how to implement LoRA (Low-Rank Adaption of Large Language Models) fine-tuning techniques for models like GPT and BERT. Fine-tuning is essential for preparing these models for production, but LoRA offers an efficient way to do it. LoRA involves adding low-parameter weights to pre-trained model weights, significantly reducing the number of parameters to update during training. This guide provides a visualized implementation of LoRA, breaking down the process step by step, and it covers both BERT and GPT implementations. It's a valuable resource for researchers and practitioners looking to enhance their understanding of efficient fine-tuning methods for large language models. Building LLMs from Scratch: Understanding the Process and Costs In this comprehensive article, you'll gain insights into the process of creating LLMs from the ground up. The text delves into the technical aspects of LLM development, focusing on models like GPT-3, Llama, and Falcon. It begins by emphasizing the historical shift from LLM development as an esoteric task to a growing interest among businesses and organizations. The article also provides a cost estimation for training LLMs, considering GPU hours and commercial cloud computing expenses. By reading this post, you'll explore the key considerations and financial aspects of embarking on the journey of building custom LLMs, gaining a deeper understanding of when and why it's worthwhile. 💡 Masterclass: AI/LLM TutorialsMaximizing Throughput for Large Language Models with Batching Techniques: In this informative guide, you'll discover strategies to enhance the throughput performance of LLMs like Llama v2 using batching techniques. The text explains the challenges associated with LLMs, including memory limitations and compute constraints, and introduces three key batching methods: Dynamic Batching, Continuous Batching, and PagedAttention Batching. Each method is thoroughly explained with configuration details, enabling you to optimize LLM inference on platforms like Amazon SageMaker. Through a comparative analysis, you'll gain insights into the significant throughput improvements achieved by these batching techniques, enhancing your understanding of LLM model serving for text generation. Improving LLMs with RLHF on Amazon SageMaker: This text provides a comprehensive guide on enhancing the performance of large language models LLMs using Reinforcement Learning from Human Feedback (RLHF) on Amazon SageMaker. RLHF is crucial for ensuring LLMs produce truthful and helpful content, aligning them with human objectives. The text covers the complexities of RLHF, including training reward models and fine-tuning LLMs, and then demonstrates a step-by-step process for implementing RLHF on Amazon SageMaker. It also explains how to perform human evaluation to quantify improvements in model outputs. The article emphasizes the effectiveness of RLHF in reducing toxicity and highlights the advantages of using Amazon SageMaker for customized LLM development, making it a valuable resource for researchers seeking to optimize LLMs. Mastering Customer Segmentation with LLM: Unlock advanced customer segmentation techniques using LLMs and improve your clustering models with advanced techniques In this post, you'll learn how to employ advanced techniques for customer segmentation, going beyond cluster definition to in-depth analysis. This article is tailored for data scientists aiming to bolster their clustering abilities. Three methods are explored: Kmeans, K-Prototype, and LLM + Kmeans, each dissected for comprehensive understanding. Notably, you'll delve into dimensionality reduction with techniques like PCA, t-SNE, and MCA. The dataset used is a public Kaggle dataset on banking, offering both numerical and categorical data, expanding segmentation possibilities. The post provides insights into data preprocessing, outlier detection using Python Outlier Detection (PyOD) library, and the process of building a Kmeans model. It further covers model evaluation metrics, visualization, and the importance of PCA and t-SNE. Finally, the article analyzes feature importance and cluster characteristics, emphasizing the need for diverse tools in real-world projects for effective customer segmentation.  🚀 HackHub: Trending AI Toolskornia/kornia: PyTorch-based differentiable computer vision library offering a collection of routines and modules to address various computer vision tasks, leveraging PyTorch's efficiency and auto-differentiation capabilities for gradient computation. confident-ai/deepeval: Tool for unit testing LLMs, providing metrics to assess the relevance, consistency, lack of bias, and non-toxicity of LLM responses. It offers a Python-friendly approach for offline evaluations and a user-friendly web UI for analysis. aiwaves-cn/agents: Open-source framework for building autonomous language agents with advanced features like long-short term memory, tool usage, web navigation, multi-agent communication, human-agent interaction, and symbolic control.  OpenBMB/AgentVerse: Versatile framework designed for creating custom multi-agent environments for LLMs with ease, allowing researchers to focus on their research without getting caught up in implementation details.  hpcaitech/ColossalAI: Offers parallel components and user-friendly tools to simplify the process of writing and deploying distributed deep learning models, making it as straightforward as working on a local machine. 
Read more
  • 0
  • 0
  • 7529

article-image-chatgpt-for-seo-and-sentiment-analysis
Valentina Alto
28 Sep 2023
12 min read
Save for later

ChatGPT for SEO and Sentiment Analysis

Valentina Alto
28 Sep 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!This article is an excerpt from the book, Modern Generative AI with ChatGPT and OpenAI Models, by Valentina Alto. Master core data architecture design concepts and Azure Data & AI services to gain a cloud data and AI architect’s perspective to developing end-to-end solutions.IntroductionIn the ever-evolving landscape of digital marketing, the emergence of AI-powered tools has redefined the way businesses engage with their audience. At the forefront of this transformation is ChatGPT, a versatile language model that is proving to be a game changer in two critical domains: Search Engine Optimization (SEO) and Sentiment Analysis. In this article, we embark on a journey to explore how ChatGPT is revolutionizing SEO strategies, enabling businesses to soar in search rankings, and how it wields its prowess in sentiment analysis to decipher customer feedback and enhance product quality.Boosting Search Engine Optimization (SEO)Another promising area for ChatGPT to be a game changer is Search Engine Optimization (SEO). This is the key element behind ranking in search engines such as Google or Bing and it determines whether your websites will be visible to users who are looking for what you promote.DefinitionSEO is a technique used to enhance the visibility and ranking of a website on search engine results pages (SERPs). It is done by optimizing the website or web page to increase the amount and quality of organic (unpaid) traffic from search engines. The purpose of SEO is to attract more targeted visitors to the website by optimizing it for specific keywords or phrases.Imagine you run an e-commerce company called Hat&Gloves, which only sells, as you might have guessed, hats and gloves. You are now creating your e-commerce website and want to optimize its ranking. Let’s ask ChatGPT to list some relevant keywords to embed in our website:Figure 7.18 – Example of SEO keywords generated by ChatGPTAs you can see, ChatGPT was able to create a list of keywords of different kinds. Some of them are pretty intuitive, such as Hats and Gloves. Others are related, with an indirect link. For example, Gift ideas are not necessarily related to my e-commerce business, however, it could be very smart to include it in my keywords, so that I can widen my audience.Another key element of SEO is search engine intent. Search engine intent, also known as user intent, refers to the underlying purpose or goal of a specific search query made by a user in a search engine. Understanding search engine intent is important because it helps businesses and marketers create more targeted and effective content and marketing strategies that align with the searcher’s needs and expectations.There are generally four types of search engine intent:Informational intent: The user is looking for information on a particular topic or question, such as What is the capital of France? or How to make a pizza at home.Navigational intent: The user is looking for a specific website or web page, such as Facebook login or Amazon.com. Commercial intent: The user is looking to buy a product or service, but may not have made a final decision yet. Examples of commercial intent searches include best laptop under $1000 or discount shoes online.Transactional intent: The user has a specific goal to complete a transaction, which might refer to physical purchases or subscribing to services. Examples of transactional intent could be buy iPhone 13 or sign up for a gym membership.By understanding the intent behind specific search queries, businesses, and marketers can create more targeted and effective content that meets the needs and expectations of their target audience. This can lead to higher search engine rankings, more traffic, and ultimately, more conversions and revenue.Now, the question is, will ChatGPT be able to determine the intent of a given request? Before answering, it is worth noticing that the activity of inferring the intent of a given prompt is the core business of Large Language Models (LLMs), including GPT. So, for sure, ChatGPT is able to capture prompts’ intents.The added value here is that we want to see whether ChatGPT is able to determine the intent in a precise domain with a precise taxonomy, that is, the one of marketing. That is the reason why prompt design is once again pivotal in guiding ChatGPT in the right direction.                                                                      Figure 7.19 – Example of keywords clustered by user intent by ChatGPTFinally, we could also go further and leverage once more the Act as… hack, which we already mentioned in Chapter 4. It would be very interesting indeed to understand how to optimize our website so that it reaches as many users as possible. In marketing, this analysis is called an SEO audit. An SEO audit is an evaluation of a website’s SEO performance and potential areas for improvement. An SEO audit is typically conducted by SEO experts, web developers, or marketers, and involves a comprehensive analysis of a website’s technical infrastructure, content, and backlink profile.During an SEO audit, the auditor will typically use a range of tools and techniques to identify areas of improvement, such as keyword analysis, website speed analysis, website architecture analysis, and content analysis. The auditor will then generate a report outlining the key issues, opportunities for improvement, and recommended actions to address them.Let’s ask ChatGPT to act as an SEO expert and instruct us on what an SEO audit report should look like and which metrics and KPIs should include:We can also ask you to give us an example of one of ChatGPT’s suggestions as follows:Figure 7.20 – Example of ChatGPT acting as an SEO expertChatGPT was able to generate a pretty accurate analysis, with relevant comments and suggestions. Overall, ChatGPT has interesting potential for SEO-related activities, and it can be a good tool whether you are building your website from scratch or you want to improve existing ones.Sentiment analysis to improve quality and increase customer satisfactionSentiment analysis is a technique used in marketing to analyze and interpret the emotions and opinions expressed by customers toward a brand, product, or service. It involves the use of natural language processing (NLP) and machine learning (ML) algorithms to identify and classify the sentiment of textual data such as social media posts, customer reviews, and feedback surveys.By performing sentiment analysis, marketers can gain insights into customer perceptions of their brand, identify areas for improvement, and make data-driven decisions to optimize their marketing strategies. For example, they can track the sentiment of customer reviews to identify which products or services are receiving positive or negative feedback and adjust their marketing messaging accordingly.Overall, sentiment analysis is a valuable tool for marketers to understand customer sentiment, gauge customer satisfaction, and develop effective marketing campaigns that resonate with their target audience.Sentiment analysis has been around for a while, so you might be wondering what ChatGPT could bring as added value. Well, besides the accuracy of the analysis (it being the most powerful model on the market right now), ChatGPT differentiates itself from other sentiment analysis tools since it is artificial general intelligence (AGI).This means that when we use ChatGPT for sentiment analysis, we are not using one of its specific APIs for that task: the core idea behind ChatGPT and OpenAI models is that they can assist the user in many general tasks at once, interacting with a task and changing the scope of the analysis according to the user’s request.So, for sure, ChatGPT is able to capture the sentiment of a given text, such as a Twitter post or a product review. However, ChatGPT can also go further and assist in identifying specific aspects of a product or brand that are positively or negatively impacting the sentiment. For example, if customers consistently mention a particular feature of a product in a negative way, ChatGPT can highlight that feature as an area for improvement. Or, ChatGPT might be asked to generate a response to a particularly delicate review, keeping in mind the sentiment of the review and using it as context for the response. Again, it can generate reports that summarize all the negative and positive elements found in reviews or comments and cluster them into categories.Let’s consider the following example. A customer has recently purchased a pair of shoes from my e-commerce company, RunFast, and left the following review:I recently purchased the RunFast Prodigy shoes and have mixed feelings about them. On one hand, the shoes are incredibly comfortable and provide great support for my feet during my daily runs. The cushioning is top-notch and my feet feel less fatigued after my runs than with my previous shoes. Additionally, the design is visually appealing and I received several compliments on them.However, on the other hand, I’ve experienced some durability issues with these shoes. The outsole seems to wear down rather quickly and the upper material, while breathable, is showing signs of wear after only a few weeks of use. This is disappointing, considering the high price point of the shoes.Overall, while I love the comfort and design of the RunFast Prodigy shoes, I’m hesitant to recommend them due to the durability issues I’ve experienced.Let’s ask ChatGPT to capture the sentiment of this review:Figure 7.21 – ChatGPT analyzing a customer reviewFrom the preceding figure, we can see how ChatGPT didn’t limit itself to providing a label: it also explained both the positive and negative elements characterizing the review, which has a mixed feeling and hence can be labeled as neutral overall.Let’s try to go deeper into that and ask some suggestions about improving the product:Figure 7.22 – Suggestions on how to improve my product based on customer feedbackFinally, let’s generate a response to the customer, showing that we, as a company, do care about customers’ feedback and want to improve our products.Figure 7.23 – Response generated by ChatGPTThe example we saw was a very simple one with just one review. Now imagine we have tons of reviews, as well as diverse sales channels where we receive feedback. Imagine the power of tools such as ChatGPT and OpenAI models, which are able to analyze and integrate all of that information and identify the pluses and minuses of your products, as well as capturing customer trends and shopping habits. Additionally, for customer care and retention, we could also automate review responses using the writing style we prefer. In fact, by tailoring your chatbot’s language and tone to meet the specific needs and expectations of your customers, you can create a more engaging and effective customer experience.Here are some examples:Empathetic chatbot: A chatbot that uses an empathetic tone and language to interact with customers who may be experiencing a problem or need help with a sensitive issueProfessional chatbot: A chatbot that uses a professional tone and language to interact with customers who may be looking for specific information or need help with a technical issueConversational chatbot: A chatbot that uses a casual and friendly tone to interact with customers who may be looking for a personalized experience or have a more general inquiryHumorous chatbot: A chatbot that uses humor and witty language to interact with customers who may be looking for a light-hearted experience or to diffuse a tense situationEducational chatbot: A chatbot that uses a teaching style of communication to interact with customers who may be looking to learn more about a product or serviceIn conclusion, ChatGPT can be a powerful tool for businesses to conduct sentiment analysis, improve their quality, and retain their customers. With its advanced natural language processing capabilities, ChatGPT can accurately analyze customer feedback and reviews in real-time, providing businesses with valuable insights into customer sentiment and preferences. By using ChatGPT as part of their customer experience strategy, businesses can quickly identify any issues that may be negatively impacting customer satisfaction and take corrective action. Not only can this help businesses improve their quality but it can also increase customer loyalty and retention.ConclusionIn this article, we learned to enhance SEO analysis, and capture the sentiment of reviews, social media posts, and other customer feedback.As ChatGPT continues to advance and evolve, it is likely that we will see even more involvement in the marketing industry, especially in the way companies engage with their customers. In fact, relying heavily on AI allows companies to gain deeper insights into customer behavior and preferences.The key takeaway for marketers is to embrace these changes and adapt to the new reality of AI-powered marketing in order to stay ahead of the competition and meet the needs of their customers.Author BioValentina Alto graduated in 2021 in data science. Since 2020, she has been working at Microsoft as an Azure solution specialist, and since 2022, she has been focusing on data and AI workloads within the manufacturing and pharmaceutical industry. She has been working closely with system integrators on customer projects to deploy cloud architecture with a focus on modern data platforms, data mesh frameworks, IoT and real-time analytics, Azure Machine Learning, Azure Cognitive Services (including Azure OpenAI Service), and Power BI for dashboarding. Since commencing her academic journey, she has been writing tech articles on statistics, machine learning, deep learning, and AI in various publications and has authored a book on the fundamentals of machine learning with Python.
Read more
  • 0
  • 0
  • 13601

article-image-creating-intelligent-chatbots-with-chatgpt
Swagata Ashwani
28 Sep 2023
5 min read
Save for later

Creating Intelligent Chatbots with ChatGPT

Swagata Ashwani
28 Sep 2023
5 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 and books. Don't miss out – sign up today!IntroductionWe are living in the era of Generative AI and Chatbots have evolved from simple rule-based systems to sophisticated AI-driven entities capable of holding intricate conversations. With tools like OpenAI's ChatGPT, creating a chatbot has never been more accessible. This article dives deep into how one can specialize these chatbots by fine-tuning them for specific industries and applications, using the Science QA dataset as an example.PrerequisitesBefore diving in, please ensure you have the necessary tools installed:OpenAI - Create an account on OpenAI and Generate API keysInstall Streamlit.pip install streamlitA dataset in CSV format for trainingFor this article, we have used the publicly available Science QA dataset for training and validation.Train dataset - ScienceQA-trainValidation dataset- ScienceQA-valWhile OpenAI powers the chatbot's brain, Streamlit will serve as the platform to interact with it.Understanding Fine-tuningFine-tuning is the process of training a pre-trained model on a new dataset to adapt to specific tasks. It's like teaching a general doctor to specialize in a field. The nuances and specific knowledge in the specialized field would aid and enhance to create your specialized dataset.Fine-tuning with OpenAIOpenAI's API simplifies the fine-tuning process. By providing your dataset, OpenAI trains your model to specialize in the subject of your data. First, we download the two CSV files from Science QA and save them in your project folder.Next, we process these CSV files and convert them into JSONL format which is required by OpenAI.import openai import pandas as pd # Use your Open AI access key openai.api_key = "sk-**************************************" # Load the training and validation datasets train_df = pd.read_csv("science-train.csv") val_df = pd.read_csv("science-val.csv") def convert_dataset_to_jsonl(df, file_name): df["conversation"] = df.apply(format_chat, axis=1)   with open(file_name, 'w') as jsonl_file:      for example in df["conversation"]:         jsonl_file.write(example + '\n') # Convert the training and validation datasets convert_dataset_to_jsonl(train_df, "fine_tune_train_data.jsonl") convert_dataset_to_jsonl(val_df, "fine_tune_val_data.jsonl")After converting the datasets to JSONL format, we will upload these datasets to OpenAI for further consumption for model fine-tuning.train = openai.File.create( file=open("fine_tune_train_data.jsonl", "rb"),purpose='fine-tune',) val = openai.File.create( file=open("fine_tune_val_data.jsonl", "rb"),purpose='fine-tune',) print(train) print(val) After printing, save these files names for train and validation sets. Next, we will create the fine-tuned model with your train and test data files. Pass the train and test data file names that we printed in step 3. model = openai.FineTuningJob.create( model = "gpt-3.5-turbo", training_file = train_data, validation_file = val_data, suffix = "scienceqa") print(model)Now, you have successfully created your fine-tuned model. You can also check the status in your OpenAI account.Building the Streamlit AppStreamlit is a game-changer for Python enthusiasts looking to deploy applications without the intricacies of web development. Integrating the fine-tuned model involves invoking OpenAI's API within the Streamlit interface. With customization features, tailor your app to resonate with the theme of your chatbot.You will also add some styling to your web app to create a visually appealing platform for your fine-tuned model.import streamlit as st # Set OpenAI API key openai.api_key = "sk-**********************" # Use your fine-tuned model ID here: FINE_TUNED_MODEL = "ft:gpt-3.5-turbo-****************" # Message system setup messages = [{ "role": "system", "content": "You are an AI specialized in Science and Tech.. "},] def get_chat(): return messages def chatbot(input): if input: chat = get_chat() chat.append({"role": "user", "content": input}) response = openai.ChatCompletion.create( model="ft:gpt-3.5-turbo-*****************", messages=chat, max_tokens=150, ) message = response['choices'][0]['message']['content'] chat.append({"role": "assistant", "content": message}) return message # Custom vibrant styling st.title('AI Chatbot Specialized in Science') st.write("Ask me about Science and Technology) # Sidebar for additional information or actions with st.sidebar: st.write("Instructions:") st.write("1. Ask anything related to Science.") st.write("2. Wait for the AI to provide an insightful answer!") # Main Chat Interface user_input = st.text_area("You: ", "") if st.button("Ask"): user_output = chatbot(user_input) st.text_area("AI's Response:", user_output, height=200)After saving the code, run the code using the following command-strealmit run 'yourpyfilename.py'Voila!! The app is ready!!The app opens on a new web page in your browser. Here is how it looks- Deployment ConsiderationsEnsure your API keys remain confidential. As for scalability, remember each query costs money; caching frequent queries can be a cost-effective strategy.Happy Fine Tuning!!ConclusionFine-tuning a model like GPT-3.5 Turbo for specific industries can immensely boost its effectiveness in those domains. Using tools like Streamlit, the deployment becomes hassle-free. Experiment, iterate, and watch your specialized chatbot come to life!Author BioSwagata Ashwani serves as a Principal Data Scientist at Boomi, where she leads the charge in deploying cutting-edge AI solutions, with a particular emphasis on Natural Language Processing (NLP). With a stellar track record in AI research, she is always on the lookout for the next state-of-the-art tool or technique to revolutionize the industry. Beyond her technical expertise, Swagata is a fervent advocate for women in tech. She believes in giving back to the community, regularly contributing to open-source initiatives that drive the democratization of technology.Swagata's passion isn't limited to the world of AI; she is a nature enthusiast, often wandering beaches and indulging in the serenity they offer. With a cup of coffee in hand, she finds joy in the rhythm of dance and the tranquility of the great outdoors.
Read more
  • 0
  • 0
  • 21266

article-image-chatgpt-for-data-analysis
Rohan Chikorde
27 Sep 2023
11 min read
Save for later

ChatGPT for Data Analysis

Rohan Chikorde
27 Sep 2023
11 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 and books. Don't miss out – sign up today!Introduction As datasets continue growing rapidly in size and complexity, exploring, preparing, and documenting data is taking data scientists more and more time. While coding is crucial for actually implementing analyses, there's an opportunity to make the conceptual work more efficient through a simpler way of interaction.ChatGPT - an AI tool that truly understands what you're saying and can have natural back-and-forth conversations. By casually describing what you need to do, its broad knowledge lets it generate sample results, code snippets, and documentation without you writing a single line.In this fast-paced world where time is precious, ChatGPT seems like a helpful extra pair of hands. You can bounce ideas off it 24/7 to test hypotheses and get artifacts to aid your own work.Now, it definitely won't take over your job. However, facilitating exploratory talks and quick prototyping through plain speech, it opens up new approaches that maximize our growing computing power.In this post, I'll demonstrate how ChatGPT streamlines common analyst tasks through example conversations. While coding is still king, it serves as a supplementary brain to speed up the often overlooked definition stage - arguably the most important part. Ultimately, combining human intuition with AI assistance will vastly improve our ability to extract value from huge datasets at lightning speed.Let's see how it can supplement analysis in practice.You can access the entire code here: https://github.com/rohanchikorde/chatgpt-for-data-analysis.gitCreating a Sample DataFrame using ChatGPTHere is a sample prompt to have ChatGPT generate a Pandas DataFrame containing fake customer data:Prompt:Hey, can you help me create a sample DataFrame containing customer data that I can use to demonstrate exploring and analyzing a dataset through conversations? I need it to contain around 10 rows with the following columns: ·        'CustomerID' (integer index) ·        'Name' (string of first and last name) ·        'Age' (integer) ·        'State' (string of two-letter state abbreviation) ·        'Annual Income' (integer dollar amount) You can generate random values for the names, ages between 20-60, state abbreviations from the 50 US states, and incomes between $30,000-$150,000. It would be great if you could print out the code to create a Pandas DataFrame with this sample data so I have something concrete to reference in example conversations. Having sample data upfront will make it easier to walk through tasks like summarizing, grouping, plotting and more.Viola! Here, is the generated Python code by ChatGPT:import pandas as pd import numpy as np # Define the number of rows num_rows = 10 # Set random seed for reproducibility np.random.seed(42) # Define the state abbreviations for the 50 US states state_abbreviations = ['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'FL', 'GA',                       'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD',                       'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ',                       'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC',                       'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY'] # Generate random sample data customer_data = {    'CustomerID': range(1, num_rows + 1),    'Name': [' '.join(np.random.choice(['John', 'Jane', 'David', 'Sarah', 'Michael', 'Emily'], 1)) +             ' ' +             np.random.choice(['Smith', 'Johnson', 'Williams', 'Jones', 'Brown', 'Davis'], 1)[0]            for _ in range(num_rows)],    'Age': np.random.randint(20, 61, num_rows),    'State': np.random.choice(state_abbreviations, num_rows),    'Annual Income': np.random.randint(30000, 150001, num_rows) } # Create the DataFrame df = pd.DataFrame(customer_data) # Print the DataFrame print(df) After running the above code:Descriptive StatisticsDescriptive statistics are a fundamental aspect of data analysis that provides a summary of the main characteristics of a dataset. They help us understand the distribution, central tendency, and variability of the data. Let's explore some common descriptive statistics and how they can be calculated and interpreted:Measures of Central Tendency:Mean: It represents the average value of a dataset and is computed by summing all the values and dividing by the number of observations.Median: It corresponds to the middle value of a dataset when it is sorted in ascending or descending order. It is less affected by extreme values compared to the mean.Mode: It is the most frequently occurring value in a dataset. Python Code by ChatGPT:import pandas as pd # Calculate the mean age_mean = df['Age'].mean() income_mean = df['Annual Income'].mean() # Calculate the median age_median = df['Age'].median() income_median = df['Annual Income'].median() # Calculate the mode age_mode = df['Age'].mode().values income_mode = df['Annual Income'].mode().values # Print the results print("Age Mean:", age_mean) print("Age Median:", age_median) print("Age Mode:", age_mode) print("Income Mean:", income_mean) print("Income Median:", income_median) print("Income Mode:", income_mode)Output in Python environment:Measures of Dispersion/VariabilityRange: It is the difference between the maximum and minimum values in a dataset, providing an idea of the spread of the data.Variance: It quantifies the average squared deviation of each data point from the mean. A higher variance indicates greater dispersion.Standard Deviation: It is the square root of the variance and provides a measure of the average distance between each data point and the mean. Python code generated by ChatGPT: import pandas as pd # Calculate the range age_range = df['Age'].max() - df['Age'].min() income_range = df['Annual Income'].max() - df['Annual Income'].min() # Calculate the variance age_variance = df['Age'].var() income_variance = df['Annual Income'].var() # Calculate the standard deviation age_std_dev = df['Age'].std() income_std_dev = df['Annual Income'].std() # Print the results print("Age Range:", age_range) print("Age Variance:", age_variance) print("Age Standard Deviation:", age_std_dev) print("Income Range:", income_range) print("Income Variance:", income_variance) print("Income Standard Deviation:", income_std_dev) Output in Python environment:PercentilesPercentiles divide a dataset into hundredths, allowing us to understand how values are distributed. The median corresponds to the 50th percentile.Quartiles divide the dataset into quarters, with the first quartile (Q1) representing the 25th percentile and the third quartile (Q3) representing the 75th percentile.Python code generated by ChatGPT:import pandas as pd # Calculate the percentiles age_percentiles = df['Age'].quantile([0.25, 0.5, 0.75]) income_percentiles = df['Annual Income'].quantile([0.25, 0.5, 0.75]) # Extract the quartiles age_q1, age_median, age_q3 = age_percentiles income_q1, income_median, income_q3 = income_percentiles # Print the results print("Age Percentiles:") print("Q1 (25th percentile):", age_q1) print("Median (50th percentile):", age_median) print("Q3 (75th percentile):", age_q3) print("\nIncome Percentiles:") print("Q1 (25th percentile):", income_q1) print("Median (50th percentile):", income_median) print("Q3 (75th percentile):", income_q3)Output in Python environment:Skewness and KurtosisSkewness measures the asymmetry of a distribution. A positive skew indicates a longer tail on the right, while a negative skew indicates a longer tail on the left.Kurtosis measures the heaviness of the tails of a distribution. High kurtosis implies more extreme values, while low kurtosis indicates a flatter distribution. Python Code generated by ChatGPT:import pandas as pd # Calculate the skewness age_skewness = df['Age'].skew() income_skewness = df['Annual Income'].skew() # Calculate the kurtosis age_kurtosis = df['Age'].kurtosis() income_kurtosis = df['Annual Income'].kurtosis() # Print the results print("Age Skewness:", age_skewness) print("Income Skewness:", income_skewness) print("\nAge Kurtosis:", age_kurtosis) print("Income Kurtosis:", income_kurtosis) Output in Python jupyter notebook:Grouping and AggregationGrouping and aggregation in Python are powerful techniques for analyzing data by grouping it based on specific criteria and calculating summary statistics or performing aggregate functions on each group. Here's the Python code to group the data by state and find the average age and income for each state:import pandas as pd # Group the data by State and calculate the average age and income grouped_data = df.groupby('State').agg({'Age': 'mean', 'Annual Income': 'mean'}) # Print the grouped data print(grouped_data) Output in Python jupyter notebook:In this code, ChatGPT uses the groupby function from the Pandas library to group the data in the DataFrame df by the 'State' column. It then uses the agg function to specify the aggregation functions we want to apply to each group. In this case, it calculates the mean of the 'Age' and 'Annual Income' columns for each state.The output of this code will be a new DataFrame containing the grouped data with the average age and income for each state. The DataFrame will have the 'State' column as the index and two additional columns: 'Age' and 'Annual Income', representing the average values for each state.Data VisualizationHistogram of AgeThe histogram provides a visual representation of the distribution of ages in the dataset. The x-axis represents the age values, and the y-axis represents the frequency or count of individuals falling into each age bin. The shape of the histogram can provide insights into the data's central tendency, variability, and any skewness in the distribution.Scatter Plot: Age vs. Annual IncomeThe scatter plot visualizes the relationship between age and annual income for each data point. Each point on the plot represents an individual's age and their corresponding annual income. By plotting the data points, we can observe patterns, clusters, or trends in the relationship between these two variables. The scatter plot helps identify any potential correlation or lack thereof between age and income.Python Code for histogram and scatterplot generated by ChatGPT:import matplotlib.pyplot as plt # Plot a histogram of the Age variable plt.hist(df['Age']) plt.xlabel('Age') plt.ylabel('Frequency') plt.title('Histogram of Age') plt.show() # Plot a scatter plot between Age and Income plt.scatter(df['Age'], df['Annual Income']) plt.xlabel('Age') plt.ylabel('Annual Income') plt.title('Scatter Plot: Age vs. Annual Income') plt.show() Output in Python jupyter notebook In this code, ChatGPT uses the hist function from the matplotlib library to plot a histogram of the 'Age' variable. The histogram visualizes the distribution of ages in the dataset. It set the x-axis label to 'Age', the y-axis label to 'Frequency' (indicating the count of individuals in each age group), and give the plot a title which is super cool.Next, it used the scatter function to create a scatter plot between 'Age' and 'Annual Income'. The scatter plot shows the relationship between age and annual income for each data point. It sets the x-axis label to 'Age', the y-axis label to 'Annual Income', and gives the plot a title.ConclusionIn this blog, we explored a couple of examples showing how ChatGPT can streamline various aspects of data analysis through natural conversation. By simply describing our needs, it was able to generate sample Python code for us without writing a single line of code. While the results require human review, ChatGPT handles much of the prototyping work rapidly.For data scientists who understand programming but want to focus more on problem definition, ChatGPT serves as a helpful digital assistant to offload some of the repetitive technical work. It also opens up analysis to those without coding skills by abstracting the process into simple question-and-response dialogue. While ChatGPT does not replace human expertise, it makes the analysis process more approachable and efficient overall.Going forward, as chatbots advance in capabilities, we may see them automating ever more complex portions of the data science lifecycle through natural language. But for now, even with its limitations, ChatGPT has proven quite useful as a dialogue-driven aid for getting initial insights, especially when time is of the essence. I hope this post demonstrates how accessible and powerful conversational data science can be.Author BioRohan Chikorde is an accomplished AI Architect professional with a post-graduate in Machine Learning and Artificial Intelligence. With almost a decade of experience, he has successfully developed deep learning and machine learning models for various business applications. Rohan's expertise spans multiple domains, and he excels in programming languages such as R and Python, as well as analytics techniques like regression analysis and data mining. In addition to his technical prowess, he is an effective communicator, mentor, and team leader. Rohan's passion lies in machine learning, deep learning, and computer vision.
Read more
  • 0
  • 0
  • 20764
article-image-getting-started-with-langchain
Sangita Mahala
27 Sep 2023
7 min read
Save for later

Getting Started with LangChain

Sangita Mahala
27 Sep 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 and books. Don't miss out – sign up today!IntroductionLangChain was launched in October 2022 as an open-source project by Harrison Chase. It is a Python framework that makes it easy to work with large language models (LLMs) such as the OpenAI GPT-3 language model. LangChain provides an easy-to-use API that makes it simple to interact with LLMs. You can use the API to generate text, translate languages, and answer the questions.Why to use LangChain?As we know, LangChain is a powerful tool that can be used to build a wide variety of applications and improve the productivity and quality of tasks. There are many reasons to use LangChain , including :Simplicity: LangChain provides a simple and easy interface for interacting with GPT-3. You don't need to worry about the details of the OpenAI API.Flexibility: LangChain allows you to customize the way you interact with GPT-3. You can use LangChain to build your own custom applications.Reduced costs: LangChain can help you to reduce costs by eliminating the need to hire human experts to perform LLM-related tasks.Increased productivity: LangChain can help you to increase your productivity by making it easy to generate high-quality text, translate languages, write creative content, and answer questions in an informative way.Getting Started with LangChain LLMIn order to completely understand LangChain and how to apply it in a practical use-case situation. Firstly, you have to set up the development environment.InstallationTo get started with LangChain, you have to:Step-1: Install the LangChain Python library:pip install langchain Step-2: Install the the openai package:pip install openaiStep-3: Obtain an OpenAI API key:In order to be able to use OpenAI’s models through LangChain you need to fetch an API key from OpenAI as well. So you have to follow these steps:Go to the OpenAI website by clicking this link: https://platform.openai.com/ Go to the top right corner of your screen and then click on the “Sign up” or “Sign in” if you already have an account. After signing in, you’ll be directed to the OpenAI Dashboard.Now navigate to the right corner of your OpenAI dashboard and click on the Personal button and then click on the “View API keys” section. Once you click “View API keys”, you will be redirected to the API keys section page. Then click on “+ Create new secret key”.Now provide a name for creating a secret key. For example : LangChain Once you click the create secret key button you will redirected to the secret key prompt then copy the API key and click done.The API key should look like a long alphanumeric string (for example: “sk-12345abcdeABCDEfghijKLMNZC”).Note- Please save this secret key safe and accessible. For security reasons, you won’t be able to view it again through your OpenAI account. If you lose this secret key, you’ll need to generate a new one.Step-4After getting the API key, you should execute the following command to add it as an environment variable: export OPENAI_API_KEY="..."If you'd prefer not to set an environment variable you can pass the key in directly via the openai_api_key named parameter when initiating the OpenAI LLM class:from langchain.llms import OpenAI llm = OpenAI(openai_api_key="...")For Example:Here are some of the best hands-on examples of LangChain applications:Content generationLangChain can also be used to generate text content, such as blog posts, marketing materials, and code. This can help businesses to save time and produce high-quality content.Output:Oh, feathered friend, so free and light, You dance across the azure sky, A symphony of colors bright, A song of joy that never dies. Your wings outstretched, you soar above, A glimpse of heaven from on high, Your spirit wild, your spirit love, A symbol of the endless sky.Translating LanguagesLangChain can also be used to translate languages accurately and efficiently. This can make it easier for people to interact with people around the world and for businesses to function in different nations.Example:Output:Question answeringLangChain can also be used to build question answering systems that can provide comprehensive and informative answers to users' questions. Question answering can be used for educational, research, and customer support tools.Example:Output:Check out LangChain’s official documentation to explore various toolkits available and to get access to their free guides and example use cases.How LangChain can be used to build the future of AIThere are several ways that LangChain can be utilized to build the AI of the future.Creating LLMs that are more effective and accurate By giving LLMs access to more information and resources, LangChain can help them perform better. LangChain, for example, can be used to link LLMs to knowledge databases or to other LLMs. LLMs can provide us with a better understanding of the world as a result, and their replies may be more accurate and insightful.Making LLMs more accessibleRegardless of a user's level of technical proficiency, LangChain makes using LLMs simpler. This may provide more equitable access to LLMs and enable individuals to use them to develop new, cutting-edge applications. For example, LangChain may be used to create web-based or mobile applications that enable users to communicate with LLMs without writing any code.Developing a new LLM applicationIt is simple with LangChain due to its chatbot, content generator, and translation systems. This could accelerate the deployment of LLMs across several businesses. For example, LangChain may be utilized for building chatbots that can assist doctors in illness diagnosis or to generate content-generating systems that can assist companies in developing personalized marketing materials.ConclusionIn this article, we've explored LangChain's main capabilities, given some interesting examples of its uses, and provided a step-by-step guide to help you start your AI adventure. LangChain is not just a tool; it's a gateway to the future of AI.  The adoption of LLMs in a variety of industries is accelerated by making it simpler to design and deploy LLM-powered applications.It will provide lots of advantages, such as higher production, enhanced quality, lower prices, simplicity of use, and flexibility. The ability of LangChain, as an entire system, to revolutionize how we interface with computers makes it a tremendous instrument. It assists in the development of the AI of the future by making it simpler to create and deploy LLM-powered applications. Now, it's your turn to unlock the full potential of AI with LangChain. The future is waiting for you, and it starts with you.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
  • 14444

article-image-personalization-at-scale-using-snowflake-and-generative-ai
Shankar Narayanan
27 Sep 2023
9 min read
Save for later

Personalization at Scale: Using Snowflake and Generative AI

Shankar Narayanan
27 Sep 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 and books. Don't miss out – sign up today!IntroductionImagine your customers still picking your competitors even after your incredible offerings. It sounds strange.We live in an era where customers want tailored offerings or solutions from businesses. Anticipating and meeting customer needs is no longer enough. Companies must exceed their expectations and create more authentic and long-term customer interactions. At every touchpoint of the customer journey, whether online or on your website, they want a tailored experience.However, exceeding these expectations can be daunting. So, how should businesses do it? Organizations need robust data management solutions and cutting-edge technologies to meet customer needs and offer tailored experiences. One such powerful combination is the use of Snowflake Data Cloud for Generative AI, which allows businesses to craft tailored customer experiences at scale.In this blog, we'll explore how Snowflake's Data Cloud and Generative AI can help achieve this and the importance of Snowflake Industry Cloud and Marketplace Data in this context.Before we hop on to it, let’s understand Generative AI and the importance of personalization.Why is Hyper-Personalization Critical for Businesses?Personalization significantly impacts customer satisfaction, loyalty, and retention rates. Tailored experiences are about more than addressing by their name several times in a message. But it's more about understanding customer needs and preferences and personalizing communication and marketing efforts that lead to higher click-through rates, conversations, and customer satisfaction.Personalization creates a more profound customer connection, drives engagement, and increases conversion rates. However, achieving personalization at scale presents significant challenges, primarily because it relies on lots of data and the ability to process and analyze it quickly.How is Generative AI Powering Personalization?Generative AI is an artificial intelligence technology capable of effectively producing various types of content like text, images, and other media. These generative models usually learn the patterns and structure of their input data and then generate new data or results with similar characteristics.This technology has undoubtedly revolutionized many businesses. And hyper-personalization is one of the reasons behind it. Generative AI doesn't just analyze data but also has the potential to achieve unprecedented levels of personalization.In this ever-evolving landscape of businesses, Generative AI models can constantly seek ways to improve customer engagement and sales through personalization. It tailors every aspect of customer experience and leaves room for real-time interactions.Here’s how it can be helpful to businesses in many ways:1. Dynamic content: It can produce different types of content like emailers, newsletters, social media copy, website content, marketing materials, and more.2. Recommendations: These models can understand and analyze customer behavior and preferences to offer personalized recommendations.3. Chatbots and virtual assistants: If businesses want real-time customer assistance, generative AI-powered virtual assistants can come to the rescue.4. Pricing strategy: Generative AI also helps optimize pricing strategies for customers by understanding and analyzing their browsing history, purchasing behavior, market pricing, and overall customer journey.5. Natural Language Processing (NLP): NLP models can understand and respond to customer inquiries and feedback in a personalized manner, enhancing customer service.Snowflake Data Cloud: A Game-Changer for Data ManagementSnowflake isn't just another technology company. Snowflake is a cloud-based data platform that has revolutionized how organizations manage and utilize their data. It offers several key advantages that are critical for personalization at scale: 1. Data IntegrationSnowflake enables seamless data integration from various sources, including structured and semi-structured data. This data consolidation is crucial for creating a holistic view of customer behavior and preferences. 2. ScalabilitySnowflake's architecture allows for elastic scalability, meaning you can effortlessly handle growing datasets and workloads, making it ideal for personalization efforts that need to accommodate a large user base.3. Data SharingSnowflake's data-sharing capabilities make it easy to collaborate with partners and share data securely, which can be valuable for personalization initiatives involving third-party data.4. SecuritySecurity is paramount when dealing with customer data. Snowflake offers robust security features to protect sensitive information and comply with data privacy regulations.5. Real-time Data ProcessingSnowflake's cloud-native architecture supports real-time data processing, a fundamental requirement for delivering personalized experiences in real-time or near-real-time.Also, to measure the effectiveness of personalization, one can conduct the A/B tests. Let us see an example to understand the same.import numpy as np from scipy import stats # A/B test data (conversion rates) group_a = [0, 1, 1, 0, 1, 0, 0, 1, 0, 0] group_b = [1, 1, 1, 0, 0, 1, 0, 1, 1, 0] # Perform a t-test to compare conversion rates t_stat, p_value = stats.ttest_ind(group_a, group_b) if p_value < 0.05:    print("Personalization is statistically significant.") else:    print("No significant difference observed.") In this way, you can analyze the results of A/B tests. It would help to determine if personalization efforts are statistically significant in improving customer experiences.Snowflake Industry Cloud and Marketplace DataWhile Snowflake's core features make it a powerful platform for data management, its Industry Cloud and Marketplace Data offerings take personalization to the next level:1. Industry CloudSnowflake's Industry Cloud solutions provide industry-specific data models and best practices. This means organizations can quickly adopt personalized solutions tailored to their specific sector, whether healthcare, retail, finance, or any other domain.2. Marketplace DataThe Snowflake Marketplace offers many data sources and tools to augment personalization efforts. This includes third-party data, pre-built machine learning models, and analytics solutions, making enriching customer profiles easier and driving better personalization.Personalization at Scale with Snowflake Data CloudGenerative AI and Snowflake Data Cloud play a pivotal role in revolutionizing businesses. And leveraging the capabilities of the Snowflake cloud data platform, along with Generative AI to scale personalization, can be a game-changer for many industries. Here's how you can accomplish this seamlessly and effectively.3. Data Ingestion Snowflake allows you to ingest data from various sources, including your CRM, website, mobile app, and third-party data providers. This data is stored in a central repository, ready for analysis.4. Data Storage Snowflake's data integration capabilities enable you to consolidate this data, creating a comprehensive customer profile that includes historical interactions, purchase history, preferences, and more. It can handle massive amounts of data, so businesses can easily store and collect data per their needs and preferences.Snowflake’s scalability helps one to handle large databases efficiently. In the SQL snippet, let us see how to create a table for storing and loading data from a CSV file.-- Create a table to store customer data CREATE TABLE customers (    customer_id INT,    first_name VARCHAR,    last_name VARCHAR,    email VARCHAR,    purchase_history ARRAY,    last_visit_date DATE ); -- Load customer data into the table COPY INTO customers FROM 's3://your-data-bucket/customer_data.csv' FILE_FORMAT = (TYPE = 'CSV' SKIP_HEADER = 1);5. Machine Learning and Generative AI With your data in Snowflake, you can leverage Generative AI models to analyze customer behavior and generate personalized content, recommendations, and predictions.We can understand this using a Python code.import openai # Your OpenAI API key api_key = "your_api_key" # Customer behavior data customer_history = "Customer recently purchased a laptop and smartphone." # Generate personalized recommendations response = openai.Completion.create(    engine="text-davinci-002",    prompt=f"Based on customer data: {customer_history}, recommend products: ",    max_tokens=50,    n = 5,  # Number of recommendations    stop=None,    temperature=0.7,    api_key=api_key ) recommendations = [choice["text"] for choice in response["choices"]] print(recommendations) Using the OpenAI GPT-3 model, we can generate personalized product recommendations, considering customers' purchase history.6. Real-time Processing Snowflake's real-time data processing capabilities ensure that these personalized experiences are delivered in real-time or with minimal latency, enhancing customer engagement. Let us see how we can utilize Snowflake and a hypothetical real-time recommendation engine:-- Create a view to fetch real-time recommendations using a stored procedure CREATE OR REPLACE VIEW real_time_recommendations AS SELECT    c.customer_id,    c.first_name,    c.last_name,    r.recommendation_text FROM    customers c JOIN    real_time_recommendations_function(c.customer_id) r ON    c.customer_id = r.customer_id; 7. Iterative Improvement Personalization is an ongoing process. Snowflake's scalability and flexibility allow you to continuously refine your personalization strategies based on customer feedback and changing preferences.ConclusionCompetition is kicking off in every industry. Businesses can't just focus on creating products that solve specific problems. Instead, the focus has shifted to personalization in this competitive landscape. It's a must-have and non-negotiable. Businesses cannot afford to avoid it if they want to create excellent customer experiences.This is where Snowflake Data Cloud comes to the rescue. Leveraging this platform along with Generative AI can empower organizations in many ways and help craft tailored customer experiences at scale. If appropriately leveraged, businesses can gain a competitive edge and cater to customers' unique demands by delivering personalized solutions.In today's competitive era, only those businesses that invest in personalized technology and marketing efforts will survive. Thus, it's time to embrace these technologies and use them to your advantage to gain long-term success.Author BioShankar Narayanan (aka Shanky) has worked on numerous different cloud and emerging technologies like Azure, AWS, Google Cloud, IoT, Industry 4.0, and DevOps to name a few. He has led the architecture design and implementation for many Enterprise customers and helped enable them to break the barrier and take the first step towards a long and successful cloud journey. He was one of the early adopters of Microsoft Azure and Snowflake Data Cloud. Shanky likes to contribute back to the community. He contributes to open source is a frequently sought-after speaker and has delivered numerous talks on Microsoft Technologies and Snowflake. He is recognized as a Data Superhero by Snowflake and SAP Community Topic leader by SAP.
Read more
  • 0
  • 0
  • 6257

article-image-getting-started-with-microsofts-guidance
Prakhar Mishra
26 Sep 2023
8 min read
Save for later

Getting Started with Microsoft’s Guidance

Prakhar Mishra
26 Sep 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 and books. Don't miss out – sign up today!IntroductionThe emergence of a massive language model is a watershed moment in the field of artificial intelligence (AI) and natural language processing (NLP). Because of their extraordinary capacity to write human-like text and perform a range of language-related tasks, these models, which are based on deep learning techniques, have earned considerable interest and acceptance. This field has undergone significant scientific developments in recent years. Researchers all over the world have been developing better and more domain-specific LLMs to meet the needs of various use cases.Large Language Models (LLMs) such as GPT-3 and its descendants, like any technology or strategy, have downsides and limits. And, in order to use LLMs properly, ethically, and to their maximum capacity, it is critical to grasp their downsides and limitations. Unlike large language models such as GPT-4, which can follow the majority of commands. Language models that are not equivalently large enough (such as GPT-2, LLaMa, and its derivatives) frequently suffer from the difficulty of not following instructions adequately, particularly the part of instruction that asks for generating output in a specific structure. This causes a bottleneck when constructing a pipeline in which the output of LLMs is fed to other downstream functions.Introducing Guidance - an effective and efficient means of controlling modern language models compared to conventional prompting methods. It supports both open (LLaMa, GPT-2, Alpaca, and so on) and closed LLMs (ChatGPT, GPT-4, and so on). It can be considered as a part of a larger ecosystem of tools for expanding the capabilities of language models.Guidance uses Handlebars - a templating language. Handlebars allow us to build semantic templates effectively by compiling templates into JavaScript functions. Making it’s execution faster than other templating engines. Guidance also integrates well with Jsonformer - a bulletproof way to generate structured JSON from language models. Here’s a detailed notebook on the same. Also, in case you were to use OpenAI from Azure AI then Guidance has you covered - notebook.Moving on to some of the outstanding features that Guidance offers. Feel free to check out the entire list of features.Features1. Guidance Acceleration - This addition significantly improves inference performance by efficiently utilizing the Key/Value caches as we proceed through the prompt by keeping a session state with the LLM inference. Benchmarking revealed a 50% reduction in runtime when compared to standard prompting approaches. Here’s the link to one of the benchmarking exercises. The below image shows an example of generating a character profile of an RPG game in JSON format. The green highlights are the generations done by the model, whereas the blue and no highlights are the ones that are copied as it is from the input prompt, unlike the traditional method that tries to generate every bit of it.SourceNote: As of now, the Guidance Acceleration feature is implemented for open LLMs. We can soon expect to see if working with closed LLMs as well.2.  Token Healing - This feature attempts to correct tokenization artifacts that commonly occur at the border between the end of a prompt and the start of a group of generated tokens.For example - If we ask LLM to auto-complete a URL with the below-mentioned Input, it’s likely to produce the shown output. Apart from the obvious limitation that the URL might not be valid. I'd like to draw your attention to the extra space it creates (highlighted in red). Such considerations make it difficult to construct a dependable parsing function and robustly absorb its result into subsequent phases.Input: “The link is <a href=http:”Actual Output: “The link is <a href=http: //www.google.com/search?q”Expected Output: “The link is <a href=http://www.google.com/search?q” This is the exact bucket of problems that Token Healing tries to solve using the backtracking method. Feel free to check out this jupyter notebook for more examples.3. Guaranteed Output Structure - Large language models are fantastic at producing useful outputs, but not so much at producing outputs in a specified format (especially open-source ones like LLaMa, GPT-2, and so on). When we want to use the output of a language model as input to another system, this is frequently an issue. With Handlebars, guidance guarantees the output format to be the same as what was being asked for.Let’s now see Guidance in action -InstallationInstalling guidance is a breeze, just do a pip :$ pip install guidanceAssume we are now creating a product description for an e-commerce website. Here's how the traditional generation compares to the guidance generation. Feel free to play with this colab notebook with both the below examples.Traditional GenerationInput:Consider you are an e-commerce expert. You need to write a product description for a product to be listed on the e-commerce website. The product description consists of fixed set of fields to be filled in the JSON. The below shown JSON is the structure of the description with constraints for each of the attributes enclosed within < > brackets. Please follow the mentioned constraint and format diligently. { prod_id: <numeric value of 5 digits>, prod_name: <name starts with the prefix 'p_'>, prod_price: <should be an integer between 1 and 16. Should end with suffix '$'> } The product description isOutput:Consider you are an e-commerce expert. You need to write a product description for a product to be listed on the e-commerce website. The product description consists of fixed set of fields to be filled in the JSON. The below shown JSON is the structure of the description with constraints for each of the attributes enclosed within < > brackets. Please follow the mentioned constraint and format diligently. {   prod_id: <numeric value of 5 digits>,     prod_name: <name starts with the prefix 'p_'>, prod_price: <should be an integer between 1 and 16. Should end with suffix '$'> } The product description is { resentprod_id: <numeric value of 5 digits>, resentprod_name: <name begins with the prefix 'p_'>, resentprod_price: <should be an integer between 1 and 16. Should end with suffix '$'> } In the above example, the product description has 5 constraint fields and 5 attribute fields. The constraints are as follows: resentprod_id: - value of 5 digits, resentprod_name: - name of the product, resentprod_price: - price of the product, resentprod_price_suffix: - suffix of the product price, resentprod_id: - the product id, resentpro diabetic_id: value of 4 digits, resentprod_ astronomer_id: - value of 4 digits, resentprod_ star_id: - value of 4 digits, resentprod_is_generic: - if the product is generic and not the generic type, resentprod_type: - the type of the product, resentprod_is_generic_typeHere’s the code for the above example with GPT-2 language model -``` from transformers import AutoModelForCausalLM, AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("gpt2-large") model = AutoModelForCausalLM.from_pretrained("gpt2-large") inputs = tokenizer(Input, return_tensors="pt") tokens = model.generate( **inputs, max_new_tokens=256, temperature=0.7, do_sample=True, )Output:tokenizer.decode(tokens[0], skip_special_tokens=True)) ```Guidance GenerationInput w/ code:guidance.llm = guidance.llms.Transformers("gpt-large") # define the prompt program = guidance("""Consider you are an e-commerce expert. You need to write a product description for a product to be listed on the e-commerce website. The product description consists of fixed set of fields to be filled in the JSON. The following is the format ```json { "prod_id": "{{gen 'id' pattern='[0-9]{5}' stop=','}}", "prod_name": "{{gen 'name' pattern='p_[A-Za-z]+' stop=','}}", "prod_price": "{{gen 'price' pattern='\b([1-9]|1[0-6])\b\$' stop=','}}" }```""") # execute the prompt Output = program()Output:Consider you are an e-commerce expert. You need to write a product description for a product to be listed on the e-commerce website. The product description consists of a fixed set of fields to be filled in the JSON. The following is the format```json { "prod_id": "11231", "prod_name": "p_pizzas", "prod_price": "11$" }```As seen in the preceding instances, with guidance, we can be certain that the output format will be followed within the given restrictions no matter how many times we execute the identical prompt. This capability makes it an excellent choice for constructing any dependable and strong multi-step LLM pipeline.I hope this overview of Guidance has helped you realize the value it may provide to your daily prompt development cycle. Also, here’s a consolidated notebook showcasing all the features of Guidance, feel free to check it out.Author BioPrakhar 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
  • 9993
article-image-chatgpt-for-time-series-analysis
Bhavishya Pandit
26 Sep 2023
11 min read
Save for later

ChatGPT for Time Series Analysis

Bhavishya Pandit
26 Sep 2023
11 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 and books. Don't miss out – sign up today!IntroductionIn the era of artificial intelligence, ChatGPT stands as a remarkable example of natural language understanding and generation. Developed by OpenAI, ChatGPT is an advanced language model designed to comprehend and generate human-like text, making it a versatile tool for a wide range of applications.One of the critical domains where ChatGPT can make a significant impact is time series analysis. Time series data, consisting of sequential observations over time, is fundamental across industries such as finance, healthcare, and energy. It enables organizations to uncover trends, forecast future values, and detect anomalies, all of which are invaluable for data-driven decision-making. Whether it's predicting stock prices, monitoring patient health, or optimizing energy consumption, the ability to analyze time series data accurately is paramount.The purpose of this article is to explore the synergy between ChatGPT and time series analysis. We will delve into how ChatGPT's natural language capabilities can be harnessed to streamline data preparation, improve forecasting accuracy, and enhance anomaly detection in time series data. Through practical examples and code demonstrations, we aim to illustrate how ChatGPT can be a powerful ally for data scientists and analysts in their quest for actionable insights from time series data.1. Understanding Time Series Data Time series data is a specialized type of data that records observations, measurements, or events at successive time intervals. Unlike cross-sectional data, which captures information at a single point in time, time series data captures data points in a sequential order, often with a regular time interval between them. This temporal aspect makes time series data unique and valuable for various applications. Characteristics of Time Series Data:Temporal Order: Time series data is ordered chronologically, with each data point associated with a specific timestamp or time period.Dependency: Data points in a time series are often dependent on previous observations, making them suitable for trend analysis and forecasting.Seasonality: Many time series exhibit repetitive patterns or seasonality, which can be daily, weekly, monthly, or annual, depending on the domain.Noise and Anomalies: Time series data may contain noise, irregularities, and occasional anomalies that need to be identified and addressed. Real-World Applications of Time Series Analysis:Time series analysis is a crucial tool in numerous domains, including:Finance: Predicting stock prices, currency exchange rates, and market trends.Healthcare: Monitoring patient vital signs, disease progression, and healthcare resource optimization.Energy: Forecasting energy consumption, renewable energy generation, and grid management.Climate Science: Analyzing temperature, precipitation, and climate patterns.Manufacturing: Quality control, demand forecasting, and process optimization.Economics: Studying economic indicators like GDP, inflation rates, and unemployment rates. Emphasis on Powerful Tools and Techniques:The complexity of time series data necessitates the use of powerful tools and techniques. Effective time series analysis often involves statistical methods, machine learning models, and data preprocessing steps to extract meaningful insights. In this article, we will explore how ChatGPT can complement these techniques to facilitate various aspects of time series analysis, from data preparation to forecasting and anomaly detection. 2. ChatGPT OverviewChatGPT, developed by OpenAI, represents a groundbreaking advancement in natural language processing. It builds upon the success of its predecessors, like GPT-3, with a focus on generating human-like text and facilitating interactive conversations.Background: ChatGPT is powered by a deep neural network architecture called the Transformer, which excels at processing sequences of data, such as text. It has been pre-trained on a massive corpus of text from the internet, giving it a broad understanding of language and context. Capabilities: ChatGPT possesses exceptional natural language understanding and generation abilities. It can comprehend and generate text in a wide range of languages and styles, making it a versatile tool for communication, content generation, and now, data analysis. Aiding Data Scientists: For data scientists, ChatGPT offers invaluable assistance. Its ability to understand and generate text allows it to assist in data interpretation, data preprocessing, report generation, and even generating code snippets. In the context of time series analysis, ChatGPT can help streamline tasks, enhance communication, and contribute to more effective analysis by providing human-like interactions with data and insights. This article will explore how data scientists can harness ChatGPT's capabilities to their advantage in the realm of time series data. 3. Preparing Time Series DataData preprocessing is a critical step in time series analysis, as the quality of your input data greatly influences the accuracy of your results. Inaccurate or incomplete data can lead to flawed forecasts and unreliable insights. Therefore, it's essential to carefully clean and prepare time series data before analysis.Importance of Data Preprocessing:1. Missing Data Handling: Time series data often contains missing values, which need to be addressed. Missing data can disrupt calculations and lead to biased results.2. Noise Reduction: Raw time series data can be noisy, making it challenging to discern underlying patterns. Data preprocessing techniques can help reduce noise and enhance signal clarity.3. Outlier Detection: Identifying and handling outliers is crucial, as they can significantly impact analysis and forecasting.4. Normalization and Scaling: Scaling data to a consistent range is important, especially when using machine learning algorithms that are sensitive to the magnitude of input features.5. Feature Engineering: Creating relevant features, such as lag values or rolling statistics, can provide additional information for analysis.Code Examples for Data Preprocessing:Here's an example of how to load, clean, and prepare time series data using Python libraries like Pandas and NumPy:import pandas as pd import numpy as np # Load time series data data = pd.read_csv("time_series_data.csv") # Clean and preprocess data data['Date'] = pd.to_datetime(data['Date']) data.set_index('Date', inplace=True) # Resample data to handle missing values (assuming daily data) data_resampled = data.resample('D').mean() data_resampled.fillna(method='ffill', inplace=True) # Feature engineering (e.g., adding lag features) data_resampled['lag_1'] = data_resampled['Value'].shift(1) data_resampled['lag_7'] = data_resampled['Value'].shift(7) # Split data into training and testing sets train_data = data_resampled['Value'][:-30] test_data = data_resampled['Value'][-30:]4. ChatGPT for Time Series ForecastingChatGPT's natural language understanding and generation capabilities can be harnessed effectively for time series forecasting tasks. It can serve as a powerful tool to streamline forecasting processes, provide interactive insights, and facilitate communication within a data science team.Assisting in Time Series Forecasting:1. Generating Forecast Narratives: ChatGPT can generate descriptive narratives explaining forecast results in plain language. This helps in understanding and communicating forecasts to non-technical stakeholders.2. Interactive Forecasting: Data scientists can interact with ChatGPT to explore different forecasting scenarios. By providing ChatGPT with context and queries, you can receive forecasts for various time horizons and conditions.3. Forecast Sensitivity Analysis: You can use ChatGPT to explore the sensitivity of forecasts to different input parameters or assumptions. This interactive analysis can aid in robust decision-making.Code Example for Using ChatGPT in Forecasting:Below is a code example demonstrating how to use ChatGPT to generate forecasts based on prepared time series data. In this example, we use the OpenAI API to interact with ChatGPT for forecasting:import openai openai.api_key = "YOUR_API_KEY" def generate_forecast(query, historical_data):    prompt = f"Forecast the next data point in the time series: '{historical_data}'. The trend appears to be {query}."    response = openai.Completion.create(        engine="text-davinci-002",        prompt=prompt,        max_tokens=20,  # Adjust for desired output length        n=1,  # Number of responses to generate        stop=None,  # Stop criteria    )    forecast = response.choices[0].text.strip()    return forecast # Example usage query = "increasing" forecast = generate_forecast(query, train_data) print(f"Next data point in the time series: {forecast}")5. ChatGPT for Anomaly DetectionChatGPT can play a valuable role in identifying anomalies in time series data by leveraging its natural language understanding capabilities. Anomalies, which represent unexpected and potentially important events or errors, are crucial to detect in various domains, including finance, healthcare, and manufacturing. ChatGPT can assist in this process in the following ways:Contextual Anomaly Descriptions: ChatGPT can provide human-like descriptions of anomalies, making it easier for data scientists and analysts to understand the nature and potential impact of detected anomalies.Interactive Anomaly Detection: Data scientists can interact with ChatGPT to explore potential anomalies and receive explanations for detected outliers. This interactive approach can aid in identifying false positives and false negatives, enhancing the accuracy of anomaly detection.Code Example for Using ChatGPT in Anomaly Detection:Below is a code example demonstrating how to use ChatGPT to detect anomalies based on prepared time series data: import openai openai.api_key = "YOUR_API_KEY" def detect_anomalies(query, historical_data):    prompt = f"Determine if there are any anomalies in the time series: '{historical_data}'. The trend appears to be {query}."    response = openai.Completion.create(        engine="text-davinci-002",        prompt=prompt,        max_tokens=20,  # Adjust for desired output length        n=1,  # Number of responses to generate        stop=None,  # Stop criteria    )    anomaly_detection_result = response.choices[0].text.strip()    return anomaly_detection_result # Example usage query = "increasing with a sudden jump" anomaly_detection_result = detect_anomalies(query, train_data) print(f"Anomaly detection result: {anomaly_detection_result}")6. Limitations and ConsiderationsWhile ChatGPT offers significant advantages in time series analysis, it is essential to be aware of its limitations and consider certain precautions for its effective utilization: 1. Lack of Domain-Specific Knowledge: ChatGPT lacks domain-specific knowledge. It may generate plausible-sounding but incorrect insights, especially in specialized fields. Data scientists should always validate its responses with domain expertise.2. Sensitivity to Input Wording: ChatGPT's responses can vary based on the phrasing of input queries. Data scientists must carefully frame questions to obtain accurate and consistent results.3. Biases in Training Data: ChatGPT can inadvertently perpetuate biases present in its training data. When interpreting its outputs, users should remain vigilant about potential biases and errors.4. Limited Understanding of Context: ChatGPT's understanding of context has limitations. It may not remember information provided earlier in a conversation, which can lead to incomplete or contradictory responses.5. Uncertainty Handling: ChatGPT does not provide uncertainty estimates for its responses. Data scientists should use it as an assistant and rely on robust statistical techniques for decision-making. Best PracticesDomain Expertise: Combine ChatGPT's insights with domain expertise to ensure the accuracy and relevance of its recommendations.Consistency Checks: Ask ChatGPT multiple variations of the same question to assess the consistency of its responses.Fact-Checking: Verify critical information and predictions generated by ChatGPT with reliable external sources.Iterative Usage: Incorporate ChatGPT iteratively into your workflow, using it to generate ideas and hypotheses that can be tested and refined with traditional time series analysis methods.Bias Mitigation: Implement bias mitigation techniques when using ChatGPT in sensitive applications to reduce the risk of biased responses.Understanding the strengths and weaknesses of ChatGPT and taking appropriate precautions will help data scientists harness its capabilities effectively while mitigating potential errors and biases in time series analysis tasks.ConclusionIn summary, ChatGPT offers a transformative approach to time series analysis. It bridges the gap between natural language understanding and data analytics, providing data scientists with interactive insights, forecasting assistance, and anomaly detection capabilities. Its potential to generate human-readable narratives, explain anomalies, and explore diverse scenarios makes it a valuable tool in various domains. However, users must remain cautious of its limitations, verify critical information, and employ it as a supportive resource alongside established analytical methods. As technology evolves, ChatGPT continues to demonstrate its promise as a versatile and collaborative companion in the pursuit of actionable insights from time series data.Author BioBhavishya Pandit is a Data Scientist at Rakuten! He has been extensively exploring GPT to find use cases and build products that solve real-world problems.
Read more
  • 0
  • 0
  • 33518

article-image-using-chatgpt-api-in-python
Martin Yanev
26 Sep 2023
14 min read
Save for later

Using ChatGPT API in Python

Martin Yanev
26 Sep 2023
14 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, Building AI Applications with ChatGPT APIs, by Martin Yanev. Master core data architecture design concepts and Azure Data & AI services to gain a cloud data and AI architect’s perspective to developing end-to-end solutions IntroductionBefore we start writing our first code, it’s important to create an environment to work in and install any necessary dependencies. Fortunately, Python has an excellent tooling system for managing virtual environments. Virtual environments in Python are a complex topic, but for the purposes of this book, it’s enough to know that they are isolated Python environments that are separate from your global Python installation. This isolation allows developers to work with different Python versions, install packages within the environment, and manage project dependencies without interfering with Python’s global installation.In order to utilize the ChatGPT API in your NLP projects, you will need to set up your Python development environment. This section will guide you through the necessary steps to get started, including the following:Installing PythonInstalling the PyCharm IDEInstalling pipSetting up a virtual environmentInstalling the required Python packagesA properly configured development environment will allow you to make API requests to ChatGPT and process the resulting responses in your Python code.Installing Python and the PyCharm IDEPython is a popular programming language that is widely used for various purposes, including machine learning and data analysis. You can download and install the latest version of Python from the official website, https://www.python.org/downloads/.  Once you have downloaded the Python installer, simply follow the instructions to install Python on your computer. The next step is to choose an Integrated Development Environment (IDE) to work with (see Figure 1.7).Figure 1.7: Python InstallationOne popular choice among Python developers is PyCharm, a powerful and user-friendly IDE developed by JetBrains. PyCharm provides a wide range of features that make it easy to develop Python applications, including code completion, debugging tools, and project management capabilities.To install PyCharm, you can download the Community Edition for free from the JetBrains website, https://www.jetbrains.com/pycharm/download/ Once you have downloaded the installer, simply follow the instructions to install PyCharm on your computer.Setting Up a Python Virtual EnvironmentSetting up a Python virtual environment is a crucial step in creating an isolated development environment for your project. By creating a virtual environment, you can install specific versions of Python packages and dependencies without interfering with other projects on your system.Creating a Python virtual environment specific to your ChatGPT application project is a recommended best practice. By doing so, you can ensure that all the packages and dependencies are saved inside your project folder rather than cluttering up your computer’s global Python installation. This approach provides a more organized and isolated environment for your project’s development and execution.PyCharm allows you to set up the Python virtual environment directly during the project creation process. Once installed, you can launch PyCharm and start working with Python. Upon launching PyCharm, you will see the Welcome Window, and from there, you can create a new project. By doing so, you will be directed to the New Project window, where you can specify your desired project name and, more importantly, set up your Python virtual environment. To do this, you need to ensure that New environment using is selected. This option will create a copy of the Python version installed on your device and save it to your local project.As you can see from Figure 1.8, the Location field displays the directory path of your local Python virtual environment situated within your project directory. Beneath it, the Base interpreter displays the installed Python version on your system. Clicking the Create button will initiate the creation of your new project.Figure 1.8: PyCharm Project SetupFigure 1.9 displays the two main indicators showing that the Python virtual environment is correctly installed and activated. One of these indications is the presence of a venv folder within your PyCharm project, which proves that the environment is installed. Additionally, you should observe Python 3.11 (ChatGPTResponse) in the lower-right corner, confirming that your virtual environment has been activated successfully.Figure 1.9: Python Virtual Environment IndicationsA key component needed to install any package in Python is pip. Lets’s see how to check whether pip is already installed on your system, and how to install it if necessary.The pip Package Installerpip is a package installer for Python. It allows you to easily install and manage third-party Python libraries and packages such as openai. If you are using a recent version of Python, pip should already be installed. You can check whether pip is installed on your system by opening a command prompt or terminal and typing pip followed by the Enter key. If pip is installed, you should see some output describing its usage and commands.If pip is not installed on your system, you can install it by following these steps:1. First, download the get-pip.py script from the official Python website: https:// bootstrap.pypa.io/get-pip.py.2. Save the file to a location on your computer that you can easily access, such as your desktop or downloads folder.3. Open a command prompt or terminal and navigate to the directory where you saved the get-pip.py file.4. Run the following command to install pip: python get-pip.py5. Once the installation is complete, you can verify that pip is installed by typing pip into the command prompt or terminal and pressing Enter.You should now have pip installed on your system and be able to use it to install packages and libraries for Python.Building a Python Virtual Environment from the TerminalAlternatively, to create a Python virtual environment, you can use the built-in venv module that comes with Python. Once you create your project in PyCharm, click on the Terminal tab located at the bottom of the screen. If you don’t see the Terminal tab, you can open it by going to View | Tool Windows | Terminal in the menu bar. Then, run this command:$ python3 -m venv myenvThis will create a new directory named myenv that contains the virtual environment. You can replace myenv with any name you want.To activate the virtual environment, run the following command: On Windows:$ myenv\Scripts\activate.batOn macOS or Linux:$ source myenv/bin/activateOnce activated, you should see the name of the virtual environment in the command prompt or terminal. From here, you can install any packages or dependencies you need for your project without interfering with other Python installations on your system.This was a complete guide on how to set up a Python development environment for using the ChatGPT API in NLP projects. The steps included installing Python, the PyCharm IDE, and pip, and setting up a virtual environment. Setting up a virtual environment was a crucial step in creating an isolated development environment for your project. You are now ready to complete your first practice exercise on using the ChatGPT API with Python to interact with the OpenAI library.A Simple ChatGPT API ResponseUsing the ChatGPT API with Python is a relatively simple process. You’ll first need to make sure you create a new PyCharm project called ChatGPTResponse (see Figure 1.8). Once you have that set up, you can use the OpenAI Python library to interact with the ChatGPT API. Open a new Terminal in PyCharm, make sure that you are in your project folder, and install the openai package:$ pip install openaiNext, you need to create a new Python file in your PyCharm project. In the top-left corner,  right-click on ChatGPTResponse | New | Python File. Name the file app.py and hit Enter. You should now have a new Python file in your project directory.Figure 1.10: Create a Python FileTo get started, you’ll need to import the openai library into your Python file. Also, you’ll need to provide your OpenAI API key. You can obtain an API key from the OpenAI website by following the steps outlined in the previous sections of this book. Then you’ll need to set it as a parameter in your Python code. Once your API key is set up, you can start interacting with the ChatGPT API:import openai openai.api_key = "YOUR_API_KEY"Replace YOUR_API_KEY with the API key you obtained from the OpenAI platform page. Now, you can ask the user for a question using the input() function:question = input("What would you like to ask ChatGPT? ")The input() function is used to prompt the user to input a question they would like to ask the ChatGPT API. The function takes a string as an argument, which is displayed to the user when the program is run. In this case, the question string is "What would you like to ask ChatGPT?". When the user types their question and presses Enter, the input() function will return the string that the user typed. This string is then assigned to the question variable.To pass the user question from your Python script to ChatGPT, you will need to use the ChatGPT API Completion function:response = openai.Completion.create( engine="text-davinci-003", prompt=question, max_tokens=1024, n=1, stop=None, temperature=0.8, )The openai.Completion.create() function in the code is used to send a request to the ChatGPT API to generate the completion of the user’s input prompt. The engine parameter allows us to specify the specific variant or version of the GPT model we want to utilize for the request, and in this case, it is set to "text-davinci-003". The prompt parameter specifies the text prompt for the API to complete, which is the user’s input question in this case.The max_tokens parameter specifies the maximum number of tokens the request and the response should contain together. The n parameter specifies the number of completions to generate for the prompt. The stop parameter specifies the sequence where the API should stop generating the response.The temperature parameter controls the creativity of the generated response. It ranges from 0 to 1. Higher values will result in more creative but potentially less coherent responses, while lower values will result in more predictable but potentially less interesting responses. Later in the book, we will delve into how these parameters impact the responses received from ChatGPT.The function returns a JSON object containing the generated response from the ChatGPT API, which then can be accessed and printed to the console in the next line of code:print(response)In the project pane on the left-hand side of the screen, locate the Python file you want to run. Right-click on the app.py file and select Run app.py from the context menu. You should receive a message in the Run window that asks you to write a question to ChatGPT (see Figure 1.11).Figure 1.11: Asking ChatGPT a QuestionOnce you have entered your question, press the Enter key to submit your request to the ChatGPT API. The response generated by the ChatGPT API model will be displayed in the Run window as a complete JSON object:{ "choices": [ { "finish_reason": "stop", "index": 0, "logprobs": null, "text": "\n\n1. Start by getting in the water. If you're swimming in a pool, you can enter the water from the side, …………. } ], "created": 1681010983, "id": "cmpl-73G2JJCyBTfwCdIyZ7v5CTjxMiS6W", "model": "text-davinci-003", "object": "text_completion", "usage": { "completion_tokens": 415, "prompt_tokens": 4, "total_tokens": 419 } }This JSON response produced by the OpenAI API contains information about the response generated by the GPT-3 model. This response consists of the following fields:The choices field contains an array of objects with the generated responses, which in this case only contains one response object as the parameter n=1.The text field within the response object contains the actual response generated by the GPT-3 model.The finish_reason field indicates the reason why the response was generated; in this case, it was because the model reached the stop condition provided in the request. Since in our case stop=None, the full response from the ChatGPT API was returned.The created field specifies the Unix timestamp of when the response was created.The id field is a unique identifier for the API request that generated this response.The model field specifies the GPT-3 model that was used to generate the response.The object field specifies the type of object that was returned, which in this case is text_ completion.The usage field provides information about the resource usage of the API request. It contains information about the number of tokens used for the completion, the number of tokens in the prompt, and the total number of tokens used.The most important parameter from the response is the text field, which contains the answer to the question asked to the ChatGPT API. This is why most API users would like to access only that parameter from the JSON object. You can easily separate the text from the main body as follows:answer = response["choices"][0]["text"] print(answer)By following this approach, you can guarantee that the answer variable will hold the complete ChatGPT API text response, which you can then print to verify. Keep in mind that ChatGPT responses can significantly differ depending on the input, making each response unique.OpenAI: 1. Start by getting in the water. If you're swimming in a pool, you can enter the water from the side, ladder, or diving board. If you are swimming in the ocean or lake, you can enter the water from the shore or a dock. 2. Take a deep breath in and then exhale slowly. This will help you relax and prepare for swimming.ChatGPT can be employed for a multitude of NLP tasks across a wide array of topics accessible in the vast expanse of available data. We can utilize our script to inquire about diverse areas of expertise and receive advanced responses from the ChatGPT API, as demonstrated here:1. Mathematics:* User: What is the square root of 256?* ChatGPT: The square root of 256 is 16.2. Sentiment analysis:* User: Analyze the sentiment of the sentence “I had a great day today!”* ChatGPT: The sentiment of the sentence “I had a great day today!” is positive.3. Programming:* User: How do you declare a variable in Python?* ChatGPT: To declare a variable in Python, you can use the following syntax: variable_ name = value.4. Science:* User: What is the process of photosynthesis in plants?* ChatGPT: The process of photosynthesis in plants is the fundamental mechanism by which they convert light energy into chemical energy.5. Literature:* User: What are the major themes in Shakespeare’s play “Hamlet”?* ChatGPT: “Hamlet,” one of Shakespeare’s most renowned tragedies, explores several major themes that continue to captivate audiences and provoke thought.In this section, you learned how to use the OpenAI Python library to interact with the ChatGPT API by sending a request to generate the completion of a user’s input prompt/question. You also learned how to set up your API key and how to prompt the user to input a question, and finally, how to access the generated response from ChatGPT in the form of a JSON object containing information about the response. You are now ready to build more complex projects and integrate the ChatGPT API with other frameworks.ConclusionIn this article, we covered how to set up a Python development environment, specifically using the PyCharm IDE, and creating a virtual environment. To help you get started with using the ChatGPT API, we walked through a simple example of obtaining a ChatGPT API response.Author BioMartin Yanev is an experienced Software Engineer who has worked in the aerospace and industries for over 8 years. He specializes in developing and integrating software solutions for air traffic control and chromatography systems. Martin is a well-respected instructor with over 280,000 students worldwide, and he is skilled in using frameworks like Flask, Django, Pytest, and TensorFlow. He is an expert in building, training, and fine-tuning AI systems with the full range of OpenAI APIs. Martin has dual master's degrees in Aerospace Systems and Software Engineering, which demonstrates his commitment to both practical and theoretical aspects of the industry.
Read more
  • 0
  • 0
  • 27390
Modal Close icon
Modal Close icon