7.7.11. OPENAI-11 — Legacy Completions

This tutorial covers the older (pre-chat) text-completion endpoint. Prefer chat (OPENAI-01 — Your First Chat) for new code; /completions exists for older models and OpenAI-compatible backends that only expose it. As with every dasOPENAI script, the consumer root needs options rtti.

7.7.11.1. The completions() Call

completions takes a CompletionRequest and returns a CompletionResult. On success, response.choices holds the generated text and finish_reason, and response.usage the token accounting:

require openai/openai_completions

let req = CompletionRequest(model = "gpt-3.5-turbo-instruct",
    prompt = "The capital of France is", max_tokens = 16)
let res = completions(client, req)
if (res.ok) {
    print("{res.response.choices[0].text}\n")
}

7.7.11.2. The complete() Wrapper

complete is the one-liner: prompt in, text out (empty string on error):

let text = complete(client, "gpt-3.5-turbo-instruct", "Once upon a time")

See also

Full source: tutorials/dasOPENAI/11_completions.das

Previous tutorial: OPENAI-10 — Moderations