Skip to content

pamnard/OpenAIApp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

OpenAIApp

OpenAI API wrapper for Google Apps Scripts

You can use OpenAIApp.gs as a part of your project.

All parameters names are the same as in the official documentation - https://platform.openai.com/docs/api-reference/introduction. Wrap the method parameters in a js-object and pass it to the method as only one parameter.

Examples

ChatGPT

var api_key = '**************************************************';
var openai = new OpenAIApp(api_key);
var result = openai.Chat().CreateChatCompletion({
    model: 'gpt-3.5-turbo',
    messages: [{
        role: 'user',
        content: 'Tell me a joke'
    }]
});
Logger.log(result.choices[0].message.content.replace(/^\n\n/, ''));

2023-03-05_15-07

To save the context, you can use caching in the script:

function cache_context(question) {
    var cache = CacheService.getScriptCache();
    var cache_str = cache.get('cache');
    var messages = [];
    if (cache_str == null) {
        messages = [{
            "role": "user",
            "content": question
        }];
    } else {
        messages = JSON.parse(cache_str);
        messages[messages.length] = {
            "role": "user",
            "content": question
        };
    }
    
    var answer = openai.Chat().CreateChatCompletion({
        model: 'gpt-3.5-turbo',
        messages: messages
    }).choices[0].message.content.replace(/^\n\n/, '');
    
    messages[messages.length] = {
        "role": "assistant",
        "content": answer
    };
    
    while (JSON.stringify(messages).length > 100000) {
        var todelete = messages.shift();
    }
    
    cache.put('cache', JSON.stringify(messages), 300);
    return answer;
}

Create image

  var api_key = '**************************************************';
  var openai = new OpenAIApp(api_key);
  var result = openai.Images().CreateImage({
    prompt: 'Fat black cat'
  })
  Logger.log(result.data[0].url);

2023-03-05_15-08

Releases

No releases published

Packages

No packages published