4. Demo of Chatbot / OpenAI¶

Instroduction¶

ChatGPT (Chat Generative Pre-Trained Transformer) is talking everywhere and so hot that big companies like Google also talk about its impact and strategy to resolve its influence.

In this short demo, I will show how to use Python and its library to access API provided by OpenAI and use some popular/useful features. These features are provided via Endpoints and use different pre-trained models to answer any issues.

Model (was Engine) Endpoints¶

There are a couple of models that can be used in different contexts/situations, hence, with different accuracy. For example, Davinci is the most capable model, and Ada is the fastest. While Davinci is generally the most capable, the other models can perform certain tasks extremely well with significant speed or cost advantages. For example, Curie can perform many of the same tasks as Davinci, but faster and for 1/10th the cost. Read more here

  • List models

GET https://api.openai.com/v1/models

Lists the currently available models, and provides basic information about each one such as the owner and availability.

Install dependencies¶

We can get "openai" library from conda

conda install -c conda-forge openai

Limitations¶

  • Data Limited knowledge of world and events after 2021

  • May occasionally generate incorrect information

  • May occasionally produce harmful instructions or biased content

In [ ]:
import openai

openai.api_key = "sk-######" # Insert your API here
# get one free at https://openai.com/api/

Completion Endpoints¶

Based on an input, we can ask an engine to complete the sentence. There are several tasks we can ask, for example, ask a question, classify sentiment or a general caterogy, transformation (translation) or complete incomplete text.

There are important paraments

  • model: Depending on speend, cost, accuracy, we can choose different models.

  • temperature: Higher values means the model will take more risks. Try 0.9 for more creative applications, and 0 (argmax sampling) for ones with a well-defined answer. Default value: 1

  • top_p: Nucleus sampling. 0.1 indicates that only the top 10% probability mass tokens will be evaluated. Default value: 1

  • max_tokens: maximum number of tokens to generate in the completion.

In [ ]:
def complete(
    prompt,
    suffix=None,
    model="text-davinci-003",
    temperature=1,
    max_tokens=60,
    top_p=1,
    frequency_penalty=0,
    presence_penalty=0,
):
    try:
        response = openai.Completion.create(
            model=model,
            prompt=prompt,
            temperature=temperature,
            max_tokens=max_tokens,
            top_p=top_p,
            suffix=suffix,
            frequency_penalty=frequency_penalty,
            presence_penalty=presence_penalty,
        )
        return response.choices

    except Exception as e:
        print(f"Error: {e}")
        return None
In [ ]:
prompt = "Tell me about Messi."
r = complete(prompt, model="text-davinci-003", temperature=0.9)
if r:
    print(r[0].text)

Lionel Messi is an Argentinian professional footballer who plays as a forward for Spanish club Barcelona and the Argentina national team. Messi is widely considered to be one of the greatest players of all time. He is the first player in history to win five FIFA Ballon d'Or awards,
In [ ]:
prompt = "How do you like Messi."
r = complete(prompt, model="text-davinci-003", temperature=0.9, max_tokens=20)
if r:
    print(r[0].text)

I think Messi is a phenomenal footballer, and I greatly admire his skill and determination. He
In [ ]:
prompt = (
    "Classify the Text's label as positive, neutral, or negative.\n"
    "Text: I loved the football!\n"
    "Sentiment:"
)

r = complete(prompt, model="text-davinci-003", temperature=0.9)
print("Prompt text:\n" + prompt + "\n")
if r:
    print(r[0].text)
Prompt text:
Classify the Text's label as positive, neutral, or negative.
Text: I loved the football!
Sentiment:

 Positive

Temperature Impact¶

Let's see how temperature change the text generation.

In [ ]:
prompt = "Classify the Text.\n Text: Today is so rainy and I love raining!\n"
print("Prompt text:\n" + prompt + "\n")
temp = [1, 0.75, 0.5, 0.25, 0]
for t in temp:
    r = complete(prompt, model="text-davinci-002", temperature=t)
    if r:
        print("Temperature = " + str(t) + ": " + r[0].text)
Prompt text:
Classify the Text.
 Text: Today is so rainy and I love raining!


Temperature = 1: 
The text is classified as weather.
Temperature = 0.75: 
The text is about the weather.
Temperature = 0.5: 
The text is classified as a description of the weather.
Temperature = 0.25: 
The text is a description of the weather.
Temperature = 0: 
The text is about the weather.
In [ ]:
# Multiple requirements at once
prompt = (
    "Classify the sentiment in these tweets:\n"
    '1. "I enjoy this program"\n'
    '2. "I don\'t like him anymore"\n'
    '3. "I can\'t wait for you"\n'
    '4. "I can\'t wait for you anymore"\n'
    '5. "I can\'t wait for Halloween!!!"\n'
    '6. "I do not hate much jokers"\n'
    '7. "I love eating chocolates <3!"\n'
    "Tweet sentiment ratings:"
)

r = complete(prompt, model="text-davinci-003", temperature=0.9)
print("Prompt text:\n" + prompt + "\n")
if r:
    print(r[0].text)
Prompt text:
Classify the sentiment in these tweets:
1. "I enjoy this program"
2. "I don't like him anymore"
3. "I can't wait for you"
4. "I can't wait for you anymore"
5. "I can't wait for Halloween!!!"
6. "I do not hate much jokers"
7. "I love eating chocolates <3!"
Tweet sentiment ratings:


1. Positive 
2. Negative 
3. Positive 
4. Negative 
5. Positive 
6. Positive 
7. Positive

Suffix¶

The suffix that comes after a completion of inserted text to helps the model for a better text generation.

In [ ]:
prompt = (
    "Messi scored multiple goals in World Cup. Before that, he played"
    " well in all soccer clubs. He helps his colleague to score as well."
)
suffix = "Messi is the greatest of all the time player."
r = complete(
    prompt,
    suffix=suffix,
    model="text-davinci-003",
    temperature=1,
    max_tokens=260,
)
print("Prompt text:\n" + prompt)
if r:
    print(r[0].text)
print(suffix + "(suffix)")
Prompt text:
Messi scored multiple goals in World Cup. Before that, he played well in all soccer clubs. He helps his colleague to score as well.
 Messi is a fantastic player with amazing skills. 
Messi is the greatest of all the time player.(suffix)
In [ ]:
prompt = "How to lose weight:\n1. Do not skip breakfast."
suffix = "12. Plan your meals"
r = complete(
    prompt,
    suffix=suffix,
    model="text-davinci-003",
    temperature=1,
    max_tokens=260,
)
print("Prompt text:\n" + prompt)
if r:
    print(r[0].text)
print(suffix + "(suffix)")
Prompt text:
How to lose weight:
1. Do not skip breakfast.

2. Eat small meals at regular intervals throughout the day.
3. Eat fewer fatty and sugary foods.
4. Eat more fruits and vegetables.
5. Exercise regularly.
6. Drink plenty of water.
7. Limit your alcohol consumption.
8. Reduce your intake of processed foods.
9. Avoid snacking on unhealthy foods.
10. Get plenty of sleep.
11. Manage your stress levels.


12. Plan your meals(suffix)

Summarization¶

The model can summarize a long paragraph. The new length could be alterred in "max_tokens"

  • max_tokens: Set small value if we want short summary

  • frequency_penalty: Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics. Number between -2.0 and 2.0

  • presence_penalty: Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. Number between -2.0 and 2.0

In [ ]:
prompt = (
    "One person is in hospital and others are without a home in the "
    "aftermath of a Christmas evening fire at a fourplex in southeast "
    "Calgary.  The Calgary Fire Department was called to the 2800 block "
    "of 15 Avenue S.E. around 8 p.m. on Sunday, Dec. 25.  Firefighters "
    "doused flames in the basement of the fourplex, keeping them from "
    "spreading beyond the suite they started in.  CFD found and rescued "
    "one resident, who was taken to hospital in critical condition.  "
    "Other residents of the fourplex home at the time of the fire were "
    "able to escape on their own.  The fire department says smoke damage "
    "inside one of the upper suites resulted in the displacement of some "
    "residents.  Investigation into the cause of the fire is ongoing."
)
print("Full text:\n" + prompt)
print("Summary:")
r = complete(
    prompt,
    model="text-davinci-002",
    temperature=1,
    max_tokens=32,
    frequency_penalty=1,
    presence_penalty=1,
)
if r:
    print(r[0].text)
Full text:
One person is in hospital and others are without a home in the aftermath of a Christmas evening fire at a fourplex in southeast Calgary.  The Calgary Fire Department was called to the 2800 block of 15 Avenue S.E. around 8 p.m. on Sunday, Dec. 25.  Firefighters doused flames in the basement of the fourplex, keeping them from spreading beyond the suite they started in.  CFD found and rescued one resident, who was taken to hospital in critical condition.  Other residents of the fourplex home at the time of the fire were able to escape on their own.  The fire department says smoke damage inside one of the upper suites resulted in the displacement of some residents.  Investigation into the cause of the fire is ongoing.
Summary:


- one person in hospital, critical condition
- others displaced

Translation:¶

We can translate even into multiple languages at once. We need to set "temperature" to 0 to get the most accurate translation.

In [ ]:
prompt = (
    "Translate this into 1. French, 2. Spanish and 3. Japanese.\n"
    "Chatbot is not something new, but there are huge room to improve."
)
r = complete(prompt, model="text-davinci-003", temperature=0, max_tokens=100)
if r:
    print(r[0].text)

1. Le chatbot n'est pas quelque chose de nouveau, mais il y a beaucoup de possibilités d'amélioration.
2. El chatbot no es algo nuevo, pero hay mucho espacio para mejorar.
3. チャットボットは新しいものではありませんが、改

Write about a Subject¶

It is not an problem to ask the machine to write something or even tell a story.

In [ ]:
prompt = (
    "Briefly summarize about future of Natural Language Processing"
    " with machine learning."
)
r = complete(prompt, model="text-davinci-003", temperature=1, max_tokens=128)
if r:
    print(r[0].text)

The future of Natural Language Processing with machine learning looks very promising. NLP is being used to solve some of the most challenging problems in marketing, healthcare, education, finance, and more. With more advances in NLP algorithms, machines will be able to understand and interact with humans in natural language more effectively. Furthermore, machine learning algorithms will be increasingly used to create better language models, improve accuracy in classifications, and also build better language-based applications. All these advances will ultimately lead to more efficient and powerful language processing applications.
In [ ]:
prompt = "Tell me a fun story but not happy ending."
r = complete(prompt, model="text-davinci-002", temperature=1, max_tokens=256)
if r: print(r[0].text)

One day, there was a boy who found a fish in a lake. He was so excited to have found such a prize, and he took the fish home to show his mother. His mother was less than thrilled, and she told him to put the fish back in the lake. The boy was so disappointed, but he did as his mother said. When he got back to the lake, the fish was gone.

Edit Text / Code¶

OpenAI’s GPT-3 model can edit text, which has various applications like grammar and punctuation correction, rewriting text, or any other simple instructions that can be performed on a text.

Parameters¶

  • model: ID of the model to use. You can use the text-davinci-edit-001 or code-davinci-edit-001

  • input: The input text to use as a starting point for the edit. Defaults to ''

  • instruction (Required): The instruction that tells the model how to edit the prompt.

In [ ]:
def edit(
    instruction,
    input="",
    model="text-davinci-edit-001",
    temperature=1,
    top_p=1,
):
    try:
        response = openai.Edit.create(
            model=model,
            instruction=instruction,
            input=input,
            temperature=temperature,
            top_p=top_p,
        )
        return response.choices

    except Exception as e:
        print(f"Error: {e}")
        return None
In [ ]:
text = "What dayy of the wek is its?"
instruction = "Fix the spelling mistakes and grammar"
r = edit(instruction=instruction, input=text, model="text-davinci-edit-001")
if r: 
    print(r[0].text)
What day of the week is it today?

In [ ]:
text = """
def trySomething(sefl, go):
    print("Good job")
     if go == "1"
     print("Nan")
    return a
"""
instruction = "Fix the code in Python"
r = edit(
    instruction=instruction,
    input=text,
    model="code-davinci-edit-001",
    temperature=0.1,
)
if r:
    print(r[0].text)
def trySomething(sefl, go):
    print("Good job")
    if go == "1":
        print("Nan")
    return a

Other Usages¶

Image Generation¶

  • Creating images from scratch based on a text prompt
  • Creating edits of an existing image based on a new text prompt
  • Creating variations of an existing image

More

Embeddings¶

An embedding is a vector (list) of floating point numbers. The distance between two vectors measures their relatedness. Small distances suggest high relatedness and large distances suggest low relatedness. Embeddings are commonly used for:

  • Search (where results are ranked by relevance to a query string)
  • Clustering (where text strings are grouped by similarity)
  • Recommendations (where items with related text strings are recommended)
  • Anomaly detection (where outliers with little relatedness are identified)
  • Diversity measurement (where similarity distributions are analyzed)
  • Classification (where text strings are classified by their most similar label)

[More] (https://platform.openai.com/docs/guides/embeddings)

More resources¶

  1. https://www.educative.io/courses/open-ai-api-natural-language-processing-python/

  2. https://platform.openai.com/docs/models

  3. https://platform.openai.com/docs/api-reference/completions

  4. https://help.openai.com/en/articles/5832130-what-s-changed-with-engine-names-and-best-practices

  5. https://jimmymwhitaker.medium.com/openais-chatgpt-19e7fd58ff73

  6. https://medium.datadriveninvestor.com/openai-quietly-released-gpt-3-5-heres-what-you-can-do-with-it-4dee22aea438