61.4 Why is ChatGPT more popular than previous models?...82 - Understanding ChatGPT - A Look Behind the Scenes...103.3 Using ChatGPT from the Web..... 1.2 Why is it?What sets ChatGPT apa
What is ChatGPT?
Figure 1 ChatGPT ChatGPT is a powerful cutting-edge language model developed by OpenAI that can generate human-like text The model is trained on a large dataset of human-generated data, such as books, articles, and websites The model takes in a prompt, or a starting text, and then it uses what it has learned from the training data to make educated guesses about what words are likely to come next, based on the patterns it has observed The generated text can be used for a variety of natural language processing (NLP) tasks, such as language translation, text summarization, and dialogue generation For instance, ChatGPT can be used to summarize long articles or documents, translate text from one language to another, or generate human-like responses in chatbot conversations Due to its versatility and broad range of potential applications, ChatGPT is expected to make significant contributions to the field of natural language processing and influence how we interact with technology in our everyday lives.
Why is it?
What sets ChatGPT apart from other large language models is its capacity to produce responses that are more natural and less robotic, unlike anything seen before This, together with its user-friendly interface, has contributed to quick adoption and hype around the product ChatGPT hit an impressive milestone of reaching 1 million users in just five days after its launch in November 2022 It has been making waves in the community ever since, gaining attention from users and experts alike In many industries, ChatGPT is helping augment roles and making employees more productive Marketers can generate content faster, software engineers can start with boilerplate functions, and managers can receive automated summaries of reports ChatGPT has received a lot of positive feedback for its overall performance compared to previousLLMs, however others remain skeptical There is still a lot of confusion about the impact ChatGPT is expected to have on the AI industry,whether it will make some jobs redundant, and how it is going to change the way we work.
Who built it?
ChatGPT was built by the artificial intelligence research laboratory OpenAI Founded in 2015, the lab started with the goal of building
“artificial general intelligence” or AGI, software that possesses a level of intelligence comparable to that of human beings OpenAI has made significant contributions to the field of AI, a few examples include:
In 2020 OpenAI launched GPT-3 (GenerativePre-trained Transformer), a large language model that sets new benchmarks for natural language processing.
In 2021 OpenAI released Codex, an AI system that translates language to code and became the backbone of the popular AI pair programmer tool, GitHub Copilot which offers autocomplete-style suggestions as you code.
In 2022, OpenAI launched Dalle-2, a deep-learning model that generates high-resolution realistic images based on text instructions This led to asignificant mass interest in the field of GenerativeAI.
In 2023 OpenAI releases GPT-4 with a paid subscription allowing users access to the Chat GPT-4 tool The biggest difference between GPT-3 and GPT-4 is shown in the number of parameters it has been trained with GPT-3 has been trained with 175 billion parameters, and GPT-4 is likely to be trained with 100 trillion parameters GPT-
4 has more data than GPT-3, which is thought to significantly improve its performance (medium ChatGPT timeline) ChatGPT is one of the latest models developed by OpenAI, built as a variant of the GPT 3.5 model that is fine-tuned to better answer questions and interact conversationally
Why is ChatGPT more popular than previous models?
ChatGPT is built on top of the GPT 3.5 model fine tuned to interact in a conversational manner The dialogue format makes it possible for ChatGPT to answer follow-up questions, admit its mistakes, challenge incorrect premises, and reject inappropriate requests Here area few reasons why ChatGPT has become more popular than previous models:
The dataset that ChatGPT was trained on includes a diverse range of conversational text, and it has been fine-tuned on several specific tasks such as question answering and dialogue generation.This allows it to perform certain tasks more accurately than previous models, such as dialogue generation, question answering,and language translation The wide range of language patterns and styles in the training data also makes it better equipped for generating diverse and nuanced responses.
Previous models didn’t have an interface, so they were mainly used by developers ChatGPT’s free, user-friendly interface makes it highly versatile and adaptable to different use cases and caters to a non-technical audience such as content creators, marketing executives, etc.
Compared to other large language models such asGPT-3, ChatGPT is much better at generating human-like responses This was made possible by the use of reinforcement learning from human feedback (RLHF) This technical approach enabled the OpenAI team to rank the different responses generated by the initial model The model was then refined to learn from the human rankings and choose the best human-like response.
To achieve mass adoption, it is critical to build responsible AI systems OpenAI has developed several guardrails to build a more safe and useful language model including substantial reductions in harmful and untruthful outputs by incorporating human feedback in the model development process Even thoughChatGPT has become more robust over time thanks to the feedback loop, it is, in fact, very easy to find examples where it has provided incorrect or misleading results in the past, demonstrating that the guardrails are superficial and can be circumvented.
OpenAI has designed ChatGPT to be capable of processing very long sequences of text, allowing it to effectively capture long-term dependencies in the input data This helps ChatGPT maintain context and track the flow of a conversation, allowing it to provide appropriate responses even in complex or multi-turn conversations.
2 - Understanding ChatGPT - A Look Behind the Scenes
ChatGPT is an amazing web app that allows users to ask various questions and receive a wide range of responses This browser-based application offers exceptional productivity enhancements Moreover, it has the potential to replace certain job functions, particularly benefiting individuals knowledgeable in its optimal usage.
In order to unleash the full potential of ChatGPT and grasp its capabilities and features, it is vital to gain insight into its development process and inner workings.
Its name
• “Chat” refers to its primary function, which is to engage users in conversation, assist with inquiries, and generate human-like text based on input.
• “Generative” indicates that it is capable of generating text or responses from a given input.
• “Pretrained” means that it has been trained on a large dataset of text before being fine-tuned for specific tasks This pre-training allows it to have a broad understanding of language and context.
• “Transformer” is the type of neural network architecture used in its design Transformers are known for their ability to handle long-range dependencies in text and are particularly effective for natural language processing tasks.
Large Language Model
SO, VERY BASICALLY, LLMS ARE GIANT COMPUTERS THAT TRY TO FIND OUT THE NEXT WORD AFTER EVERY WORD.
Figure 3 The field of Artificial Intelligence in layers.
As LLMs, GPT-4 and ChatGPT are the latest type of model obtained in the field of NLP, which is itself a subfield of machine learning (ML) and AI AI is a very broad term, but generally it is the development of computer systems that can perform tasks that typically require human intelligence. With this definition, many algorithms fall under the AI umbrella ML is a subfield of AI In ML, we do not try to directly implement the decision rules used by the AI system Instead, we try to develop algorithms that allow the system to learn by itself from examples Since the 1950s, when
ML research began, many ML algorithms have been proposed in scientific literature Among them, deep learning algorithms have come to the fore.
Deep learning is the field within ML that focuses on algorithms inspired by the structure of the brain These algorithms are called artificial neural networks They can handle very large amounts of data and perform very well on tasks such as image and speech recognition and NLP.GPT-4 andChatGPT are based on a particular type of deep learning algorithm called transformers Transformers are like reading machines They pay attention to different parts of a sentence or block of text to understand its context and produce a coherent response They can also understand the order of words in a sentence and their context This makes them highly effective at tasks such as language translation, question answering, and text generation
Some practical approaches
• Code Conversion: ChatGPT’s proficiency in understanding complex programming structures and its ability to generate precise code transformations make it an ideal companion for developers seeking to streamline their workflows.
Language to language (JavaScript to Python) File format to file format (Markdown to JSON) Framework to framework (React to Vue) CSS to SCSS, SASS, Tailwind…
Document Code: In practice, writing documentation is hard and can take a lot of time and effort, so it isn’t a favorite task for many developers, and it is often pushed to the side This is a serious problem because written documentation is the main way developers explain how a software system should function and be used Poor documentation significantly hampers software quality by obscuring the understanding of the codebase But now, with new tools like ChatGPT entering the scene, there’s hope that poor documentation won’t be a headache anymore ChatGPT can serve as a code documentation tool and assess its effectiveness in making code documentation a smoother process.
Prompt engineering
Prompt engineering is essential because it directly influences the quality of results produced by language models Writing good prompts leads to better outcomes in terms of specificity, relevance, and readability By investing time and effort into prompt engineering, programmers can tailor the generated content to be more specific and engaging.
The Role of Context: Context plays a crucial role in writing prompts. Without sufficient context, the information provided to ChatGPT may be too limited, resulting in uninspiring outputs On the contrary, by providing additional context, programmers can guide the language model to generate more detailed and valuable content The inclusion of context allows ChatGPT to better understand the desired goal and create content that aligns with specific requirements Therefore, context becomes a vital component of prompt engineering This context can be divided into two types: a role assigned to ChatGPT, and extra constraints or information provided Including a role specifies a more defined behavior for ChatGPT beyond its default AI assistant capabilities.
• Adding a Role: Adding a role to the prompt is optional but can greatly benefit certain prompts For instance, if the objective is to create a captivating tweet about hiking for posting on Twitter, a specific role can be assigned to ChatGPT This role determines how
ChatGPT should behave and provides the necessary context for generating the desired output.
• Extra Constraints and Information: In addition to assigning a role, extra constraints or information can be included in the prompt. While this step is also optional, it is highly recommended as it improves the quality of most prompts and subsequently enhances the responses generated by ChatGPT By incorporating such constraints, the prompt can guide ChatGPT to produce more accurate and desirable outcomes.
When adding context, use short-focused sentences with relevant data for optimal results Ensure all important keywords are included without unnecessary information to avoid distractions Another important aspect of context is the output format By default, ChatGPT generates plain text, which suits most use cases However, ChatGPT can be employed for various scenarios For example, it can be used to generate dummy data for project purposes, such as application development or data science practice In such scenarios, specifying the desired output format, such as JSON or CSV, can be helpful Additionally, defining the target audience, tone, style, and length of the generated response can enhance the output’s effectiveness.
Utilizing Followup Messages: A key advantage of using ChatGPT is the incorporation of followup messages Since ChatGPT considers the entire chat history, not just the latest message, it enables a more comprehensive understanding of the conversation context and results in improved responses By leveraging followup messages, better starting points and refined outputs can be achieved.
Leveraging Contextual Understanding: Since ChatGPT takes into account the entire chat history, including the follow-up messages, it is not necessary to provide extensive information in each followup message A simple message stating the desired goal or change is sufficient ChatGPT will automatically connect the new message with the existing history to provide contextually appropriate responses This powerful feature allows for efficient utilization of ChatGPT.
Importance of Refinement: It is not essential to have a perfect initial prompt when using ChatGPT The refinement of responses and achieved results can be an iterative process over time The user can gradually fine- tune and improve the outcomes by sending follow-up messages and adjusting as needed Being aware of this key ChatGPT feature will significantly enhance the experience in using the platform effectively.
Using ChatGPT from the Web
Using ChatGPT from the Web Interacting with ChatGPT via the OpenAI website is incredibly straightforward OpenAI provides a web-based interface that can be found at ChatGPT, enabling users to engage with the model without any prior coding knowledge or setup required Once the website is visited, questions or prompts can be entered, and the model will produce its best possible answer or generated text Notably, ChatGPTWeb also provides users with various settings and options that allow them to track the conversation’s context and save the history of all interactions with the AI This feature-rich approach to web-based AI interactions allows users to effortlessly experiment with the model’s capabilities and gain insight into its vast potential applications To get started with the web-based interface, an account with OpenAI is required Once an account is created, the web interface can be accessed and the model’s capabilities can be explored, including various settings and options to enhance the AI interactions.
Before using ChatGPT or the ChatGPT API, an account on the OpenAI website must be created To do that, visit https://chat.openai.com.
Simply clicking the Sign up button and following the prompts will access the registration window From there, the option to enter an email address and click Continue, or to register using a Google or Microsoft account, is available Once this step is complete, select a password and validate email, just like with any other website registration process.
After completing the registration process, ChatGPT’s full range of features can be explored Simply clicking the Log in button and entering the credentials into the Log In window With this straightforward approach to access, the full capabilities of ChatGPT can be seamlessly explored and its power for natural language processing tasks can be seen firsthand.
Now the features and functionality of the ChatGPT web interface can be explored in greater detail The interface will be navigated and its various options will be used to get the best possible results from the AI model.
ChatGPT Web Interface The 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 The chat window also displays previous messages and prompts, allowing users to keep track of the conversation’s context.
In 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 Users are now able to distinguish and use the advancements of different ChatGPT models.
The inclusion of the custom instructions feature enhances the capabilities of ChatGPT by allowing users to specify their preferences or requirements that should be taken into consideration during response generation By consistently taking these instructions into account, the model eliminates the need for users to repetitively express their preferences or provide the same information in each interaction.
For instance, consider a scenario where a Python developer aiming for efficient code can express their preference just once, and ChatGPT will consistently consider it.
Figure 8 ChatGPT’s Custom Instructions Interface
The custom instructions feature includes two boxes: one for adding information about users and their role, and the other for specifying the desired formatting of responses from ChatGPT, including tone and style.
In these boxes, users can specify their occupation, enabling ChatGPT to generate relevant responses based on their professional field, and indicating the preferred level of formality, length, and conciseness.
Click on “Show tips” to see examples of the type of content to include.Users are then required to enter their instructions and save them It is important to note that there is a character limit of 1,500 for each response.
Bring ChatGPT into IDE
Github Copilot
Numerous studies and software products have focused on enhancing developers’ efficiency by creating systems that recommend and generate code With the rapid growth of Large Language Models (LLMs), AI tools for code generation, trained on extensive code snippets, have become increasingly popular These tools enable programmers to generate code automatically with less human intervention On June 29, 2021, GitHub, the largest community of developers, and OpenAI, a leading AI research organization, jointly introduced GitHub Copilot This innovative tool is powered by OpenAI’s Codex, a large-scale neural network trained on a massive dataset of source code and natural language text GitHub Copilot aims to provide advanced code autocompletion and generation capabilities, essentially acting as an “AI pair programmer” to assist developers in real-time coding tasks It seamlessly integrates with various Integrated Development Environments (IDEs) and text editors, such as VSCode, Visual Studio, Neovim, and JetBrains By gathering contextual information like function names and comments, Copilot can generate code snippets in different programming languages, thereby enhancing developers’ productivity and streamlining the completion of coding tasks
Basic Usage Github Copilot: Inline Suggestions
- Start writing code (or code-related items, like comments or tests)
- Receive a Copilot suggestion in gray text (may offer multiple suggestions)
- Choose to accept Copilot’s suggestion or continue typing
- Ask Copilot for help with any question/command (interactive)
- Copilot provides potential or related follow-up questions or commands
- Copilot maintains the history of users’ conversation
- Get more relevant answers by indicating the scope and intent
- Use the @ symbol to mention the two agent commands
- Use the / symbol to mention the slash commands /explain /tests /fix /new /newNotebook /search /api /help /clear
Figure 10 Github Copilot Chat Interface
Figure 11 Generate Commit Message Feature
OpenAI provides its models as a service, through an API 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 use for their specific use case
To use the ChatGPT API, developers will need to obtain an API key This can be obtained from OpenAI This key will allow developers to authenticate their requests to the API and ensure that only authorized users can access this account First, access the OpenAI Platform at https://platform.openai.com Login and then navigate to the API access page: https://platform.openai.com/account/api-keys Click on the
“Create new secret key” button to generate a new API key This key is a long string of characters starting with sk-
Note: After creating an API key, there is only one chance to copy it It’s important to keep API key secure and confidential
Figure 12 Creating an API Key
When 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.
Check out OpenAI’s Tokenizer tool to learn more about how text is translated into tokens at https://platform.openai.com/tokenizer.
Developers can track their API usage and monitor their billing information through the Usage dashboard at https://platform.openai.com/account/usage.
First, install the Python library with pip: pip install openai
Next, create a new Python file: import openai openai.api_key = "sk- wMHabDy5MjV72WZhsAsDT3BlbkFJtV3ycSsTslfgx1Qzmapc" response = openai.chat.completions.create( model="gpt-3.5-turbo", messages=[{"role":"user", "content": "Hello"}] ) print(response.choices[0].message.content)
Hello! How can I assist you today?
Recommendation is to follow a widespread convention for environment variables: store the API key in a env file, which is removed from source control in the gitignore file In Python, run the load_dotenv function to load the environment variables and import the openai library. from dotenv import load_dotenv load_dotenv() import openai
It is important to have the openai import declaration after loading the env file; otherwise, the settings for OpenAI will not be applied correctly.
The Python code provided below initiates a simplified ChatGPT clone. This code enables users to engage in a conversational interaction, inputting messages and receiving responses generated. import openai openai.api_key = "sk- wMHabDy5MjV72WZhsAsDT3BlbkFJtV3ycSsTslfgx1Qzmapc" def chat_with_gpt(user_input):
# Start a conversation with a system message conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
# Add user input to the conversation conversation.append({'role': 'user', 'content': user_input})
# Generate a response from ChatGPT response = openai.chat.completions.create( model="gpt-3.5-turbo", messages=conversation
# Extract and print the assistant's reply reply = response.choices[0].message.content conversation.append({'role': 'assistant', 'content': reply}) print("Assistant:", reply)
# Main loop for user interaction while True: user_input = input("You: ")
# Exit the loop if the user types "exit" if user_input.lower() == "exit": print("Ending the conversation Goodbye!") break
# Get a response from ChatGPT chat_with_gpt(user_input) print(conversation)
• chat_with_gpt: This function handles the interaction flow It adds user input to the conversation, queries the GPT-3.5-turbo model, appends the assistant’s reply, and prints the response.
• The main loop prompts the user for input, processes it through the chat_with_gpt function, and continues until the user decides to exit by typing “exit.”
• The conversation variable starts with a system message, introducing the assistant’s role Note that the API does not store previous messages in its context The question “Why?” refers to the previous answer and only makes sense if the model has knowledge of this answer The entire conversation must be sent each time to simulate a chat session.
Now, by declaring the conversation variable outside the confines of the chat_with_gpt function, its scope extends beyond the confines of a single function call This means that the entire history of the conversation, including system messages, user inputs, and assistant replies, persists across multiple interactions. import openai
# Set your OpenAI API key openai.api_key = "sk- wMHabDy5MjV72WZhsAsDT3BlbkFJtV3ycSsTslfgx1Qzmapc"
# Start a conversation with a system message conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}] def chat_with_gpt(user_input):
# Add user input to the conversation conversation.append({'role': 'user', 'content': user_input})
# Generate a response from ChatGPT response = openai.chat.completions.create( model="gpt-3.5-turbo", messages=conversation
# Extract and print the assistant's reply reply = response.choices[0].message.content conversation.append({'role': 'assistant', 'content': reply}) print("Assistant:", reply)
# Main loop for user interaction while True: user_input = input("You: ")
# Exit the loop if the user types "exit" if user_input.lower() == "exit": print("Ending the conversation Goodbye!") break
# Get a response from ChatGPT chat_with_gpt(user_input)
4.5 Building a YouTube Video Transcript Summarizer
LLMs have proven to be good at summarizing text In most cases, they manage to extract the core ideas and reformulate the original input so that the generated summary feels smooth and clear.
This is a simple Streamlit app, Streamlit is used for creating the web app, YouTubeTranscriptApi for fetching YouTube video transcripts, and OpenAI for using the GPT-3.5-turbo model. import streamlit as st from youtube_transcript_api import YouTubeTranscriptApi import openai openai.api_key = 'sk- wMHabDy5MjV72WZhsAsDT3BlbkFJtV3ycSsTslfgx1Qzmapc'
# Define the Streamlit app def main(): st.title("YouTube Transcript Summarizer")
# Get user input for YouTube video URL or ID video_url = st.text_input("Enter YouTube Video URL or ID:") if st.button("Summarize Transcript"):
# Extract video ID from URL if provided video_id = extract_video_id(video_url) if video_id: try:
YouTubeTranscriptApi.get_transcript(video_id)
# Summarize the transcript using GPT-3 text = "" for line in transcript: text += line["text"] + "\n" summary = summarize_transcript(text)
# Display the summary st.subheader("Summary:") st.write(summary) except Exception as e: st.error(f"Error: {e}") else: st.warning("Please enter a valid YouTube Video URL or ID.") def extract_video_id(url): video_id = url if "youtube.com" in url or "youtu.be" in url: video_id = url.split("v=")[1] if "v=" in url else url.split("/")[-1] if "&" in video_id: video_id = video_id.split("&")[0] return video_id def summarize_transcript(transcript): response = openai.chat.completions.create( model="gpt-3.5-turbo", messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": f"Please summarize this transcript: {transcript}"},
) return response.choices[0].message.content
# Run the Streamlit app if name == " main ": main()
Figure 15 YouTube Video Transcript Summarizer Interface def main(): st.title("YouTube Transcript Summarizer")
# Get user input for YouTube video URL or ID video_url = st.text_input("Enter YouTube Video URL or ID:")
This function defines the Streamlit app and its components This includes the title and the user input for the YouTube video URL or ID. if name == " main ": main()
This section ensures that the main() function is executed when the script is run, making it a standalone app.
This small project has proven how integrating simple summarization features into an application can bring value—with very few lines of code. Plugging it into some use cases can lead to a very useful application.
Using OpenAI Python Library
First, install the Python library with pip: pip install openai
Next, create a new Python file: import openai openai.api_key = "sk- wMHabDy5MjV72WZhsAsDT3BlbkFJtV3ycSsTslfgx1Qzmapc" response = openai.chat.completions.create( model="gpt-3.5-turbo", messages=[{"role":"user", "content": "Hello"}] ) print(response.choices[0].message.content)
Hello! How can I assist you today?
Recommendation is to follow a widespread convention for environment variables: store the API key in a env file, which is removed from source control in the gitignore file In Python, run the load_dotenv function to load the environment variables and import the openai library. from dotenv import load_dotenv load_dotenv() import openai
It is important to have the openai import declaration after loading the env file; otherwise, the settings for OpenAI will not be applied correctly.
Building a simple ChatGPT Clone
The Python code provided below initiates a simplified ChatGPT clone. This code enables users to engage in a conversational interaction, inputting messages and receiving responses generated. import openai openai.api_key = "sk- wMHabDy5MjV72WZhsAsDT3BlbkFJtV3ycSsTslfgx1Qzmapc" def chat_with_gpt(user_input):
# Start a conversation with a system message conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
# Add user input to the conversation conversation.append({'role': 'user', 'content': user_input})
# Generate a response from ChatGPT response = openai.chat.completions.create( model="gpt-3.5-turbo", messages=conversation
# Extract and print the assistant's reply reply = response.choices[0].message.content conversation.append({'role': 'assistant', 'content': reply}) print("Assistant:", reply)
# Main loop for user interaction while True: user_input = input("You: ")
# Exit the loop if the user types "exit" if user_input.lower() == "exit": print("Ending the conversation Goodbye!") break
# Get a response from ChatGPT chat_with_gpt(user_input) print(conversation)
• chat_with_gpt: This function handles the interaction flow It adds user input to the conversation, queries the GPT-3.5-turbo model, appends the assistant’s reply, and prints the response.
• The main loop prompts the user for input, processes it through the chat_with_gpt function, and continues until the user decides to exit by typing “exit.”
• The conversation variable starts with a system message, introducing the assistant’s role Note that the API does not store previous messages in its context The question “Why?” refers to the previous answer and only makes sense if the model has knowledge of this answer The entire conversation must be sent each time to simulate a chat session.
Now, by declaring the conversation variable outside the confines of the chat_with_gpt function, its scope extends beyond the confines of a single function call This means that the entire history of the conversation, including system messages, user inputs, and assistant replies, persists across multiple interactions. import openai
# Set your OpenAI API key openai.api_key = "sk- wMHabDy5MjV72WZhsAsDT3BlbkFJtV3ycSsTslfgx1Qzmapc"
# Start a conversation with a system message conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}] def chat_with_gpt(user_input):
# Add user input to the conversation conversation.append({'role': 'user', 'content': user_input})
# Generate a response from ChatGPT response = openai.chat.completions.create( model="gpt-3.5-turbo", messages=conversation
# Extract and print the assistant's reply reply = response.choices[0].message.content conversation.append({'role': 'assistant', 'content': reply}) print("Assistant:", reply)
# Main loop for user interaction while True: user_input = input("You: ")
# Exit the loop if the user types "exit" if user_input.lower() == "exit": print("Ending the conversation Goodbye!") break
# Get a response from ChatGPT chat_with_gpt(user_input)
Building a YouTube Video Transcript Summarizer
LLMs have proven to be good at summarizing text In most cases, they manage to extract the core ideas and reformulate the original input so that the generated summary feels smooth and clear.
This is a simple Streamlit app, Streamlit is used for creating the web app, YouTubeTranscriptApi for fetching YouTube video transcripts, and OpenAI for using the GPT-3.5-turbo model. import streamlit as st from youtube_transcript_api import YouTubeTranscriptApi import openai openai.api_key = 'sk- wMHabDy5MjV72WZhsAsDT3BlbkFJtV3ycSsTslfgx1Qzmapc'
# Define the Streamlit app def main(): st.title("YouTube Transcript Summarizer")
# Get user input for YouTube video URL or ID video_url = st.text_input("Enter YouTube Video URL or ID:") if st.button("Summarize Transcript"):
# Extract video ID from URL if provided video_id = extract_video_id(video_url) if video_id: try:
YouTubeTranscriptApi.get_transcript(video_id)
# Summarize the transcript using GPT-3 text = "" for line in transcript: text += line["text"] + "\n" summary = summarize_transcript(text)
# Display the summary st.subheader("Summary:") st.write(summary) except Exception as e: st.error(f"Error: {e}") else: st.warning("Please enter a valid YouTube Video URL or ID.") def extract_video_id(url): video_id = url if "youtube.com" in url or "youtu.be" in url: video_id = url.split("v=")[1] if "v=" in url else url.split("/")[-1] if "&" in video_id: video_id = video_id.split("&")[0] return video_id def summarize_transcript(transcript): response = openai.chat.completions.create( model="gpt-3.5-turbo", messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": f"Please summarize this transcript: {transcript}"},
) return response.choices[0].message.content
# Run the Streamlit app if name == " main ": main()
Figure 15 YouTube Video Transcript Summarizer Interface def main(): st.title("YouTube Transcript Summarizer")
# Get user input for YouTube video URL or ID video_url = st.text_input("Enter YouTube Video URL or ID:")
This function defines the Streamlit app and its components This includes the title and the user input for the YouTube video URL or ID. if name == " main ": main()
This section ensures that the main() function is executed when the script is run, making it a standalone app.
This small project has proven how integrating simple summarization features into an application can bring value—with very few lines of code. Plugging it into some use cases can lead to a very useful application.
With the power of NLP available with the OpenAI services, developers can integrate incredible functionalities into their applications and leverage this technology to build services that could not have been possible before However, as with any new technology, the state of the art is evolving extremely quickly, and there are better ways to interact withChatGPT and GPT-4 models.