-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchat.go
48 lines (39 loc) · 1.18 KB
/
chat.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
package openrouter
import (
"context"
"errors"
"net/http"
)
// Chat message role defined by the Sensa API.
type ModelName string
const (
ChatMessageRoleUser = "user"
ChatMessageRoleSystem = "system"
ChatMessageRoleAssistant = "assistant"
)
var (
ErrChatCompletionStreamNotSupported = errors.New("streaming is not supported with this method, please use CreateChatCompletionStream") //nolint:lll
ErrCompletionUnsupportedModel = errors.New("this model is not supported with this method") //nolint:lll
)
// CreateChatCompletion — API call to Create a completion for the chat message.
func (c *Client) CreateChatCompletion(
ctx context.Context,
request *ChatCompletionRequest,
) (response *ChatCompletionResponse, err error) {
if request.Stream {
err = ErrChatCompletionStreamNotSupported
return
}
urlSuffix := "/chat/completions"
request.Model = wrapperModels[request.Model]
if !checkSupportsModel(request.Model) {
err = ErrCompletionUnsupportedModel
return
}
req, err := c.requestBuilder.Build(ctx, http.MethodPost, c.fullURL(urlSuffix), request)
if err != nil {
return
}
err = c.sendRequest(req, &response)
return
}