openapiexamples

package module
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 15, 2025 License: MIT Imports: 10 Imported by: 0

README

Go OpenAI Examples

Build Status Lint Status Go Report Card Docs License

This is a repo to contain various experiments with OpenAI, such as ChatGPT.

openaichat CLI app

openaichat provides a CLI method for interacting with the OpenAI Chat API.

Installation
% go install github.com/grokify/go-openai-examples/cmd/openaichat
Usage

Use the -p prompt parameter to send your request to ChatGPT.

There are several ways to provide the OpenAI token to the openaichat CLI app as shown below.

Usage with Environment Variable

If no CLI parameters are used, the token is assumed to be in the OPENAI_API_KEY environment variable.

% OPENAI_API_KEY = my_key openaichat -p 'hello ChatGPT!'
Usage with .env file

When using an .env file, use the -e parameter, and the the CLI app will look for the OPENAI_API_KEY environment variable.

% openaichat -e /path/to/envfile -p 'hello ChatGPT!'
Usage with goauth credentials set file

The github.com/grokify/goauth credentials set file is a single format to handle multiple types of authentication. When using a credentials set file, use the -c and -a parameters.

% openaichat -c /path/to/credentialsfile.json` -a yourAccountName -p 'hello ChatGPT!'

This code will look for the static access token as an OAuth 2.0 credential due to use of the bearer token format.

If your credentials file looks like the following, you can use -a OPENAI.

{
  "credentials": {
    "OPENAI": {
      "oauth2": {
        "token": {
          "access_token": "<yourOpenAIAPIKey>",
        }
      }
    }
  }
}

Documentation

Index

Constants

View Source
const EnvOpenAIKey = "OPENAI_API_KEY"

Variables

This section is empty.

Functions

func ChatRequest

func ChatRequest(client *openai.Client, prompt, model string) (string, error)

Types

type Options

type Options struct {
	EnvFile      string `short:"e" long:"env" description:".env file"`
	CredsFile    string `short:"c" long:"credsfile" description:"Credentials file"`
	CredsAccount string `short:"a" long:"account" credsaccount:"Credentials account"`
	Prompt       string `short:"p" long:"prompt" description:"Prompt"`
	Key          string `short:"k" long:"key" description:"API Key"`
}

func (Options) APIKey

func (opts Options) APIKey() (string, error)

func (Options) NewClient

func (opts Options) NewClient() (*openai.Client, error)

func (Options) PromptOrExample

func (opts Options) PromptOrExample(idx int, def string) string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL