Repository: sashabaranov/go-openai
Pull request #183 (2023-03-20 09:25:26)

add optional params for audio api, e.g. prompt
I found the prompt argument is useful in audio api, so added following optional args from official doc
https://platform.openai.com/docs/api-reference/audio/create :
	Prompt      string // For translation, it should be in English
	Temperature float32
	Language    string // For translation, just do not use it. It seems "en" works, not confirmed...

btw.  I found there already exist a similar pull request from someone else.  I added this change in my local env last week,  today I've done some test works(for real env), so It's just a coincidence....

It is ok, that just close this pr, I just want the changes merged ASAP~

codecov[bot]: ## [Codecov](https://codecov.io/gh/sashabaranov/go-openai/pull/183?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#183](https://codecov.io/gh/sashabaranov/go-openai/pull/183?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (3fd7c0b) into [master](https://codecov.io/gh/sashabaranov/go-openai/commit/d529d13ba15d31928a414f83f9ae9730d0a9223b?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (d529d13) will **decrease** coverage by `1.80%`.
> The diff coverage is `33.33%`.

```diff
@@            Coverage Diff             @@
##           master     #183      +/-   ##
==========================================
- Coverage   70.65%   68.86%   -1.80%     
==========================================
  Files          18       18              
  Lines         535      562      +27     
==========================================
+ Hits          378      387       +9     
- Misses        121      133      +12     
- Partials       36       42       +6     
```


| [Impacted Files](https://codecov.io/gh/sashabaranov/go-openai/pull/183?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [audio.go](https://codecov.io/gh/sashabaranov/go-openai/pull/183?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-YXVkaW8uZ28=) | `45.83% <33.33%> (-7.50%)` | :arrow_down: |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)

sashabaranov: thank you for the PR! 🙌🏻 

Patch:
diff --git a/audio.go b/audio.go
index 54bd32f..6d1681a 100644
--- a/audio.go
+++ b/audio.go
@@ -16,9 +16,13 @@ const (
 )
 
 // AudioRequest represents a request structure for audio API.
+// ResponseFormat is not supported for now. We only return JSON text, which may be sufficient.
 type AudioRequest struct {
-	Model    string
-	FilePath string
+	Model       string
+	FilePath    string
+	Prompt      string // For translation, it should be in English
+	Temperature float32
+	Language    string // For translation, just do not use it. It seems "en" works, not confirmed...
 }
 
 // AudioResponse represents a response structure for audio API.
@@ -94,6 +98,47 @@ func audioMultipartForm(request AudioRequest, w *multipart.Writer) error {
 	if _, err = io.Copy(fw, modelName); err != nil {
 		return fmt.Errorf("writing model name: %w", err)
 	}
+
+	// Create a form field for the prompt (if provided)
+	if request.Prompt != "" {
+		fw, err = w.CreateFormField("prompt")
+		if err != nil {
+			return fmt.Errorf("creating form field: %w", err)
+		}
+
+		prompt := bytes.NewReader([]byte(request.Prompt))
+		if _, err = io.Copy(fw, prompt); err != nil {
+			return fmt.Errorf("writing prompt: %w", err)
+		}
+	}
+
+	// Create a form field for the temperature (if provided)
+	if request.Temperature != 0 {
+		fw, err = w.CreateFormField("temperature")
+		if err != nil {
+			return fmt.Errorf("creating form field: %w", err)
+		}
+
+		temperature := bytes.NewReader([]byte(fmt.Sprintf("%.2f", request.Temperature)))
+		if _, err = io.Copy(fw, temperature); err != nil {
+			return fmt.Errorf("writing temperature: %w", err)
+		}
+	}
+
+	// Create a form field for the language (if provided)
+	if request.Language != "" {
+		fw, err = w.CreateFormField("language")
+		if err != nil {
+			return fmt.Errorf("creating form field: %w", err)
+		}
+
+		language := bytes.NewReader([]byte(request.Language))
+		if _, err = io.Copy(fw, language); err != nil {
+			return fmt.Errorf("writing language: %w", err)
+		}
+	}
+
+	// Close the multipart writer
 	w.Close()
 
 	return nil


Test patch:
diff --git a/audio_test.go b/audio_test.go
index 2a035c9..8a9b578 100644
--- a/audio_test.go
+++ b/audio_test.go
@@ -69,6 +69,59 @@ func TestAudio(t *testing.T) {
 	}
 }
 
+func TestAudioWithOptionalArgs(t *testing.T) {
+	server := test.NewTestServer()
+	server.RegisterHandler("/v1/audio/transcriptions", handleAudioEndpoint)
+	server.RegisterHandler("/v1/audio/translations", handleAudioEndpoint)
+	// create the test server
+	var err error
+	ts := server.OpenAITestServer()
+	ts.Start()
+	defer ts.Close()
+
+	config := DefaultConfig(test.GetTestToken())
+	config.BaseURL = ts.URL + "/v1"
+	client := NewClientWithConfig(config)
+
+	testcases := []struct {
+		name     string
+		createFn func(context.Context, AudioRequest) (AudioResponse, error)
+	}{
+		{
+			"transcribe",
+			client.CreateTranscription,
+		},
+		{
+			"translate",
+			client.CreateTranslation,
+		},
+	}
+
+	ctx := context.Background()
+
+	dir, cleanup := createTestDirectory(t)
+	defer cleanup()
+
+	for _, tc := range testcases {
+		t.Run(tc.name, func(t *testing.T) {
+			path := filepath.Join(dir, "fake.mp3")
+			createTestFile(t, path)
+
+			req := AudioRequest{
+				FilePath:    path,
+				Model:       "whisper-3",
+				Prompt:      "用简体中文",
+				Temperature: 0.5,
+				Language:    "zh",
+			}
+			_, err = tc.createFn(ctx, req)
+			if err != nil {
+				t.Fatalf("audio API error: %v", err)
+			}
+		})
+	}
+}
+
 // createTestFile creates a fake file with "hello" as the content.
 func createTestFile(t *testing.T, path string) {
 	file, err := os.Create(path)