Source: site.view [edit]
Function name: llm
Arguments: prompt
Description: Call an LLM version with a prompt. OPTIONAL_ARGS: model (gpt-3.5-turbo, gpt-4, gpt-4-turbo-preview), temperature (0.7), max_tokens (777)
Page type: webl
Render function:  
Module: sandbox

Page source:

// key,version,prompt

var model = "gpt-3.5-turbo";  // gpt-3.5-turbo, gpt-4, gpt-4-turbo-preview
var temperature = 0.7;
var maxTokens = 777;

if Size(OPTIONALARGS) > 0 then
   model = Str_Trim(OPTIONALARGS[0])
end;

if Size(OPTIONALARGS) > 1 then
   temperature = ToReal(OPTIONALARGS[1])
end;

if Size(OPTIONALARGS) > 2 then
   maxTokens = ToInt(OPTIONALARGS[2])
end;

prompt = Wub_ReplaceAll(prompt, `"`, `'`);

var pJson = `{
  "model": "` + Str_Trim(model) + `",
  "messages": [
    {
        "role": "system",
        "content": "You are a helpful assistant designed to output JSON."
    },
    {
      "role": "user",
      "content": "` + prompt + `"
    }
  ],
  "temperature": ` + ToString(temperature) + `,
  "max_tokens": ` + ToString(maxTokens) + `
}`;

var P = PostURL("https://api.openai.com/v1/chat/completions", pJson, [. "Content-Type"="application/json", "Authorization" = "Bearer NOKEY" .], [. mimetype="text/plain" .]);

var response = WubCall("jsonToWebL", [Markup(P)]);


// choices = [[. "index" = 0, "message" = [. "role" = "assistant", "content" = "1. \"Coastal Escape: Discovering New England's Beaches and Islands\"\n2. \"Mountain Adventure: Exploring the White Mountains and Lakes Region\"\n3. \"City and Sea: A Bostonian's Guide to Summer Fun in the Northeast\"" .], "logprobs" = nil, "finish_reason" = "stop" .]]

var answer = response.choices[0].message.content ? "";

response["response"] := answer;

response;