openai

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2023 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CompletionCmd = &cobra.Command{
	Use:   "completion",
	Short: "send a prompt to the completion API",
	Args:  cobra.MinimumNArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		prompts := []string{}

		for _, file := range args {

			if file == "-" {
				file = "/dev/stdin"
			}

			f, err := os.ReadFile(file)
			cobra.CheckErr(err)

			prompts = append(prompts, string(f))
		}

		clientSettings, err := openai.NewClientSettingsFromCobra(cmd)
		cobra.CheckErr(err)

		err = completionStepFactory.UpdateFromCobra(cmd)
		cobra.CheckErr(err)

		client, err := clientSettings.CreateClient()
		cobra.CheckErr(err)

		ctx := context.Background()
		settings := completionStepFactory.StepSettings
		if settings.Engine == nil {
			cobra.CheckErr(fmt.Errorf("engine is required"))
		}
		resp, err := client.CompletionWithEngine(ctx, *settings.Engine,
			gpt3.CompletionRequest{
				Prompt:           prompts,
				MaxTokens:        settings.MaxResponseTokens,
				Temperature:      settings.Temperature,
				TopP:             settings.TopP,
				N:                settings.N,
				LogProbs:         settings.LogProbs,
				Echo:             false,
				Stop:             settings.Stop,
				PresencePenalty:  0,
				FrequencyPenalty: 0,
				Stream:           false,
			},
		)
		cobra.CheckErr(err)

		printUsage, _ := cmd.Flags().GetBool("print-usage")
		usage := resp.Usage
		evt := log.Debug()
		if printUsage {
			evt = log.Info()
		}
		evt.
			Int("prompt-tokens", usage.PromptTokens).
			Int("completion-tokens", usage.CompletionTokens).
			Int("total-tokens", usage.TotalTokens).
			Msg("Usage")

		gp, of, err := cli.SetupProcessor(cmd)
		cobra.CheckErr(err)

		printRawResponse, _ := cmd.Flags().GetBool("print-raw-response")

		if printRawResponse {

			rawResponse, err := json.MarshalIndent(resp, "", "  ")
			cobra.CheckErr(err)

			// deserialize to map[string]interface{}
			var rawResponseMap map[string]interface{}
			err = json.Unmarshal(rawResponse, &rawResponseMap)
			cobra.CheckErr(err)

			err = gp.ProcessInputObject(rawResponseMap)
			cobra.CheckErr(err)

		} else {
			for i, choice := range resp.Choices {
				logProbs := map[string]interface{}{}

				logProbJson, err := json.MarshalIndent(choice.LogProbs, "", "  ")
				cobra.CheckErr(err)

				// deserialize to map[string]interface{}
				var logProbMap map[string]interface{}
				err = json.Unmarshal(logProbJson, &logProbMap)
				cobra.CheckErr(err)

				idx := i / len(resp.Choices)
				prompt := ""
				if len(prompts) > idx {
					prompt = prompts[idx]
				}

				text := strings.Trim(choice.Text, " \t\n")
				text = strings.ReplaceAll(text, "\n", "\\n")
				prompt = strings.Trim(prompt, " \t\n")
				prompt = strings.ReplaceAll(prompt, "\n", "\\n")

				row := map[string]interface{}{
					"index":         choice.Index,
					"text":          text,
					"logprobs":      logProbs,
					"prompt":        prompt,
					"finish_reason": choice.FinishReason,
					"engine":        *settings.Engine,
				}
				err = gp.ProcessInputObject(row)
				cobra.CheckErr(err)
			}
		}

		s, err := of.Output()
		if err != nil {
			_, _ = fmt.Fprintf(os.Stderr, "Error rendering output: %s\n", err)
			os.Exit(1)
		}
		fmt.Print(s)
		cobra.CheckErr(err)
	},
}
View Source
var EditsCmd = &cobra.Command{
	Use:   "edits",
	Short: "Compute edits for a file",
	Run: func(cmd *cobra.Command, args []string) {

	},
}
View Source
var EmbeddingsCmd = &cobra.Command{
	Use:   "embeddings",
	Short: "Compute embeddings for a series of files",
	Args:  cobra.MinimumNArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		user, _ := cmd.PersistentFlags().GetString("user")

		prompts := []string{}

		for _, file := range args {

			if file == "-" {
				file = "/dev/stdin"
			}

			f, err := os.ReadFile(file)
			cobra.CheckErr(err)

			prompts = append(prompts, string(f))
		}

		clientSettings, err := openai.NewClientSettingsFromCobra(cmd)
		cobra.CheckErr(err)

		err = completionStepFactory.UpdateFromCobra(cmd)
		cobra.CheckErr(err)

		client, err := clientSettings.CreateClient()
		cobra.CheckErr(err)

		engine, _ := cmd.Flags().GetString("engine")

		ctx := context.Background()
		resp, err := client.Embeddings(ctx, gpt3.EmbeddingsRequest{
			Input: prompts,
			Model: engine,
			User:  user,
		})
		cobra.CheckErr(err)

		printUsage, _ := cmd.Flags().GetBool("print-usage")
		usage := resp.Usage
		evt := log.Debug()
		if printUsage {
			evt = log.Info()
		}
		evt.
			Int("prompt-tokens", usage.PromptTokens).
			Int("total-tokens", usage.TotalTokens).
			Msg("Usage")

		gp, of, err := cli.SetupProcessor(cmd)
		cobra.CheckErr(err)

		printRawResponse, _ := cmd.Flags().GetBool("print-raw-response")

		if printRawResponse {

			rawResponse, err := json.MarshalIndent(resp, "", "  ")
			cobra.CheckErr(err)

			// deserialize to map[string]interface{}
			var rawResponseMap map[string]interface{}
			err = json.Unmarshal(rawResponse, &rawResponseMap)
			cobra.CheckErr(err)

			err = gp.ProcessInputObject(rawResponseMap)
			cobra.CheckErr(err)

		} else {
			for _, embedding := range resp.Data {
				row := map[string]interface{}{
					"object":    embedding.Object,
					"embedding": embedding.Embedding,
					"index":     embedding.Index,
				}
				err = gp.ProcessInputObject(row)
				cobra.CheckErr(err)
			}
		}

		s, err := of.Output()
		if err != nil {
			_, _ = fmt.Fprintf(os.Stderr, "Error rendering output: %s\n", err)
			os.Exit(1)
		}
		fmt.Print(s)
		cobra.CheckErr(err)
	},
}
View Source
var EngineInfoCmd = &cobra.Command{
	Use:   "engine-info",
	Short: "get engine info",
	Args:  cobra.ExactArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		engine := args[0]

		clientSettings, err := openai.NewClientSettingsFromCobra(cmd)
		cobra.CheckErr(err)

		client, err := clientSettings.CreateClient()
		cobra.CheckErr(err)

		ctx := context.Background()
		resp, err := client.Engine(ctx, engine)
		cobra.CheckErr(err)

		gp, of, err := cli.SetupProcessor(cmd)
		cobra.CheckErr(err)

		row := map[string]interface{}{
			"id":     resp.ID,
			"owner":  resp.Owner,
			"ready":  resp.Ready,
			"object": resp.Object,
		}
		err = gp.ProcessInputObject(row)
		cobra.CheckErr(err)

		s, err := of.Output()
		if err != nil {
			_, _ = fmt.Fprintf(os.Stderr, "Error rendering output: %s\n", err)
			os.Exit(1)
		}
		fmt.Print(s)
	},
}
View Source
var FamiliesCmd = &cobra.Command{
	Use:   "ls-families",
	Short: "list families",
	Run: func(cmd *cobra.Command, args []string) {
		models := SimpleModelsJSON{}
		err := json.Unmarshal([]byte(modelsJson), &models)
		cobra.CheckErr(err)

		gp, of, err := cli.SetupProcessor(cmd)
		cobra.CheckErr(err)

		for _, family := range models.Families {
			err = gp.ProcessInputObject(family)
			cobra.CheckErr(err)
		}

		s, err := of.Output()
		if err != nil {
			_, _ = fmt.Fprintf(os.Stderr, "Error rendering output: %s\n", err)
			os.Exit(1)
		}
		fmt.Print(s)
		cobra.CheckErr(err)
	},
}
View Source
var ListEnginesCmd = &cobra.Command{
	Use:   "list-engines",
	Short: "list engines",
	Run: func(cmd *cobra.Command, args []string) {
		clientSettings, err := openai.NewClientSettingsFromCobra(cmd)
		cobra.CheckErr(err)

		client, err := clientSettings.CreateClient()
		cobra.CheckErr(err)

		ctx := context.Background()
		resp, err := client.Engines(ctx)
		cobra.CheckErr(err)

		gp, of, err := cli.SetupProcessor(cmd)
		cobra.CheckErr(err)

		idGlob, _ := cmd.Flags().GetString("id")
		ownerGlob, _ := cmd.Flags().GetString("owner")
		ready, _ := cmd.Flags().GetBool("ready")

		for _, engine := range resp.Data {
			if idGlob != "" {

				matching, err := glob.Match(idGlob, engine.ID)
				cobra.CheckErr(err)

				if !matching {
					continue
				}
			}

			if ownerGlob != "" {

				matching, err := glob.Match(ownerGlob, engine.Owner)
				cobra.CheckErr(err)

				if !matching {
					continue
				}
			}

			if cmd.Flags().Changed("ready") {

				if ready != engine.Ready {
					continue
				}
			}

			row := map[string]interface{}{
				"id":     engine.ID,
				"owner":  engine.Owner,
				"ready":  engine.Ready,
				"object": engine.Object,
			}
			err = gp.ProcessInputObject(row)
			cobra.CheckErr(err)
		}

		s, err := of.Output()
		if err != nil {
			_, _ = fmt.Fprintf(os.Stderr, "Error rendering output: %s\n", err)
			os.Exit(1)
		}
		fmt.Print(s)
	},
}
View Source
var ModelsCmd = &cobra.Command{
	Use:   "ls-models",
	Short: "list models",
	Run: func(cmd *cobra.Command, args []string) {

		models := SimpleModelsJSON{}
		err := json.Unmarshal([]byte(modelsJson), &models)
		cobra.CheckErr(err)

		gp, of, err := cli.SetupProcessor(cmd)
		cobra.CheckErr(err)

		for _, completion := range models.Completion {
			err = gp.ProcessInputObject(completion)
			cobra.CheckErr(err)
		}

		s, err := of.Output()
		if err != nil {
			_, _ = fmt.Fprintf(os.Stderr, "Error rendering output: %s\n", err)
			os.Exit(1)
		}
		fmt.Print(s)
		cobra.CheckErr(err)
	},
}
View Source
var OpenaiCmd = &cobra.Command{
	Use:   "openai",
	Short: "OpenAI commands",
}

Functions

func LoadModelsHelpFiles

func LoadModelsHelpFiles() ([]*help.Section, error)

Types

type Completion

type Completion struct {
	Name                   string `json:"name"`
	Family                 string `json:"family"`
	Description            string `json:"description"`
	MaxTokens              int    `json:"max_tokens"`
	TrainingDataCutoffDate string `json:"training_data_cutoff_date"`
}

type Family

type Family struct {
	Name             string   `json:"name"`
	Description      string   `json:"description"`
	PricePer1kTokens float64  `json:"price_per_1k_tokens"`
	GoodAt           []string `json:"good_at"`
	KeyPoints        []string `json:"key_points"`
	Subtitle         string   `json:"subtitle"`
	Short            string   `json:"short"`
}

type ModelsJSON

type ModelsJSON struct {
	Completion []Completion `json:"completion"`
	Families   []Family     `json:"families"`
}

type SimpleModelsJSON

type SimpleModelsJSON struct {
	Completion []map[string]interface{} `json:"completion"`
	Families   []map[string]interface{} `json:"families"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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