This repository has been archived by the owner on May 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.go
82 lines (73 loc) · 1.81 KB
/
client.go
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package anthropic
import (
"time"
"github.com/3JoB/ulid"
"github.com/valyala/fasthttp"
"pgregory.net/rand"
"github.com/3JoB/anthropic-sdk-go/v2/data"
"github.com/3JoB/anthropic-sdk-go/v2/pkg/pool"
)
type Client struct {
client *fasthttp.Client
key string
model string
pool *pool.Pool
header map[string]string
timeout time.Duration
}
// Set the response timeout in minutes.
func (c *Client) SetTimeOut(times int) {
if times != 0 {
c.timeout = time.Duration(times) * time.Minute
}
}
// Send data to the API endpoint. Before sending out,
// the data will be processed into a form that the API can recognize.
func (c *Client) Send(sender *Sender) (*pool.Session, error) {
var err error
if (sender.Message == data.MessageModule{}) {
return nil, data.ErrSessionIsNil
}
c.check(sender)
s := sender.newSession()
if sender.SessionID == "" {
id, _ := ulid.New(ulid.Timestamp(time.Now()), rand.New())
s.ID = id.String()
err = sender.Sender.Set(&sender.Message)
} else {
s.ID = sender.SessionID
p, ok := c.pool.Get(s.ID)
if !ok {
return nil, data.ErrSessionNotFound
}
err = sender.Sender.Build(p, &sender.Message)
}
if err != nil {
return nil, err
}
c.pool.Set(s.ID, sender.Sender.Prompt)
if err := sender.Complete(c, s); err != nil {
return nil, err
}
c.pool.Append(s.ID, s.Response.Completion)
return s, nil
}
// Should only be used when needed.
func (c *Client) CloseSession(s *pool.Session) bool {
return c.pool.Del(s.ID)
}
// Basic check
func (c *Client) check(s *Sender) {
if s.Sender.Model == "" {
s.Sender.Model = c.model
}
if len(s.Sender.StopSequences) == 0 {
s.Sender.StopSequences = data.StopSequences
}
if s.Sender.MaxToken < 400 {
s.Sender.MaxToken = 400
}
}
func (c *Client) do(req *fasthttp.Request, res *fasthttp.Response) error {
return c.client.Do(req, res)
}