Skip to content

toitware/toit-openai

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenAI

A OpenAI client for Toit.

Key

Get an API key from https://platform.openai.com/account/api-keys.

We recommend to provide the key through an environment variable on desktop machines and through a separate main-file on microcontrollers.

For example, you would have a file esp32.toit which imports the main.toit file and provides the API key.

Typically your esp32.toit file would look like this:

import .main as real_main

KEY ::= "YOUR API KEY"

main args:
  real_main.main args --key=KEY

Check this file in.

Locally, make a copy of this file (as esp32.toit) and insert your API key. Mark the esp32.toit file as ignored in your .gitignore file.

Examples

import openai show *

main args/List --key/string:
  prompt := "This is a test"
  if args.size > 0: prompt = args[0]

  client := Client --key=key

  response := client.complete --prompt=prompt --stop=["."]
  print "response: $response"

  conversation := [
     ChatMessage.system "You are a helpful assistant.",
     ChatMessage.user "Help we write an API for an OpenAI client.",
     ChatMessage.user "The language is Toit.",
     ChatMessage.user """
       Example for the Usage class:
       ```
        class Usage:
        /**
        The number of tokens in the prompt.
        */
        prompt-tokens/int

        /**
        The number of tokens in the completion.
        */
        completion-tokens/int?

        /**
        The total number of tokens in the prompt and completion.
        */
        total-tokens/int

        constructor.from-json json/Map:
            prompt-tokens = json["prompt_tokens"]
            completion-tokens = json.get "completion_tokens"
            total-tokens = json["total_tokens"]

       ```
       """,
  ]
  response := client.complete_chat --conversation=conversation --max_tokens=1000
  print "response: $response"

Features and bugs

Please file feature requests and bugs at the issue tracker.

References

About

An OpenAI client

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages