@@ -15,8 +15,12 @@ import (
15
15
// GLOBALS
16
16
17
17
var (
18
- anthropicName = "claude"
19
- anthropicClient * anthropic.Client
18
+ anthropicName = "claude"
19
+ anthropicClient * anthropic.Client
20
+ anthropicModel string
21
+ anthropicTemperature * float64
22
+ anthropicMaxTokens * uint64
23
+ anthropicStream bool
20
24
)
21
25
22
26
///////////////////////////////////////////////////////////////////////////////
@@ -46,6 +50,21 @@ func anthropicParse(flags *Flags, opts ...client.ClientOpt) error {
46
50
anthropicClient = client
47
51
}
48
52
53
+ // Get the command-line parameters
54
+ anthropicModel = flags .GetString ("model" )
55
+ if temp , err := flags .GetValue ("temperature" ); err == nil {
56
+ t := temp .(float64 )
57
+ anthropicTemperature = & t
58
+ }
59
+ if maxtokens , err := flags .GetValue ("max-tokens" ); err == nil {
60
+ t := maxtokens .(uint64 )
61
+ anthropicMaxTokens = & t
62
+ }
63
+ if stream , err := flags .GetValue ("stream" ); err == nil {
64
+ t := stream .(bool )
65
+ anthropicStream = t
66
+ }
67
+
49
68
// Return success
50
69
return nil
51
70
}
@@ -54,7 +73,36 @@ func anthropicParse(flags *Flags, opts ...client.ClientOpt) error {
54
73
// METHODS
55
74
56
75
func anthropicChat (ctx context.Context , w * tablewriter.Writer , args []string ) error {
57
- // Request -> Response
76
+
77
+ // Set options
78
+ opts := []anthropic.Opt {}
79
+ if anthropicModel != "" {
80
+ opts = append (opts , anthropic .OptModel (anthropicModel ))
81
+ }
82
+ if anthropicTemperature != nil {
83
+ opts = append (opts , anthropic .OptTemperature (float32 (* anthropicTemperature )))
84
+ }
85
+ if anthropicMaxTokens != nil {
86
+ opts = append (opts , anthropic .OptMaxTokens (int (* anthropicMaxTokens )))
87
+ }
88
+ if anthropicStream {
89
+ opts = append (opts , anthropic .OptStream (func (choice schema.MessageChoice ) {
90
+ w := w .Output ()
91
+ if choice .Delta != nil {
92
+ if choice .Delta .Role != "" {
93
+ fmt .Fprintf (w , "\n %v: " , choice .Delta .Role )
94
+ }
95
+ if choice .Delta .Content != "" {
96
+ fmt .Fprintf (w , "%v" , choice .Delta .Content )
97
+ }
98
+ }
99
+ if choice .FinishReason != "" {
100
+ fmt .Printf ("\n finish_reason: %q\n " , choice .FinishReason )
101
+ }
102
+ }))
103
+ }
104
+
105
+ // Append user message
58
106
message := schema .NewMessage ("user" )
59
107
for _ , arg := range args {
60
108
message .Add (schema .Text (arg ))
@@ -63,11 +111,15 @@ func anthropicChat(ctx context.Context, w *tablewriter.Writer, args []string) er
63
111
// Request -> Response
64
112
responses , err := anthropicClient .Messages (ctx , []* schema.Message {
65
113
message ,
66
- })
114
+ }, opts ... )
67
115
if err != nil {
68
116
return err
69
117
}
70
118
71
- // Write table
72
- return w .Write (responses )
119
+ // Write table (if not streaming)
120
+ if ! anthropicStream {
121
+ return w .Write (responses )
122
+ } else {
123
+ return nil
124
+ }
73
125
}
0 commit comments