-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchat_generation.go
82 lines (74 loc) · 1.98 KB
/
chat_generation.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 vertexai
import (
"context"
"fmt"
"net/http"
)
// ChatGenerationRequest is the request for the ChatGeneration method
type ChatGenerationRequest struct {
ProjectID string
EndpointID string
Context string
ChatMessages []ChatMessage
Examples []Example
Temperature *float64
MaxDecodeSteps *int
TopK *int
TopP *float64
}
// ChatGenerationResponse is the response for the ChatGeneration method
type ChatGenerationResponse struct {
ChatPredictions []ChatPrediction `json:"predictions"`
DeployedModelID string `json:"deployedModelId"`
Model string `json:"model"`
ModelDisplayName string `json:"modelDisplayName"`
ModelVersionID string `json:"modelVersionId"`
}
// ChatPrediction is the generated response
type ChatPrediction struct {
ChatMessages []ChatMessage `json:"candidates"`
SafetyAttributes []SafetyAttributes `json:"safetyAttributes"`
}
const (
chatGenerationEndpoint = ":predict"
)
func (c *client) ChatGeneration(
ctx context.Context,
req ChatGenerationRequest,
) (*ChatGenerationResponse, error) {
payload := c.getChatGenerationPayload(req)
httpReq, err := c.requestBuilder.build(
ctx,
http.MethodPost,
req.ProjectID,
req.EndpointID,
chatGenerationEndpoint,
payload,
)
if err != nil {
return nil, fmt.Errorf("failed to create http request for predict endpoint: %v", err)
}
resp := &ChatGenerationResponse{}
err = c.sendRequest(httpReq, resp)
if err != nil {
return nil, fmt.Errorf("failed to send request to predict endpoint: %v", err)
}
return resp, nil
}
func (c *client) getChatGenerationPayload(
req ChatGenerationRequest,
) payload {
return payload{
Instances: []inputInstances{{
Context: req.Context,
Examples: req.Examples,
Messages: req.ChatMessages,
}},
Parameters: parameters{
Temperature: req.Temperature,
MaxDecodeSteps: req.MaxDecodeSteps,
TopP: req.TopP,
TopK: req.TopK,
},
}
}