Files
go-openai/completion.go
Alexander Baranov f5b3ec4ffe Add api client code
2020-08-19 12:57:32 +03:00

61 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package gogpt
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
)
type CompletionRequest struct {
Prompt string `json:"prompt,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
Temperature float32 `json:"temperature,omitempty"`
TopP float32 `json:"top_p,omitempty"`
N int `json:"n,omitempty"`
LogProbs int `json:"logobs,omitempty"`
Echo bool `json:"echo,omitempty"`
Stop string `json:"stop,omitempty"`
PresencePenalty float32 `json:"presence_penalty,omitempty"`
FrequencyPenalty float32 `json:"frequency_penalty,omitempty"`
}
type Choice struct {
Text string `json:"text"`
Index int `json:"index"`
FinishReason string `json:"finish_reason"`
}
type CompletionResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Created uint64 `json:"created"`
Model string `json:"model"`
Сhoices []Choice `json:"choices"`
}
// CreateCompletion — API call to create a completion. This is the main endpoint of the API. Returns new text as well as, if requested, the probabilities over each alternative token at each position.
func (c *Client) CreateCompletion(ctx context.Context, engineID string, request CompletionRequest) (response CompletionResponse, err error) {
var reqBytes []byte
reqBytes, err = json.Marshal(request)
if err != nil {
return
}
urlSuffix := fmt.Sprintf("/engines/%s/completions", engineID)
req, err := http.NewRequest("POST", c.fullURL(urlSuffix), bytes.NewBuffer(reqBytes))
if err != nil {
return
}
req = req.WithContext(ctx)
err = c.sendRequest(req, &response)
return
}