openai-d 0.1.1

OpenAI Clinet 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

This library provides unofficial D clients for OpenAI API

Features

Endpoint

  • [x] OpenAI
  • [ ] Azure OpenAI Service

API

OpenAI
  • [x] List models
  • [x] Completions
  • [x] Chat
  • [ ] Edits
  • [ ] Images
  • [x] Embeddings
  • [ ] Audio
  • [ ] Files
  • [ ] Fine-tunes
  • [x] Moderations

Optimization

  • [ ] Switch HTTP client to 'requests'
    • 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.

Usage

Installation

dub add openai-d

OpenAIClient

Completion

import std;
import openai;

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

// POST /completions
auto message = completionRequest("text-davinci-003", "Hello, D Programming Language!\n", 10, 0);
message.stop = "\n";

auto response = client.completion(message);

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

Chat

import std;
import openai;

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

// POST /chat/completions
const request = chatCompletionRequest("gpt-3.5-turbo", [
    systemChatMessage("You are a helpful assistant."),
    userChatMessage("Hello!")
], 16, 0);

auto response = client.chatCompletion(request);

writeln(response.choices[0].message.content);

Embedding

import std;
import openai;

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

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

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

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.");

OpenAIClientConfig

Environment variables

import std.process;
import openai;

environment["OPENAI_API_KEY"] = "<Your API Key>";
environment["OPENAI_ORGANIZATION"] = "<Your Organization>";

auto config = OpenAIClientConfig.fromEnvironment();

assert(config.apiKey == "<Your API Key>");
assert(config.organization == "<Your Organization>");

Configuration file

import std.file;
import openai;

write("config.json", `{"apiKey": "<Your API Key>", "organization": null}`);
scope (exit) remove("config.json");

auto config = OpenAIClientConfig.fromFile("config.json");

assert(config.apiKey == "<Your API Key>");
assert(config.organization is null);
Authors:
  • lempiji
Dependencies:
mir-ion
Versions:
0.5.0 2024-Aug-13
0.4.0 2024-Feb-03
0.3.0 2023-Oct-28
0.2.0 2023-Jun-17
0.1.1 2023-Jun-03
Show all 7 versions
Download Stats:
  • 0 downloads today

  • 0 downloads this week

  • 0 downloads this month

  • 27 downloads total

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