Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
h0rv committed Jan 28, 2024
1 parent de70ed9 commit 05a152e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 25 deletions.
39 changes: 16 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,29 @@ Import:

```go
import (
wx "github.com/h0rv/go-watsonx/foundation_models"
wx "github.com/h0rv/go-watsonx/models"
)
```

## Example Usage

### Builder Pattern

```go
model, err := wx.NewModelBuilder().
SetModelId(wx.ModelTypesEnum.LLAMA_2_70B_CHAT).
SetApiKey(yourWatsonxApiKey).
SetProjectId(yourWatsonxProjectID).
SetTemperature(yourtemperature).
SetMaxNewTokens(yourMaxNewTokens).
SetDecodingMethod(wx.DecodingMethodsEnum.GREEDY).
Build()
if err != nil {
// Failed to get watsonx model
return err
}

result, err := model.GenerateText(
"Hi, how are you?",
nil, /* or your Generation Params */
model, _ := wx.NewModel(
yourIBMCloudAPIKey,
yourWatsonxProjectID,
wx.WithModel(wx.LLAMA_2_70B_CHAT),
)

result, _ := model.GenerateText(
"Hi, who are you?",
wx.WithTemperature(0.9),
wx.WithTopP(.5),
wx.WithTopK(10),
wx.WithMaxNewTokens(512),
wx.WithDecodingMethod(wx.Greedy),
)
if err != nil {
// Failed to call generate on model
return err
}

println(result)
```

## Setup
Expand Down
2 changes: 1 addition & 1 deletion models/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Model struct {
httpClient Doer
}

func New(apiKey, projectID string, options ...ModelOption) (*Model, error) {
func NewModel(apiKey, projectID string, options ...ModelOption) (*Model, error) {

opts := defaulModelOptions()
for _, opt := range options {
Expand Down
38 changes: 37 additions & 1 deletion models/test/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func getModel(t *testing.T) *models.Model {
}

// Create a test model with dummy data
model, err := models.New(
model, err := models.NewModel(
apiKey,
DummyProjectID,
models.WithModel(models.FLAN_UL2),
Expand Down Expand Up @@ -60,3 +60,39 @@ func TestValidPrompt(t *testing.T) {
t.Errorf("Expected no error, but got an error: %v", err)
}
}

func TestGenerateText(t *testing.T) {
model := getModel(t)

prompt := "Hi, who are you?"
result, err := model.GenerateText(
prompt,
models.WithTemperature(0.9),
models.WithTopP(.5),
models.WithTopK(10),
models.WithMaxNewTokens(512),
models.WithDecodingMethod(models.Greedy),
)
if err != nil {
t.Errorf("Expected no error, but got an error: %v", err)
}
if result == "" {
t.Error("Expected a result, but got an empty string")
}
}

func TestGenerateTextWithNilOptions(t *testing.T) {
model := getModel(t)

prompt := "Who are you?"
result, err := model.GenerateText(
prompt,
nil,
)
if err != nil {
t.Errorf("Expected no error, but got an error: %v", err)
}
if result == "" {
t.Error("Expected a result, but got an empty string")
}
}

0 comments on commit 05a152e

Please sign in to comment.