英文:
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()}")
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论