Skip to content

Commit

Permalink
add example: fine tune (#438)
Browse files Browse the repository at this point in the history
* add example for fine tune

* update example for fine tune

* fix comments
  • Loading branch information
mehulgohil authored Jul 14, 2023
1 parent e22a29d commit 0234c1e
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,73 @@ if errors.As(err, &e) {
```
</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.

### Integration tests:
Expand Down

0 comments on commit 0234c1e

Please sign in to comment.