openai-d

所属分类:编程语言基础
开发工具:D
文件大小:0KB
下载次数:0
上传日期:2023-10-01 10:19:25
上 传 者sh-1993
说明:  D编程语言中的OpenAI API客户端,
(OpenAI API Client in D Programming Language,)

文件列表:
LICENSE (1055, 2023-10-28)
dub.json (156, 2023-10-28)
examples/ (0, 2023-10-28)
examples/chat/ (0, 2023-10-28)
examples/chat/config.json (52, 2023-10-28)
examples/chat/dub.sdl (118, 2023-10-28)
examples/chat/dub.selections.json (191, 2023-10-28)
examples/chat/source/ (0, 2023-10-28)
examples/chat/source/app.d (608, 2023-10-28)
examples/chat_db/ (0, 2023-10-28)
examples/chat_db/config.json (52, 2023-10-28)
examples/chat_db/dub.sdl (207, 2023-10-28)
examples/chat_db/source/ (0, 2023-10-28)
examples/chat_db/source/app.d (4325, 2023-10-28)
examples/chat_functions/ (0, 2023-10-28)
examples/chat_functions/config.json (52, 2023-10-28)
examples/chat_functions/dub.sdl (128, 2023-10-28)
examples/chat_functions/dub.selections.json (191, 2023-10-28)
examples/chat_functions/source/ (0, 2023-10-28)
examples/chat_functions/source/app.d (3246, 2023-10-28)
examples/completion/ (0, 2023-10-28)
examples/completion/config.json (52, 2023-10-28)
examples/completion/dub.sdl (124, 2023-10-28)
examples/completion/dub.selections.json (191, 2023-10-28)
examples/completion/source/ (0, 2023-10-28)
examples/completion/source/app.d (568, 2023-10-28)
examples/embedding/ (0, 2023-10-28)
... ...

# OpenAI API for D [![main CI](https://github.com/lempiji/openai-d/actions/workflows/main-ci.yaml/badge.svg)](https://github.com/lempiji/openai-d/actions/workflows/main-ci.yaml) This library provides unofficial D clients for [OpenAI API](https://platform.openai.com) ## Features ### Endpoint - [x] OpenAI - [ ] Azure OpenAI Service ### API #### OpenAI - [x] List models - [x] Completions - [x] Chat - [x] function_call - [ ] Images - [x] Embeddings - [ ] Audio - [ ] Files - [ ] Fine-tunes - [x] Moderations __deprecated__ - Edits - Use chat API instead. See: https://platform.openai.com/docs/deprecations/edit-models-endpoint ### 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__ ```d name=completion 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()); ``` __Chat__ ```d name=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__ ```d name=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__ ```d name=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__ ```d name=config_env import std.process; import openai; environment["OPENAI_API_KEY"] = ""; environment["OPENAI_ORGANIZATION"] = ""; auto config = OpenAIClientConfig.fromEnvironment(); assert(config.apiKey == ""); assert(config.organization == ""); ``` __Configuration file__ ```d name=config_file import std.file; import openai; write("config.json", `{"apiKey": "", "organization": null}`); scope (exit) remove("config.json"); auto config = OpenAIClientConfig.fromFile("config.json"); assert(config.apiKey == ""); assert(config.organization is null); ```

近期下载者

相关文件


收藏者