Skip to content

Commit 0234c1e

Browse files
authored
add example: fine tune (#438)
* add example for fine tune * update example for fine tune * fix comments
1 parent e22a29d commit 0234c1e

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,73 @@ if errors.As(err, &e) {
611611
```
612612
</details>
613613

614+
<details>
615+
<summary>Fine Tune Model</summary>
616+
617+
```go
618+
package main
619+
620+
import (
621+
"context"
622+
"fmt"
623+
"github.com/sashabaranov/go-openai"
624+
)
625+
626+
func main() {
627+
client := openai.NewClient("your token")
628+
ctx := context.Background()
629+
630+
// create a .jsonl file with your training data
631+
// {"prompt": "<prompt text>", "completion": "<ideal generated text>"}
632+
// {"prompt": "<prompt text>", "completion": "<ideal generated text>"}
633+
// {"prompt": "<prompt text>", "completion": "<ideal generated text>"}
634+
635+
// you can use openai cli tool to validate the data
636+
// For more info - https://platform.openai.com/docs/guides/fine-tuning
637+
638+
file, err := client.CreateFile(ctx, openai.FileRequest{
639+
FilePath: "training_prepared.jsonl",
640+
Purpose: "fine-tune",
641+
})
642+
if err != nil {
643+
fmt.Printf("Upload JSONL file error: %v\n", err)
644+
return
645+
}
646+
647+
// create a fine tune job
648+
// 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)
649+
// use below get method to know the status of your model
650+
tune, err := client.CreateFineTune(ctx, openai.FineTuneRequest{
651+
TrainingFile: file.ID,
652+
Model: "ada", // babbage, curie, davinci, or a fine-tuned model created after 2022-04-21.
653+
})
654+
if err != nil {
655+
fmt.Printf("Creating new fine tune model error: %v\n", err)
656+
return
657+
}
658+
659+
getTune, err := client.GetFineTune(ctx, tune.ID)
660+
if err != nil {
661+
fmt.Printf("Getting fine tune model error: %v\n", err)
662+
return
663+
}
664+
fmt.Println(getTune.FineTunedModel)
665+
666+
// once the status of getTune is `succeeded`, you can use your fine tune model in Completion Request
667+
668+
// resp, err := client.CreateCompletion(ctx, openai.CompletionRequest{
669+
// Model: getTune.FineTunedModel,
670+
// Prompt: "your prompt",
671+
// })
672+
// if err != nil {
673+
// fmt.Printf("Create completion error %v\n", err)
674+
// return
675+
// }
676+
//
677+
// fmt.Println(resp.Choices[0].Text)
678+
}
679+
```
680+
</details>
614681
See the `examples/` folder for more.
615682

616683
### Integration tests:

0 commit comments

Comments
 (0)