Skip to content
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

assistant method passes without responding #325

Closed
Popbrain opened this issue Apr 21, 2024 · 3 comments
Closed

assistant method passes without responding #325

Popbrain opened this issue Apr 21, 2024 · 3 comments
Labels
question Further information is requested

Comments

@Popbrain
Copy link

Popbrain commented Apr 21, 2024

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.

@aallam aallam added question Further information is requested and removed question Further information is requested labels Apr 29, 2024
@aallam
Copy link
Owner

aallam commented Apr 29, 2024

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 aallam added the question Further information is requested label Apr 29, 2024
@adityak6798
Copy link

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).

@Popbrain
Copy link
Author

Popbrain commented May 30, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants