-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.go
80 lines (73 loc) · 2.32 KB
/
inference.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
package togetherai
import (
"encoding/json"
"fmt"
)
type InferenceConfig struct {
Model string `json:"model"`
Prompt string `json:"prompt"`
MaxTokens int32 `json:"max_tokens"`
Stop *[]string `json:"stop"`
Temperature *float32 `json:"temperature"`
TopP *float32 `json:"top_p"`
TopK *int32 `json:"top_k"`
RepetitionPenalty *float32 `json:"repetition_penalty"`
LogProbs *int32 `json:"logprobs"`
}
type inferenceRequestBody struct {
InferenceConfig
StreamTokens bool `json:"stream_tokens"`
}
type InferenceResponseBody struct {
Status string `json:"status"`
Prompt []string `json:"prompt"`
Model string `json:"model"`
ModelOwner string `json:"model_owner"`
Tags struct {
} `json:"tags"`
NumReturns int `json:"num_returns"`
Args struct {
Model string `json:"model"`
Prompt string `json:"prompt"`
MaxTokens int `json:"max_tokens"`
Stop []string `json:"stop"`
Temperature float64 `json:"temperature"`
TopP float64 `json:"top_p"`
TopK int `json:"top_k"`
RepetitionPenalty int `json:"repetition_penalty"`
} `json:"args"`
Subjobs []interface{} `json:"subjobs"`
Output struct {
Choices []struct {
FinishReason string `json:"finish_reason"`
Index int `json:"index"`
Text string `json:"text"`
} `json:"choices"`
RawComputeTime float64 `json:"raw_compute_time"`
ResultType string `json:"result_type"`
} `json:"output"`
}
// NewInference creates a new inference using the provided configuration.
//
// config: The configuration for the inference.
// respBody: The response body containing the inference result.
// err: An error if the inference creation fails.
func (c *Client) NewInference(config InferenceConfig) (respBody *InferenceResponseBody, err error) {
resp, err := c.getRestyClient().
R().
SetBody(inferenceRequestBody{
InferenceConfig: config,
StreamTokens: false,
}).
Post("/inference")
if err != nil {
return
}
if resp.StatusCode() != 200 {
err = fmt.Errorf("received status code %d with body %s", resp.StatusCode(), resp.Body())
return
}
respBody = &InferenceResponseBody{}
err = json.Unmarshal(resp.Body(), respBody)
return
}