add example: fine tune (#438)

* add example for fine tune

* update example for fine tune

* fix comments
This commit is contained in:
Mehul Gohil
2023-07-15 03:43:05 +05:30
committed by GitHub
parent e22a29d84e
commit 0234c1e0c2

View File

@@ -611,6 +611,73 @@ if errors.As(err, &e) {
``` ```
</details> </details>
<details>
<summary>Fine Tune Model</summary>
```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": "<prompt text>", "completion": "<ideal generated text>"}
// {"prompt": "<prompt text>", "completion": "<ideal generated text>"}
// {"prompt": "<prompt text>", "completion": "<ideal generated text>"}
// 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)
}
```
</details>
See the `examples/` folder for more. See the `examples/` folder for more.
### Integration tests: ### Integration tests: