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/respjson"
 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	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
 50	JSON struct {
 51		Text        respjson.Field
 52		ExtraFields map[string]respjson.Field
 53		raw         string
 54	} `json:"-"`
 55}
 56
 57// Returns the unmodified JSON received from the API
 58func (r Translation) RawJSON() string { return r.JSON.raw }
 59func (r *Translation) UnmarshalJSON(data []byte) error {
 60	return apijson.UnmarshalRoot(data, r)
 61}
 62
 63type AudioTranslationNewParams struct {
 64	// The audio file object (not file name) translate, in one of these formats: flac,
 65	// mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm.
 66	File io.Reader `json:"file,omitzero,required" format:"binary"`
 67	// ID of the model to use. Only `whisper-1` (which is powered by our open source
 68	// Whisper V2 model) is currently available.
 69	Model AudioModel `json:"model,omitzero,required"`
 70	// An optional text to guide the model's style or continue a previous audio
 71	// segment. The
 72	// [prompt](https://platform.openai.com/docs/guides/speech-to-text#prompting)
 73	// should be in English.
 74	Prompt param.Opt[string] `json:"prompt,omitzero"`
 75	// The sampling temperature, between 0 and 1. Higher values like 0.8 will make the
 76	// output more random, while lower values like 0.2 will make it more focused and
 77	// deterministic. If set to 0, the model will use
 78	// [log probability](https://en.wikipedia.org/wiki/Log_probability) to
 79	// automatically increase the temperature until certain thresholds are hit.
 80	Temperature param.Opt[float64] `json:"temperature,omitzero"`
 81	// The format of the output, in one of these options: `json`, `text`, `srt`,
 82	// `verbose_json`, or `vtt`.
 83	//
 84	// Any of "json", "text", "srt", "verbose_json", "vtt".
 85	ResponseFormat AudioTranslationNewParamsResponseFormat `json:"response_format,omitzero"`
 86	paramObj
 87}
 88
 89func (r AudioTranslationNewParams) MarshalMultipart() (data []byte, contentType string, err error) {
 90	buf := bytes.NewBuffer(nil)
 91	writer := multipart.NewWriter(buf)
 92	err = apiform.MarshalRoot(r, writer)
 93	if err == nil {
 94		err = apiform.WriteExtras(writer, r.ExtraFields())
 95	}
 96	if err != nil {
 97		writer.Close()
 98		return nil, "", err
 99	}
100	err = writer.Close()
101	if err != nil {
102		return nil, "", err
103	}
104	return buf.Bytes(), writer.FormDataContentType(), nil
105}
106
107// The format of the output, in one of these options: `json`, `text`, `srt`,
108// `verbose_json`, or `vtt`.
109type AudioTranslationNewParamsResponseFormat string
110
111const (
112	AudioTranslationNewParamsResponseFormatJSON        AudioTranslationNewParamsResponseFormat = "json"
113	AudioTranslationNewParamsResponseFormatText        AudioTranslationNewParamsResponseFormat = "text"
114	AudioTranslationNewParamsResponseFormatSRT         AudioTranslationNewParamsResponseFormat = "srt"
115	AudioTranslationNewParamsResponseFormatVerboseJSON AudioTranslationNewParamsResponseFormat = "verbose_json"
116	AudioTranslationNewParamsResponseFormatVTT         AudioTranslationNewParamsResponseFormat = "vtt"
117)