如何在使用Kotlin的OpenAI API客户端时捕获异常?

huangapple go评论65阅读模式
英文:

How can I catch the exception when I use OpenAI API client for Kotlin?

问题

我在我的Android Studio项目中使用OpenAI API客户端来进行Kotlin编程。

使用Code A将语音转录为文本。

1: 我不知道val transcription = openAI.transcription(transcriptionRequest)是否会抛出异常,你能告诉我吗?

2: 当val transcription = openAI.transcription(transcriptionRequest)崩溃时,我该如何获取错误信息?

顺便说一下,Code B是源代码,它返回Transcription类,并且似乎不会抛出异常。

英文:

I use OpenAI API client for Kotlin in my Android Studio project.

I use Code A to transcribe a voice to text

1: I don't know if val transcription = openAI.transcription(transcriptionRequest) can throw exception, could you tell me?

2: How can I get the error informatioin when val transcription = openAI.transcription(transcriptionRequest) crashed ?

BTW, the Code B is source code, It returns Transcription class, and it seems not to throw exception.

Code A

  val apiKey = "sk-..."
    val openAI = OpenAI(apiKey)

    val transcriptionRequest = TranscriptionRequest(
        audio = FileSource(path = mInfo.soundFilename.toPath(), fileSystem = FileSystem.SYSTEM),
        model = ModelId("whisper-1"),
    )

   val transcription = openAI.transcription(transcriptionRequest)

    mInfo.text = transcription.text

Code B

  @BetaOpenAI
    public suspend fun transcription(request: TranscriptionRequest): Transcription

答案1

得分: 1

以下是您要求的代码部分的中文翻译:

val apiKey = "sk-..."
val openAI = OpenAI(apiKey)

val transcriptionRequest = TranscriptionRequest(
    audio = FileSource(path = mInfo.soundFilename.toPath(), fileSystem = FileSystem.SYSTEM),
    model = ModelId("whisper-1"),
)

try {
    val transcription = openAI.transcription(transcriptionRequest)
    mInfo.text = transcription.text
} catch (e: OpenAIException) {
    // 处理 OpenAI 异常
    println("OpenAIException: ${e.message}")
} catch (e: OpenAIHttpException) {
    // 处理 OpenAIHttpException
    println("OpenAIHttpException: ${e.message}")
} catch (e: OpenAIAPIException) {
    // 处理 OpenAIAPIException
    println("OpenAIAPIException: ${e.message}")
} catch (e: Exception) {
    // 处理其他异常
    println("意外错误: ${e.message}")
}

希望这对您有所帮助。如果您需要更多信息或其他翻译,请告诉我。

英文:

Looking at aallam/openai-kotlin issue 131, you can try and encapsulate your OpenAI code with the exceptions from the com.aallam.openai.api.exception package:

val apiKey = "sk-..."
val openAI = OpenAI(apiKey)

val transcriptionRequest = TranscriptionRequest(
    audio = FileSource(path = mInfo.soundFilename.toPath(), fileSystem = FileSystem.SYSTEM),
    model = ModelId("whisper-1"),
)

try {
    val transcription = openAI.transcription(transcriptionRequest)
    mInfo.text = transcription.text
} catch (e: OpenAIException) {
    // Handle OpenAIExceptions
    println("OpenAIException: ${e.message}")
} catch (e: OpenAIHttpException) {
    // Handle OpenAIHttpExceptions
    println("OpenAIHttpException: ${e.message}")
} catch (e: OpenAIAPIException) {
    // Handle OpenAIAPIExceptions
    println("OpenAIAPIException: ${e.message}")
} catch (e: Exception) {
    // Handle any other exceptions
    println("Unexpected error: ${e.message}")
}

That would handle the specific exceptions OpenAIException, OpenAIHttpException, and OpenAIAPIException that may be thrown by the OpenAI Kotlin API client, in addition to a generic catch-all for any other exceptions.


Note that, if you are getting an empty error message, that would typically mean that the Exception itself is being thrown, but no specific message has been assigned to it.
In this case, it would appear that the OpenAI Kotlin client library is not assigning a message to the exception that it throws when the API key is invalid.

You might get more information by calling e.toString() instead of e.message in your catch blocks, as toString() typically includes both the exception class name and the message, while e.message only provides the message.
However, if the library does not provide a message, e.toString() might not provide much more information.

That would be:

val apiKey = "sk-..."
val openAI = OpenAI(apiKey)

val transcriptionRequest = TranscriptionRequest(
    audio = FileSource(path = mInfo.soundFilename.toPath(), fileSystem = FileSystem.SYSTEM),
    model = ModelId("whisper-1"),
)

try {
    val transcription = openAI.transcription(transcriptionRequest)
    mInfo.text = transcription.text
} catch (e: OpenAIException) {
    // Handle OpenAIExceptions
    println("OpenAIException: ${e.toString()}")
} catch (e: OpenAIHttpException) {
    // Handle OpenAIHttpExceptions
    println("OpenAIHttpException: ${e.toString()}")
} catch (e: OpenAIAPIException) {
    // Handle OpenAIAPIExceptions
    println("OpenAIAPIException: ${e.toString()}")
} catch (e: Exception) {
    // Handle any other exceptions
    println("Unexpected error: ${e.toString()}")
}

huangapple
  • 本文由 发表于 2023年5月13日 18:55:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定