Playing with the AI code

Can you tell me where the error takes place!

Hi @kritikasehrawat296

Please make sure that you close the open inverted quotes (" "). after “?”

for eg.
{ "role": "user", "content": "Where was the 2011 world cup played and who won it?" }

Please try this and let us know. If you still face any issues, please feel free to get back to us.

Hi there @kritikasehrawat296

I have noticed that the problem in your code is on line 37, where you have written -

{
"role": "user", "content": "Where was the 2011
cricket world cup played and
who won it?"?
}])

As you can see, there is a type on line 37, where you have added an extra ‘?’. Try removing that.

After the correction, your code should look something like this:

{"role": "user", "content": "Where was the 2011 cricket world cup played and who won it?"}

Another thing is to type your statements in a complete line, so you don’t have to deal with multiple lines for one error. (This is purely a suggestion, you can code however you want :D)

Regards,
@chai

Again this happened … i tried what uhh said !

import openai
import os
import sys

openai.api_key = os.environ[‘OPENAI_API_KEY’]
if openai.api_key == “”:
sys.stderr.write(“”"
You haven’t set up your API key yet.

If you don’t have an API key yet, visit:

https://platform.openai.com/signup

  1. Make an account or sign in
  2. Click “View API Keys” from the top right menu.
  3. Click “Create new secret key”

Then, open the Secrets Tool and add OPENAI_API_KEY as a secret.
“”")
exit(1)

response = openai.chat.completions.create(
model=“gpt-3.5-turbo”,
messages=[{
“role”: “system”,
“content”: “You are a helpful assistant.”
}, {
#“role”: “user”,
“content”: “Who won the world series in 2020?”
}, {
#“role”:“assistant”,
“content”:
“The Los Angeles Dodgers won the World Series in 2020.”
}, {
“role”: “user”, “content”:“Where was the 2011
cricket world cup played and who won it?”
}])

output = response[‘choices’][0][‘message’][‘content’]
print(output)

Hi @kritikasehrawat296

Below is the working code . Please try this and let us know.

import openai
import os
import sys

openai.api_key = os.environ['OPENAI_API_KEY']
if openai.api_key == "":
  sys.stderr.write("""
  You haven't set up your API key yet.
  
  If you don't have an API key yet, visit:
  
  https://platform.openai.com/signup

  1. Make an account or sign in
  2. Click "View API Keys" from the top right menu.
  3. Click "Create new secret key"

  Then, open the Secrets Tool and add OPENAI_API_KEY as a secret.
  """)
  exit(1)

response = openai.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[{
        "role": "system",
        "content": "You are a helpful assistant."
    }, {
        "role": "user",
        "content": "Who won the world series in 2020?"
    }, {
        "role":
        "assistant",
        "content":
        "The Los Angeles Dodgers won the World Series in 2020."
    }, {
        "role": "user",
        "content": "Where was it played?"
    }])

output = response.choices[0].message.content
print(output)

If you still face any issues, please feel free to get back to us


Can you help me why it is not answering the question?

Hi there @kritikasehrawat296
Could you please send your finished code so I can help you with your issue.

Regards,
@chai


i guess it is the complete code!!

Hi there @kritikasehrawat296
Great to see you have solved the issue! If you have any more questions or errors, don’t hesitate to get in touch with me.

Regards,
@chai

My question is why the AI not answering my question…why it is not writing an essay?

See the AI is not answering the question. It is only saying how can i help you !!!

Hi there @kritikasehrawat296
Could you please share your code so I can review it and help you?

Regards,
@chai

import openai
import os
import sys

question=input( “Q: what is your question / instruction? \n”)

while True:
openai.api_key = os.environ[‘OPENAI_API_KEY’]
if openai.api_key == “”:
sys.stderr.write(“”"
You haven’t set up your API key yet.

If you don't have an API key yet, visit:

https://platform.openai.com/signup

1. Make an account or sign in
2. Click "View API Keys" from the top right menu.
3. Click "Create new secret key"

Then, open the Secrets Tool and add OPENAI_API_KEY as a secret.
""")
exit(1)

response = openai.chat.completions.create(
model=“gpt-3.5-turbo”,
messages=[{
“role”: “system”,
“content”: “You are a helpful assistant.”
}, {
“role”: “user”,
“content”: “Who won the world series in 2020?”
}, {
“role”:
“assistant”,
“content”:
“The Los Angeles Dodgers won the World Series in 2020.”
}, {
“role”: “user”,
“content”: “question”
}])

output = response.choices[0].message.content
print(“Ans : output,”"")
question=input(“Q : what is your next question / instruction? \n”)

Hi there @kritikasehrawat296
The issue with your code is that you are trying to use the ‘openai’ library, but you have not imported it. You need to add ‘from openai import GPT3’ at the beginning of your code.

Here is the corrected code:

from openai import GPT3
import os
import sys

question = input("Q: what is your question / instruction? \n")

while True:
    openai.api_key = os.environ.get('OPENAI_API_KEY')
    if openai.api_key == "":
        sys.stderr.write("""
You haven't set up your API key yet.

If you don't have an API key yet, visit:

https://platform.openai.com/signup

1. Make an account or sign in
2. Click "View API Keys" from the top right menu.
3. Click "Create new secret key"

Then, open the Secrets Tool and add OPENAI_API_KEY as a secret.
""")
        exit(1)
    response = GPT3.completions.create(
        model="gpt-3.5-turbo",
        messages=[{
            "role": "system",
            "content": "You are a helpful assistant."
        }, {
            "role": "user",
            "content": "Who won the World Series in 2020?"
        }, {
            "role": "assistant",
            "content": "The Los Angeles Dodgers won the World Series in 2020."
        }, {
            "role": "user",
            "content": question
        }])

    output = response.choices[0].message.content
    print("Ans:", output)
    question = input("Q: what is your next question / instruction? \n")

Hopefully, this works :slight_smile:

Regards,
@chai


I copied your code but it is showing this i.e. error

Hi there @kritikasehrawat296
Maybe try this code:

import os
import sys
from openai.api_resources import Completion

question = input("Q: what is your question / instruction? \n")

while True:
    openai_api_key = os.environ.get('OPENAI_API_KEY')
    if openai_api_key == "":
        sys.stderr.write("""
You haven't set up your API key yet.

If you don't have an API key yet, visit:

https://platform.openai.com/signup

1. Make an account or sign in
2. Click "View API Keys" from the top right menu.
3. Click "Create new secret key"

Then, open the Secrets Tool and add OPENAI_API_KEY as a secret.
""")
        exit(1)
    response = Completion.create(
        model="gpt-3.5-turbo",
        messages=[{
            "role": "system",
            "content": "You are a helpful assistant."
        }, {
            "role": "user",
            "content": "Who won the World Series in 2020?"
        }, {
            "role": "assistant",
            "content": "The Los Angeles Dodgers won the World Series in 2020."
        }, {
            "role": "user",
            "content": question
        }],
        api_key=openai_api_key
    )

    output = response.choices[0].message.content
    print("Ans:", output)
    question = input("Q: what is your next question / instruction? \n")

Tell me if this works or not

Regards,
@chai

1 Like

Hi @kritikasehrawat296

I have rectified the code you sent and made necessary changes. You code logic was correct however there were some minor indentation errors. I have also attached a screen shot of the output for your reference.

Do try this and let us know. If you still face any issues, please feel free to get back to us.

import openai
import os
import sys

while True:
    question = input("Q: What is your question/instruction?\n")

    openai.api_key = os.environ['OPENAI_API_KEY']
    if openai.api_key == "":
        sys.stderr.write("""
        You haven't set up your API key yet.

        If you don't have an API key yet, visit:

        https://platform.openai.com/signup

        1. Make an account or sign in
        2. Click "View API Keys" from the top right menu.
        3. Click "Create new secret key"

        Then, open the Secrets Tool and add OPENAI_API_KEY as a secret.
        """)
        exit(1)

    response = openai.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[{
            "role": "system",
            "content": "You are a helpful assistant."
        }, {
            "role": "user",
            "content": question
        }]
    )

    output = response.choices[0].message.content
    print(f"Ans: {output}")

    next_question = input("Q: What is your next question/instruction?\n")

Output: