Class 8 - OPEN AI API USING PYTHON Notes from the AI Basic Course by Irfan Malik & Dr Sheraz Naseer (Xeven Solutions)
OPEN AI API USING PYTHON
Notes from the AI Basic Course (Class 8) by Irfan Malik & Dr Sheraz Naseer (Xeven Solutions)
What is Classification?
Classification is a machine learning technique used to categorize data into different classes or categories.
It is supervised machine learning approach where the algorithm learns from labeled training data to make predictions or classify new, unseen data.
Applications of Classification:
Spam email detection
Credit risk assessment
Disease diagnosis
Image recognition
Sentiment analysis (Branch of NLP, Sentiments of people, developing of thought). It is done through LLM (Large Language Model)
In NLP, there is lot of opportunities in Freelancing.
Sentiment Analysis:
Sentiment analysis, also known as opinion mining, is a subfield of NLP that focuses on determining the sentiment or emotional tone of a piece of text.
What is an API?
API stands for Application Programming Interface.
It is a set of rules & protocols that allows different software applications to communicate and interact with each other.
Why to use API'S?
APIs enable developers to access the functionality of other applications or services having to understand their internal functionality.
They provide a standardized way for applications to exchange data and perform specific tasks.
Types of APIs:
Web APIs:
These are APIs exposed over the web using standard protocols like HTTP, allowing developers to interact with remote services.
Library or Framework APIs:
These APIs are provided by libraries or frameworks and offer pre-defined functions and classes to simplify development.
Operating System APIs:
These APIs provide access to operating system features and functionalities, allowing developers to interact with the underlying system.
APIs are like a bridge or a third party function.
APIs are secure control bridge b/w different channels.
Goolge Colab link:
Installing of open.ai:
!pip install openai
!pip install openai
to generate the key you can go to the link https://platform.openai.com/account/api-keys
openai.api_key = ''
Sentiment Analysis using open.ai api:
import openai
openai.api_key = ''
def Senitment_analysis(text):
messages = [
{"role": "system", "content": """You are trained to analyze and detect the sentiment of given text.
If you're unsure of an answer, you can say "not sure" and recommend users to review manually."""},
{"role": "user", "content": f"""Analyze the following text and determine if the sentiment is: positive or negative.
Return answer in single word as either positive or negative: {text}"""}
]
领英推荐
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
max_tokens=1,
n=1,
stop=None,
temperature=0)
response_text = response.choices[0].message.content.strip().lower()
return response_text
# calling the function
input = 'I hate fast food'
response = Senitment_analysis(input)
print(input,': The Sentiment is', response)
def generate_blog(topic):
messages = [
{"role": "system", "content": """You are trained to analyze a topic and generate a blog post.
The blog post must contain 1500 to 3000 words (No less than 1500 words)."""},
{"role": "user", "content": f"""Analyze the topic and generate a blog post. The topic is {topic}
The blog post should contain the following format.
1) Title (Not more than one line).
2) Introduction (Give introducion about the topic)
3) Add an image url relevent to the topic.
4) Add 2/3 subheadings and explain them.
5) Body (should describe the facts and findings)
6) Add an image url relevent to the topic.
7) Add 2/3 subheadings and explain them.
8) General FAQ regarding the topic.
9) Conclusion of the topic. """}
]
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
max_tokens= 3000,
n=1,
stop=None,
temperature=0.5)
response_text = response.choices[0].message.content.strip().lower()
return response_text
user_input ="Machine Learning"
blog = generate_blog(user_input)
print(blog)
to learn more visit https://huggingface.co/docs/transformers/main_classes/pipelines
Temperature means creativity.
Don't just copy, paste the code.
Write every line of it.
#AI #artificialintelligence #datascience #irfanmalik #drsheraz #xevensolutions #hamzanadeem