Skip to content

Commit d5c9535

Browse files
authored
Cleanup and Update Generic API (#17)
* update api * update readme --------- Co-authored-by: Robby <[email protected]>
1 parent 92b5b03 commit d5c9535

File tree

14 files changed

+163
-130
lines changed

14 files changed

+163
-130
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,17 @@ type Person struct {
3838
func main() {
3939
ctx := context.Background()
4040

41-
client, err := instructor.FromOpenAI[Person](
41+
client, err := instructor.FromOpenAI(
4242
openai.NewClient(os.Getenv("OPENAI_API_KEY")),
4343
instructor.WithMode(instructor.ModeJSON),
44-
instructor.WithMaxRetries(5),
44+
instructor.WithMaxRetries(3),
4545
)
4646
if err != nil {
4747
panic(err)
4848
}
4949

50-
person, err := client.CreateChatCompletion(
50+
var person Person
51+
err = client.CreateChatCompletion(
5152
ctx,
5253
instructor.Request{
5354
Model: openai.GPT4Turbo20240409,
@@ -58,6 +59,7 @@ func main() {
5859
},
5960
},
6061
},
62+
&person,
6163
)
6264
if err != nil {
6365
panic(err)

examples/classifcation/main.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,17 @@ type Prediction struct {
2828
func classify(data string) *Prediction {
2929
ctx := context.Background()
3030

31-
client, err := instructor.FromAnthropic[Prediction](
31+
client, err := instructor.FromAnthropic(
3232
anthropic.NewClient(os.Getenv("ANTHROPIC_API_KEY")),
3333
instructor.WithMode(instructor.ModeToolCall),
34-
instructor.WithMaxRetries(1),
34+
instructor.WithMaxRetries(3),
3535
)
3636
if err != nil {
3737
panic(err)
3838
}
3939

40-
prediction, err := client.CreateChatCompletion(
40+
var prediction Prediction
41+
err = client.CreateChatCompletion(
4142
ctx,
4243
instructor.Request{
4344
Model: anthropic.ModelClaude3Haiku20240307,
@@ -48,12 +49,13 @@ func classify(data string) *Prediction {
4849
},
4950
},
5051
},
52+
&prediction,
5153
)
5254
if err != nil {
5355
panic(err)
5456
}
5557

56-
return prediction
58+
return &prediction
5759
}
5860

5961
func main() {
@@ -62,8 +64,13 @@ func main() {
6264
prediction := classify(ticket)
6365

6466
assert(prediction.contains(LabelTechIssue), "Expected ticket to be related to tech issue")
65-
assert(prediction.contains(LabelTechIssue), "Expected ticket to be related to billing")
67+
assert(prediction.contains(LabelBilling), "Expected ticket to be related to billing")
68+
assert(!prediction.contains(LabelGeneralQuery), "Expected ticket NOT to be a general query")
6669

70+
fmt.Printf("%+v\n", prediction)
71+
/*
72+
&{Labels:[{Type:tech_issue} {Type:billing}]}
73+
*/
6774
}
6875

6976
/******/

examples/function_calling/main.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ func (s *Search) execute() {
2929

3030
type Searches = []Search
3131

32+
// type Searches struct {
33+
// Items []Search `json:"searches" jsonschema:"title=Searches,description=A list of search results"`
34+
// }
35+
3236
func segment(ctx context.Context, data string) *Searches {
3337

34-
client, err := instructor.FromOpenAI[Searches](
38+
client, err := instructor.FromOpenAI(
3539
openai.NewClient(os.Getenv("OPENAI_API_KEY")),
3640
instructor.WithMode(instructor.ModeToolCall),
3741
instructor.WithMaxRetries(3),
@@ -40,23 +44,25 @@ func segment(ctx context.Context, data string) *Searches {
4044
panic(err)
4145
}
4246

43-
searches, err := client.CreateChatCompletion(
47+
var searches Searches
48+
err = client.CreateChatCompletion(
4449
ctx,
4550
instructor.Request{
46-
Model: openai.GPT4Turbo20240409,
51+
Model: openai.GPT4o,
4752
Messages: []instructor.Message{
4853
{
4954
Role: instructor.RoleUser,
5055
Content: fmt.Sprintf("Consider the data below: '\n%s' and segment it into multiple search queries", data),
5156
},
5257
},
5358
},
59+
&searches,
5460
)
5561
if err != nil {
5662
panic(err)
5763
}
5864

59-
return searches
65+
return &searches
6066
}
6167

6268
func main() {

examples/images/anthropic/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (bc *MovieCatalog) PrintCatalog() {
3232
func main() {
3333
ctx := context.Background()
3434

35-
client, err := instructor.FromAnthropic[MovieCatalog](
35+
client, err := instructor.FromAnthropic(
3636
anthropic.NewClient(os.Getenv("ANTHROPIC_API_KEY")),
3737
instructor.WithMode(instructor.ModeJSONSchema),
3838
instructor.WithMaxRetries(3),
@@ -43,7 +43,8 @@ func main() {
4343

4444
url := "https://utfs.io/f/bd0dbae6-27e3-4604-b640-fd2ffea891b8-fxyywt.jpeg"
4545

46-
movieCatalog, err := client.CreateChatCompletion(
46+
var movieCatalog MovieCatalog
47+
err = client.CreateChatCompletion(
4748
ctx,
4849
instructor.Request{
4950
Model: "claude-3-haiku-20240307",
@@ -65,6 +66,7 @@ func main() {
6566
},
6667
},
6768
},
69+
&movieCatalog,
6870
)
6971
if err != nil {
7072
panic(err)

examples/images/openai/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (bc *BookCatalog) PrintCatalog() {
3030
func main() {
3131
ctx := context.Background()
3232

33-
client, err := instructor.FromOpenAI[BookCatalog](
33+
client, err := instructor.FromOpenAI(
3434
openai.NewClient(os.Getenv("OPENAI_API_KEY")),
3535
instructor.WithMode(instructor.ModeJSON),
3636
instructor.WithMaxRetries(3),
@@ -41,7 +41,8 @@ func main() {
4141

4242
url := "https://utfs.io/f/fe55d6bd-e920-4a6f-8e93-a4c9dd851b90-eivhb2.png"
4343

44-
bookCatalog, err := client.CreateChatCompletion(
44+
var bookCatalog BookCatalog
45+
err = client.CreateChatCompletion(
4546
ctx,
4647
instructor.Request{
4748
Model: openai.GPT4o,
@@ -63,6 +64,7 @@ func main() {
6364
},
6465
},
6566
},
67+
&bookCatalog,
6668
)
6769

6870
if err != nil {

examples/user/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Person struct {
1717
func main() {
1818
ctx := context.Background()
1919

20-
client, err := instructor.FromOpenAI[Person](
20+
client, err := instructor.FromOpenAI(
2121
openai.NewClient(os.Getenv("OPENAI_API_KEY")),
2222
instructor.WithMode(instructor.ModeJSON),
2323
instructor.WithMaxRetries(3),
@@ -26,7 +26,8 @@ func main() {
2626
panic(err)
2727
}
2828

29-
person, err := client.CreateChatCompletion(
29+
var person Person
30+
err = client.CreateChatCompletion(
3031
ctx,
3132
instructor.Request{
3233
Model: openai.GPT4Turbo20240409,
@@ -37,6 +38,7 @@ func main() {
3738
},
3839
},
3940
},
41+
&person,
4042
)
4143
if err != nil {
4244
panic(err)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.21.8
44

55
require (
66
github.com/invopop/jsonschema v0.12.0
7-
github.com/liushuangls/go-anthropic/v2 v2.0.3
7+
github.com/liushuangls/go-anthropic/v2 v2.1.0
88
github.com/sashabaranov/go-openai v1.24.0
99
)
1010

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ github.com/invopop/jsonschema v0.12.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uO
99
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
1010
github.com/liushuangls/go-anthropic/v2 v2.0.3 h1:vNA74jYpBxqXxpj3b/+iLtvfQ6fwCY56pseIAPCItQs=
1111
github.com/liushuangls/go-anthropic/v2 v2.0.3/go.mod h1:8BKv/fkeTaL5R9R9bGkaknYBueyw2WxY20o7bImbOek=
12+
github.com/liushuangls/go-anthropic/v2 v2.1.0 h1:5ntOeehozlMin0+hgnhxbTru+tmBH84ADaSPelG5fPg=
13+
github.com/liushuangls/go-anthropic/v2 v2.1.0/go.mod h1:8BKv/fkeTaL5R9R9bGkaknYBueyw2WxY20o7bImbOek=
1214
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
1315
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
1416
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

pkg/instructor/anthropic.go

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,38 @@ import (
99
anthropic "github.com/liushuangls/go-anthropic/v2"
1010
)
1111

12-
type AnthropicClient[T any] struct {
12+
type AnthropicClient struct {
1313
Name string
1414

1515
client *anthropic.Client
16-
schema *Schema[T]
17-
mode Mode
1816
}
1917

20-
var _ Client[any] = &AnthropicClient[any]{}
18+
var _ Client = &AnthropicClient{}
2119

22-
func NewAnthropicClient[T any](client *anthropic.Client, schema *Schema[T], mode Mode) (*AnthropicClient[T], error) {
23-
o := &AnthropicClient[T]{
20+
func NewAnthropicClient(client *anthropic.Client) (*AnthropicClient, error) {
21+
o := &AnthropicClient{
2422
Name: "Anthropic",
2523
client: client,
26-
schema: schema,
27-
mode: mode,
2824
}
2925
return o, nil
3026
}
3127

32-
func (a *AnthropicClient[T]) CreateChatCompletion(ctx context.Context, request Request) (string, error) {
33-
return a.completionModeHandler(ctx, request)
34-
}
35-
36-
func (a *AnthropicClient[any]) completionModeHandler(ctx context.Context, request Request) (string, error) {
37-
switch a.mode {
28+
func (a *AnthropicClient) CreateChatCompletion(ctx context.Context, request Request, mode Mode, schema *Schema) (string, error) {
29+
switch mode {
3830
case ModeToolCall:
39-
return a.completionToolCall(ctx, request)
31+
return a.completionToolCall(ctx, request, schema)
4032
case ModeJSONSchema:
41-
return a.completionJSONSchema(ctx, request)
33+
return a.completionJSONSchema(ctx, request, schema)
4234
default:
43-
return "", fmt.Errorf("mode '%s' is not supported for %s", a.mode, a.Name)
35+
return "", fmt.Errorf("mode '%s' is not supported for %s", mode, a.Name)
4436
}
4537
}
4638

47-
func (a *AnthropicClient[any]) completionToolCall(ctx context.Context, request Request) (string, error) {
39+
func (a *AnthropicClient) completionToolCall(ctx context.Context, request Request, schema *Schema) (string, error) {
4840

4941
tools := []anthropic.ToolDefinition{}
5042

51-
for _, function := range a.schema.Functions {
43+
for _, function := range schema.Functions {
5244
t := anthropic.ToolDefinition{
5345
Name: function.Name,
5446
Description: function.Description,
@@ -92,15 +84,15 @@ func (a *AnthropicClient[any]) completionToolCall(ctx context.Context, request R
9284

9385
}
9486

95-
func (a *AnthropicClient[any]) completionJSONSchema(ctx context.Context, request Request) (string, error) {
87+
func (a *AnthropicClient) completionJSONSchema(ctx context.Context, request Request, schema *Schema) (string, error) {
9688

9789
system := fmt.Sprintf(`
9890
Please responsd with json in the following json_schema:
9991
10092
%s
10193
10294
Make sure to return an instance of the JSON, not the schema itself.
103-
`, a.schema.String)
95+
`, schema.String)
10496

10597
messages, err := toAnthropicMessages(&request)
10698
if err != nil {

pkg/instructor/client.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import (
44
"context"
55
)
66

7-
type Client[T any] interface {
7+
type Client interface {
88
CreateChatCompletion(
99
ctx context.Context,
1010
request Request,
11+
mode Mode,
12+
schema *Schema,
1113
) (string, error)
1214

1315
// TODO: implement streaming

0 commit comments

Comments
 (0)