-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathconfig.go
127 lines (112 loc) · 3.34 KB
/
config.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
package deepseek
import (
"fmt"
"net/http"
"net/url"
"time"
)
// BaseURL is the base URL for the Deepseek API
const BaseURL string = "https://api.deepseek.com/v1"
// HTTPDoer is an interface for the Do method of http.Client
type HTTPDoer interface {
Do(req *http.Request) (*http.Response, error)
}
// Client is the main struct for interacting with the Deepseek API.
type Client struct {
AuthToken string // The authentication token for the API
BaseURL string // The base URL for the API
Timeout time.Duration // The timeout for the current Client
Path string // The path for the API request. Defaults to "chat/completions"
HTTPClient HTTPDoer // The HTTP client to send the request and get the response
}
// NewClient creates a new client with an authentication token and an optional custom baseURL.
// If no baseURL is provided, it defaults to "https://api.deepseek.com/".
// You can't set path with this method. If you want to set path, use NewClientWithOptions.
func NewClient(AuthToken string, baseURL ...string) *Client {
if AuthToken == "" {
return nil
}
// check if this is a valid URL
if len(baseURL) > 0 {
_, err := url.ParseRequestURI(baseURL[0])
if err != nil {
fmt.Printf("Invalid URL: %s. \nIf you are using options please use NewClientWithOptions", baseURL[0])
return nil
}
}
url := "https://api.deepseek.com/"
if len(baseURL) > 0 {
url = baseURL[0]
}
return &Client{
AuthToken: AuthToken,
BaseURL: url,
Path: "chat/completions",
}
}
// Option configures a Client instance
type Option func(*Client) error
// NewClientWithOptions creates a new client with required authentication token and optional configurations.
// Defaults:
// - BaseURL: "https://api.deepseek.com/"
// - Timeout: 5 minutes
func NewClientWithOptions(authToken string, opts ...Option) (*Client, error) {
client := &Client{
AuthToken: authToken,
BaseURL: "https://api.deepseek.com/",
Timeout: 5 * time.Minute,
Path: "chat/completions",
}
for _, opt := range opts {
if err := opt(client); err != nil {
return nil, fmt.Errorf("failed to apply option: %w", err)
}
}
return client, nil
}
// WithBaseURL sets the base URL for the API client
func WithBaseURL(url string) Option {
return func(c *Client) error {
c.BaseURL = url
return nil
}
}
// WithTimeout sets the timeout for API requests
func WithTimeout(d time.Duration) Option {
return func(c *Client) error {
if d < 0 {
return fmt.Errorf("timeout must be a positive duration")
}
c.Timeout = d
return nil
}
}
// WithTimeoutString parses a duration string and sets the timeout
// Example valid values: "5s", "2m", "1h"
func WithTimeoutString(s string) Option {
return func(c *Client) error {
d, err := time.ParseDuration(s)
if err != nil {
return fmt.Errorf("invalid timeout duration %q: %w", s, err)
}
return WithTimeout(d)(c)
}
}
// WithPath sets the path for the API request. Defaults to "chat/completions", if not set.
// Example usages would be "/c/chat/" or any http after the baseURL extension
func WithPath(path string) Option {
if path == "" {
path = "chat/completions"
}
return func(c *Client) error {
c.Path = path
return nil
}
}
// WithHTTPClient sets the http client for the API client.
func WithHTTPClient(httpclient HTTPDoer) Option {
return func(c *Client) error {
c.HTTPClient = httpclient
return nil
}
}