openai-d ~main

OpenAI Client APIs


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

OpenAI API for D

main CI codecov Latest Release DUB License: MIT GitHub stars

This library provides unofficial D clients for OpenAI API.

Overview

Key capabilities are summarized below.

Installation

Once dmd and dub are available, add this library to your project:

dub add openai-d

Quick Start

import openai;

auto client = new OpenAIClient();
auto res = client.chatCompletion(
    chatCompletionRequest(openai.GPT4O, [userChatMessage("Hello!")]));
writeln(res.choices[0].message.content);

See the examples directory for full sample programs.

Features

Endpoint

  • [x] OpenAI
  • [x] Azure OpenAI Service

API

OpenAI

Legacy

  • [x] Completions (Legacy)

Deprecated

  • Edits
  • Use chat API instead. See: https://platform.openai.com/docs/deprecations/edit-models-endpoint

Optimization

  • [ ] Switch HTTP client to requests (TODO)
    • Not adopted because it is less convenient due to Windows' handling of system certificates. Version flag is required for support.
    • Adopting 'requests' is expected to lead to more efficient use of Fiber in vibe.d.

Completion (Legacy)

import std;
import openai;

// Load API key from environment variable
auto client = new OpenAIClient();

// POST /completions
auto message = completionRequest("gpt-3.5-turbo-instruct", "Hello, D Programming Language!\n", 10, 0);
message.stop = "\n";

auto response = client.completion(message);

writeln(response.choices[0].text.chomp());

Embedding

import std;
import openai;

// Load API key from environment variable
auto client = new OpenAIClient();

// POST /embeddings
const request = embeddingRequest("text-embedding-3-small", "Hello, D Programming Language!");
auto response = client.embedding(request);

float[] embedding = response.data[0].embedding;
writeln(embedding.length); // text-embedding-3-small -> 1536

Files

import std.stdio;
import std.file : write;
import openai;

auto client = new OpenAIClient();
auto up = uploadFileRequest("input.jsonl", FilePurpose.FineTune);
auto uploaded = client.uploadFile(up);

auto retrieved = client.retrieveFile(uploaded.id);
writeln("retrieved: ", retrieved.filename);

auto content = client.downloadFileContent(uploaded.id);
write("copy.jsonl", content);

client.deleteFile(uploaded.id);

See examples/files for the full example.

Moderation

import std;
import openai;

// Load API key from environment variable
auto client = new OpenAIClient();

// POST /moderations
const request = moderationRequest("D is a general-purpose programming language with static typing, systems-level access, and C-like syntax. With the D Programming Language, write fast, read fast, and run fast.");
auto response = client.moderation(request);

if (response.results[0].flagged)
    writeln("Warning!");
else
    writeln("Probably safe.");

Transcription

import std;
import openai;

// Load API key from environment variable
auto client = new OpenAIClient();

// POST /audio/transcriptions
auto request = transcriptionRequest("audio.mp3", "whisper-1");
auto response = client.transcription(request);

writeln(response.text);

See examples/audio_transcription for the full example.

Translation

import std;
import openai;

// Load API key from environment variable
auto client = new OpenAIClient();

// POST /audio/translations
auto request = translationRequest("audio.mp3", "whisper-1");
auto response = client.translation(request);

writeln(response.text);

See examples/audio_translation for the full example.

Images

import std;
import openai;

// Load API key from environment variable
auto client = new OpenAIClient();

// POST /images/generations
auto request = imageGenerationRequest("A cute baby sea otter");
request.responseFormat = ImageResponseFormatB64Json;
auto response = client.imageGeneration(request);
write("image.png", Base64.decode(response.data[0].b64Json));

See examples/images for the full example.

Administration

import std;
import openai;

auto client = new OpenAIAdminClient();
auto list = client.listProjects(listProjectsRequest(20));
writeln(list.data.length);

Requires an admin API key. See examples/administration* for complete examples.

OpenAIClientConfig

Environment variables

import std.process;
import openai;

environment["OPENAI_API_KEY"] = "<Your API Key>";
environment["OPENAI_ORGANIZATION"] = "<Your Organization>";
environment["OPENAI_API_BASE"] = "https://example.api.cognitive.microsoft.com"; // optional
environment["OPENAI_DEPLOYMENT_ID"] = "<Your deployment>"; // Azure only
environment["OPENAI_API_VERSION"] = "2024-10-21"; // Azure only

auto config = OpenAIClientConfig.fromEnvironment();

assert(config.apiKey == "<Your API Key>");
assert(config.organization == "<Your Organization>");
assert(config.apiBase == "https://example.api.cognitive.microsoft.com");

Development

Requirements

dmd and dub must be installed. Use the official D installer.

License

This project is released under the MIT license.

Authors:
  • lempiji
Dependencies:
mir-ion
Versions:
0.9.0 2025-Jun-29
0.8.0 2025-Jun-08
0.7.0 2025-Feb-28
0.6.0 2025-Jan-19
0.5.0 2024-Aug-13
Show all 11 versions
Download Stats:
  • 0 downloads today

  • 9 downloads this week

  • 9 downloads this month

  • 131 downloads total

Score:
1.5
Short URL:
openai-d.dub.pm