Calling OpenAI APIs from code

Calling OpenAI APIs from code

Steps

a. Get openAI API Key

openaAI API Key

b. Call openai APIs using Postman

https://web.postman.co/workspace

Place API_KEY got in step-1 in Authorization Header


GET https://api.openai.com/v1/models
Header
    Authorization: Bearer API_KEY

POST https://api.openai.com/v1/chat/completions
Header
    Authorization: Bearer API_KEY
Body
    {
        "model": "gpt-3.5-turbo",
        "messages": [{"role": "user", "content": "What is capital of France?"}],
        "temperature": 0.7
    }            

c. Calling APIs using Code

Create a Virtual Environment (Windows)

//Install python from Microsoft Store
> python
Python 3.13.2 (tags/v3.13.2:4f8bb39, Feb  4 2025, 15:23:48) [MSC v.1942 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> ^Z

// Create venv
> python -m venv ./python_venv
>

// Activate venv
> cd python_venv
> Scripts\activate
(python_venv) path\python_venv>        

Install openai inside Virtual Env

// Install openai
(python_venv) c:\go-here\Code\python\virtual_avatar> pip install openai

//Set API Key in User environment variable
OPENAI_API_KEY = value        

Call openai API from Code

from openai import OpenAI

def get_chatgpt_response():
    client = OpenAI()
    completion = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system", "content": 
                "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."},
            {"role": "user", "content": 
                "Compose a poem that explains the concept of recursion in programming."}
        ]
    )

OR

def get_chatgpt_response():
    client = OpenAI()
    completion = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[
            {
                "role": "user",
                "content": "What's Capital of Russia?"
            }
        ]
    )
    print(completion.choices[0].message)        //Moscow

get_chatgpt_response()        
Vinay Singh

Senior QA Engineer at Mtree Software Pvt Ltd, Noida

2 周

Bas bhai..time aacha nahi chal raha..apna number bhej

回复
Vinay Singh

Senior QA Engineer at Mtree Software Pvt Ltd, Noida

2 周

Great bhai

要查看或添加评论,请登录

Amit K.的更多文章

  • Pyunit or Unitest

    Pyunit or Unitest

    Used to test a unit of source code Features: 1. PyUnit is very simple yet very flexible 2.

  • FlatList with Example in React Native

    FlatList with Example in React Native

    What is FlatList? displays a scrolling list of changing, but similarly structured, data. Unlike the more generic…

  • Create Postgres Database, Tables, Schema using Diesel(ORM) Rust

    Create Postgres Database, Tables, Schema using Diesel(ORM) Rust

    What is Diesel Diesel is a ORM(object-relational mapping). ORM is programming technique that connects object-oriented…

  • Location Sharing App System Design (Bump)

    Location Sharing App System Design (Bump)

    What is Bump Bump is location sharing Mobile App. Install bump on 2 phones(add as friends).

  • Load Balancers & Caches

    Load Balancers & Caches

    What is Load Balancer? Load Balancer evenly distributes incoming traffic/load among webservers/workers that are defined…

  • REST API / Representation State Transfer

    REST API / Representation State Transfer

    Restful Web Server/Application? Web application that implements HTTP CRUD methods in Restful way. Eg: Twitter, facebook…

  • Inter Thread Communication in Rust using Channels

    Inter Thread Communication in Rust using Channels

    What is Channel? Sender and Receiver are connected via Channel. They can send/recv data via channel.

  • Slices in Rust

    Slices in Rust

    What is Slice Slice is somepart of larger data, it always borrow data(Hence RO) from the sliced type. It gives you a…

  • Traits in Rust

    Traits in Rust

    What is Triat in Rust Interface/class in Rust(declared with keyword trait) having Virtual Functions(not pure Virtual)…

  • Facebook News Feed High Level System Design

    Facebook News Feed High Level System Design

    HLD Json Web Token / oauth_token / auth_token based authentication to Facebook GET Newsfeed What we will do? We will…

其他会员也浏览了