audiotranslation.go

  1// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2
  3package openai
  4
  5import (
  6	"bytes"
  7	"context"
  8	"io"
  9	"mime/multipart"
 10	"net/http"
 11
 12	"github.com/openai/openai-go/internal/apiform"
 13	"github.com/openai/openai-go/internal/apijson"
 14	"github.com/openai/openai-go/internal/requestconfig"
 15	"github.com/openai/openai-go/option"
 16	"github.com/openai/openai-go/packages/param"
 17	"github.com/openai/openai-go/packages/resp"
 18)
 19
 20// AudioTranslationService contains methods and other services that help with
 21// interacting with the openai API.
 22//
 23// Note, unlike clients, this service does not read variables from the environment
 24// automatically. You should not instantiate this service directly, and instead use
 25// the [NewAudioTranslationService] method instead.
 26type AudioTranslationService struct {
 27	Options []option.RequestOption
 28}
 29
 30// NewAudioTranslationService generates a new service that applies the given
 31// options to each request. These options are applied after the parent client's
 32// options (if there is one), and before any request-specific options.
 33func NewAudioTranslationService(opts ...option.RequestOption) (r AudioTranslationService) {
 34	r = AudioTranslationService{}
 35	r.Options = opts
 36	return
 37}
 38
 39// Translates audio into English.
 40func (r *AudioTranslationService) New(ctx context.Context, body AudioTranslationNewParams, opts ...option.RequestOption) (res *Translation, err error) {
 41	opts = append(r.Options[:], opts...)
 42	path := "audio/translations"
 43	err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
 44	return
 45}
 46
 47type Translation struct {
 48	Text string `json:"text,required"`
 49	// Metadata for the response, check the presence of optional fields with the
 50	// [resp.Field.IsPresent] method.
 51	JSON struct {
 52		Text        resp.Field
 53		ExtraFields map[string]resp.Field
 54		raw         string
 55	} `json:"-"`
 56}
 57
 58// Returns the unmodified JSON received from the API
 59func (r Translation) RawJSON() string { return r.JSON.raw }
 60func (r *Translation) UnmarshalJSON(data []byte) error {
 61	return apijson.UnmarshalRoot(data, r)
 62}
 63
 64type AudioTranslationNewParams struct {
 65	// The audio file object (not file name) translate, in one of these formats: flac,
 66	// mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm.
 67	File io.Reader `json:"file,required" format:"binary"`
 68	// ID of the model to use. Only `whisper-1` (which is powered by our open source
 69	// Whisper V2 model) is currently available.
 70	Model AudioModel `json:"model,omitzero,required"`
 71	// An optional text to guide the model's style or continue a previous audio
 72	// segment. The
 73	// [prompt](https://platform.openai.com/docs/guides/speech-to-text#prompting)
 74	// should be in English.
 75	Prompt param.Opt[string] `json:"prompt,omitzero"`
 76	// The sampling temperature, between 0 and 1. Higher values like 0.8 will make the
 77	// output more random, while lower values like 0.2 will make it more focused and
 78	// deterministic. If set to 0, the model will use
 79	// [log probability](https://en.wikipedia.org/wiki/Log_probability) to
 80	// automatically increase the temperature until certain thresholds are hit.
 81	Temperature param.Opt[float64] `json:"temperature,omitzero"`
 82	// The format of the output, in one of these options: `json`, `text`, `srt`,
 83	// `verbose_json`, or `vtt`.
 84	//
 85	// Any of "json", "text", "srt", "verbose_json", "vtt".
 86	ResponseFormat AudioTranslationNewParamsResponseFormat `json:"response_format,omitzero"`
 87	paramObj
 88}
 89
 90// IsPresent returns true if the field's value is not omitted and not the JSON
 91// "null". To check if this field is omitted, use [param.IsOmitted].
 92func (f AudioTranslationNewParams) IsPresent() bool { return !param.IsOmitted(f) && !f.IsNull() }
 93
 94func (r AudioTranslationNewParams) MarshalMultipart() (data []byte, contentType string, err error) {
 95	buf := bytes.NewBuffer(nil)
 96	writer := multipart.NewWriter(buf)
 97	err = apiform.MarshalRoot(r, writer)
 98	if err != nil {
 99		writer.Close()
100		return nil, "", err
101	}
102	err = writer.Close()
103	if err != nil {
104		return nil, "", err
105	}
106	return buf.Bytes(), writer.FormDataContentType(), nil
107}
108
109// The format of the output, in one of these options: `json`, `text`, `srt`,
110// `verbose_json`, or `vtt`.
111type AudioTranslationNewParamsResponseFormat string
112
113const (
114	AudioTranslationNewParamsResponseFormatJSON        AudioTranslationNewParamsResponseFormat = "json"
115	AudioTranslationNewParamsResponseFormatText        AudioTranslationNewParamsResponseFormat = "text"
116	AudioTranslationNewParamsResponseFormatSRT         AudioTranslationNewParamsResponseFormat = "srt"
117	AudioTranslationNewParamsResponseFormatVerboseJSON AudioTranslationNewParamsResponseFormat = "verbose_json"
118	AudioTranslationNewParamsResponseFormatVTT         AudioTranslationNewParamsResponseFormat = "vtt"
119)