这是indexloc提供的服务,不要输入任何密码
Skip to content

Added timestamp_granularities parameter to the Audio API #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;

import java.util.List;

/**
* A request for OpenAi to create transcription based on an audio file
* All fields except model are optional
Expand Down Expand Up @@ -43,4 +45,13 @@ public class CreateTranscriptionRequest {
* The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency.
*/
String language;

/**
* The timestamp granularities to populate for this transcription. response_format must be set verbose_json to use timestamp granularities.<br>
* Either or both of these options are supported: word, or segment. <br>
* Note: There is no additional latency for segment timestamps, but generating word timestamps incurs additional latency.
*/
@JsonProperty("timestamp_granularities ")
List<String> timestampGranularities;

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ public class TranscriptionResult {
*/
List<TranscriptionSegment> segments;

List<Words> words;

}
16 changes: 16 additions & 0 deletions api/src/main/java/com/theokanning/openai/audio/Words.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.theokanning.openai.audio;

import lombok.Data;

/**
* @author LiangTao
* @date 2024年05月14 09:56
**/
@Data
public class Words {
private String word;

private Double start;

private Double end;
}
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,11 @@ public TranscriptionResult createTranscription(CreateTranscriptionRequest reques
if (request.getLanguage() != null) {
builder.addFormDataPart("language", request.getLanguage());
}

if (request.getTimestampGranularities() != null && !request.getTimestampGranularities().isEmpty()) {
for (String granularity : request.getTimestampGranularities()) {
builder.addFormDataPart("timestamp_granularities[]", granularity);
}
}
return execute(api.createTranscription(builder.build()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.io.IOException;
import java.time.Duration;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -34,6 +35,7 @@ void createTranscriptionVerbose() {
CreateTranscriptionRequest createTranscriptionRequest = CreateTranscriptionRequest.builder()
.model("whisper-1")
.responseFormat("verbose_json")
.timestampGranularities(Arrays.asList("word", "segment"))
.build();

TranscriptionResult result = service.createTranscription(createTranscriptionRequest, englishAudioFilePath);
Expand All @@ -42,6 +44,7 @@ void createTranscriptionVerbose() {
assertEquals("english", result.getLanguage());
assertTrue(result.getDuration() > 0);
assertEquals(1, result.getSegments().size());
assertEquals(2, result.getWords().size());
}

@Test
Expand Down