-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
54f3383
commit 4abc964
Showing
3 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
openai-core/src/main/scala/io/cequence/openaiscala/domain/ChatCompletionInterceptData.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.cequence.openaiscala.domain | ||
|
||
import io.cequence.openaiscala.domain.response.ChatCompletionResponse | ||
import io.cequence.openaiscala.domain.settings.CreateChatCompletionSettings | ||
|
||
case class ChatCompletionInterceptData( | ||
messages: Seq[BaseMessage], | ||
setting: CreateChatCompletionSettings, | ||
response: ChatCompletionResponse, | ||
timeRequestReceived: java.util.Date, | ||
timeResponseReceived: java.util.Date | ||
) |
58 changes: 58 additions & 0 deletions
58
...c/main/scala/io/cequence/openaiscala/service/adapter/ChatCompletionInterceptAdapter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package io.cequence.openaiscala.service.adapter | ||
|
||
import io.cequence.openaiscala.domain.{BaseMessage, ChatCompletionInterceptData} | ||
import io.cequence.openaiscala.domain.response.ChatCompletionResponse | ||
import io.cequence.openaiscala.domain.settings.CreateChatCompletionSettings | ||
import io.cequence.openaiscala.service.OpenAIChatCompletionService | ||
import io.cequence.wsclient.service.CloseableService | ||
import io.cequence.wsclient.service.adapter.ServiceWrapper | ||
|
||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
private class ChatCompletionInterceptAdapter[S <: OpenAIChatCompletionService]( | ||
intercept: ChatCompletionInterceptData => Future[Unit] | ||
)( | ||
underlying: S | ||
)( | ||
implicit ec: ExecutionContext | ||
) extends ServiceWrapper[S] | ||
with CloseableService | ||
with OpenAIChatCompletionService { | ||
|
||
// we just delegate all the calls to the underlying service | ||
override def wrap[T]( | ||
fun: S => Future[T] | ||
): Future[T] = fun(underlying) | ||
|
||
// but for the chat completion we adapt the messages and settings | ||
override def createChatCompletion( | ||
messages: Seq[BaseMessage], | ||
settings: CreateChatCompletionSettings | ||
): Future[ChatCompletionResponse] = { | ||
val timeRequestReceived = new java.util.Date() | ||
|
||
for { | ||
response <- underlying.createChatCompletion( | ||
messages, | ||
settings | ||
) | ||
|
||
_ <- { | ||
val timeResponseReceived = new java.util.Date() | ||
|
||
intercept( | ||
ChatCompletionInterceptData( | ||
messages, | ||
settings, | ||
response, | ||
timeRequestReceived, | ||
timeResponseReceived | ||
) | ||
) | ||
} | ||
} yield response | ||
} | ||
|
||
override def close(): Unit = | ||
underlying.close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters