openai-kotlin icon indicating copy to clipboard operation
openai-kotlin copied to clipboard

assistant method passes without responding

Open Popbrain opened this issue 10 months ago • 2 comments

Description

Run the assistant method as in the test code below. AsyncTaskService is used for asynchronous execution. However, when I run it, the process ends in the assistant method and "Before start" is output to the log, but "ID : {assistant.id}" is not. Additionally, no exceptions seem to occur.

By the way, it works fine when I run it inside coroutineScope{ }. I use custom CoroutineScope, it will not work. Is there something wrong with the implementation?

Steps to Reproduce

@SpringBootTest
@ActiveProfiles("devLocal")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class ChatGPTServiceTest @Autowired constructor(
	private val asyncTask: AsyncTaskService
) {

	@Test
        fun performTest() {
                runBlocking {
		        try {
                                println("Before start")
                                val assistant = OpenAI("{API_KEY}").assistant(id = AssistantId("{ASSISTANT_ID}"))
                                println("ID : ${assistant.id}")
                        } catch(t: Throwable) {
                                t.printStackTrace()
                        }
                }
        }
}

JUnit class

@Service
class AsyncTaskService @Autowired constructor(
	private val trackingUseCase: TrackingUseCase
) {

	@OptIn(ExperimentalCoroutinesApi::class)
	private val asyncTaskDispatcher = Dispatchers.IO.limitedParallelism(5)
	private val supervisor = SupervisorJob()

	private val scope = CoroutineScope(asyncTaskDispatcher + supervisor)

	fun execute(task: suspend () -> Unit) {
		scope.launch {
			performTask(task)
		}
	}

	private suspend fun performTask(task: suspend () -> Unit) {
		try {
			task()
		} catch (e: Exception) {
			trackingUseCase.asyncSaveErrorLog(e =  e)
		}
	}
}

This class is singleton.

Environment

  • openai-kotlin version: 3.7.1
  • Kotlin version: 1.8.22
  • OS: macOS

Additional Info

Developing with SpringBoot.

Popbrain avatar Apr 21 '24 12:04 Popbrain

I can't see where asyncTask is used inside ChatGPTServiceTest. The function assistant is suspendable, meaning the execution shouldn't proceed until a response is received. However, calling asyncTask.execute { ... } starts a new coroutine asynchronously and moves to the next line; it won't wait for the coroutine to finish.

aallam avatar Apr 29 '24 22:04 aallam

I have the same issue on a windows machine. The call works only in coroutineScope, but doesn't work in any other way - the function execution ends on that call (no further code is executed, and the assistant is not initialized).

adityak6798 avatar May 06 '24 18:05 adityak6798

@aallam @adityak6798 Thanks for the response. This issue is resolved. The cause was my mistake. It was not a bug in the SDK.

Popbrain avatar May 30 '24 14:05 Popbrain