Let us dive into Backend of OpenAI API First
To get API key, Pl. refer
https://platform.openai.com/api-keys
https://ametodl.blogspot.com/p/creating-chatbot-using-openai.html
Once you get the key, keep in safe place.
In Visual Studio, create folder, create usecase1.py file and write the following code.
In terminal
pip install openai.
In the following file usecase1.py,
change openai.api.key = "copy the above key with in quotes" as shown below:
import os
import openai
openai.api_key = "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"
completion = openai.ChatCompletion.create(
model = "gpt-3.5-turbo",
messages =[
{"role" : "system", "content" : "You are intelligent Assistant"},
{"role" : "user", "content" : "Write a slogan for Annamalai University" }
], #temperature = 2
#max_tokens = 5,
#max_tokens = 50,
temperature = 0
)
print(completion.choices[0].message)
Run to get similar response as shown below:
{
"role": "assistant",
"content": "\"Empowering Minds, Enriching Futures: Annamalai University\""
}
-----------------------------------------------------------------------------------
Let us dive into frontend of OpenAI.API Next
First we will modify the above as below for backend: as usecase2.py in the same project folder
import os
import openai
openai.api_key = "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"
def get_response(query):
completion = openai.ChatCompletion.create(
model = "gpt-3.5-turbo",
messages =[
{"role" : "system", "content" : "You are intelligent Assistant"},
{"role" : "user", "content" : query }
],
#max_tokens = 5,
#max_tokens = 50,
temperature = 0
#temperature = 2
)
return(completion.choices[0].message["content"])
For this in terminal,
pip install streamlit
open a new file app.py and type the below lines
import streamlit as st
st.title("AUCSE ChatBot")
To check, pl run in terminal prompt streamlit run app.py
Welcome to Streamlit!
If you’d like to receive helpful onboarding emails, news, offers, promotions,
and the occasional swag, please enter your email address below. Otherwise,
leave this field blank.
Email:
Again modify app.py as shown below:
import streamlit as st
st.title("AUCSE ChatBot")
# Welcome to Streamlit!
# If you’d like to receive helpful onboarding emails, news, offers, promotions,
# and the occasional swag, please enter your email address below. Otherwise,
# leave this field blank.
# Email:
# to test fronend in browser
# with st.form("AUCHATFORM"):
# query=st.text_area("Query:", "Ask any Question? ")
# submit = st.form_submit_button("Submit")
# if submit:
# st.info("hello cse")
#to connect backend
from usecase2 import get_response
with st.form("AUCHATFORM"):
query=st.text_area("Query:", "Ask any Question? ")
submit = st.form_submit_button("Submit")
if submit:
response = get_response(query)
st.info(response)
In terminal stremalit run app.py.
Browser will open and ask for input.
In text area: type Tell me a joke and hit submit button.
You will see the response shown below:
Wow!!! We have just done our own chatbot with open api with backend and front end.
What is needed excactly is give here.
Try with different query write a poem on computer, You may get similar response as shown below:
You can beautify your frontend very blue cool as shown by going through stramlit documentation: https://docs.streamlit.io/library/api-reference
For models Pl Refer https://platform.openai.com/docs/models.
For Prompts Pl. Refer https://platform.openai.com/docs/guides/prompt-engineering.
For endpoints Pl. Refer https://platform.openai.com/docs/guides/text-generation/chat-completions-vs-completions
Happy ChatBot developing with Python!!!
No comments:
Post a Comment