Create Python Flask API For OpenAI’s Chat Models

Hello Learners…

Welcome to the blog…

Topic: Create Python Flask API For OpenAI’s Chat Models

Table Of Contents

  • Introduction
  • Create Python Flask API For OpenAI’s Chat Models
  • Chat Completion Simple Flask API Using OpeanAI’s gpt-3.5-turbo Model
  • Summary
  • References

Introduction

In this post, we learn How To Create Flask API For OpenAI’s Chat Models. right now everyone is using OpenAI’s API for their business use cases and growing their businesses using OpenAI API.

There are many different APIs provided by OpenAI, here we see APIs for the Chat.

Create Python Flask API For OpenAI’s Chat Models

Here we are going to create a Flask API For OpenaAI’s Chat Models, In Chat Models there is a Chat Completion Function That we are using.

First, we set up the environment and installed the required libraries.

pip install openai

pip install python-dotenv

Here we are using OpenAI’s paid API key, you can buy it from the below URL,

After that we have to set the API key in the .env file, OpeanAI recommends it.

Simple Flask API Of Chat Completion Using OpeanAI’s gpt-3.5-turbo Model

Now we create a chat_completion.py file and paste the below code into this file.

import os
import openai

from dotenv import load_dotenv,find_dotenv
__ = load_dotenv(find_dotenv()) #read local .env file
openai.api_key=os.environ["OPENAI_API_KEY"]

from flask import Flask

app=Flask(__name__)

@app.route("/")
def chat_completion():
    completion = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
    ]
    )

    result=completion.choices[0].message
    print(result)
    return result

if __name__=='__main__':
    app.run()

Run the chat_completion.py file,

python3 chat_completion.py

In this above code, we already give the question “Hello!” as a user and we get the response to this question context. you can change the user content based on your requirements.

When we run the above file we get an URL http://127.0.0.1:5000/

Now open the URL in the browser and we can see the response of this Flask API using OpenAI’s Chat Completion Model.

#Output 

{
  "role": "assistant",
  "content": "Hello! How may I assist you today?"
}

Also, we can see the output in our terminal because we use the print function in our code.

How To Create Flask API Of OpenAI's Chat Models

The above API Call (chat_completion.py) Charges $0.00005 Approx.

Send User’s Input To The Flask API Of Chat Completion With OpeanAI’s gpt-3.5-turbo Model

Here we do some changes in the above chat_completion.py code which takes the user’s input and based on that it will generates the answer.

import os
import openai

from dotenv import load_dotenv,find_dotenv
__ = load_dotenv(find_dotenv()) #read local .env file
openai.api_key=os.environ["OPENAI_API_KEY"]

from flask import Flask

app=Flask(__name__)

@app.route("/")
def chat_completion(user_text):
    completion = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": user_text}
    ]
    )
    result=completion.choices[0].message['content']
    print("Answer>>>>>>",result)
    return result

if __name__=='__main__':
    user_text=input(str("Enter your text question >>> "))
    chat_completion(user_text)
    app.run()

now run the chat_completion.py file and it will ask for your question. here we ask “What is ml” and you can see the response below.

#Input In Terminal 
Enter your text question >>> what is ml

#Output In Terminal 

Answer>>>>>> "ML" stands for machine learning. It is a field of computer science and artificial intelligence that focuses on developing algorithms and statistical models that allow computer systems to improve their performance on a specific task through experience or training data, without being explicitly programmed. Machine learning has numerous applications, such as natural language processing, image recognition, predictive analytics, and recommendation systems.

The above API Call (chat_completion.py) Charges $0.000299 Approx (Including Some mistakes).

Summary

So these are the ways we can use OpenAI’s Chat Completions Models with gpt-3.5-turbo. you can play with this and try to get the best according to your requirements and create a Flask API of your requirements.

References

Leave a Comment