-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon_utils.go
More file actions
52 lines (48 loc) · 1.87 KB
/
Copy pathcommon_utils.go
File metadata and controls
52 lines (48 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package examples
import (
"os"
"github.com/bububa/instructor-go"
"github.com/bububa/instructor-go/instructors"
cohereClient "github.com/cohere-ai/cohere-go/v2/client"
cohereOption "github.com/cohere-ai/cohere-go/v2/option"
anthropic "github.com/liushuangls/go-anthropic/v2"
"github.com/openai/openai-go"
"github.com/openai/openai-go/option"
)
func NewInstructor(provider instructor.Provider, modes ...instructor.Mode) instructor.Instructor {
mode := instructor.ModeJSON
if len(modes) > 0 {
mode = modes[0]
}
switch provider {
case instructor.ProviderAnthropic:
authToken := os.Getenv("ANTHROPIC_API_KEY")
baseURL := os.Getenv("ANTHROPIC_API_BASE_URL")
opts := make([]anthropic.ClientOption, 0, 1)
if baseURL != "" {
opts = append(opts, anthropic.WithBaseURL(baseURL))
}
clt := anthropic.NewClient(authToken, opts...)
return instructors.FromAnthropic(clt, instructor.WithMode(mode), instructor.WithMaxRetries(1), instructor.WithValidation())
case instructor.ProviderCohere:
authToken := os.Getenv("COHERE_API_KEY")
baseURL := os.Getenv("COHERE_API_BASE_URL")
opts := make([]cohereOption.RequestOption, 0, 2)
opts = append(opts, cohereOption.WithToken(authToken))
if baseURL != "" {
opts = append(opts, cohereOption.WithBaseURL(baseURL))
}
clt := cohereClient.NewClient(opts...)
return instructors.FromCohere(clt, instructor.WithMode(mode), instructor.WithMaxRetries(1), instructor.WithValidation())
default:
authToken := os.Getenv("OPENAI_API_KEY")
baseURL := os.Getenv("OPENAI_BASE_URL")
opts := make([]option.RequestOption, 0, 2)
opts = append(opts, option.WithAPIKey(authToken))
if baseURL != "" {
opts = append(opts, option.WithBaseURL(baseURL))
}
clt := openai.NewClient(opts...)
return instructors.FromOpenAI(&clt, instructor.WithMode(mode), instructor.WithMaxRetries(1), instructor.WithValidation(), instructor.WithVerbose())
}
}