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

7019 Articles
article-image-unleashing-the-potential-of-gpus-for-training-llms
Shankar Narayanan
22 Sep 2023
8 min read
Save for later

Unleashing the Potential of GPUs for Training LLMs

Shankar Narayanan
22 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. Don't miss out – sign up today!IntroductionThere is no doubt about Language Models being the true marvels in the arena of artificial intelligence. These sophisticated systems have the power to manipulate human language, understand, and even generate with astonishing accuracy.However, one can often complain about the immense computational challenges beyond these medical abilities. For instance, LLM training requires the incorporation of complex mathematical operations along with the processing of vast data. This is where the Graphics Processing Units (GPU) come into play. It serves as the engine that helps to power the language magic.Let me take you through the GPU advancement and innovations to support the Language Model. Parallely, we will explore how Nvidia helps revolutionize the enterprise LLM use cases.Role of GPUs in LLMs To understand the significance of GPU, let us first understand the concept of LLM.What is LLM?LLM or Large Language Models are AI systems that help generate human language. They have various applications, including translation services, sentiment analysis, chatbots, and content generation. Generative Pre-trained Transformer or GPT models, including BERT and GPT3, are popular among every LLM.These models require training, including vast data sets with billions of phrases and words. The model learns to predict while mastering the nuances and structure of language. It is like an intricate puzzle that requires enormous computational power.The need for GPUsThe Graphics Processing Units are specifically designed to undergo parallel processing. This characteristic makes them applicable to train the LLMs. The GPU can tackle thousands of tasks simultaneously, unlike the Central Processing Unit or CPU, which excels at handling sequential tasks.The training of a Large Language Model is like a massive jigsaw puzzle. Each puzzle piece represents a smaller portion of the model's language understanding. Using a CPU could only help one to work on one of these pieces at a simple time. But with GPU, one could work on various pieces parallelly while speeding up the whole process.Besides, GPU offers high computational throughput that one requires for complex mathematical operations. Their competency lies in metric multiplication, one of the fundamentals of neural network training. All these attributes make GPU indispensable for deep learning tasks like LLMs.Here is one of the practical example of how GPU works in LLM training: (Python)import time import torch # Create a large random dataset data = torch.randn(100000, 1000) # Training with CPU start_time = time.time() for _ in range(100):    model_output = data.matmul(data) cpu_training_time = time.time() - start_time print(f"CPU Training Time: {cpu_training_time:.2f} seconds") # Training with GPU if torch.cuda.is_available():    data = data.cuda()    start_time = time.time()    for _ in range(100):        model_output = data.matmul(data)    gpu_training_time = time.time() - start_time    print(f"GPU Training Time: {gpu_training_time:.2f} seconds") else:    print("GPU not available.")GPU Advancements and LLMDue to the rising demands of LLMs and AI, GPU technology is evolving rapidly. These advancements, however, play a significant role in constituting the development of sophisticated language models.One such advancement is the increase in GPU memory capacity. Technically, the larger model requires more excellent memory to process massive data sets. Hence, modern GPUs offer substantial memory capacity, allowing researchers to build and train more substantial large language models.One of the critical aspects of training a Large Language Model is its speed. Sometimes, it can take months to prepare and train a large language model. But with the advent of faster GPU, things have changed dramatically. The quicker GPU reduces the training time and accelerates research and development. Apart from that, it also reduces the energy consumption that is often associated with training these large models.Let us explore the memory capacity of the GPU using a code snippet.(Python)import torch # Check GPU memory capacity if torch.cuda.is_available():    gpu_memory = torch.cuda.get_device_properties(0).total_memory    print(f"GPU Memory Capacity: {gpu_memory / (1024**3):.2f} GB") else:    print("GPU not available.")For the record, Nvidia's Tensor Core technology has been one of the game changers in this aspect. It accelerates one of the core operations in deep learning, i.e., the matrix computation process, allowing the LLMs to train faster and more efficiently.Using matrix Python and PYTorh, you can showcase the speedup with GPU processing.import time import torch # Create large random matrices matrix_size = 1000 cpu_matrix = torch.randn(matrix_size, matrix_size) gpu_matrix = torch.randn(matrix_size, matrix_size).cuda()  # Move to GPU # Perform matrix multiplication with CPU start_time = time.time() result_cpu = torch.matmul(cpu_matrix, cpu_matrix) cpu_time = time.time() - start_time # Perform matrix multiplication with GPU start_time = time.time() result_gpu = torch.matmul(gpu_matrix, gpu_matrix) gpu_time = time.time() - start_time print(f"CPU Matrix Multiplication Time: {cpu_time:.4f} seconds") print(f"GPU Matrix Multiplication Time: {gpu_time:.4f} seconds")Nvidia's Contribution to GPU InnovationRegarding GPU innovation, the presence of Nvidia cannot be denied. It has a long-standing commitment to Machine Learning and advancing AI. Hence, it is a natural ally for the large language model community.Here is how Tensor Cores can be utilized with PYTorch.import torch # Enable Tensor Cores (requires a compatible GPU) if torch.cuda.is_available():    torch.backends.cuda.matmul.allow_tf32 = True # Create a tensor x = torch.randn(4096, 4096, device="cuda") # Perform matrix multiplication using Tensor Cores result = torch.matmul(x, x)It is interesting to know that Nvidia's graphics processing unit has powered several breakthroughs in LLM and AI models. BERT and GPT3 are known to harness the computational might of Nvidia's Graphics Processing Unit to achieve remarkable capabilities. Nvidia's dedication to the Artificial Intelligence world encompasses power and efficiency. The design of the graphics processing unit handles every AI workload with optimal performance per watt. It makes Nvidia one of the eco-friendly options for Large Language Model training procedures.As part of AI-focused hardware and architecture, the Tensor Core technology enables efficient and faster deep learning. This technology is instrumental in pushing the boundaries of LLM research.Supporting Enterprise LLM Use-caseThe application of LLM has a far-fetched reach, extending beyond research, labs, and academia. Indeed, they have entered the enterprise world with a bang. From analyzing massive datasets for insights to automating customer support through chatbots, large language models are transforming how businesses operate.Here, the Nvidia Graphics Processing Unit supports the enterprise LLM use cases. Enterprises often require LLM to handle vast amounts of data in real-time. With optimized AI performance and parallel processing power, Nvidia's GPU can provide the needed acceleration for these applications.Various companies across industries are harnessing the Nvidia GPU for developing LLM-based solutions to automate tasks, provide better customer experiences, and enhance productivity. From healthcare organizations analyzing medical records to financial institutions and predicting market trends, Nvidia drives enterprise LLM innovations.ConclusionNvidia continues to be the trailblazer in the captivating journey of training large language models. They are not only the hardware muscle for LLM but constantly innovate to make GPU capable and efficient with each generation.LLM is on the run to become integral to our daily lives. From business solutions to personal assistants, Nvidia's commitment to its GPU innovation ensures more power to the growth of language models. The synergy between AI and Nvidia GPU is constantly shaping the future of enterprise LLM use cases, helping organizations to achieve new heights in innovation and efficiency.Frequently Asked Questions1. How does the GPU accelerate the training process of large language models?The Graphics Processing Unit has parallel processing capabilities to allow the work of multiple tasks simultaneously. Such parallelism helps train Large Language Models by efficiently processing many components in understanding and generating human language.2. How does Nvidia contribute to GPU innovation for significant language and AI models?Nvidia has developed specialized hardware, including Tensor Core, optimized for AI workloads. The graphic processing unit of Nvidia powered numerous AI breakthroughs while providing efficient AI hardware to advance the development of Large Language Models.3. What are the expectations for the future of GPU innovation and launch language model?The future of GPU innovation promises efficient, specialized, and robust hardware tailored to the needs of AI applications and Large Language Models. It will continuously drive the development of sophisticated language models while opening up new possibilities for AI-power solutions.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
  • 2099

article-image-duet-ai-for-google-workspace
Aryan Irani
22 Sep 2023
6 min read
Save for later

Duet AI for Google Workspace

Aryan Irani
22 Sep 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!IntroductionDuet AI was announced at Google Cloud Next 23 as a powerful AI collaborator that can help you get more done in Google Workspace. It can help you write better emails, sort tables, create presentations, and more. Duet AI is still under development, but it has already learned to perform many kinds of tasks, including:Helping you write better in Google Docs.Generate images for better presentations in Google SlidesOrganizing and analyzing data in Google SheetsThere is so much more that Duet AI provides and Google will be announcing more updates to it. In this blog post, we will be taking a look at these features that Duet AI provides in detail with some interesting examples.Help me write in Google DocsThe help me write feature in Google Docs helps you to write better content, faster. It can help you generate new text, rewrite existing content or even improve your writing style.Generate new text: You can use the Help Me Write feature to generate new text for your document, such as a blog post, social media campaign and more. All you have to do is type in a prompt and it will generate text for you according to your instructions.Rewrite Existing text: You can use the help me write feature to rewrite existing text in the document. For example, you can use it to make your writing more concise, more formal, and creative.Improve your writing style: This allows you to improve your writing style by suggesting edits and improvements you should make. It can even tell you to correct your grammar, improve your sentence structure, and make your writing more engaging.Now that we have understood what the capabilities of the Help Me Write feature in Google Docs is, let's take a look at it in action.On opening the new Google Doc, you can see the Help Me Write feature pops up.On clicking the button, it allows you to enter a prompt that you want. For this example, we are going to tell it to write an advertisement for men’s soap bars.On structuring the prompt, to generate the text just go ahead and click on Create. In just a few seconds you will be able to see that Duet AI has generated a complete new advertisement.Here you can see we have successfully generated an advertisement for the soap bars. On reviewing the advertisement, let’s say you do not like the advertisement and maybe want to refine it and change the tone of it. You can do that by clicking on Refine.On clicking Refine, you will be allowed to choose from a variety of options on how you want to refine the paragraph Duet AI just generated for you. Additionally, you can manually design another prompt for how you want to refine the paragraph by typing it in the custom section.For this example, we are going to move forward and change the tone of the advertisement to Casual.On refining the paragraph, just in a few seconds, we can see that it has given me a new informal version of it. Once you like the paragraph Duet AI has generated for you, go ahead and click on insert, the paragraph will be inserted inside your Google Doc.Here you can see the paragraph has been pasted in the Google Doc and we have now successfully generated a new advertisement using Duet AI.Generate Images in SlidesThere have been so many times I have spent time trying to find the right photo to fit my slide and have been unsuccessful. With the new feature that Duet AI provides for Google Slides, I can generate images inside of slides and integrate them at the click of a button.Now that we have understood what the capabilities of this feature are, let’s take a look at it in action.When you open up your Google Slides, you will see something like this called Help me visualize. Once you click on this a new sidebar will open up on the right side of the screen.In this sidebar, you have to enter the prompt for the image you want to generate. Once you enter the prompt you have an option to select a style for the image.Once you select the style of the image, go ahead and click on Create.On clicking Create, in about 15–20 seconds you will see multiple photos generated according to the prompt we entered.Here you can see on successful execution we have been able to generate images inside of your Google Slides.Organizing and analyzing data in Google SheetsWe looked at how we can generate new images in Google Slides followed by the Help Me Write feature in Google Docs. All these features helped us understand the power of Duet AI inside of Google Workspace Tools.The next feature that we will be taking a look at is inside of Google Sheets, which allows us to turn ideas into actions and data into insights.Once you open up your Google Sheet, you will see a sidebar on the right side of the screen saying help me organize.Once you have your Google Sheet ready and the sidebar ready, it's time to enter a prompt for which you want to create a custom template. For this example, I am going to ask it to generate a template for the following prompt. On clicking create, in a few seconds you will see that it has generated some data inside of your Google Sheet.On successful execution, it has generated data according to the prompt we designed. If you are comfortable with this template it has generated go ahead and click on insert.On clicking Insert, the data will be inserted into the Google Sheet and you can start using it like a normal Google Sheet.ConclusionCurrently, all these features are not available for everybody and it is on a waitlist. If you want to grab the power of AI inside of Google Workspace Tools like Google Sheets, Google Docs, Google Slides and more, apply for the waitlist by clicking here.In this blog, we looked at how we can use AI inside of our Google Docs to help us write better. Later, we looked at how we can generate images inside of our Google Slides to make our presentations more engaging, and in the end, we looked at how we can generate templates inside of Google Sheets. I hope you have understood how to get the basics done with Duet AI for Google Workspace.Feel 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.Website
Read more
  • 0
  • 0
  • 637

article-image-preparing-high-quality-training-data-for-llm-fine-tuning
Louis Owen
22 Sep 2023
9 min read
Save for later

Preparing High-Quality Training Data for LLM Fine-Tuning

Louis Owen
22 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. Don't miss out – sign up today!IntroductionLarge Language Models (LLM) such as GPT3.5, GPT4, or Claude have shown a very good general capability that can be utilized across different tasks, starting from question and answering, coding assistant, marketing campaign, and many more. However, utilizing those general LLMs in production, especially for enterprises, is not an easy task:Those models are very large in terms of the number of parameters - resulting in lower latency compared to the smaller modelWe need to give a very long prompt to achieve good results - again, resulting in lower latencyReliability is not ensured - sometimes they can return the response with an additional prefix, which is really annoying when we expect only a JSON format response for exampleOne of the solutions to solve those problems is fine-tuning a smaller LLM that is specific to the task that we want to handle. For example, we need a QnA model that is able to answer user queries based only on the provided passage. Instead of utilizing those general LLMs, we can just fine-tune a smaller LLM, let’s say a 7 billion parameters LLM to do this specific task. Why to utilize such a giant LLM when our use case is only for QnA?The quality of training data plays a pivotal role in the success of fine-tuning. Garbage in, garbage out holds true in the world of LLMs. When you fine-tune low-quality data, you risk transferring noise, biases, and inaccuracies to your model. Let’s take the newly released paper, Textbooks Are All You Need II: phi-1.5 Technical Report, as an example. Despite the relatively low number of parameters (1.5B), this model is able to perform as well as models five times its size. Additionally, it excels in complex reasoning tasks, surpassing most non-frontier LLMs. What’s their secret sauce? High-quality of training data! The next question is how to prepare the training data for LLM fine-tuning. Moreover, how to prepare high-quality training data? Since fine-tuning needs labeled training data, we need to annotate the unlabeled data that we have. Annotating unlabeled data for classification tasks is much easier compared to more complex tasks like summarization. We just need to give labels based on the available classes in the classification task. If you have deployed an application with those general LLMs before and you have the data coming from real production data, then you can use those data as the training data. Actually, you can also use the response coming from the general LLM as the label directly, no need to do any data annotation anymore. However, what if you don’t have real production data? Then, you can use open-source data or even synthetic data generated by the general LLM as your unlabeled data.Throughout this article, we’ll discuss ways to give high-quality labels to the unlabeled training data, whether it’s annotated by humans or by general LLM. We’ll discuss what are the pros and cons of each of the annotation options. Furthermore, we’ll discuss in more detail how to utilize general LLM to do the annotation task - along with the step-by-step example.Without wasting any more time, let’s take a deep breath, make yourselves comfortable, and be ready to learn how to prepare high-quality training data for LLM fine-tuning!Human Annotated DataThe first option to create high-quality training data is by using the help of human annotators. In the ideal scenario, well-trained human annotators not only are able to produce high-quality training data but also produce labels that are fully steerable according to the criteria (SOP). However, using humans as the annotators will surely be both time and money-consuming. It is also not scalable since we need to wait for a not short time until we can get the labeled data. Finally, the ideal scenario is also hard to achieve since each of the annotators has their own bias towards a specific domain or even the label quality is most often based on their mood.LLM Annotated DataAnother better option is to utilize general LLM as the annotator. LLMs will always give not only high-quality training data but also full steerability according to the criteria if we do the prompt engineering correctly. It is also cheaper both in terms of time and money. Finally, it’s absolutely scalable and no bias included - except for hallucination.Let’s see how general LLM is usually utilized as an annotator. We’ll use conversation summarization as the task example. The goal of the task is to summarize the given conversation between two users (User A and User B) and return all important information discussed in the conversation in the form of a summarized paragraph.1. Write the initial promptWe need to start from an initial prompt that we will use to generate the summary of the given conversation, or in general, that will be used to generate the label of the given unlabeled sample.You are an expert in summarizing the given conversation between two users. Return all important information discussed in the conversation in the form of summarized paragraph. Conversation: {}2. Evaluate the generated output with a few samples - qualitativelyUsing the initial prompt, we need to evaluate the generated label with few number of samples - let’s say <20 random samples. We need to do this manually by eyeballing through each of the labeled samples and judging qualitatively if they are good enough or not. If the output quality on these few samples is good enough, then we can move into the next step. If not, then revise the prompt and re-evaluate using another <20 random samples. Repeat this process until you are satisfied with the label quality.3. Evaluate the generated output with large samples - quantitativelyOnce we’re confident enough with the generated labels, we can further assess the quality using a more quantitative approach and with a larger number of samples - let’s say >500 samples. For classification tasks, such as sentiment analysis, evaluating the quality of the labels is easy, we just need to compare the generated label with the ground truth that we have, and then we can calculate the precision, recall, or any other classification metrics that we’re interested in. However, for more complex tasks, such as the task in this example, we need a more sophisticated metric. There are a couple of widely used metrics for summarization task - BLEU, ROUGE, and many more. However, those metrics are based on a string-matching algorithm only, which means if the generated summary doesn’t contain the exact word used in the conversation, then this score will suggest that the summary quality is not good. To overcome this, many engineers nowadays are utilizing GPT-4 to assess the label quality. For example, we can write a prompt as follows to assess the quality of the generated labels. Read the given conversation and summary pair. Give the rating quality for the summary with 5 different options: “very bad”, “bad”, “moderate”, “good”, “excellent”. Make sure the summary captures all of the important information in the conversation and does not contain any misinformation. Conversation: {} Summary: {} Rating:  Once you get the rating, you can map them into integers - for example, “very bad”:0, “bad”: 1, “moderate”: 2, … Please make sure that the LLM that you’re using as the evaluator is not in the same LLMs family with the LLM that you’re using as the annotator. For example, GPT3.5 and GPT4 are both in the same family since they’re both coming from OpenAI.If the quantitative metric looks decent and meets the criteria, then we can move into the next step. If it’s not, then we can do a subset analysis to see in what kind of cases the label quality is not good. From there, we can revise the prompt and re-evaluate on the same test data. Repeat this step until you’re satisfied enough with the quantitative metric.4. Apply the final prompt to generate labels in the full dataFinally, we can apply the best prompt that we get from all of those iterations and apply it to generate labels in the full unlabeled data that we have.ConclusionCongratulations on keeping up to this point! Throughout this article, you have learned why LLM fine-tuning is important and when to do fine-tuning. You have also learned how to prepare high-quality training data for LLM fine-tuning. Hope the best for your LLM fine-tuning experiments 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
  • 2145

article-image-chatgpt-for-ab-testing-in-marketing-campaigns
Valentina Alto
22 Sep 2023
5 min read
Save for later

ChatGPT for A/B Testing in Marketing Campaigns

Valentina Alto
22 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. 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, staying competitive and meeting customer expectations is paramount. This article explores the revolutionary potential of ChatGPT in enhancing multiple aspects of marketing. From refining A/B testing strategies to elevating SEO optimization techniques and harnessing sentiment analysis for measuring customer satisfaction, ChatGPT emerges as a pivotal tool. A/B testing for marketing comparisonAnother interesting field where ChatGPT can assist marketers is A/B testing.A/B testing in marketing is a method of comparing two different versions of a marketing campaign, advertisement, or website to determine which one performs better. In A/B testing, two variations of the same campaign or element are created, with only one variable changed between the two versions. The goal is to see which version generates more clicks, conversions, or other desired outcomes.An example of A/B testing might be testing two versions of an email campaign, using different subject lines, or testing two versions of a website landing page, with different call-to-action buttons. By measuring the response rate of each version, marketers can determine which version performs better and make data-driven decisions about which version to use going forward.A/B testing allows marketers to optimize their campaigns and elements for maximum effectiveness, leading to better results and a higher return on investment.Since this method involves the process of generating many variations of the same content, the generative power of ChatGPT can definitely assist in that.Let’s consider the following example. I’m promoting a new product I developed: a new, light and thin climbing harness for speed climbers. I’ve already done some market research and I know my niche audience. I also know that one great channel of communication for that audience is publishing on an online climbing blog, of which most climbing gyms’ members are fellow readers.My goal is to create an outstanding blog post to share the launch of this new harness, and I want to test two different versions of it in two groups. The blog post I’m about to publish and that I want to be the object of my A/B testing is the following:Figure – An example of a blog post to launch climbing gearHere, ChatGPT can help us on two levels:The first level is that of rewording the article, using different keywords or different attention grabbing slogans. To do so, once this post is provided as context, we can ask ChatGPT to work on the article and slightly change some elements:Figure – New version of the blog post generated by ChatGPTAs per my request, ChatGPT was able to regenerate only those elements I asked for (title, subtitle, and closing sentence) so that I can monitor the effectiveness of those elements by monitoring the reaction of the two audience groups.The second level is working on the design of the web page, namely, changing the collocation of the image rather than the position of the buttons. For this purpose, I created a simple web page for the blog post published in the climbing blog (you can find the code in the book’s GitHub repository at https://github.com/PacktPublishing/The-Ultimate-Guideto-ChatGPT-and-OpenAI/tree/main/Chapter%207%20-%20ChatGPT%20 for%20Marketers/Code):Figure  – Sample blog post published on the climbing blogWe can directly feed ChatGPT with the HTML code and ask it to change some layout elements, such as the position of the buttons or their wording. For example, rather than Buy Now, a reader might be more gripped by an I want one! button.So, lets feed ChatGPT with the HTML source code:Figure – ChatGPT changing HTML codeLet’s see what the output looks like:Figure – New version of the websiteAs you can see, ChatGPT only intervened at the button level, slightly changing their layout, position, color, and wording.Indeed, inspecting the source code of the two versions of the web pages, we can see how it differs in the button sections:Figure – Comparison between the source code of the two versions of the websiteConclusionChatGPT is a valuable tool for A/B testing in marketing. Its ability to quickly generate different versions of the same content can reduce the time to market of new campaigns. By utilizing ChatGPT for A/B testing, you can optimize your marketing strategies and ultimately drive better results for your business.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
  • 420

article-image-ai-distilled-18-oracles-clinical-digital-assistant-google-deepminds-alphamissense-ai-powered-stable-audio-prompt-lifecycle-3d-gaussian-splatting
Merlyn Shelley
21 Sep 2023
12 min read
Save for later

AI_Distilled #18: Oracle’s Clinical Digital Assistant, Google DeepMind's AlphaMissense, AI-Powered Stable Audio, Prompt Lifecycle, 3D Gaussian Splatting

Merlyn Shelley
21 Sep 2023
12 min read
👋 Hello,“A computer would deserve to be called intelligent if it could deceive a human into believing that it was human.” - Alan Turing, Visionary Computer Scientist.This week, we begin by spotlighting Turing's test, a crucial concept in computer science. It sparks discussions about how AI emulates human intelligence, ultimately elevating productivity and creativity. A recent Hardvard study revealed how AI improves worker productivity and reduces task completion time by 25% while also improving quality by 40%. A study with 758 Boston Consulting Group consultants revealed that GPT-4 boosted productivity by 12.2% on tasks it could handle. Welcome to AI_Distilled #18, your ultimate source for everything related to AI, GPT, and LLMs.  In this edition, we’ll talk about OpenAI expanding to EU with Dublin office and key hires, AI-Powered Stable Audio transforming text into high-quality music, a Bain study predicting how generative AI will dominate game development in 5-10 years, and Oracle introducing AI-powered clinical digital assistant for healthcare. A fresh batch of AI secret knowledge and tutorials is here too! Look out for a comprehensive guide to prompt lifecycle, exploring LLM selection and evaluation, a primer on 3D gaussian splatting: rasterization and its future in graphics, and a step-by-step guide to text generation with GPT using Hugging Face transformers library in Python.In addition, we're showcasing an article by our author Ben Auffarth about Langchain, offering a sneak peek into our upcoming virtual conference. 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  ⚡ TechWave: AI/GPT News & Analysis OpenAI Expands to EU with Dublin Office and Key Hires: The ChatGPT creator is opening its first European Union office in Dublin, signaling its readiness for upcoming AI regulatory challenges. This move follows OpenAI's announcement of its third office, with locations in San Francisco and London. The expansion into Ireland is strategically significant, as many tech companies choose it as a hub to engage with European regulators and clients while benefiting from favorable tax rates. OpenAI is actively hiring for positions in Dublin, including an associate general counsel, policy and partnerships lead, privacy program manager, software engineer focused on privacy, and a media relations lead. This expansion highlights OpenAI's commitment to addressing privacy concerns, especially in the EU, where ChatGPT faced scrutiny and regulatory actions related to data protection. AI-Powered Stable Audio Transforms Text into High-Quality Music: Stability AI has unveiled Stable Audio, an AI model capable of converting text descriptions into stereo 44.1 kHz music and sound effects. This breakthrough technology raises the potential of AI-generated audio rivaling human-made compositions. Stability AI collaborated with AudioSparx, incorporating over 800,000 audio files and text metadata into the model, enabling it to mimic specific sounds based on text commands. Stable Audio operates efficiently, rendering 95 seconds of 16-bit stereo audio at 44.1 kHz in under a second using Nvidia A100 GPUs. It comes with free and Pro plans, offering users the ability to generate music with varying lengths and quantities, marking a significant advancement in AI-generated audio quality. Oracle Introduces AI-Powered Clinical Digital Assistant for Healthcare: Oracle has unveiled its AI-powered Clinical Digital Assistant to enhance electronic health record (EHR) solutions in healthcare. This innovation aims to automate administrative tasks for caregivers, allowing them to focus on patient care. It addresses concerns related to the adoption of generative AI technologies in healthcare. The assistant offers multimodal support, responding to both text and voice commands, streamlining tasks such as accessing patient data and prescriptions. It remains active during appointments, providing relevant information and suggesting actions. Patients can also interact with it for appointment scheduling and medical queries. Oracle plans a full rollout of capabilities over the next year.  Generative AI to Dominate Game Development in 5-10 Years, Says Bain Study: A study by global consulting firm Bain & Company predicts that generative AI will account for more than 50% of game development in the next 5 to 10 years, up from less than 5% currently. The research surveyed 25 gaming executives worldwide, revealing that most believe generative AI will enhance game quality and expedite development, but only 20% think it will reduce costs. Additionally, 60% don't expect generative AI to significantly alleviate the talent shortage in the gaming industry, emphasizing the importance of human creativity. The study highlights that generative AI should complement human creativity rather than replace it.  Google DeepMind's AI Program, AlphaMissense, Predicts Harmful DNA Mutations: Researchers at Google DeepMind have developed AlphaMissense, an artificial intelligence program that can predict whether genetic mutations are harmless or likely to cause diseases, with a focus on missense mutations, where a single letter is misspelled in the DNA code. AlphaMissense assessed 71 million single-letter mutations affecting human proteins, determining 57% were likely harmless, 32% likely harmful, and uncertain about the rest. The program's predictions have been made available to geneticists and clinicians to aid research and diagnosis. AlphaMissense performs better than current programs, potentially helping identify disease-causing mutations and guiding treatment.  📥 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! 🔮 Looking for a New Book from Packt’s Expert Community? Splunk 9.x Enterprise Certified Admin Guide - By Srikanth Yarlagadda If Splunk is a part of your professional toolkit, consider exploring the Splunk 9.x Enterprise Certified Admin Guide. In an era where the IT sector's demand for Splunk expertise is consistently increasing, this resource proves invaluable. It comprehensively addresses essential aspects of Splunk Enterprise, encompassing installation, license management, user and forwarder administration, index creation, configuration file setup, data input handling, field extraction, and beyond. Moreover, the inclusion of self-assessment questions facilitates a thorough understanding, rendering it an indispensable guide for Splunk Enterprise administrators aiming to excel in their field. Interested in getting a sneak peek of Chapter 1 without any commitment? Simply click the button below to access it. Read through the Chapter 1 unlocked here...  🌟 Secret Knowledge: AI/LLM Resources Understanding the Prompt Lifecycle: A Comprehensive Guide: A step-by-step guide to the prompt lifecycle, which is crucial for effective prompt engineering in AI applications. The guide covers four main stages: Design & Experiment, Differentiate & Personalize, Serve & Operate, and Analyze Feedback & Adapt. In each stage, you'll learn how to design, differentiate, serve, and adapt prompts effectively, along with the specific tools required. Additionally, the post addresses the current state of tooling solutions for prompt lifecycle management and highlights the existing gaps in prompt engineering tooling.  Exploring LLM Selection and Evaluation: A Comprehensive Guide: In this post, you'll discover a comprehensive guide to selecting and evaluating LLMs. The guide delves into the intricate process of choosing the right LLM for your specific task and provides valuable insights into evaluating their performance effectively. By reading this post, you can expect to gain a thorough understanding of the criteria for LLM selection, the importance of evaluation metrics, and practical tips to make informed decisions when working with these powerful language models. A Primer on 3D Gaussian Splatting: Rasterization and Its Future in Graphics: In this post, you'll delve into the world of 3D Gaussian Splatting, a rasterization technique with promising implications for graphics. You'll explore the core concept of 3D Gaussian Splatting, which involves representing scenes using gaussians instead of triangles. The post guides you through the entire process, from Structure from Motion (SfM) to converting points to gaussians and training the model for optimal results. It also touches on the importance of differentiable Gaussian rasterization.  How to Build a Multi-GPU System for Deep Learning in 2023: A Step-by-Step Guide: Learn how to construct a multi-GPU system tailored for deep learning while staying within budget constraints. The guide begins by delving into crucial GPU considerations, emphasizing the importance of VRAM, performance (evaluated via FLOPS and tensor cores), slot width, and power consumption. It offers practical advice on choosing the right GPU for your budget. The post then moves on to selecting a compatible motherboard and CPU, paying special attention to PCIe lanes and slot spacing. The guide also covers RAM, disk space, power supply, and PC case considerations, offering insights into building an efficient multi-GPU system.  ✨ Expert Insights from Packt Community  This week’s featured article is written by Ben Auffarth, the Head of Data Science at loveholidays. LangChain provides an intuitive framework that makes it easier for AI developers, data scientists, and even those new to NLP technology to create applications using LLMs. What can I build with LangChain? LangChain empowers various NLP use cases such as virtual assistants, content generation models for summaries or translations, question answering systems, and more. It has been used to solve a variety of real-world problems.  For example, LangChain has been used to build chatbots, question answering systems, and data analysis tools. It has also been used in a number of different domains, including healthcare, finance, and education. You can build a wide variety of applications with LangChain, including: Chatbots: It can be used to build chatbots that can interact with users in a natural way. Question answering: LangChain can be used to build question answering systems that can answer questions about a variety of topics. Data analysis: You can use it for automated data analysis and visualization to extract insights. Code generation: You can set up software pair programming assistants that can help to solve business problems. And much more! This is an excerpt from the Author’s upcoming book Generative AI with LangChain with Packt. If you're intrigued by this, we invite you to join us at our upcoming virtual conference for an in-depth exploration of LangChain and gain a better understanding of how to responsibly apply Large Language Models (LLMs) and move beyond merely producing statistically driven responses. The author will then take you on the practical journey of crafting your own chatbot, akin to the capabilities of ChatGPT. Missed the Early Bird Special offer for the big event? No worries! You can still save 40% by booking your seat now. Reserve your seat at 40%OFF 💡 Masterclass: AI/LLM TutorialsLearn How to Orchestrate Ray-Based ML Workflows with Amazon SageMaker Pipelines: Discover the benefits of combining Ray and Amazon SageMaker for distributed ML in this comprehensive guide. Understand how Ray, an open-source distributed computing framework, simplifies distributed ML tasks, and how SageMaker seamlessly integrates with it. This post provides a step-by-step tutorial on building and deploying a scalable ML workflow using these tools, covering data ingestion, data preprocessing with Ray Dataset, model training, hyperparameter tuning with XGBoost-Ray, and more. You'll also explore how to orchestrate these steps using SageMaker Pipelines, enabling efficient and automated ML workflows. Dive into the detailed code snippets and unleash the potential of your ML projects. Building and Deploying Tool-Using LLM Agents with AWS SageMaker JumpStart Foundation Models: Discover how to create and deploy LLM agents with extended capabilities, including access to external tools and self-directed task execution. This post introduces LLM agents and guides you through building and deploying an e-commerce LLM agent using Amazon SageMaker JumpStart and AWS Lambda. This agent leverages tools to enhance its functionality, such as answering queries about returns and order updates. The architecture involves a Flan-UL2 model deployed as a SageMaker endpoint, data retrieval tools with AWS Lambda, and integration with Amazon Lex for use as a chatbot.  Step-by-Step Guide to Text Generation with GPT using Hugging Face Transformers Library in Python: In this post, you'll learn how to utilize the Hugging Face Transformers library for text generation and natural language processing without the need for OpenAI API keys. The Hugging Face Transformers library offers a range of models, including GPT-2, GPT-3, GPT-4, T5, BERT, and more, each with unique characteristics and use cases. You'll explore how to install the required libraries, choose a pretrained language model, and generate text based on a prompt or context using Python and the Flask framework. This comprehensive guide will enable you to implement text generation applications with ease, making AI-powered interactions accessible to users.  💬 AI_Distilled User Insights Space Would you like to participate in our user feedback interview to shape AI_Distilled's content and address your professional challenges?Share your content requirements and ideas in 15 simple questions. Plus, be among the first 25 respondents to receive a free Packt credit for claiming a book of your choice from our vast digital library. Don't miss this chance to improve the newsletter and expand your knowledge. Join us today! Share Your Insights Now! 🚀 HackHub: Trending AI Toolsise-uiuc/Repilot: Patch generation tool designed for Java and based on large language models and code completion engines. turboderp/exllamav2: Early release of an inference library for local LLMs on consumer GPUs, requiring further testing and development.  liuyuan-pal/SyncDreamer: Focuses on creating multiview-consistent images from single-view images. FL33TW00D/whisper-turbo: Fast, cross-platform Whisper implementation running in your browser or electron app offering real-time streaming and privacy. OpenBMB/ChatDev: Virtual software company run by intelligent agents with various roles aiming to revolutionize programming and study collective intelligence. 
Read more
  • 0
  • 0
  • 3332

article-image-building-an-investment-strategy-in-the-era-of-llms
Anshul Saxena
21 Sep 2023
16 min read
Save for later

Building an Investment Strategy in the Era of LLMs

Anshul Saxena
21 Sep 2023
16 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!IntroductionFor many, the world of stock trading can seem like a puzzle, but it operates on some core principles. People in the stock market use different strategies to decide when to buy or sell. One popular approach is observing market trends and moving with them, much like a sailor adjusting sails to the wind. Another strategy believes that if prices swing too high or too low, they'll eventually return to their usual state - akin to a pendulum finding its center. Some traders have a straightforward method: buy when things look good and sell when they don't, as simple as following a recipe. And then there are those who patiently wait for prices to break past their usual limits, similar to a birdwatcher waiting for the perfect moment to spot a rare bird. This guide aims to unpack each of these strategies in an easy-to-understand manner, offering insights into the foundational methods of stock trading. A few strategies which we are going to implement is discussed below. Trend Following capitalizes on the market's momentum in a specific direction, often using tools such as moving averages, MACD, and the ADX to decipher potential gains. In contrast, Mean Reversion operates on the belief that prices or returns gravitate back to their historical average; tools like Bollinger Bands and RSI become crucial in identifying overextended assets. Momentum (or Trend Momentum) takes a similar trajectory but focuses on amplifying returns by purchasing high-performing assets and shedding those underperforming ones, with instruments like the Rate of Change (ROC) or Relative Strength acting as key metrics. Lastly, Breakout Trading is about capitalizing on decisive market moves, wherein a trader either buys an asset breaking past a resistance or sells one dropping below a support level. Among these strategies, the moving average crossover technique stands out as a pivotal tool, especially for those keen on trend following, capturing moments when short-term and long-term averages intersect to suggest potential market shifts.Understanding the stock market isn't always straightforward. Many people use different methods to figure out when to buy or sell stocks. Some observe general market movements and go with the flow, while others believe prices usually return to a familiar middle ground after big shifts. There's also the group that buys when things look promising and sells when they seem less favorable. And some? They wait for those standout moments when stock prices move outside their usual range. To get a clearer picture of these methods, modern tools like CHATGPT's data analysis have become handy. They sift through loads of data to help people sharpen their trading tactics. This guide will shed light on these strategies and how tools like CHATGPT make things simpler in the fast-moving world of stocks.You can access the data here (Link)Prompt 1: As a Stock Analyst specializing in trend-following strategies, develop a concise report on APPLE stock using the moving average crossover technique, focusing on the 50-day and 200-day averages. Plot the moving averages to identify "Buy" or "Sell" signals when they cross and backtest these signals against historical data to gauge performance. Present your findings in a visually accessible format with charts for clarity.Response: Let's apply the trend-following strategy using the moving average crossover technique on this new dataset. We'll compute the short-term (50-day) and long-term (200-day) moving averages, generate buy/sell signals based on the crossovers, and then backtest the strategy.After backtesting the trend-following strategy using moving average crossovers on the new dataset: The final balance at the end of the period is approximately -$8,669.88.This resulted in a loss of approximately $108,669.88.It seems there was a significant loss during the period using this strategy on the dataset. This could be due to the specific characteristics of the stock during this timeframe, the chosen moving average windows (50-day and 200-day), or other market factors.To gain a better understanding, let's visualize the stock price, short-term moving average, and long-term moving average with buy and sell points.The graph visualizes the stock's closing price (in blue), the short-term 50-day moving average (in green), and the long-term 200-day moving average (in red). Buy signals are represented by green upward-pointing triangles, while sell signals are shown as red downward-pointing triangles.Observations:The strategy did generate buy and sell signals, which are evident from the visual.The buy signals mostly seem to have been generated during periods when the stock was in a declining phase, leading to losses when subsequently selling.Conversely, sell signals were generated during recovering phases, missing potential gains.The moving averages did not react quickly enough to price changes, suggesting that this particular trend-following strategy might not be ideal for this specific dataset or timeframe.Prompt 2: As a Stock Analyst specializing in mean reversion strategies, develop a succinct report on APPLE stock. Analyze periods when the stock deviates significantly from its historical average and identify potential opportunities where the stock may revert to its mean. Use both statistical metrics and visual charts to highlight these deviations. Ensure to backtest the identified mean reversion points against historical data to assess the strategy's effectiveness. Summarize your findings in a visually engaging format with relevant charts.Response: A mean reversion strategy assumes that prices will tend to revert to their historical mean over time. One common tool used for a mean reversion strategy is the Bollinger Bands, which envelop the price structure of a stock to describe a trading band.Bollinger Bands Mean Reversion Strategy:Bollinger Bands consist of:Middle Band: A \(n\)-day simple moving average (usually \(n = 20\)).Upper Band: \(n\)-day SMA + (\(k\) times \(n\)-day standard deviation of price). \(k\) is usually set to 2.Lower Band: \(n\)-day SMA - (\(k\) times \(n\)-day standard deviation of price).Trading Rules:Buy Signal: When the stock price crosses below the Lower Band, it suggests that the stock is oversold and is likely to revert to the mean.Sell Signal: When the stock price crosses above the Upper Band, it implies that the stock is overbought and might revert back to its mean.Let's apply the Bollinger Bands mean reversion strategy to the dataset, compute the buy/sell signals, and then backtest the strategy. After backtesting the Bollinger Bands-based mean reversion strategy on the dataset:The final balance at the end of the period is approximately $9,744.30.This resulted in a loss of approximately $90,255.70.The outcome suggests that the strategy performed poorly on this dataset during the given timeframe. This emphasizes the importance of thorough backtesting and strategy evaluation before actual deployment.To understand the strategy's behavior, let's visualize the stock price, the Bollinger Bands (Middle, Upper, and Lower bands), and the buy/sell points. The graph illustrates the stock's closing price (in blue), the middle Bollinger Band (20-day SMA in green), the upper Bollinger Band (in red), and the lower Bollinger Band (in cyan). Buy signals are denoted by green upward-pointing triangles, while sell signals are shown as red downward-pointing triangles.Observations:The strategy generated several buy signals when the stock price touched or crossed below the lower Bollinger Band, indicating potential oversold conditions.Similarly, sell signals were generated when the stock price touched or crossed above the upper Bollinger Band, suggesting potential overbought conditions.However, the price often continued its trend after generating a buy/sell signal, leading to losses.Prompt 3: As a Stock Analyst focusing on momentum strategies, compile a detailed report on APPLE stock using the Rate of Change (ROC) as an indicator. Investigate periods where the ROC indicates significant positive or negative momentum, suggesting potential buying or selling opportunities, respectively. Backtest the ROC signals against historical data to validate the strategy's robustness. Present your conclusions using charts that clearly display ROC values alongside stock price movements.Response: Momentum strategies seek to capitalize on the continuation of existing trends in the market. The underlying idea is that assets that have performed well in the past are likely to continue performing well, and vice versa. Momentum Strategy using Rate of Change (ROC):The Rate of Change (ROC) is a momentum oscillator that measures the percentage change in price from one period to the next. The ROC can be used to identify the direction of the trend or gauge its strength.Trading Rules:1. Buy Signal: When the ROC crosses above a predefined threshold (e.g., 0 or another positive value), indicating positive momentum.2. Sell Signal: When the ROC crosses below a predefined threshold (e.g., 0 or another negative value), suggesting negative momentum.Steps:1. Compute the ROC for a specified period (e.g., 14 days).2. Generate buy/sell signals based on the ROC crossing predefined thresholds.3. Backtest the strategy using historical data.Let's implement and backtest this momentum strategy on the provided dataset. After backtesting the momentum strategy using the Rate of Change (ROC) on the dataset:The final balance at the end of the period is approximately -$8,154.72.This results in a loss of approximately $108,154.72.The outcome suggests that the strategy did not perform well on this dataset during the given timeframe. The chosen threshold or the ROC period might not have been optimal for this specific stock.To gain a better understanding, let's visualize the stock price, the ROC values, and the buy/sell points.The visualization provides insights into the momentum strategy using the Rate of Change (ROC):1. The top graph displays the stock's closing price (in blue). Buy signals are shown with green upward-pointing triangles, while sell signals are denoted by red downward-pointing triangles.2. The bottom graph visualizes the ROC values (in purple) with a black zero line for reference.Observations:The ROC indicates the percentage change in price over the specified period (in this case, 14 days). Positive values suggest upward momentum, while negative values indicate downward momentum.The strategy generated buy signals when the ROC was positive and sell signals when the ROC was negative.However, there were instances where the price continued its trend even after a buy/sell signal, leading to losses.Prompt 4: As a Stock Analyst specializing in breakout trading strategies, prepare a comprehensive report on APPLE stock. Identify periods where the stock price breaks out from a defined range, either reaching new highs or lows, hinting at a potential prolonged move in the breakout direction. Incorporate technical indicators like volume or relative strength to validate the strength of the breakout. Backtest identified breakout signals against historical data to evaluate the strategy's consistency. Present your findings with charts illustrating breakout points and accompanying volume or indicator data.Response: Breakout trading is a strategy where traders aim to enter the market when the price makes a breakout from a previously defined resistance or support level. The rationale behind this strategy is that when a significant resistance or support level is broken, it's likely a large number of traders will enter or exit their positions, leading to a sharp price movement. Breakout Trading Strategy:Resistance Level: This is a price level where the stock has had difficulty going above in the past. It acts as a ceiling from which the stock price struggles to break through.Support Level: This is a price level where the stock has had difficulty going below in the past. It acts as a floor where the stock price seems to bounce off.Trading Rules:Buy Signal: When the stock price breaks above the resistance level.Sell Signal: When the stock price breaks below the support level.To implement this strategy, we'll need to define our resistance and support levels. One common method is to use recent highs as resistance and recent lows as support. For our implementation, we'll use:Resistance Level: The highest price over the previous \( n \) days.Support Level: The lowest price over the previous \( n \) days.For this demonstration, we'll use \( n = 14 \) (a 14-day window) to define our resistance and support levels. After determining these levels, we'll generate buy/sell signals based on price breakouts and then backtest the strategy.Let's implement and backtest the breakout trading strategy on the provided dataset.It seems there was an error due to non-numeric data types in the "High" and "Low" columns. Let's first clean up the "High" and "Low" columns by converting them to float, and then we'll proceed with the breakout trading strategy.After backtesting the breakout trading strategy on the dataset:The final balance at the end of the period remains at $100,000.There's no profit or loss, which suggests that the strategy didn't trigger any trades.The breakout strategy is contingent on significant price movements that breach the defined resistance or support levels. It's possible that within the dataset's timeframe, there weren't any significant breakouts that met the strategy's criteria, or the breakout window (14 days) chosen was not optimal.To better understand the strategy's behavior, let's visualize the stock price, resistance, support levels, and the buy/sell points.The graph showcases the stock's closing price (in blue), the resistance level (in red), and the support level (in green). Buy signals are represented with green upward-pointing triangles, while sell signals are shown as red downward-pointing triangles.Observations:There are no visible buy or sell signals on the graph, which aligns with our backtesting results that no trades were executed during the period.The stock price seemed to oscillate within the defined resistance and support levels for the majority of the period, without significant breakouts.Prompt 5:As a Stock Analyst with a focus on technical strategies, draft an in-depth analysis of APPLE stock employing the moving average crossover technique. Concentrate on instances where a short-term moving average (e.g., 50-day) intersects a long-term moving average (e.g., 200-day), signaling potential buy or sell opportunities. For added clarity, chart these moving averages on APPLE's stock price graph, marking crossover points. Backtest observed crossover events against past data to determine the strategy's reliability and effectiveness. Conclude with a visualization that combines stock prices with moving average lines, emphasizing key crossover events.Response: The Moving Average Crossover strategy is one of the most widely recognized techniques in technical analysis. It involves two moving averages: one representing a short period and the other a longer period. Moving Average Crossovers Strategy:1. Short-term Moving Average: A moving average of a shorter period (e.g., 50 days).2. Long-term Moving Average: A moving average of a longer period (e.g., 200 days).Trading Rules:1. Buy Signal (Golden Cross): When the short-term moving average crosses above the long-term moving average.2. Sell Signal (Death Cross): When the short-term moving average crosses below the long-term moving average.For this demonstration, we'll use:Short-term Moving Average: 50-day moving average.Long-term Moving Average: 200-day moving average.We'll calculate these moving averages, identify the crossover points, and backtest the strategy on the provided dataset.Using the Moving Average Crossover strategy:The final balance at the end of the period remains at $100,000.There's no profit or loss, which implies that the strategy didn't execute any trades during the period.This outcome suggests that there weren't any significant crossovers between the 50-day and 200-day moving averages during the dataset's timeframe. To better understand the strategy's behavior, let's visualize the stock price, the two moving averages, and any potential crossover points. The graph showcases the stock's closing price (in blue), the 50-day moving average (in green), and the 200-day moving average (in red). Golden Crosses (potential buy signals) are represented with green upward-pointing triangles, while Death Crosses (potential sell signals) are shown as red downward-pointing triangles.Observations:It's evident that there were no Golden Cross or Death Cross signals during the period, confirming our backtesting results.The 50-day moving average and 200-day moving average seem to move closely together throughout the dataset's timeframe without crossing each other.ConclusionSo, after navigating the vast sea of stock trading, it's evident that people have a variety of ways to approach their decisions. From sailing with the direction of the market to waiting patiently for those significant shifts, everyone's got their own strategy. It's a bit like choosing between hiking trails; some prefer the scenic route while others opt for the straightforward path. And with the help of modern tools like CHATGPT's data analysis, making these choices becomes a tad simpler. It's like having a handy guidebook for a complex journey. By understanding these methods and using available resources, anyone can navigate the stock market more confidently. It's all about finding what works best for you and sticking to it.Dr. Anshul Saxena is an author, corporate consultant, inventor, and educator who assists clients in finding financial solutions using quantum computing and generative AI. He has filed over three Indian patents and has been granted an Australian Innovation Patent. Anshul is the author of two best-selling books in the realm of HR Analytics and Quantum Computing (Packt Publications). He has been instrumental in setting up new-age specializations like decision sciences and business analytics in multiple business schools across India. Currently, he is working as Assistant Professor and Coordinator – Center for Emerging Business Technologies at CHRIST (Deemed to be University), Pune Lavasa Campus. Dr. Anshul has also worked with reputed companies like IBM as a curriculum designer and trainer and has been instrumental in training 1000+ academicians and working professionals from universities and corporate houses like UPES, CRMIT, and NITTE Mangalore, Vishwakarma University, Pune & Kaziranga University, and KPMG, IBM, Altran, TCS, Metro CASH & Carry, HPCL & IOC. With a work experience of 5 years in the domain of financial risk analytics with TCS and Northern Trust, Dr. Anshul has guided master's students in creating projects on emerging business technologies, which have resulted in 8+ Scopus-indexed papers. Dr. Anshul holds a PhD in Applied AI (Management), an MBA in Finance, and a BSc in Chemistry. He possesses multiple certificates in the field of Generative AI and Quantum Computing from organizations like SAS, IBM, IISC, Harvard, and BIMTECH.Author of the book: Financial Modeling Using Quantum Computing
Read more
  • 0
  • 0
  • 464
Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at €18.99/month. Cancel anytime
article-image-develop-your-app-from-start-to-finish-with-chatgpt
Jakov Semenski
21 Sep 2023
8 min read
Save for later

Develop Your App from Start to Finish with ChatGPT

Jakov Semenski
21 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. Don't miss out – sign up today!IntroductionEveryone is using ChatGPT to code.But almost everyone is stuck in “hello world” mode.I will show you 9 techniques on how you can get massively ahead by using these prompts to create an app from start to finishBefore we start, let's dive into a typical scenario when coding an app from 0.Why creating an app is so difficult?So, you've got coding basics.But jumping from that to building a full-blown app seems like leaping over a skyscraper. Right?”Every developer faces the same dilemma.Where do I start?Which tech stack to use?Should I use a framework?What steps do I need to take?There are just too many options out there, and without having vast experience, which approach you take?Traditional approachSpend hours reading documentationTrial and error codingSeeking help on forumsChatGPT-Assisted approachDescribe your objective or challenge to ChatGPTGet guided solutionsRefine as per your needsAnd implementIssues with ChatGPT-assisted approachMaybe you’ve tried using ChatGPT, but you’re still stuck in a “HELLO WORLD” mode.Most of the time ChatGPT spits out Hello World examples.You don’t even have an idea about the tech stack.All you get are code snippets.Output is nowhere near a full-blown app.The coding output might be working, but the delivery is not so much, let’s change that.Use these 9 steps instead:Step 1: Ideation processYou know what you want to build an app, but don’t know where to start.You can start with, “give me ideas on library app”.The problem with that one, it’s too vague, and ChatGPT cannot give you a clear and concise response.Do this instead:Specify as much as you can regarding the appSpecify your roleAsk for output in table format with pros, consWHO AM I I work as a software developer APP DESCRIPTION I need to build a backend of an application from scratch where users can borrow technical books, users with admin role can administrate books and other users TOPIC Outline 4 technical solutions which tech stack is used, frameworks, language and framework versions where it can be deployed FORMAT When discussing potential solutions present the information in a table format, outlining the pros and cons of each option—allowing for easier comparison and decision-making. ChatGPT will respond#Tech StackDeployment OptionsProsCons1MEAN (MongoDB, Express.js, Angular, Node.js)Heroku, AWS, Digital OceanJavaScript across full stack<br> - Scalable <br> - Large communityCallback hell if not careful<br> - Angular has a steeper learning curve2MERN (MongoDB, Express.js, React, Node.js)Heroku, AWS, Netlify, VercelPopularity of React<br> - Flexibility and modularity<br> - Large communityLots of libraries/tools to manage<br> - Callback hell if not careful3Spring Boot (Java) with PostgreSQLAWS, Azure, Heroku, Google CloudRobust and mature<br> - High performance<br> - Type safety with JavaHigher memory consumption<br> - Slower development cycle4Django (Python) with PostgreSQLAWS, Heroku, Google Cloud, Digital Ocean"Batteries-included" framework<br> - Admin interface out of the box<br> - High productivityORM might not suit large-scale apps<br> - Heavier compared to FlaskElaboration:MEAN Stack:MongoDB: A NoSQL…..Step 2: Specify the knowledge levelChatGPT doesn’t know which language, tools and frameworks you are familiar with.For example, a junior might sayI’m a backend software developer and solely use Java. I am familiar with Java 7, but don't have experience with Stream API Or a senior might sayI’m an experienced backend software developer and solely use Java. I am familiar with Java 17, and with latest Spring boot framework You can use this prompt to further narrow down tech stack, or to start codingStep 3: Outline the app before we start codingNow you have the idea, but still, you don’t know how many files you need to cover.Before you dive into coding, ask ChatGpt to give you the representation of your project and how it would look in your IDE.Outline project structure with the usage of emojis representing file and directory types.For our project show me the directory structure using ASCII and emojis to represent directory and file types Step 4: Specify coding outputSome of us hate when ChatGPT gives back so verbose explanations .And then barely can give out code before it hits the output limit.Do this instead:When I ask you for code, please just give me the code without any explanation on how it works. Don't include import statements. Bias towards the most efficient solution. Remember: The more context you provide, the better the AI can guide and offer tailored solutions.Step 5: We don’t want code vomitBack in the days when there were no coding guidelines, developers were rewarded with the amount of code they produced.Nowadays, with so many new hires and leavers, we need to leave the code base in a good state, so the next person who joins can follow up.To prevent ChatGPT from vomiting code, let it use the latest coding principles.Write clean code Code needs to be covered with Logging and proper exception handling Use principles: Kiss & DRY, SOLID Keep in mind to use design patterns where it is applicable Now you can ask ChatGPT to give you code.Using coding instructions I gave you, give me code for each class Apart from Code, you will also get an explanation of where ChatGPT used those principles.ChatGPT explaining output.KISS: It's straightforward and easy to understand.DRY: The exception handling and logging are centralized.SOLID:S: Single Responsibility - Each class has one job.O: Open/Closed - Classes are open for extension, but closed for modification.L: Liskov Substitution - Derived class must be substitutable for its base class.I: Interface Segregation - We've used the necessary interfaces.D: Dependency Inversion - High-level modules are not dependent on low-level ones; both depend on abstractions.Design Pattern:Repository Pattern: Used to separate database access code from business logic.Service Pattern: Business logic is placed in the service layer.Singleton Pattern: Spring beans are singleton by default.Decorator Pattern: The ControllerAdvice acts as a decorator around all controllers, providing centralized exception handling.Optional: IDE supportIn case you are new to coding and don’t have much experience with using IDE (Integrated development environment).Ask ChatGPT to instruct you how to import this project.I am using IntelliJ IDE, maven project, give me steps I need to take to import such a project Step 6: Write test classesWe can ask it to write tests, but let's be more specific.Give ChatGPT library names, and approaches and specify which kind of tests you want to write.Using most popular testing libraries, write Integration test, Unit test Use Mockito BDD and AssertJ libraries For Integration tests use Test slices Step 7: Give example dataOur app will not work without testing data, this could be.Database sample dataTesting strategyWrite SQL statements that prepopulate our app the first time it starts with testing data Define which files need to be created and where they should be placed Step 8: Specify other infrastructure componentsMaybe you are using an embedded database, or you already have one running in the cloud or locally in a docker container.Give ChatGPT instructions on what systems do you use and how.I want to use an embedded database and authenticate against spring embedded oauth Step 9: How can I use the appNow you have your REST service available, but how do you test it?Ask ChatGPT to give you requests that you can use with Postman.Write CURL statements I can execute in terminal or import in postman to Get access token List all available books To add new book Here is an example of what ChatGPT can produce.ConclusionNow you went from the “Hello World” stage to the “I am Crushing” stageI have a full step-by-step guide to writing a full-blown app from start to finish by following the latest coding guidelines and principles.Use those steps, play around, and you can even follow through all prompts directly in ChatGPT by going to this link:https://chat.openai.com/share/68fbca70-d5d8-4f99-96c9-18d121f4bcb8Author BioJakov Semenski is an IT Architect working at IBMiX with almost 20 years of experience.He is also a ChatGPT Speaker at the WeAreDevelopers conference and shares valuable tech stories on LinkedIn.
Read more
  • 0
  • 0
  • 496

article-image-how-large-language-models-reshape-trading-stats
Anshul Saxena
21 Sep 2023
15 min read
Save for later

How Large Language Models Reshape Trading Stats

Anshul Saxena
21 Sep 2023
15 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!IntroductionStock analysis is not just about numbers; it's a sophisticated dance of interpretation and prediction. Advanced techniques, such as the ones discussed here, offer deeper insights into the world of stocks. The journey begins with Volatility Analysis, utilizing Rolling Standard Deviation to grasp the extent of stock price movements, offering a window into the stock's inherent risk. Predictive Modeling then takes the stage, harnessing past data to provide a lens into potential future stock prices. Yet, for any analysis to stand on solid ground, the data itself must be pristine. This is where Data Cleaning comes into play, meticulously weeding out inconsistencies and errors, and laying a trustworthy foundation. To make sense of this vast sea of data, Visualizations transform intricate datasets into digestible charts and graphs, bridging the gap between numbers and narratives. Now, with these advanced steps in view, it's time to Dive In. This guide is designed for those keen to delve into a more profound layer of stock analysis, be it beginners eager to learn or veterans aiming to hone their skills.Step 1. Volatility Analysis gauges stock price fluctuations using Rolling Standard Deviation.Step 2. Data Cleaning: Ensure the data is free of inconsistencies and errors before diving into analysis.Step 3. Visualizations: Create intuitive charts and graphs to represent data, making it easily comprehensible at a glance.Step 4. Heat Map: Utilize heatmaps to visualize monthly average closing prices, helping traders spot recurring patterns, seasonality, and stock price shifts for informed investment decisions.Step 5. Predictive Modeling: Leverage historical data to attempt predictions of future stock prices.You can access the data here (Link)Step 1: Volatility Analysis - Rolling Standard DeviationVolatility Analysis employs the Rolling Standard Deviation to measure the consistency of stock price fluctuations over time.Prompt 1: As a diligent stock analyst, recognizing and quantifying a stock's volatility is paramount in understanding its risk and potential reward. To delve into this aspect, let's employ the rolling standard deviation methodology on the daily returns. Using a 21-day window - a conventional choice for monthly volatility assessments - will yield a dynamic depiction of the stock's volatility. The subsequent plot showcasing the 21-day rolling volatility essentially captures the standard deviation of the stock's daily returns over this time frame. Such a visualization can aid investors in discerning periods of heightened uncertainty or stability, assisting in refined decision-making.Response: Absolutely, the rolling standard deviation of daily returns is a commonly used metric to assess the volatility of a stock. A higher standard deviation signifies greater volatility, while a lower value indicates stability.To assess the stock's volatility:1. Compute the rolling standard deviation of the daily returns using a 21-day window.2. Plot the resulting 21-day rolling volatility.Let's proceed with these steps.The graph showcases the 21-day rolling volatility of the stock, which is essentially the standard deviation of the stock's daily returns over a 21-day window.# Compute the 21-day rolling standard deviation of the daily returns data['21-day Volatility'] = data['Daily Returns'].rolling(window=21).std() # Plotting the 21-day rolling volatility plt.figure(figsize=(15, 7)) plt.plot(data.index, data['21-day Volatility'], label='21-day Volatility', color='purple') plt.title("21-day Rolling Volatility") plt.xlabel("Date") plt.ylabel("Volatility (Standard Deviation of Daily Returns)") plt.legend() plt.grid(True) plt.tight_layout() plt.show() Interpretation: The chart shows the stock's ups and downs over time using a purple line. When this line peaks, it means the stock had a lot of movement, possibly because of big company news or changes in the wider market. On the other hand, when the line is low, the stock was more stable. While some investors might see these peaks as chances to make quick gains, others might prefer the steadier, low points. Simply put, this chart helps investors decide when to buy or sell based on how much the stock is expected to move.After analyzing the volatility, let’s clean the data  for further analysisStep 2: Data Cleaning and TransformationData Cleaning and Transformation involves addressing missing values, normalizing or standardizing data for modeling purposes, and ensuring date uniformity for a robust time series analysis by setting it as the index.Prompt 2: As a meticulous stock analyst, it's crucial to ensure the integrity and completeness of our dataset before deep-diving into further analysis. Start by scanning the dataset for any missing values, and addressing them appropriately, either through imputation or deletion based on the dataset's nature and the missing data's extent. Moreover, consistency is key; hence, confirm that the "Date" column adheres to a uniform format. This foundational data preparation step ensures the reliability of our subsequent analyses and insights.Response: Absolutely, ensuring the data's integrity and completeness is a foundational step in any analysis. To maintain the reliability of our subsequent analyses, we'll:1. Scan the dataset for missing values.2. Address these missing values, deciding on either imputation or deletion based on the nature and extent of the missing data.3. Confirm that the "Date" column is in a consistent format.Let's start by checking for missing values in our dataset.# Check for missing values in the dataset missing_values = data.isnull().sum() missing_valuesThe dataset has missing values in the following columns:1. Daily Returns: 1 missing value - This is expected since the first entry won't have a previous day to compute the return from.2. 50-day MA: 49 missing values - The 50-day moving average will have missing values for the initial 49 days since there aren't enough data points to compute the average.3. 200-day MA: 199 missing values - Similarly, the 200-day moving average will lack values for the initial 199 days.4. 21-day Volatility: 21 missing values - The 21-day rolling volatility will be absent for the initial 21 days.Given the nature of these missing values (they're a result of our calculations and are expected due to the rolling window computations), we don't need to impute them. However, for some analyses, we might want to focus on dates after these initial missing values to ensure continuity.Next, let's ensure the "Date" column (which is now our index) is consistent in format.# Check the data type and consistency of the "Date" column (which is now the index) date_format_consistency = data.index date_format_consistency The "Date" column, which is now our index, is of type `datetime64[ns]`. This confirms that the dates are in a consistent datetime format, ensuring that our subsequent analyses are accurate and reliable.Step 3: Visualizations - Distribution PlotsVisualizations encompass the use of candlestick plots to intricately depict stock price movements, distribution plots to showcase price and volume variances, and heatmaps to represent monthly or yearly average stock prices.Prompt 3: In your role as a stock analyst, visualizing the distribution of data can offer profound insights into its behavior and characteristics. Now, direct your attention to the distribution plots of both the stock prices (specifically the Close/Last prices) and the trading volume. These plots will elucidate the spread, skewness, peaks, and central tendencies of the data.1. Distribution of Close/Last Prices: By examining this plot, you can discern the most frequent closing prices, identify any outliers, and understand the distribution's symmetry or skewness. Does it lean towards higher or lower prices? Are there any significant peaks?2. Distribution of Trading Volume: This visualization will shed light on the typical trading volume levels. Are there days of extraordinarily high or low trading volumes? Does the distribution hint at a particular trend in trading activity?Through these plots, you're better positioned to understand the stock's price movements and the market's interest in it, guiding future analysis or investment recommendations.Response: To visualize the distribution of data, we'll employ distribution plots (or histograms) for both the stock's closing prices and the trading volume.1. Distribution of Close/Last Prices: This will help us understand the frequency distribution of the closing prices, offering insights into the most common price levels, the data's symmetry or skewness, and potential outliers.2. Distribution of Trading Volume: This will shed light on the frequency distribution of trading volumes, allowing us to discern common volume levels and any anomalies in trading activity.Let's begin by visualizing the distribution of the stock's Close/Last prices.# Importing necessary libraries for visualization import seaborn as sns # Setting up the figure and axes fig, ax = plt.subplots(nrows=2, ncols=1, figsize=(15, 10)) # Distribution of Close/Last Prices sns.histplot(data['Close/Last'], bins=50, kde=True, ax=ax[0], color='blue') ax[0].set_title("Distribution of Close/Last Prices") ax[0].set_xlabel("Close/Last Price ($)") ax[0].set_ylabel("Frequency") # Distribution of Trading Volume sns.histplot(data['Volume'], bins=50, kde=True, ax=ax[1], color='gray') ax[1].set_title("Distribution of Trading Volume") ax[1].set_xlabel("Trading Volume") ax[1].set_ylabel("Frequency") plt.tight_layout() plt.show() Interpretation: The plots show how often the stock closes at different prices and how its trading volume varies. For closing prices, most often they land in a certain range, but sometimes they go a bit higher. There don't seem to be any unusual values, but a different chart could confirm this. The trading volume, or how much the stock is traded, usually stays within a specific range. However, there are rare days when it's traded a lot, possibly due to big news about the company. In short, these charts give a snapshot of the stock's usual behavior and any deviations from it, helping guide investment choices.Step 4: Visualizations – HeatmapHeat Map visualization showcases monthly average closing prices, empowering traders to identify recurring patterns, discern seasonality, and detect stock price shifts, thereby guiding informed investment choices.Prompt 9: As an astute stock analyst, appreciating the power of visualizations to detect patterns is vital. To enhance our understanding of the stock's behavior over time, let's craft a heatmap that displays the monthly average closing prices. Heatmaps, with their gradient color schemes, provide an intuitive way to quickly spot variations and trends. By focusing on monthly averages, this visualization will surface any recurring monthly patterns, seasonality, or discernible shifts in the stock's closing prices. Such insights can be instrumental for traders and investors seeking to capitalize on consistent temporal trends in the stock market.Response: The heatmap showcases the monthly average closing prices for the stock:# Define the price columns price_columns = ['Close/Last', 'Open', 'High', 'Low'] # Convert the price columns to float values by removing the '$' sign and converting to float for col in price_columns:    data[col] = data[col].str.replace('$', '', regex=False).astype(float) # Set the 'Date' column as the index data.set_index('Date', inplace=True) # Compute the daily returns data['Daily Returns'] = data['Close/Last'].pct_change() # Resample the data for monthly average and transform for heatmap monthly_avg = data['Close/Last'].resample('M').mean() heatmap_data = monthly_avg.groupby([monthly_avg.index.year, monthly_avg.index.month]).mean().unstack() # Plotting the heatmap plt.figure(figsize=(12, 6)) sns.heatmap(heatmap_data, cmap="YlGnBu", annot=True, fmt=".2f", cbar_kws={'label': 'Average Closing Price ($)'}) plt.title("Monthly Average Closing Prices") plt.xlabel("Month") plt.ylabel("Year") plt.show()  Interpretation:Color Gradient: The color gradient, ranging from light to dark shades of blue, represents the average closing price. Darker shades indicate higher average closing prices for that particular month, while lighter shades suggest lower averages.Annotations: Each cell in the heatmap is annotated with the specific average closing price for that month and year.By examining this heatmap:Patterns & Seasonality: You can spot any recurring monthly patterns, seasonality, or discernible shifts in the stock's closing prices over the years.Variations: The color variations can quickly help identify months with particularly high or low average closing prices.This visualization provides a comprehensive and intuitive view of the stock's price movements on a monthly basis, aiding traders and investors in spotting consistent temporal trends and guiding future analyses or investment strategies. Now let’s look into the future trend.Step 5: Predictive Modeling (Time Series Forecasting)In the realm of stock analysis, forecasting is crucial to anticipate market movements. Starting with chronological time series data, one should ensure its stationarity, utilize ACF and PACF for ARIMA parameter selection, then train, forecast, visualize the results, and continuously validate and refine, ensuring well-informed investment decisions.Prompt 5: As a dedicated stock analyst, I understand the nuances and intricacies of time series forecasting. When working with stock price data, the ARIMA (Autoregressive Integrated Moving Average) model can be an invaluable tool. Begin with your time series data, ensuring chronological ordering and handling any missing values. It's crucial to ensure the data's stationarity, as ARIMA thrives on stationary datasets. This can be validated visually or with tests like the Augmented Dickey-Fuller. Diving deeper, ARIMA's efficacy lies in its parameters: \(p\) for the lagged values, \(d\) for differencing, and \(q\) for the moving average term. Helpful tools like ACF and PACF plots can guide the selection of these parameters. Once defined, train your ARIMA model on the dataset. With this trained model, predict future stock prices for your desired time frame. Visual representation can be achieved by plotting the original time series alongside the ARIMA predictions. For a more holistic view, overlay the forecast's confidence intervals. Remember, in the world of stock forecasting, model validation is paramount. Compare your model's predictions against a holdout set if available, employing metrics such as MAE or RMSE. Continual refinement might be necessary, so be prepared to iterate and adjust as needed for the most accurate forecasting.Response: We can fit the ARIMA model to our data. However, it's worth noting that multiple combinations of \(p\), \(d\), and \(q\) might be suitable. Model selection criteria, like AIC or BIC, can help refine our choices.Let's proceed by fitting the ARIMA model using the initial parameters and then forecast future stock prices.from statsmodels.tsa.arima.model import ARIMA from datetime import timedelta # Initial parameters based on ACF and PACF plots p, d, q = 1, 1, 1 # Fit the ARIMA model model = ARIMA(data['Close/Last'], order=(p, d, q)) model_fit = model.fit() # Forecast the next 30 days forecast_steps = 30 forecast = model_fit.get_forecast(steps=forecast_steps) forecast_index = [data.index[-1] + timedelta(days=i) for i in range(1, forecast_steps + 1)] # Extract the forecast mean and confidence intervals forecast_mean = forecast.predicted_mean confidence_intervals = forecast.conf_int() # Plot the original series and the forecast plt.figure(figsize=(15, 7)) data['Close/Last'].plot(label='Observed', color='blue') plt.plot(forecast_index, forecast_mean, color='red', label='Forecast') plt.fill_between(forecast_index, confidence_intervals.iloc[:, 0], confidence_intervals.iloc[:, 1], color='pink', alpha=0.3) plt.title('Stock Closing Price Forecast using ARIMA') plt.xlabel('Date') plt.ylabel('Close Price ($)') plt.legend() plt.grid(True) plt.tight_layout() plt.show() Interpretation: The chart shows past stock prices (in blue) and what they might be in the next 30 days (in red), based on a prediction model called ARIMA. Around the red line is a pink area that suggests where the stock prices might realistically land. However, it's vital to remember that this is just an estimate. Predicting stock prices is tricky because so many different things can affect them. As time goes on, the pink area gets broader, meaning the predictions are less certain. While this model offers a glimpse into potential future prices, always be cautious when basing decisions on predictions, as the stock market is full of surprises.ConclusionStock analysis, often seen as a realm of pure numbers, is actually a delicate blend of art and science, interpretation paired with prediction. As we've journeyed through, advanced techniques like Volatility Analysis have provided clarity on the unpredictable nature of stocks, while Data Cleaning ensures that our foundation is rock-solid. Visual tools, especially intuitive heatmaps, act as a compass, highlighting subtle patterns and variations in monthly stock prices. At the heart of it all, Predictive Modeling stands as a beacon, illuminating potential future paths using the wisdom of past data. Whether one is just stepping into this vast ocean or is a seasoned navigator, the tools and techniques discussed here not only simplify the journey but also enhance the depth of understanding. In stock analysis, as in many fields, knowledge is power, and with these methods in hand, both newcomers and experts are well-equipped to make informed, strategic decisions in the dynamic world of stocks.Author BioDr. Anshul Saxena is an author, corporate consultant, inventor, and educator who assists clients in finding financial solutions using quantum computing and generative AI. He has filed over three Indian patents and has been granted an Australian Innovation Patent. Anshul is the author of two best-selling books in the realm of HR Analytics and Quantum Computing (Packt Publications). He has been instrumental in setting up new-age specializations like decision sciences and business analytics in multiple business schools across India. Currently, he is working as Assistant Professor and Coordinator – Center for Emerging Business Technologies at CHRIST (Deemed to be University), Pune Lavasa Campus. Dr. Anshul has also worked with reputed companies like IBM as a curriculum designer and trainer and has been instrumental in training 1000+ academicians and working professionals from universities and corporate houses like UPES, CRMIT, and NITTE Mangalore, Vishwakarma University, Pune & Kaziranga University, and KPMG, IBM, Altran, TCS, Metro CASH & Carry, HPCL & IOC. With a work experience of 5 years in the domain of financial risk analytics with TCS and Northern Trust, Dr. Anshul has guided master's students in creating projects on emerging business technologies, which have resulted in 8+ Scopus-indexed papers. Dr. Anshul holds a PhD in Applied AI (Management), an MBA in Finance, and a BSc in Chemistry. He possesses multiple certificates in the field of Generative AI and Quantum Computing from organizations like SAS, IBM, IISC, Harvard, and BIMTECH.Author of the book: Financial Modeling Using Quantum Computing
Read more
  • 0
  • 0
  • 412

article-image-getting-started-with-the-chatgpt-api
Martin Yanev
21 Sep 2023
9 min read
Save for later

Getting Started with the ChatGPT API

Martin Yanev
21 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. 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 IntroductionIn this article, we'll walk you through the essential steps to get started with ChatGPT, from creating your OpenAI account to accessing the ChatGPT API. Whether you're a seasoned developer or a curious beginner, you'll learn how to harness the capabilities of ChatGPT, understand tokens, and pricing, and explore its versatility in various NLP tasks. Get ready to unlock the potential of ChatGPT and embark on a journey of seamless communication with AI.Creating an OpenAI AccountBefore using ChatGPT or the ChatGPT API, you must create an account on the OpenAI website, which will give you access to all the tools that the company has developed. To do that, you can visit https://chat.openai.com, where you will be asked to either login or sign up for a new account, as shown in Figure 1.1: OpenAI Welcome WindowSimply click the Sign up button and follow the prompts to access the registration window (see Figure 1.2). From there, you have the option to enter your email address and click Continue, or you can opt to register using your Google or Microsoft account. Once this step is complete, you can select a password and validate your email, just like with any other website registration process.After completing the registration process, you can begin exploring ChatGPT’s full range of features. Simply click the Log in button depicted in Figure 1.1 and enter your credentials into the Log In window. Upon successfully logging in, you’ll gain full access to ChatGPT and all other OpenAI products. With this straightforward approach to access, you can seamlessly explore the full capabilities of ChatGPT and see firsthand why it’s become such a powerful tool for natural language processing tasks.OpenAI Registration WindowNow we can explore the features and functionality of the ChatGPT web interface in greater detail. We’ll show you how to navigate the interface and make the most of its various options to get the best possible results from the AI model.ChatGPT Web InterfaceThe ChatGPT web interface allows users to interact with the AI model. Once a user registers for the service and logs in, they can enter text prompts or questions into a chat window and receive responses from the model. You can ask ChatGPT anything using the Send a message… text field. The chat window also displays previous messages and prompts, allowing users to keep track of the conversation’s context, as shown in the below figure:ChatGPT Following Conversational ContextIn addition to that, ChatGPT allows users to easily record the history of their interactions with the model. Users’ chat logs are automatically saved, which can later be accessed from the left sidebar for reference or analysis. This feature is especially useful for researchers or individuals who want to keep track of their conversations with the model and evaluate its performance over time. The chat logs can also be used to train other models or compare the performance of different models. You are now able to distinguish and use the advancements of different ChatGPT models. You can also use ChatGPT from the web, including creating an account and generating API keys. The ChatGPT API is flexible, customizable, and can save developers time and resources, making it an ideal choice for chatbots, virtual assistants, and automated content generation. In the next section, you will learn how to access the ChatGPT API easily using Python.Getting Started with the ChatGPT APIThe ChatGPT API is an application programming interface developed by OpenAI that allows developers to interact with Generative Pre-trained Transformer (GPT) models for natural language processing (NLP) tasks. This API provides an easy-to-use interface for generating text, completing prompts, answering questions, and carrying out other NLP tasks using state-of-the-art machine learning models.The ChatGPT API is used for chatbots, virtual assistants, and automated content generation. It can also be used for language translation, sentiment analysis, and content classification. The API is flexible and customizable, allowing developers to fine-tune the model’s performance for their specific use case. Let’s now discover the process of obtaining an API key. This is the first step to accessing the ChatGPT API from your own applications.Obtaining an API KeyTo use the ChatGPT API, you will need to obtain an API key. This can be obtained from OpenAI. This key will allow you to authenticate your requests to the API and ensure that only authorized users can access your account.To obtain an API key, you must access the OpenAI Platform at https://platform.openai. com using your ChatGPT credentials. The OpenAI Platform page provides a central hub for managing your OpenAI resources. Once you have signed up, you can navigate to the API access page: https:// platform.openai.com/account/api-keys. On the API access page, you can manage your API keys for the ChatGPT API and other OpenAI services. You can generate new API keys, view and edit the permissions associated with each key, and monitor your usage of the APIs. The page provides a clear overview of your API keys, including their names, types, and creation dates, and allows you to easily revoke or regenerate keys as needed.Click on the +Create new secret key button and your API key will be created: Creating an API KeyAfter creating your API key, you will only have one chance to copy it (see below figure). It’s important to keep your API key secure and confidential, as anyone who has access to your key could potentially access your account and use your resources. You should also be careful not to share your key with unauthorized users and avoid committing your key to public repositories or sharing it in plain text over insecure channels.Saving an API KeyCopying and pasting the API key in our applications and scripts allows us to use the ChatGPT API. Now, let’s examine the ChatGPT tokens and their involvement in the OpenAI pricing model.API Tokens and PricingWhen working with ChatGPT APIs, it’s important to understand the concept of tokens. Tokens are the basic units of text used by models to process and understand the input and output text.Tokens can be words or chunks of characters and are created by breaking down the text into smaller pieces. For instance, the word “hamburger” can be broken down into “ham,” “bur,” and “ger,” while a shorter word such as “pear” is a single token. Tokens can also start with whitespace, such as “ hello” or “ bye”.The number of tokens used in an API request depends on the length of both the input and output text. As a rule of thumb, one token corresponds to approximately 4 characters or 0.75 words in English text. It’s important to note that the combined length of the text prompt and generated response must not exceed the maximum context length of the model. Table 1.1 shows the token limits of some of the popular ChatGPT models.API model token limitsTo learn more about how text is translated into tokens, you can check out OpenAI’s Tokenizer tool. The tokenizer tool is a helpful resource provided by OpenAI for understanding how text is translated into tokens. This tool breaks down text into individual tokens and displays their corresponding byte offsets, which can be useful for analyzing and understanding the structure of your text.You can find the tokenizer tool at https://platform.openai.com/tokenizer. To use the tokenizer tool, simply enter the text you want to analyze and select the appropriate model and settings.The tool will then generate a list of tokens, along with their corresponding byte offsets (see below figure).The Tokenizer ToolThe ChatGPT API pricing is structured such that you are charged per 1,000 tokens processed, with a minimum charge per API request. This means that the longer your input and output texts are, the more tokens will be processed and the higher the cost will be. Table 1.2 displays the cost of processing 1,000 tokens for several commonly used ChatGPT models.ChatGPT API Model PricingImportant noteIt is important to keep an eye on your token usage to avoid unexpected charges. You can track your usage and monitor your billing information through the Usage dashboard at https:// platform.openai.com/account/usage.As you can see, ChatGPT has an easy-to-use interface that allows developers to interact with GPT models for natural language processing tasks. Tokens are the basic units of text used by the models to process and understand the input and output text. The pricing structure for the ChatGPT API is based on the number of tokens processed, with a minimum charge per API request.ConclusionIn conclusion, this article has provided a comprehensive overview of the essential steps to embark on your journey with OpenAI and ChatGPT. We began by guiding you through the process of creating an OpenAI account, ensuring you have seamless access to the myriad tools offered by the company. We then delved into the ChatGPT web interface, showing you how to navigate its features effectively for productive interactions with the AI model. Moreover, we explored the ChatGPT API, highlighting its versatility and use cases in various NLP tasks. Understanding tokens and pricing was demystified, allowing you to make informed decisions. As you embark on your ChatGPT journey, you are well-equipped with the knowledge to harness its potential for your unique needs. Happy exploring!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
  • 1776

article-image-chatgpt-for-everyday-productivity
Valentina Alto
20 Sep 2023
10 min read
Save for later

ChatGPT for Everyday Productivity

Valentina Alto
20 Sep 2023
10 min read
Dive deeper into the world of AI innovation and stay ahead of the AI curve! Subscribe to our AI_Distilled newsletter for the latest insights. Don't miss out – sign up today!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 this article guide, we will cover the main activities ChatGPT can perform for general users daily to boost their productivity. The chapter will focus on concrete examples of writing assistance, decision-making, information retrieval, and so on, with suggestions and prompts so that you can implement them on your own.By the end of this article, you will have learned how to use ChatGPT as a booster for the following activities: Daily activities such as organizing agendas, meal-prepping, grocery shopping, and so onGenerating brand-new text contentImproving your writing skills and adapting the same content to different audiencesRetrieving documentation and information for research and competitive intelligenceTechnical requirementsFor this article, you will require a ChatGPT account. You can create one for free.ChatGPT as a daily assistantChatGPT can serve as a valuable daily assistant, helping you manage your tasks and streamline your workflow. It can optimize your daily routine by providing personalized assistance, thus saving you time and enhancing your efficiency.Let’s start with a general suggestion on how I could make my day more productive:Figure – An example of ChatGPT generating a productive routineThe preceding routine is pretty generic since it doesn’t focus on a specific list of activities (except studying). Let’s challenge ChatGPT to create a more task-oriented routine, including a list of activities we have to carry on during the day:Figure – Optimized routine generated by ChatGPT on top of my activitiesAs you can see, here, ChatGPT acted as a to-do list maker, managing time for me and helping me in organizing my busy day.Now, let’s look at some more examples of using ChatGPT for more specific tasks, including text generation, writing assistance, and information retrieval.Generating textAs a language model, ChatGPT is particularly suited for generating text based on users’ instructions. For example, you could ask ChatGPT to generate emails, drafts, or templates that target a specific audience:Figure – Example of an email generated by ChatGPTAnother example might be asking ChatGPT to create a pitch structure for a presentation you have to prepare:Figure – Slideshow agenda and structure generated by ChatGPTImproving writing skills and translationSometimes, rather than generating new content, you might want to revisit an existing piece of text. It this be for style improvement purposes, audience changes, language translation, and so on.Let’s look at some examples. Imagine that I drafted an email to invite a customer of mine to a webinar. I wrote two short sentences. Here, I want ChatGPT to improve the form and style of this email since the target audience will be executive-level:Figure – Example of an email revisited by ChatGPT to target an executive audienceNow, let’s ask the same thing but with a different target audience:Figure – Example of the same email with a different audience, generated by ChatGPTChatGPT can also give you some feedback about your writing style and structure.Imagine, for example, that you wrote a script with scenography for your YouTube channel. You included the speech as well as images, clips, and video editing activities. You also know that your typical audience is between 15 and 25 years old. You want feedback on your script and ask for this from ChatGPT:                                                                          Figure – Example of ChatGPT providing feedback on a video scriptAs you can see, not only was ChatGPT able to give me feedback about the writing style, but also it suggested how I could improve the scenography of the whole video, by including more visuals.I was impressed by this last one. ChatGPT was smart enough to see that there was no specific mention of the history of NLP in my introduction. Nevertheless, it sets up the expectation about that topic to be treated later on. This means that ChatGPT also has expertise in terms of how an essay should be structured and it was very precise in applying its judgment, knowing that it was just an introduction.It is also impressive to note how the model can give different feedback, depending on the context. With the video script, ChatGPT’s feedback took into account that the final consumption of that content would have been on screen. On the other hand, the essay’s introduction lives in a more formal and academic context, with a specific structure, that ChatGPT was able to capture once more.Last but not least, ChatGPT is also an excellent tool for translation. It knows at least 95 languages (if you have doubts about whether your language is supported, you can always ask ChatGPT directly). Here, however, there is a consideration that might arise: what is the added value of ChatGPT for translation when we already have cutting-edge tools such as Google Translate?To answer this question, we have to consider some key differentiators and how we can leverage ChatGPT’s embedded translations capabilities:ChatGPT can capture the intent. This means that you could also bypass the translation phase since it is something that ChatGPT can do in the backend. For example, if you write a prompt to produce a social media post in French, you could write that prompt in any language you want – ChatGPT will automatically detect it (without the need to specify it in advance) and understand your intent:Figure – Example of ChatGPT generating an output in a language that is different from the inputChatGPT can capture the more refined meaning of particular slang or idioms. This allows for a translation that is not literal so that it can preserve the underlying meaning. Namely, let’s consider the British expression It’s not my cup of tea, to indicate something that is not the type of thing you like. Let’s ask both ChatGPT and Google Translate to translate it into Italian:Figure – Comparison between ChatGPT and Google Translate while translating from English into ItalianAs you can see, ChatGPT can provide several Italian idioms that are equivalent to the original one, also in their slang format. On the other hand, Google Translate performed a literal translation, leaving behind the real meaning of the idiom.As with any other task, you can always provide context to ChatGPT. So, if you want your translation to have a specific slang or style, you can always specify it in the prompt. Or, even funnier, you can ask ChatGPT to translate your prompt with a sarcastic touch:       Figure – Example of ChatGPT translating a prompt with a sarcastic touch. The original content of the prompt was taken from OpenAI’s Wikipedia pageAll these scenarios highlight one of the key killing features of ChatGPT and OpenAI models in general. Since they represent the manifestation of what OpenAI defined as Artificial General Intelligence (AGI), they are not meant to be specialized (that is, constrained) on a single task. On the contrary, they are meant to serve multiple scenarios dynamically so that you can address a wide range of use cases with a single model.In conclusion, ChatGPT is able not only to generate new text but also to manipulate existing material to tailor it to your needs. It has also proven to be very precise at translating between languages, also keeping the jargon and language-specific expressions intact.In the next section, we will see how ChatGPT can assist us in retrieving information and competitive intelligence.Quick information retrieval and competitive intelligenceInformation retrieval and competitive intelligence are yet other fields where ChatGPT is a game-changer. The very first example of how ChatGPT can retrieve information is the most popular way it is used right now: as a search engine. Every time we ask ChatGPT something, it can retrieve information from its knowledge base and reframe it in an original way.One example involves asking ChatGPT to provide a quick summary or review of a book we might be interested in reading:Figure - Example of ChatGPT providing a summary and review of a bookQuick information retrieval and competitive intelligence Alternatively, we could ask for some suggestions for a new book we wish to read based on our preferences:Figure – Example of ChatGPT recommending a list of books, given my preferencesFurthermore, if we design the prompt with more specific information, ChatGPT can serve as a tool for pointing us toward the right references for our research or studies.Namely, you might want to quickly retrieve some background references about a topic you want to learn more about – for example, feedforward neural networks. Something you might ask ChatGPT is to point you to some websites or papers where this topic is widely treated:Figure – Example of ChatGPT listing relevant referencesAs you can see, ChatGPT was able to provide me with relevant references to start studying the topic. However, it could go even further in terms of competitive intelligence.Let’s consider I’m writing a book titled Introduction to Convolutional Neural Networks – an Implementation with Python. I want to do some research about the potential competitors in the market. The first thing I want to investigate is whether there are already some competitive titles around, so I can ask ChatGPT to generate a list of existing books with the same content:Figure – Example of ChatGPT providing a list of competitive booksYou can also ask for feedback in terms of the saturation of the market you want to publish in:Figure– ChatGPT advising about how to be competitive in the marketFinally, let’s ask ChatGPT to be more precise about what I should do to be competitive in the market where I will operate:Figure – Example of how ChatGPT can suggest improvements regarding your book content to make it stand outChatGPT was pretty good at listing some good tips to make my book unique.Overall, ChatGPT can be a valuable assistant for information retrieval and competitive intelligence. However, it is important to remember the knowledge base cut-off is 2021: this means that, whenever we need to retrieve real-time information, or while making a competitive market analysis for today, we might not be able to rely on ChatGPT.Nevertheless, this tool still provides excellent suggestions and best practices that can be applied, regardless of the knowledge base cut-off.ConclusionAll the examples we saw in this article were modest representations of what you can achieve with ChatGPT to boost your productivity. These small hacks can greatly assist you with activities that might be repetitive (answering emails with a similar template rather than writing a daily routine) or onerous (such as searching for background documentation or competitive intelligence).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
  • 581
article-image-prompt-engineering-for-beginners
Sangita Mahala
20 Sep 2023
8 min read
Save for later

Prompt Engineering for Beginners

Sangita Mahala
20 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!IntroductionIn this article, you will learn the basics of prompt engineering. Then you will get to know about What is prompt engineering, why it is important, and various techniques to master this skill. Additionally, you will learn what are the examples of effective prompts for different tasks which will help you to jumpstart your journey into prompt engineering.What is Prompt EngineeringThe main focus of prompt engineering is to enhance the interaction between humans and Artificial Intelligence. We can term prompt engineering as the process of creating and constructing prompts to lead a large language model to produce the required output. These LLMs are trained on a large number of datasets containing text and code. While using LLMs one must know that they can be difficult to use effectively without careful prompting. By offering users with precise instructions, relevant context, and useful examples, prompt engineering can assist them in getting the most out of large language models.Importance of Prompt EngineeringPrompt engineering is important for making large language models work better. The following are the importance of prompt engineering:Prompt engineering can help us to shorten the effort and time required to generate LLM output.Give more accurate and instructive responses from LLMs.Enhance the quality and creativity of LLM output.Enlarge the range of tasks that are to be performed using LLMs.Example:Suppose you want to use an LLM to generate an article about the Eiffel Tower in Paris. You can simply provide the LLM with a prompt that says "Write an article about the Eiffel Tower in Paris." However, this is likely to result in an article that is poorly written, inaccurate, or irrelevant.Instead, you can use prompt engineering to guide the LLM to generate a more accurate and informative article. For example, you can provide the LLM with a prompt that specifies the following: The topic of the article: "The Eiffel Tower: A Symbol of Paris" The desired length of the article: "500 words" The audience for the article: "General readers" Tone and style of the article: "Informative and engaging" The following diagram illustrates how prompt engineering can be used to improve the quality of LLM output:                                                                                                                      (Diagram No. 1)The above diagram shows how a prompt can be used to guide the LLM to generate a more accurate and informative news article about Eiffel Tower in Paris.Different Kinds of Prompt Engineering TechniquesThere are a variety of prompt engineering techniques that can be used to achieve different goals. Some common techniques include:Zero-shot prompting: It provides the LLM with a prompt that describes the desired output, without providing any examples. Zero-shot prompting is often used for tasks such as summarization, translation, and question-answering.Few-shot prompting: It provides the LLM with a few examples of the desired output, along with a prompt that describes the task. Few-shot prompting is often used for tasks such as creative writing and code generation etc.In-context prompting: It provides the LLM with a sequence of prompts, where each prompt builds on the previous one. In-context prompting is often used for tasks such as reasoning, problem-solving, and dialogue.Examples of Effective Prompts for Different Tasks:The few examples below illustrate how you can use well-crafted prompts to perform different types of tasks.Tasks:Text SummarizationQuestion AnsweringText ClassificationCode GenerationInformation ExtractionConversationReasoning                                                                                                                         (Diagram No. 2)Text SummarizationIt is the ability to summarize articles and concepts into quick and easy-to-read summaries. Let’s have a look on a basic summarization task using prompts.Let's say you are interested in learning about Blockchain Technology, you can try a prompt like this:Prompt:Here, you used it to inform the model that a subsequent response was expected. Let's imagine that you feel that this material is too much and that you would like a more thorough summary. In fact, you may tell the model to compress everything into a single sentence:Prompt:Here you can see the model tried to summarize the whole paragraph in one sentence. Question AnsweringOne of the best ways to get the model to respond is to improve the format of the prompt. Basically, a prompt can combine given input , output ,instructions, context indicators to get enhanced results.Here are some examples of how QA systems can be used to answer the questions you provided.Prompt:A QA system would be able to answer this question by searching its knowledge base for information about the chemical formula of water. It would then generate a response that includes the answer to the question.Text ClassificationIt is the process of designing prompts that guide language models to perform text classification tasks. This can be achieved by providing the model with specific instructions and context, as well as examples of the different kinds of classes for text, so that it will be able to identify.For example, the following prompt could be used to classify customer reviews as neutral, positive or negative:Prompt:The tech industry continues to revolutionize our world with cutting-edge innovations that redefine the way we live and work. Code GenerationIt is the process of designing prompts that guide language models for generating code. This can be achieved by providing the model with specific instructions and context, as well as examples of the desired code output.For example, the following prompt could be used to generate a code that takes a string as input and returns the reverse of the string by using Python.Prompt: Information ExtractionIt is the process of designing prompts that guide language models to extract specific information from text. This can be achieved by providing the model with instructions and context, as well as examples.For example, the following prompt can be used to extract the names of all people mentioned in the news article:Prompt:Conversation:It is a technique used to create more natural and engaging conversations between humans and AI language models. Conversation provides the model with context and information about the user's goals, and then it asks questions in such a way that encourages the model to respond in a conversational manner.Now, we will create a conversational system that can answer queries in a more technical and perfect manner, for example. Keep in mind that by giving it instructions, you are clearly directing it how it must act. This is sometimes referred to as role prompting. Prompt:Reasoning:It involves designing prompts that will encourage language models to use their knowledge and reasoning skills to generate accurate and informative responses. This can be achieved by providing the model with clear feedback, instructions and examples.Prompt-1: Prompt-2:As we can see in the above example, the model is first asked to add 98 and 2. Then, it is asked to subtract 4 from 8. Finally, it is asked to multiply 6 by 4. By performing these steps one at a time, the model is able to solve the problem correctly.In each of these examples, the prompt is carefully designed to guide the LLM to generate the desired output. By following these examples, you can start to use prompt engineering to get more out of LLMs.ConclusionIn this article, we have explored the basics of prompt engineering, including its definition, importance, and different techniques. We have also provided examples of effective prompts for different tasks, such as text summarization, question answering, text classification, code generation, information extraction, conversation, and reasoning. As we all know that Prompt engineering is a rapidly evolving field, that can be used to generate a wide variety of outputs from LLMs. As LLMs become more powerful and versatile, prompt engineering will surely play an increasingly important role in how we interact with AI.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 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
  • 1
  • 0
  • 2255

article-image-tapping-into-financial-data-via-gpt-4
Anshul Saxena
20 Sep 2023
15 min read
Save for later

Tapping into Financial Data via GPT-4

Anshul Saxena
20 Sep 2023
15 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!IntroductionFor anyone new to the world of stocks, looking at the charts and numbers can feel a bit like trying to read a foreign language. But with the right tools and a step-by-step approach, understanding these numbers becomes a whole lot easier. This guide aims to walk beginners through a basic analysis of APPLE's stock data. Here's what will be covered:Step 1. Descriptive Statistics: The first step is to look at APPLE's stock prices. This means seeing the high points, the low points, and understanding how often they change.Step 2. Time Series Analysis: This is all about checking the price of the stock over time. It helps to see if the price is going up, going down, or staying about the same.Step 3. Volatility Analysis: This part helps to see how quickly and by how much the price changes. It's a way to see if the stock is calm or if it has a lot of ups and downs. Step 4. Technical Indicators: These are simple tools that help guess what might happen next with the stock price. It's like trying to guess the end of a story by looking at what happened in the middle. Step 5. Volume Analysis: This is about seeing how many times the stock was bought or sold. It gives a clue about how many people are interested in this stock. With these five easy steps aided by prompts and Python codes, anyone can start to understand the basics of APPLE's stock data. It's a straightforward way to see the story behind the numbers. You can access the data here (Link)Step 1: Descriptive StatisticsUsing descriptive statistics, we assess the stock's central tendencies and dispersion by calculating the mean, median, and standard deviation of prices and volume, and pinpointing the highest and lowest values over the specified period.Prompt 1: As a stock analyst, you are tasked with understanding the nuances of a given stock dataset. Start by loading the dataset that encompasses stock prices, specifically the Open, High, Low, and Close values, alongside trading volume. Compute fundamental descriptive statistics like the mean, median, standard deviation, as well as the maximum and minimum values for each mentioned category. Once done, provide a succinct summary detailing the key findings, highlighting any discernible trends or potential outliers that could be of significance to investors.Response: We'll start by generating basic descriptive statistics for the stock prices (Open, High, Low, Close) and the trading volume.The descriptive statistics for the stock prices and volume are as follows:  The next step is to visualize the dataset as a time seriesStep 2: Time Series AnalysisThrough time series analysis, we visualize stock price trends by plotting Open, High, Low, and Close values, while also examining seasonality, identifying prevailing trends, and detecting any anomalies or outliers in prices or volume.Prompt 2: As a stock analyst, begin by loading the dataset and ensure it includes Date, Open, High, Low, and Close prices. Standardize the "Date" column format and set it as the index. Plot these stock prices against dates, making each distinct. Analyze the graph for evident trends, significant peaks, or troughs, and ponder potential influencing factors.Response: It appears that the dataset contains the columns "Date", "Close/Last", "Volume", "Open", "High", and "Low". The "Date" column has inconsistent formats, so we'll need to standardize it. Additionally, the prices are represented as strings with dollar signs; we'll need to convert them to float values for analysis.I'll begin by:1.      Standardizing the "Date" column format.2.      Converting the price columns to float values.3.      Setting the "Date" column as the index. import pandas as pd # Load the dataset data = pd.read_excel("/mnt/data/HistoricalData_1Yr.xlsx") # Display the first few rows of the dataset data.head() The dataset has been appropriately formatted. The next step is to plot the stock prices (Open, High, Low, and Close/Last) against their respective dates. Each price will be represented with a distinct line for clarity.import matplotlib.pyplot as plt # Plotting the stock prices plt.figure(figsize=(15, 7)) for col in price_columns:    plt.plot(data.index, data[col], label=col) plt.title("Stock Prices Over Time") plt.xlabel("Date") plt.ylabel("Price ($)") plt.legend() plt.grid(True) plt.tight_layout() plt.show()Interpretation: When looking at a stock chart, I first noticed its overall upward movement. Yet, near the end, it seemed to stabilize or maybe even decline a bit. Two clear points caught my attention: a high point in the middle and a low point at the start. Stock prices can change due to company news or broader events, and sometimes, high trading volumes can make prices shift dramatically. It's also important to remember that the general mood of investors can set the direction for a stock's movement. Now let’s look at the volatility to check if the share is tradable or notStep 3: Volatility AnalysisBy calculating daily returns and computing the rolling standard deviation, we can effectively analyze and track the volatility of a financial instrument over time.Prompt 3: As a stock analyst, it's imperative to grasp the volatility of a stock, a crucial aspect for investors. To achieve this, compute the stock's daily returns by measuring the percentage change in the closing price from one day to the subsequent one. Once computed, proceed to plot these daily returns, which will visually represent the stock's volatility throughout the chosen time frame. Your plot, reflecting the daily returns over time, will offer invaluable insights into the stock's stability or fluctuations, helping stakeholders make informed decisions.Response: To grasp the volatility of the stock:We'll compute the stock's daily returns. This is achieved by measuring the percentage change in the closing price from one day to the subsequent one.We'll plot these daily returns over time, which will offer a visual representation of the stock's volatility throughout the chosen time frame.# Compute the daily returns data['Daily Returns'] = data['Close/Last'].pct_change() # Display the first few rows of the daily returns data['Daily Returns'].head()The daily returns have been successfully computed. As expected, the first entry is NaN since there's no previous day to calculate the percentage change from.Now, let's plot these daily returns over time to visually assess the stock's volatility.# Plotting the daily returns plt.figure(figsize=(15, 7)) plt.plot(data.index, data['Daily Returns'], label='Daily Returns', color='blue') plt.axhline(0, color='red', linestyle='--')  # Adding a horizontal line at y=0 for reference plt.title("Daily Returns Over Time") plt.xlabel("Date") plt.ylabel("Daily Return") plt.legend() plt.grid(True) plt.tight_layout() plt.show()Interpretation: When I look at the stock's daily returns, I see its ups and downs around the red zero line, showing its riskiness. Close-to-zero movements mean the stock is steady, while bigger jumps indicate more unpredictability. Different events, like company news or global happenings, can cause these shifts. So, when choosing stocks, it's crucial to know whether you prefer a calm ride or a roller coaster of returns.Since we know every information is inbuilt price data itself let’s analyze it to decode that informationStep 4: Forming Technical IndicatorsThrough the calculation of moving averages, such as the 50-day and 200-day, and the identification of golden or death crosses, coupled with the computation of the RSI, we can gauge market momentum and discern overbought or oversold conditions.Prompt 4: As a seasoned stock analyst, you recognize the importance of technical indicators like moving averages to gauge the potential direction of a stock's momentum. For this analysis, focus on two primary moving averages: the 50-day and the 200-day. By calculating these, you can ascertain pivotal trend indications. Notably, watch for the "Golden Cross," a bullish signal that arises when the 50-day moving average surpasses the 200-day moving average. Conversely, be wary of the "Death Cross," which is a bearish sign indicating potential downtrends when the 50-day moving average drops below the 200-day one. These cross points can serve as critical markers for investors, helping them strategize their market positions.Response: The 50-day and 200-day moving averages are among the most observed technical indicators in stock analysis. These moving averages help to smooth out price data to create a single flowing line, making it easier to identify the direction of the trend.Let's proceed with the following steps:Calculate the 50-day moving average for the closing price.Calculate the 200-day moving average for the closing price.Plot both moving averages alongside the stock's closing price.Identify and annotate potential "Golden Cross" and "Death Cross" points on the plot.Let's begin by calculating the two moving averages.# Calculate the 50-day and 200-day moving averages data['50-day MA'] = data['Close/Last'].rolling(window=50).mean() data['200-day MA'] = data['Close/Last'].rolling(window=200).mean() # Display the first few rows of the moving averages data[['Close/Last', '50-day MA', '200-day MA']].tail() The 50-day and 200-day moving averages have been calculated successfully. To better understand their implications:We'll plot the stock's closing price alongside the two moving averages.We'll highlight and annotate any "Golden Cross" and "Death Cross" occurrences on the chart. Let's visualize the data. # Plotting the closing price and moving averages plt.figure(figsize=(15, 7)) plt.plot(data.index, data['Close/Last'], label='Close Price', color='black', alpha=0.5) plt.plot(data.index, data['50-day MA'], label='50-day MA', color='blue') plt.plot(data.index, data['200-day MA'], label='200-day MA', color='red') # Identifying the Golden Cross and Death Cross golden_cross = data[(data['50-day MA'] > data['200-day MA']) & (data['50-day MA'].shift(1) <= data['200-day MA'].shift(1))] death_cross = data[(data['50-day MA'] < data['200-day MA']) & (data['50-day MA'].shift(1) >= data['200-day MA'].shift(1))] # Annotating the Golden Cross and Death Cross on the plot for date in golden_cross.index:    plt.annotate('Golden Cross', xy=(date, data['200-day MA'].loc[date]),                 xytext=(date, data['200-day MA'].loc[date] + 10),                 arrowprops=dict(facecolor='green', arrowstyle='->'), color='green')   for date in death_cross.index:    plt.annotate('Death Cross', xy=(date, data['200-day MA'].loc[date]),                 xytext=(date, data['200-day MA'].loc[date] - 10),                 arrowprops=dict(facecolor='red', arrowstyle='->'), color='red') # Other plot settings plt.title("Stock Price with 50-day and 200-day Moving Averages") plt.xlabel("Date") plt.ylabel("Price ($)") plt.legend() plt.grid(True) plt.tight_layout() plt.show() Interpretation: The chart shows the stock's closing price with two different line averages: a 50-day (blue) and a 200-day (red). When the 50-day line goes above the 200-day line, marked by green arrows, it usually suggests the stock might go up. On the other hand, red arrows where the 50-day line dips below indicate the stock might drop. These crossing points, known as the "Golden" and "Death" Cross, can help investors gauge the stock's potential direction. Simply put, these averages provide hints on whether the stock is on an upward or downward path. Let’s have a look at the volume of the APPLE stock.Step 5: Volume AnalysisBy analyzing volume spikes in relation to stock price movements and plotting volume over time, we can discern potential patterns and their impact on market dynamics.Prompt 5: In your role as a stock analyst, understanding trading volume's nuances is equally as vital as tracking price movements. Now, turn your attention to plotting the trading volume against time. This visualization can reveal patterns or substantial spikes in trading volume, which might correlate with notable price shifts. Such correlations can often indicate key market events or investor sentiment shifts. The graph you produce, illustrating the trading volume over the period, will provide a clearer picture of the stock's liquidity and the market's interest in it. This, in turn, will guide investment decisions and strategies.Response: Trading volume is a critical metric for stock analysts. It provides insights into the stock's liquidity and the market's interest in it. High trading volumes can indicate strong interest in a stock, and when paired with price movements, can reveal significant market events or shifts in investor sentiment.Let's proceed with the following steps:1. Plot the trading volume against time.2. Highlight and annotate any substantial spikes in trading volume.3. Overlay the stock's closing price to see if there's any correlation between trading volume spikes and significant price shifts.# Plotting the trading volume against time fig, ax1 = plt.subplots(figsize=(15, 7)) # Twin the axes for two different y-axes ax2 = ax1.twinx() # Plotting trading volume on ax1 ax1.bar(data.index, data['Volume'], color='gray', alpha=0.5, label='Volume') ax1.set_xlabel('Date') ax1.set_ylabel('Trading Volume', color='gray') ax1.tick_params(axis='y', labelcolor='gray') ax1.set_title("Trading Volume and Stock Price Over Time") # Plotting closing price on ax2 ax2.plot(data.index, data['Close/Last'], color='blue', label='Close Price') ax2.set_ylabel('Close Price ($)', color='blue') ax2.tick_params(axis='y', labelcolor='blue') # Highlighting and annotating substantial volume spikes average_volume = data['Volume'].mean() high_volume_dates = data[data['Volume'] > 1.5 * average_volume] for date in high_volume_dates.index:    ax1.annotate('High Volume', xy=(date, high_volume_dates['Volume'].loc[date]),                 xytext=(date, high_volume_dates['Volume'].loc[date] + 5e6),                 arrowprops=dict(facecolor='red', arrowstyle='->'), color='red') plt.grid(True) plt.tight_layout() plt.show() Let's begin by visualizing the trading volume against time.Interpretation: The chart displays both the stock's closing price (blue line) and how often it was traded (gray bars). Larger gray bars show days when the stock was traded more frequently. Some days, highlighted with red arrows, even saw an unusually high trading activity. Looking at these spikes alongside the blue line, we can guess if positive or negative news affected the stock. Generally, when a stock is traded often, it's easier to buy or sell without greatly changing its price. This chart helps investors gauge interest in the stock and its stability, supporting smarter investment choices.ConclusionIn conclusion, while the world of stocks might initially seem like an intricate puzzle, it truly isn't as daunting as it first appears. By systematically breaking down APPLE's stock data through the aforementioned steps, even a beginner can gain valuable insights into the dynamics of the stock market. Think of it as assembling the pieces of a story – from setting the scene with descriptive statistics to reaching the climax with volume analysis. And with the added advantage of specific prompts and Python code to guide the way, understanding the ebb and flow of stocks becomes a clear and attainable goal. So, here's to turning those charts and numbers from intimidating to intriguing, and uncovering the fascinating story they hold within. Author BioDr. Anshul Saxena is an author, corporate consultant, inventor, and educator who assists clients in finding financial solutions using quantum computing and generative AI. He has filed over three Indian patents and has been granted an Australian Innovation Patent. Anshul is the author of two best-selling books in the realm of HR Analytics and Quantum Computing (Packt Publications). He has been instrumental in setting up new-age specializations like decision sciences and business analytics in multiple business schools across India. Currently, he is working as Assistant Professor and Coordinator – Center for Emerging Business Technologies at CHRIST (Deemed to be University), Pune Lavasa Campus. Dr. Anshul has also worked with reputed companies like IBM as a curriculum designer and trainer and has been instrumental in training 1000+ academicians and working professionals from universities and corporate houses like UPES, CRMIT, and NITTE Mangalore, Vishwakarma University, Pune & Kaziranga University, and KPMG, IBM, Altran, TCS, Metro CASH & Carry, HPCL & IOC. With a work experience of 5 years in the domain of financial risk analytics with TCS and Northern Trust, Dr. Anshul has guided master's students in creating projects on emerging business technologies, which have resulted in 8+ Scopus-indexed papers. Dr. Anshul holds a PhD in Applied AI (Management), an MBA in Finance, and a BSc in Chemistry. He possesses multiple certificates in the field of Generative AI and Quantum Computing from organizations like SAS, IBM, IISC, Harvard, and BIMTECH.Author of the book: Financial Modeling Using Quantum Computing
Read more
  • 0
  • 0
  • 722

article-image-fine-tuning-gpt-35-and-4
Alan Bernardo Palacio
18 Sep 2023
8 min read
Save for later

Fine-Tuning GPT 3.5 and 4

Alan Bernardo Palacio
18 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!IntroductionFine-tuning with OpenAI is a new feature that might become a crucial aspect of enhancing AI language models for specific tasks and contexts. It holds significant importance as it allows these models to be adapted to perform tasks beyond their initial capabilities in a different way that could be done just with Prompt Engineering. In this article, we will use traditional fine-tuning, which involves training a model on a specialized dataset. The dataset we will be using consists of conversations formatted in a JSON lines structure, where each exchange is a sequence of chat message dictionaries. Each dictionary includes role assignments (system, user, or assistant) and the corresponding content of the message. This approach aims to adapt the model to better understand and generate human-like conversations. Let’s start by taking a look at the different alternatives to adapt a Large Language Model for custom tasks. Fine-tuning versus Prompt Engineering There are two distinct approaches for adapting a model to work with custom data: prompt engineering and traditional fine-tuning. While both methods aim to customize LLMs for specific tasks, they differ in their approaches and objectives. Prompt engineering entails crafting precise input prompts to guide the AI's responses effectively. It involves tailoring the prompts to elicit desired outcomes from the AI. This technique requires developers to experiment with different prompts, instructions, and formats to achieve precise control over the model's behavior. By providing explicit instructions within prompts, developers can elicit specific answers for tasks like code generation or translation. Prompt engineering is particularly valuable when clear guidance is essential, but finding the optimal prompts might require iterative testing. On the other hand, fine-tuning focuses on adapting a pre-trained LLM to perform better on a particular task or context. This process involves training the model on custom datasets that align with the desired application. Fine-tuning allows LLMs to develop a deeper understanding of context and language nuances, making them more adaptable to diverse prompts and human-like conversations. While it offers less direct control compared to prompt engineering, fine-tuning improves the model's ability to generate coherent responses across a broader range of scenarios. In essence, prompt engineering emphasizes precision and specific instruction within prompts, while fine-tuning enhances the LLM's adaptability and comprehension of context. Both prompt engineering and traditional fine-tuning serve as techniques to enhance the AI's conversational abilities. Prompt engineering emphasizes precise instruction, while traditional fine-tuning focuses on training the model to comprehend and generate conversations more effectively. Looking at the Training Data Before training a model, we need to understand the required data format for the OpenAI fine-tuning endpoints. This format utilizes JSON lines and consists of a primary key "messages," followed by an array of dictionaries representing chat messages. These dictionaries collectively form a complete conversation. The expected structure to train an Open AI model looks like this: {"messages": [{"role": "system", "content": "..."}, ...]} {"messages": [{"role": "system", "content": "..."}, ...]} {"messages": [{"role": "system", "content": "..."}, ...]} {"messages": [{"role": "system", "content": "..."}, ...]} Each chat message dictionary includes two essential components: The "role" field: This identifies the source of the message, which can be system, user, or assistant. It indicates the origin of the message. The "content" field: This contains the actual textual content of the message. In this article, we will be using an already available training dataset that complies with this structure within the Hugging Face datasets repository. Before we get this data, let’s first install the datasets package alongside the open ai and langchain modules using pip. !pip install datasets==2.14.4 openai==0.27.9 langchain==0.0.274Next, we can download the dataset using the datasets library and write it into a JSON file.from datasets import load_dataset # data = load_dataset( "jamescalam/agent-conversations-retrieval-tool", split="train" ) data.to_json("conversations.jsonl") To verify the structure of the file, we open it and load it into separate conversations. import json with open('conversations.jsonl', 'r') as f: conversations = f.readlines() # Assuming each line is a JSON string, you can iterate through the lines and load each JSON string parsed_conversations = [json.loads(line) for line in conversations] len(parsed_conversations)We get 270 conversations, and if we want, we can inspect the first element of the list.parsed_conversations[0] In the following code snippet, the OpenAI Python library is imported, and the API key is set using the environment variable. The script then uses the OpenAI API to create a fine-tuning job for GPT-3.5 Turbo. It reads the contents of a JSON Lines file named conversations.jsonl and sets the purpose of the file as 'fine-tune'. The resulting file ID is saved for later use.import openai import os # Set up environment variables for API keys os.environ['OPENAI_API_KEY'] = 'your-key' res = openai.File.create( file=open("conversations.jsonl", "r"), purpose='fine-tune' ) # We save the file ID for later file_id = res["id"] Now we can start the Fine-tuning job. res = openai.FineTuningJob.create( training_file=file_id, model="gpt-3.5-turbo" ) job_id = res["id"]In this part of the code, the fine-tuning job is initiated by calling the Openai.FineTuningJob.create() function. The training data file ID obtained earlier is passed as the training_file parameter, and the model to be fine-tuned is specified as "gpt-3.5-turbo". The resulting job ID is saved for monitoring the fine-tuning progress. Monitoring Fine-Tuning Progress from time import sleep while True:    print('*'*50)    res = openai.FineTuningJob.retrieve(job_id)    print(res)    if res["finished_at"] != None:        ft_model = res["fine_tuned_model"]        print('Model trained, id:',ft_model)        break    else:        print("Job still not finished, sleeping")        sleep(60) . In this section, the code enters a loop to continuously check the status of the fine-tuning job using the openai.FineTuningJob.retrieve() method. If the job has finished indicated by the "finished_at" field in the response, the ID of the fine-tuned model is extracted and printed. Otherwise, if the job is not finished yet, the script pauses or waits for a minute using the "sleep(60)" function before checking the job status again.Using the Fine-Tuned Model for Chat from langchain.chat_models import ChatOpenAI from langchain.prompts.chat import (    ChatPromptTemplate,    SystemMessagePromptTemplate,    AIMessagePromptTemplate,    HumanMessagePromptTemplate, ) from langchain.schema import AIMessage, HumanMessage, SystemMessage chat = ChatOpenAI(    temperature=0.5,    model_name=ft_model ) messages = [    SystemMessage(        content="You are a helpful assistant."    ),    HumanMessage(        content="tell me about Large Language Models"    ), ] chat(messages)  In this last part of the code, the fine-tuned model is integrated into a chat using the LangChain library. A ChatOpenAI instance is created with specified settings, including a temperature of 0.5 and the name of the fine-tuned model (ft_model). A conversation is then simulated using a sequence of messages, including a system message and a human message. The chat interaction is executed using the chat() method. The provided code is a step-by-step guide to set up, fine-tune, monitor, and utilize a chatbot model using OpenAI's API and the LangChain library. It showcases the process of creating, training, and interacting with a fine-tuned model for chat applications. ConclusionIn conclusion, fine-tuning GPT-3.5 and GPT-4 marks a significant leap in customizing AI language models for diverse applications. Whether you opt for precise prompt engineering or traditional fine-tuning, both approaches offer unique strategies to enhance conversational abilities. This step-by-step article demonstrates how to prepare data, initiate fine-tuning, monitor progress, and leverage the fine-tuned model for chat applications.As AI evolves, fine-tuning empowers language models with specialized capabilities, driving innovation across various fields. Developers can harness these techniques to excel in tasks ranging from customer support to complex problem-solving. With the power of fine-tuning at your disposal, the possibilities for AI-driven solutions are limitless, promising a brighter future for AI technology.Author BioAlan Bernardo Palacio is a data scientist and an engineer with vast experience in different engineering fields. His focus has been the development and application of state-of-the-art data products and algorithms in several industries. He has worked for companies such as Ernst and Young, and Globant, and now holds a data engineer position at Ebiquity Media helping the company to create a scalable data pipeline. Alan graduated with a Mechanical Engineering degree from the National University of Tucuman in 2015, participated as the founder of startups, and later on earned a Master's degree from the faculty of Mathematics at the Autonomous University of Barcelona in 2017. Originally from Argentina, he now works and resides in the Netherlands.LinkedIn
Read more
  • 0
  • 0
  • 1613
article-image-automatic-prompt-engineering-with-prompt-royale
Louis Owen
18 Sep 2023
8 min read
Save for later

Automatic Prompt Engineering with Prompt-Royale

Louis Owen
18 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!IntroductionAI has never been more accessible than ever since the launch of ChatGPT. With Generative AI, people can now build their own AI engine by only giving commands in natural language. There is no need to know how to code, no need to prepare the training data, and no need to do any model hyperparameter tuning. What we need to do to build our own AI system is to only give commands, more widely known as prompt engineering.Prompt engineering is more of an art than science. There are so many ways to do prompt engineering. The simplest form of prompt engineering is called zero-shot prompting, where the user just needs to directly give their command to the Large Language Model (LLM). For example: “Write an acrostic poem in Hindi”, “Write a 7-day itinerary in Bali”, etc.Another prompting technique is called few-shot prompting, where we need to give several examples of the expected output inside the prompt itself. Let's say we want to utilize LLM to do sentiment analysis. We can write the prompt to be something like the following:You are an expert in performing sentiment analysis. You can only return the output with 3 options: “negative”, “neutral”, and “positive”. Example 1: I love this product! It works perfectly. Sentiment: positive Example 2: The weather today is terrible. It's raining non-stop. Sentiment: negative Example 3: I’m feeling sleepy Sentiment: neutral Text: Attending the concert last night was a dream come true. The music was incredible! Sentiment:The more sophisticated way to do prompt engineering is by performing Chain-of-Though (CoT). Basically, in this technique, we prompt the LLM to give a step-by-step explanation of why it arrives at the final answer. This technique is adopted widely by the AI community since it gives better output in a lot of cases. The drawback of implementing this prompt-engineering technique is the increase of the generated number of tokens which correlates positively with the latency.There are still many prompting techniques available out there. Choosing the right prompt technique or even doing the prompt engineering itself is indeed not an easy task. We need to pass through many iterations until we find the best prompt for our use case.In this article, I’ll guide you to do automatic prompt engineering which can surely save our time in creating the best prompt for our use case. We’ll discuss two popular automatic prompt engineering frameworks: GPT-Prompt-Engineer and Prompts-Royale, where we’ll dive deeper more into Prompts-Royale. Finally, there will be a dedicated section on how to install and utilize prompts-royale.Without wasting any more time, let’s take a deep breath, make yourselves comfortable, and be ready to learn how to perform automatic prompt engineering!Automatic Prompt Generator FrameworksThere are two most popular automatic prompt generator frameworks: GPT-Prompt-Engineer and Prompts-Royale. Let’s start looking into the first one.GPT-Prompt-Engineer [github] is a very famous GitHub repository with more than 4.5k starts. It can be utilized to automatically generate the best prompt by simply inputting the task description along with several test cases. Then, the system will generate, test, and rank several variations of prompts with the goal of finding the best among all of them. The steps to use this framework are very straightforward:Define your use-case and test casesChoose how many prompts to generateThe system will generate a list of potential prompts, and test and rate their performanceThe final evaluation score will be printed in a table.There is no UI available for this package, so it might not be very compelling to the non-coders. However, there are two ready-to-use Google Colab notebooks that can be directly used. The first notebook can be utilized for general tasks other than the classification task. ELO rating will be used to evaluate the best prompt amongst several prompt candidates. The second notebook is created specifically for the classification task where the evaluation process is conducted based on the available ground truth.Another framework that is relatively new and the “improved” version of the GPT-Prompt-Engineer package is Prompts-Royale [github]. Similar to GPT-Prompt-Engineer, it’s also very straightforward to utilize this framework. You just need to give the description of the task along with a couple of example scenarios and the expected outputs, then the system will do the rest.There are indeed several plus points offered by this framework:Automatic test cases generation: automatically creating test cases from the description, we just need to provide several examples and the system will generate more test cases automatically.Monte Carlo matchmaking: not only utilizing ELO rating as in GPT-Prompt-Engineer, Prompts-Royale also uses the Monte Carlo method for matchmaking to ensure you get as much information with the least amount of iteration.User Interface: unlike GPT-Prompt-Engineer, Prompts-Royale offers a nice UI where users can directly give all of the inputs and get the returned outputs all in a single nice UI. Since Prompts-Royale offers additional benefits compared to the famous GPT-Prompt-Engineer framework, we’ll dive deeper into this framework instead in this article. Without wasting any more time, let’s see Prompts-Royale in action!Prompts-Royale in Action!InstallationTo use Prompts-Royale, you can directly visit promptsroyale.com, or you can clone the repository and run it locally. To run locally, you just need to do the following things:1. Clone the repository            ```git clone git@github.com:meistrari/prompts-royale.git            ```2. Install all dependencies with Bun```bun i```3. Run prompts-royale as a server in your local machine```bun run dev```This is the page that you will see once the server is up.Using the Prompts RoyaleTo use prompts-royale, we first need to input the OpenAI API key. We can use GPT3.5 or GPT4. You can find the key in your OpenAI account. If you don’t have an account yet, you can easily sign yourself up here. Once you insert the API key, you can start giving the necessary inputs in the form. You need to insert the task description and several test cases. Task description can be something like “Write a prompt that creates a headline for a website.” For test cases, we need to provide the scenario and the expected output, just like how we implement the few-shot prompting technique.  Next, we just let the system generate several prompt candidates by clicking the “Generate prompts” button. Note that we can also add our own written prompt to the list of prompt candidates.Finally, once we have the list of prompt candidates, we need to let the system choose which one is the best prompt. To do that, we need to input the number of battles that will be executed by the system. “Battle” simply means the process of selecting the best prompt out of all candidates. The battle will be between 2 prompts. Remember, the higher the number of battles the higher the cost to find the best prompt. By default, prompts-royale will run 60 battles. The results of the battles will be shown at the bottom of the page. There’s a chart of ratings over iterations and the battle log. The final prompt ranking can be seen on the right side of the page, as follows. You can of course click each of the prompt button and see what’s the generated prompt. ConclusionCongratulations on keeping up to this point! Throughout this article, you have learned how to do automatic prompt engineering with the help of prompts-royale. You’ve also learned several prompting techniques and another automatic prompt engineering technique called GPT-Prompt-Engineer. 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
  • 1221

article-image-prompt-engineering-best-practices
Amita Kapoor
18 Sep 2023
11 min read
Save for later

Prompt Engineering Best Practices

Amita Kapoor
18 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!IntroductionPrompt Engineering isn't just about throwing questions at a machine and hoping for a brilliant answer. Oh no, it's a meticulous dance of semantics and syntax. Think of it as the secret sauce that turns raw data into Michelin-star outputs. It's the act of finessing questions, statements, and other inputs in such a way that our ever-so-complex language models (yes, like those GPT variants you've heard so much about) know exactly what performance we're expecting.To put it cheekily: If you've ever tried to get a diva to perform without a rehearsal, you'd know the importance of Prompt Engineering. It's not merely about the questions we ask but the elegance and intent with which we pose them. The spotlight's on, let the show begin!Why is Prompt Engineering Important?Step into any grand opera house, and you'll immediately grasp the importance of a well-directed performance. Similarly, in the vast concert hall of machine learning, Prompt Engineering is the esteemed conductor orchestrating every note and crescendo. So, what makes prompt engineering indispensable? The answers unfold in the visual guide belowIn essence, while having a cutting-edge language model is like owning a Stradivarius violin, it's the skill of the violinist—or in our case, the precision of the prompt—that elicits the true magic. So, if you're looking to harness the full symphony of capabilities from your AI, mastering the art of Prompt Engineering isn't just recommended; it's indispensableTypes of PromptsMastering the art of prompting is akin to being a maestro of a symphony, understanding when each section of the orchestra needs to play to produce harmonious music. Each type of prompt offers a different angle to solicit the desired response from a language model. Here's your guide to their nuances:Patterned PromptsPatterned prompts serve as the scaffold for models, guiding them in producing outputs of a specified type and structure. These prompts leverage templates or distinct patterns to tailor the generation process, ensuring the output not only aligns with a desired format but also exhibits originality and creativity.Suppose you want a quatrain about the night sky. Your patterned prompt might be:Write a quatrain with an ABAB rhyme scheme about the night sky.The LLM might then generate:The stars above in splendor gleam (A) Amidst the vast and dark expanse (B) Moonlight casts a gentle beam (A) In this nightly, cosmic dance (B)Persona-based Prompts:Persona-based prompts harness the power of fictional character profiles — personas — to steer the LLM in generating responses tailored to a particular user type or demographic. These profiles encapsulate key traits such as age, interests, objectives, and challenges, providing a backdrop against which the model can craft a more relevant and empathetic output. Before delving into persona-based prompts, one must craft a comprehensive persona.  Here is an exampleMarketing: Generate promotional materials that echo the sentiments and inclinations of potential customers.Alex is a 30-year-old fitness instructor from Miami with a penchant for high-intensity workouts. He's a massive fan of HIIT sessions, often referring to the methods of Jillian Michaels. Creating content for Alex, draft a promotional email to introduce a new line of high-intensity workout gear targeting fitness enthusiasts like him.Zero-shot Prompts:Zero-shot prompts empower language models to generate responses without needing prior task-specific training. They draw upon the vast knowledge and patterns that large language models (LLMs) have assimilated during their extensive general training. Simple in nature, these prompts can be as straightforward as Pen a poem on autumnorRender this phrase into French: Good morning.The strength of zero-shot prompts lies in the extensive and diverse training data of LLMs. By recognizing myriad linguistic structures, relations, and nuances, LLMs can often tackle tasks with remarkable accuracy, even without having seen a direct example before.Few-shot promptsFew-shot prompts are a type of prompt that provides the large language model (LLM) with a few examples of the desired output before asking the main question. By doing this, you're helping the model to "warm up" and better understand the task at hand. Consider prompting the LLM to write a poem about a dog. You could use the following few-shot prompt:Write a poem about a dog.Example 1:Loyal and ever by my side,With a tail that won't stop its wag,You bark, you play, and you glide,My faithful friend, more than just a tag.Example 2:Golden fur, eyes shining bright,Chasing balls, every single throw,Your joy and bark, light up the night,A bond with you, forever to grow.Based on these two examples, the LLM is now primed to understand the rhythm, style, and thematic elements about dogs.Chain-of-Thought PromptsLike a mastermind planning a heist, these prompts break down a challenge into digestible bits. It's all about guiding the model step by step. They are ideal for complex tasks that benefit from being broken down into structured, smaller tasks.Let's consider we want the LLM to design a lesson plan for teaching basic physics to high school students, so we guide LLM by asking these questions in a step by step manner, as shown below:As the LLM progresses through each prompt in the chain, it is guided through a structured thought process, ensuring that the end result is comprehensive and aligned with the desired objective.Context-Aware PromptsContext-aware prompts are designed to give the large language model (LLM) essential background or situational information related to the task at hand. By grounding the LLM in a specific context, these prompts aid in generating outputs that are more relevant and nuanced. Whether it's the historical backdrop, cultural nuances, or the specific setting in which a piece of information will be used, context-aware prompts equip the LLM to tailor its response appropriately to the given circumstances. Here is an example:Context: You are writing for an audience of young adults aged 18-25 who are passionate about environmental conservation and are considering veganism for ecological reasons.Prompt: Given the context, provide a brief article discussing the environmental benefits of adopting a vegan lifestyle.Tree of Thought PromptsThese prompts aren't just a series; they're a hierarchical orchestration. Imagine guiding the LLM with a blueprint, where every level delves deeper into the task. This tree-based approach organizes complex tasks by breaking them into branches and leaves, each representing different stages of the task. They shine brightest when handling intricate tasks that demand meticulous planning, reasoning, or a structured approach. By breaking a task into nodes and leaves, it transforms abstract assignments into navigable journeys. Example:Objective: Plan a unique birthday celebration.Depth 1 - Idea GenerationToT Prompt 1: "Provide 5 unique ideas for a birthday celebration."Potential Responses:     An underwater-themed party at a local aquarium.     A time-travel costume party where guests dress from different eras.     Renting out a cinema for a private movie screening with friends.     Hosting a cooking competition with birthday-related recipes.     A mystery-solving escape room challenge tailored for the birthday person.Depth 2 - EvaluationToT Prompt for Voting: "Analyse the choices below, then conclude which is the most promising for a memorable birthday celebration."Selected Response: "A time-travel costume party where guests dress from different eras."Depth 3 - Detailing Based on Chosen IdeaToT Prompt 2: "Provide 5 detailed activities or features for a time-travel costume party."Potential Responses:     Setting up photo booths from different eras, like the Roaring 20s, the Renaissance, and the Future.     Time-specific games or challenges, such as jousting or a 60s dance-off.     A time-travel passport that gets stamped as guests move through different eras.     Food and drinks menu tailored to different time periods.     Prizes for the best costume from each era.Depth 4 - EvaluationToT Prompt for Voting: "Analyse the choices below, then decide which feature will be most engaging for guests at a time-travel costume party."Selected Response: "Setting up photo booths from different eras, like the Roaring 20s, the Renaissance, and the Future."By using the Tree of Thought Prompts, the decision-making process becomes structured, allowing the exploration of various options at each stage and refining the choices based on evaluations.Retrieval Augmented Generation (RAG) PromptsThis technique marries the power of retrieval with the flair of generation. In the vast ocean of data, the RAG ensures the LLM fishes out the most relevant pearls of information from a knowledge base and then weaves them into coherent, insightful narratives. RAG is the hero you want when detailed, factual accuracy is paramount. Think of tasks where there's a need to dive deep into databases or sources, such as Wikipedia, for question answering, translating texts, summarization, or even adding a factual touch to creative compositions.Although ChatGPT, Bard, Bing, and other LLMs aren't RAG models in the conventional sense, when supplied with links or equipped with plugins granting access to specific documents, they can harness their expansive knowledge banks. This enables them to yield outputs that are not only detailed and insightful but also precise and factually accurate.Prompting Tips and Best PracticesNavigating the world of prompts with a Large Language Model (LLM) is a tad like tango dancing. The clearer your moves (prompts), the better the performance. To ensure you and your LLM dance in perfect harmony, consider these golden rules of prompting:Precision is Key: Always aim for laser-like specificity in your prompts. The less room there is for guesswork, the more aligned the response will be to your desired outcome.Clarity Over Complexity: A well-phrased question is half the answer! Opt for clear, concise language, ensuring your prompts are easily decipherable.Skip the Gibberish: While using industry-specific jargon might make you sound smart at conferences, your LLM prefers simplicity. Sidestep any ambiguous terms or jargon that might lead to misinterpretation.Bite-Sized is Right-Sized: Complex tasks can be daunting, not just for humans but for LLMs too. Break them down into digestible, smaller tasks. It's easier to tackle a pie slice-by-slice than in one go.Context is King: The more you feed the LLM in terms of background, the richer and more tailored its output will be. Context sets the stage for relevance.The Prompts Playground: There's no one-size-fits-all in prompting. It's an art as much as it is a science. So, roll up your sleeves and experiment. Different tasks might resonate with different types of prompts. Keep tinkering until you strike gold!Remember, the magic happens when you communicate with your LLM effectively. These best practices are your playbook to unlocking its full potential and ensuring a harmonious tango dance every time.ConclusionIn the ever-evolving realm of AI and LLMs, the art of prompt engineering is akin to fine-tuning a musical instrument. With the right notes, or in this case, the right prompts, the symphony you create can be both harmonious and impactful. Whether you're dabbling in Zero-shot prompts or diving deep into CoT, remember that the essence lies in clear communication. By embracing best practices and staying adaptable, we not only harness the true prowess of these models but also pave the way for AI-human collaborations that are more seamless and productive. As we continue this dance with AI, may our prompts always lead, guide, and inspire.Author BioAmita Kapoor is an accomplished AI consultant and educator with over 25 years of experience. She has received international recognition for her work, including the DAAD fellowship and the Intel Developer Mesh AI Innovator Award. She is a highly respected scholar with over 100 research papers and several best-selling books on deep learning and AI. After teaching for 25 years at the University of Delhi, Amita retired early and turned her focus to democratizing AI education. She currently serves as a member of the Board of Directors for the non-profit Neuromatch Academy, fostering greater accessibility to knowledge and resources in the field. After her retirement, Amita founded NePeur, a company providing data analytics and AI consultancy services. In addition, she shares her expertise with a global audience by teaching online classes on data science and AI at the University of Oxford. 
Read more
  • 1
  • 1
  • 2130