Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add example: fine tune #438

Merged
merged 3 commits into from
Jul 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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