Saturday, May 18, 2024

OpenAI API: from bash, PowerShell, JS, Python, C#, Go, TypeScript

example of making simple OpenAI API (Chat) GPT request with various prog. languages

to run examples must have env var set: OPENAI_API_KEY
as declared in first line of shell scripts examples

Examples:

API Reference - OpenAI API

OpenAI provides "official" (SDK) libs/modules for Python and JavaScript
There are some additional community and Microsoft supported packages.

Libraries - OpenAI API

bash shell, REST

Unix: Linux / Mac / Windows WSL2

> export OPENAI_API_KEY="your_api_key_here"

this is calling REST API endpoint directly

curl https://api.openai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      {
        "role": "system",
        "content": "You are a helpful assistant."
      },
      {
        "role": "user",
        "content": "How tall is Acadia?"
      }
    ]
  }'

> ./openai1.sh
{
  "id": "chatcmpl-9QRuW3j2l9COgyf7oovTxeScMc7RN",
  "object": "chat.completion",
  "created": 1716090844,
  "model": "gpt-4o-2024-05-13",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Acadia National Park, located in Maine, doesn't have one specific height since it encompasses a variety of landscapes including mountains, forests, and coastline. However, the tallest peak in the park is Cadillac Mountain, which is 1,530 feet (466 meters) tall. Cadillac Mountain is also notable for being the highest point along the North Atlantic seaboard and one of the first places in the United States to see the sunrise, particularly from October through early March."
      },
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 23,
    "completion_tokens": 93,
    "total_tokens": 116
  },
  "system_fingerprint": "fp_729ea513f7"
}

PowerShell, REST

Windows default shell

> $env:OPENAI_API_KEY = "your_api_key_here"

Observe backtick (`) on the end of line that continues on next line

$headers = @{
    "Content-Type" = "application/json"
    "Authorization" = "Bearer $env:OPENAI_API_KEY"
}

$body = @{
    "model" = "gpt-3.5-turbo"
    "messages" = @( @{
        "role" = "user"
        "content" = "How tall is Mount Everest, in meters, feet and miles?"
    } )
    "temperature" = 0.7
} | ConvertTo-Json

$response = Invoke-WebRequest -Uri "https://api.openai.com/v1/chat/completions" `
  -Method Post -Body $body -Headers $headers

$response.Content | Out-String -Width 4096

> .\openai1.ps1

{
  "id": "chatcmpl-9QRZh4zGi8LvepxDE0xNYdlmCVcM4",
  "object": "chat.completion",
  "created": 1716089553,
  "model": "gpt-3.5-turbo-0125",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Mount Everest is approximately 8,848 meters (29,029 feet) tall. In miles, it is approximately 5.5 miles high."
      },
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 20,
    "completion_tokens": 30,
    "total_tokens": 50
  },
  "system_fingerprint": null
}

JavaScript, with official lib / npm module

to run, first install required module with

> npm init -y
> npm install -g openai

import OpenAI from "openai";

const openai = new OpenAI();

async function main() {
  const completion = await openai.chat.completions.create({
    messages: [
      { role: "system", content: "You are a helpful assistant." },
      {
        role: 'user',
        content: 'How tall is Mont Blanc?'
      },
    ],
    model: "gpt-3.5-turbo",
  });

  console.log(completion.choices[0]);
}

main();

> node openai1.js

{
  index: 0,
  message: {
    role: 'assistant',
    content: 'Mont Blanc is 4,808 meters (15,774 feet) tall.'
  },
  logprobs: null,
  finish_reason: 'stop'
}

Python, with official package/ lib

first install required package with

> pip install openai

from openai import OpenAI
client = OpenAI()

completion = client.chat.completions.create(
  model="gpt-4o",
  messages=[
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "How tall is Mount Atlas!"}
  ]
)

print(completion.choices[0].message)

> python openai1.py

ChatCompletionMessage(content='Mount Atlas is not a specific mountain; rather, it is a mountain range known as the Atlas Mountains. The Atlas Mountains stretch across several countries in North Africa, including Morocco, Algeria, and Tunisia. The highest peak in the Atlas Mountains is Toubkal, which is located in southwestern Morocco. Mount Toubkal stands at 4,167 meters (13,671 feet) above sea level.', role='assistant', function_call=None, tool_calls=None)

DotNet: C#, REST

there is no "official" DotNet OpenAI SDK

here is an example using calling OpenAPI REST API call directly

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        string apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");

        using (HttpClient client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");

            var content = new StringContent(
                @"{
                    ""model"": ""gpt-4o"",
                    ""messages"": [
                        {
                            ""role"": ""system"",
                            ""content"": ""You are a helpful assistant.""
                        },
                        {
                            ""role"": ""user"",
                            ""content"": ""How tall are Appalachian Mountains""
                        }
                    ]
                }", Encoding.UTF8, "application/json");

            var response = await client.PostAsync("https://api.openai.com/v1/chat/completions", content);
            var responseString = await response.Content.ReadAsStringAsync();

            Console.WriteLine(responseString);
        }
    }
}

requires min. project file, i.e. openai.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
  </PropertyGroup>
</Project>

> dotnet run .\gpt1.cs
{
  "id": "chatcmpl-9QSCeSOzRawUxKRm8rqYmsQLVuJZF",
  "object": "chat.completion",
  "created": 1716091968,
  "model": "gpt-4o-2024-05-13",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "The Appalachian Mountains vary in height along their length. The highest peak in the Appalachian range is Mount Mitchell, located in North Carolina, which stands at 6,684 feet (2,037 meters) above sea level. This mountain is the highest peak in the eastern United States. Other notable peaks in the range also reach substantial heights, typically between 3,000 to 6,000 feet (910 to 1,830 meters). The terrain generally decreases in elevation as the mountains extend northward towards Canada."   
      },
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 22,
    "completion_tokens": 103,
    "total_tokens": 125
  },
  "system_fingerprint": "fp_729ea513f7"
}

C#, dotnet script CLI, REST

the same can be done even simpler, from a single file by using dotnet script tool


using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

string apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");

using (HttpClient client = new HttpClient())
{
    client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");

    var content = new StringContent(
@"{
    ""model"": ""gpt-4o"",
    ""messages"": [
        {
            ""role"": ""system"",
            ""content"": ""You are a helpful assistant.""
        },
        {
            ""role"": ""user"",
            ""content"": ""How tall is Denali?""
        }
    ]
}", Encoding.UTF8, "application/json");

    var response = await client.PostAsync("https://api.openai.com/v1/chat/completions", content);
    var responseString = await response.Content.ReadAsStringAsync();

    Console.WriteLine(responseString);
}

dotnet tool install -g dotnet-script
dotnet script .\openai-cli.cs

{
  "id": "chatcmpl-9Qbnz6u6qLRheyf7qSIkd3k80JRLZ",
  "object": "chat.completion",
  "created": 1716128879,
  "model": "gpt-4o-2024-05-13",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Denali, also known as Mount McKinley, is the tallest peak in North America, standing at approximately 20,310 feet (6,190 meters) above sea level."      
      },
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 23,
    "completion_tokens": 37,
    "total_tokens": 60
  },
  "system_fingerprint": "fp_729ea513f7"
}

Go Lang, REST

Example calling REST API directly

package main

import (
    "bytes"
    "fmt"
    "io"
    "net/http"
    "os"
)

func main() {
    apiKey := os.Getenv("OPENAI_API_KEY")

    client := &http.Client{}

    data := []byte(`{
        "model": "gpt-4o",
        "messages": [
            {
                "role": "system",
                "content": "You are a helpful assistant."
            },
            {
                "role": "user",
                "content": "How tall is Mount Washington?"
            }
        ]
    }`)

    req, _ := http.NewRequest("POST", "https://api.openai.com/v1/chat/completions", bytes.NewBuffer(data))
    req.Header.Set("Authorization", "Bearer "+apiKey)
    req.Header.Set("Content-Type", "application/json")

    resp, _ := client.Do(req)
    defer resp.Body.Close()

    body, _ := io.ReadAll(resp.Body)

    fmt.Println(string(body))
}


> go run .\openai1.go

{
  "id": "chatcmpl-9QSOwP8eg2db6uwQIDZWMa0Xv7Hw2",
  "object": "chat.completion",
  "created": 1716092730,
  "model": "gpt-4o-2024-05-13",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Mount Washington, located in New Hampshire, is the highest peak in the Northeastern United States. It stands at 6,288 feet (1,917 meters) above sea level."
      },
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 23,
    "completion_tokens": 37,
    "total_tokens": 60
  },
  "system_fingerprint": "fp_729ea513f7"
}

TypeScript, REST

calling REST API directly

async function main() {
    const apiKey = process.env.OPENAI_API_KEY;

    // @ts-ignore // fetch() is natively supported in Node.js 18 and newer, no need for node-fetch  
    const response = await fetch('https://api.openai.com/v1/chat/completions', {
        method: 'POST',
        headers: {
            'Authorization': `Bearer ${apiKey}`,
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            model: 'gpt-4o',
            messages: [
                {
                    role: 'system',
                    content: 'You are a helpful assistant.'
                },
                {
                    role: 'user',
                    content: 'How tall is Grand Teton Mountain?'
                }
            ]
        })
    });
    const responseString = await response.text();
    console.log(responseString);
}

main();


to configure ts complier (tsc, tsnode) to support native fetch() function need tsconfig.json like this
{
    "include": ["**/*.ts"],
    "compilerOptions": {
        "target": "ES2020", // to support "native" fetch() in node.js
        "module": "commonjs",
        "lib": ["ES2020"],
    },
    "ts-node": {
        "compilerOptions": {
            "esModuleInterop": true
        },
        "transpileOnly": true,
        "files": true
    },
}

to run

> npm i -g typescript
> npm i -g ts-node
> ts-node openai1.ts

{
  "id": "chatcmpl-9Qaxo1brTFVc4wQMAVtxNfkP9RHVu",
  "object": "chat.completion",
  "created": 1716125644,
  "model": "gpt-4o-2024-05-13",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Grand Teton Mountain, located in the Teton Range of Wyoming, stands at an elevation of 13,775 feet (4,199 meters) above sea level. It is the second-highest peak in Wyoming, after Gannett Peak."
      },
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 25,
    "completion_tokens": 50,
    "total_tokens": 75
  },
  "system_fingerprint": "fp_729ea513f7"
}