forked from tmc/langchaingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cohereclient.go
144 lines (117 loc) · 2.94 KB
/
cohereclient.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package cohereclient
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"github.com/cohere-ai/tokenizer"
)
var (
ErrEmptyResponse = errors.New("empty response")
ErrModelNotFound = errors.New("model not found")
)
type Client struct {
token string
baseURL string
model string
httpClient Doer
encoder *tokenizer.Encoder
}
type Option func(*Client) error
// Doer performs a HTTP request.
type Doer interface {
Do(req *http.Request) (*http.Response, error)
}
// WithHTTPClient allows setting a custom HTTP client.
func WithHTTPClient(client Doer) Option {
return func(c *Client) error {
c.httpClient = client
return nil
}
}
func New(token string, baseURL string, model string, opts ...Option) (*Client, error) {
encoder, err := tokenizer.NewFromPrebuilt("coheretext-50k")
if err != nil {
return nil, fmt.Errorf("create tokenizer: %w", err)
}
c := &Client{
token: token,
baseURL: baseURL,
model: model,
httpClient: http.DefaultClient,
encoder: encoder,
}
for _, opt := range opts {
if err := opt(c); err != nil {
return nil, err
}
}
return c, nil
}
type GenerationRequest struct {
Prompt string `json:"prompt"`
}
type Generation struct {
Text string `json:"text"`
}
type generateRequestPayload struct {
Prompt string `json:"prompt"`
Model string `json:"model"`
}
type generateResponsePayload struct {
ID string `json:"id,omitempty"`
Message string `json:"message,omitempty"`
Generations []struct {
ID string `json:"id,omitempty"`
Text string `json:"text,omitempty"`
} `json:"generations,omitempty"`
}
func (c *Client) CreateGeneration(ctx context.Context, r *GenerationRequest) (*Generation, error) {
if c.baseURL == "" {
c.baseURL = "https://api.cohere.ai"
}
payload := generateRequestPayload{
Prompt: r.Prompt,
Model: c.model,
}
payloadBytes, err := json.Marshal(&payload)
if err != nil {
return nil, fmt.Errorf("marshal payload: %w", err)
}
req, err := http.NewRequestWithContext(
ctx,
http.MethodPost,
fmt.Sprintf("%s/v1/generate", c.baseURL),
bytes.NewReader(payloadBytes),
)
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
}
req.Header.Set("content-type", "application/json")
req.Header.Set("authorization", "bearer "+c.token)
res, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("send request: %w", err)
}
defer res.Body.Close()
var response generateResponsePayload
if err := json.NewDecoder(res.Body).Decode(&response); err != nil {
return nil, fmt.Errorf("parse response: %w", err)
}
if len(response.Generations) == 0 {
if strings.HasPrefix(response.Message, "model not found") {
return nil, ErrModelNotFound
}
return nil, ErrEmptyResponse
}
var generation Generation
generation.Text = response.Generations[0].Text
return &generation, nil
}
func (c *Client) GetNumTokens(text string) int {
encoded, _ := c.encoder.Encode(text)
return len(encoded)
}