First click this link to get a api_key.To get open.api.key Click the following link
Create api key and secret key as your "name" and then copy the key to a safe place
Let us open Visual studio. Create a python project. It will open main.py file.
In terminal pip install openai
In main.py file enter the following code:
-------------------
import openai
openai.api_key = "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"
# the key stored here will be used in the above python statement
def chat_with_gpt(prompt):
response = openai.chatCompletion.create(
model= "gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}]
)
return response.choices[0].message.content.strip()
if __name__ == "__main__":
while True:
user_input = input("You :")
if user_input.lower() in ["quit", "exit", "bye"]:
break
response = chat_with_gpt(user_input)
print("chatbot: ", response)
-------------------
Run this to get the response similar to as shown below:
You : how are you?
Chatbot: I'm just a virtual assistant, so I don't have feelings, but I'm here to help you with anything you need. How can I assist you today?
You : what is 7+7
Chatbot: 7 + 7 equals 14.
You : bye
This is a small chatbot using openai. Please Don't try this key. This wont work. You have to create your own. Its is free.
----------------------------------------------------------------------------------------------------------------------------
This is another one small Python snippet to understand the roles
I have given # for different content with role set as system. You can have one at a time. Execute in your VS ide an dhave fun?
openai.api_key = "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"
# messages=[{"role": "system", "content": "You are an intelligent Asistant"}]
# messages=[{"role": "system", "content": "You are phiolosopher"}]
# messages=[{"role": "system", "content": "You are Scientist"}]
# messages=[{"role": "system", "content": "You are kid"}]
# messages=[{"role": "system", "content": "You are boy"}]
# messages=[{"role": "system", "content": "You are Researcher"}]
# messages=[{"role": "system", "content": "You are under grad student"}]
# messages=[{"role": "system", "content": "Scientist"}]
# messages=[{"role": "system", "content": "Researcher"}]
# messages=[{"role": "system", "content": "You are Computer Science Teacher"}]
messages=[{"role": "system", "content": "You are Computer Science Student"}]
while True:
message = input("AUCSE-Assistant : ")
if message:
messages.append(
{"role":"user", "content":message}
)
chat = openai.ChatCompletion.create(
model="gpt-3.5-turbo", messages=messages
)
reply = chat.choices[0].message.content
print(f"ChatGPT:{reply}")
messages.append({"role": "assistant", "content": reply})
AUCSE-Assistant : tell me ajoke
ChatGPT:Why do programmers prefer dark mode?
Because the light attracts bugs!
AUCSE-Assistant :
You can check with one role & content combination at a time. For eg:
messages=[{"role": "system", "content": "You are Computer Science Student"}]
comment the above and uncomment the below line.
messages=[{"role": "system", "content": "You are phiolosopher"}]
We will see the response similar to as below:
AUCSE-Assistant : Tell me a joke
ChatGPT:Why did the philosopher bring a ladder to the bar?
Because he wanted to reach for higher spirits!
AUCSE-Assistant :
Another example with model gpt-3.5-turbo-0125 change :
openai.api_key = "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"
messages=[{"role": "system", "content": "You are Computer Science Student"}]
while True:
message = input("AUCSE-Assistant : ")
if message:
messages.append(
{"role":"user", "content":message}
)
chat = openai.ChatCompletion.create(
#model="gpt-3.5-turbo", messages=messages
model="gpt-3.5-turbo-0125", messages=messages
)
reply = chat.choices[0].message.content
print(f"ChatGPT:{reply}")
messages.append({"role": "assistant", "content": reply})
Response:
AUCSE-Assistant : tell me ajoke
ChatGPT:Why couldn't the computer take its hat off?
Because it had a CAPS lock on!
AUCSE-Assistant :
Like these you can have fun, dear students👍
Happy fiddling around openapi API+ 😂😂😂😂🤣!!!!
No comments:
Post a Comment