From 0234c1e0c2769c9599f0799259fb8db5c4e3e011 Mon Sep 17 00:00:00 2001 From: Mehul Gohil Date: Sat, 15 Jul 2023 03:43:05 +0530 Subject: [PATCH] add example: fine tune (#438) * add example for fine tune * update example for fine tune * fix comments --- README.md | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/README.md b/README.md index 1f708af..19aadde 100644 --- a/README.md +++ b/README.md @@ -611,6 +611,73 @@ if errors.As(err, &e) { ``` +
+Fine Tune Model + +```go +package main + +import ( + "context" + "fmt" + "github.com/sashabaranov/go-openai" +) + +func main() { + client := openai.NewClient("your token") + ctx := context.Background() + + // create a .jsonl file with your training data + // {"prompt": "", "completion": ""} + // {"prompt": "", "completion": ""} + // {"prompt": "", "completion": ""} + + // you can use openai cli tool to validate the data + // For more info - https://platform.openai.com/docs/guides/fine-tuning + + file, err := client.CreateFile(ctx, openai.FileRequest{ + FilePath: "training_prepared.jsonl", + Purpose: "fine-tune", + }) + if err != nil { + fmt.Printf("Upload JSONL file error: %v\n", err) + return + } + + // create a fine tune job + // Streams events until the job is done (this often takes minutes, but can take hours if there are many jobs in the queue or your dataset is large) + // use below get method to know the status of your model + tune, err := client.CreateFineTune(ctx, openai.FineTuneRequest{ + TrainingFile: file.ID, + Model: "ada", // babbage, curie, davinci, or a fine-tuned model created after 2022-04-21. + }) + if err != nil { + fmt.Printf("Creating new fine tune model error: %v\n", err) + return + } + + getTune, err := client.GetFineTune(ctx, tune.ID) + if err != nil { + fmt.Printf("Getting fine tune model error: %v\n", err) + return + } + fmt.Println(getTune.FineTunedModel) + + // once the status of getTune is `succeeded`, you can use your fine tune model in Completion Request + + // resp, err := client.CreateCompletion(ctx, openai.CompletionRequest{ + // Model: getTune.FineTunedModel, + // Prompt: "your prompt", + // }) + // if err != nil { + // fmt.Printf("Create completion error %v\n", err) + // return + // } + // + // fmt.Println(resp.Choices[0].Text) +} +``` +
See the `examples/` folder for more. ### Integration tests: