From d4140e36a98724addc772e070c7de4a78f663472 Mon Sep 17 00:00:00 2001 From: Mehul Gohil Date: Wed, 12 Jul 2023 17:58:40 +0530 Subject: [PATCH 1/3] add example for fine tune --- README.md | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/README.md b/README.md index 1f708af70..35688db8a 100644 --- a/README.md +++ b/README.md @@ -611,6 +611,67 @@ 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", + }) + if err != nil { + fmt.Printf("Creating new fine tune model error: %v\n", err) + return + } + + getTune, err := client.GetFineTune(context.Background(), tune.ID) + if err != nil { + fmt.Printf("Getting fine tune model error: %v\n", err) + return + } + + fmt.Println(getTune.Status) + + resp, err := client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{}) + if err != nil { + fmt.Printf("Create chat completion error %v\n", err) + return + } +} +``` +
See the `examples/` folder for more. ### Integration tests: From fab20b0395f8ada3b1d7c3b7dbc5a1f6a6c49ec4 Mon Sep 17 00:00:00 2001 From: Mehul Gohil Date: Wed, 12 Jul 2023 23:17:46 +0530 Subject: [PATCH 2/3] update example for fine tune --- README.md | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 35688db8a..6f7557f87 100644 --- a/README.md +++ b/README.md @@ -649,26 +649,32 @@ func main() { //use below get method to know the status of your model tune, err := client.CreateFineTune(ctx, openai.FineTuneRequest{ TrainingFile: file.ID, - Model: "ada", + Model: "ada", //babbage,curie,davinci }) if err != nil { fmt.Printf("Creating new fine tune model error: %v\n", err) return } - getTune, err := client.GetFineTune(context.Background(), tune.ID) + 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) - fmt.Println(getTune.Status) + //once the status of getTune is `succeeded`, you can use your fine tune model in Completion Request - resp, err := client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{}) - if err != nil { - fmt.Printf("Create chat completion error %v\n", err) - return - } + //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) } ``` From 89b99ca67a45e7b18a58f9ec1d7947b5c08986f6 Mon Sep 17 00:00:00 2001 From: Mehul Gohil Date: Fri, 14 Jul 2023 10:53:55 +0530 Subject: [PATCH 3/3] fix comments --- README.md | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 6f7557f87..19aadde2a 100644 --- a/README.md +++ b/README.md @@ -627,13 +627,13 @@ func main() { client := openai.NewClient("your token") ctx := context.Background() - //create a .jsonl file with your training data - //{"prompt": "", "completion": ""} - //{"prompt": "", "completion": ""} - //{"prompt": "", "completion": ""} + // 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 + // 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", @@ -644,12 +644,12 @@ func main() { 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 + // 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 + 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) @@ -663,18 +663,18 @@ func main() { } fmt.Println(getTune.FineTunedModel) - //once the status of getTune is `succeeded`, you can use your fine tune model in Completion Request + // 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 - //} + // 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) + // fmt.Println(resp.Choices[0].Text) } ```