Skip to content

Commit

Permalink
feat(java): expose the instances used to create Futures (#5)
Browse files Browse the repository at this point in the history
This adds a `ChatFutures.getChatHistory()` helper method for Java
developers to be able to obtain the chat history. This new method should
be equivalent to Kotlin's `Chat.history`.
  • Loading branch information
thatfiredev authored Dec 14, 2023
1 parent 1a68081 commit 440f960
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions .changes/cemetery-caption-bird-authority.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type":"MINOR","changes":["Expose the instances used to create Future APIs in java"]}
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,17 @@ abstract class ChatFutures internal constructor() {
*/
abstract fun sendMessageStream(prompt: Content): Publisher<GenerateContentResponse>

private class FuturesImpl(val chat: Chat) : ChatFutures() {
/** Returns the [Chat] instance that was used to create this instance */
abstract fun getChat(): Chat

private class FuturesImpl(private val chat: Chat) : ChatFutures() {
override fun sendMessage(prompt: Content): ListenableFuture<GenerateContentResponse> =
SuspendToFutureAdapter.launchFuture { chat.sendMessage(prompt) }

override fun sendMessageStream(prompt: Content): Publisher<GenerateContentResponse> =
chat.sendMessageStream(prompt).asPublisher()

override fun getChat(): Chat = chat
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ abstract class GenerativeModelFutures internal constructor() {
*/
abstract fun startChat(history: List<Content>): ChatFutures

private class FuturesImpl(val model: GenerativeModel) : GenerativeModelFutures() {
/** Returns the [GenerativeModel] instance that was used to create this object */
abstract fun getGenerativeModel(): GenerativeModel

private class FuturesImpl(private val model: GenerativeModel) : GenerativeModelFutures() {
override fun generateContent(
vararg prompt: Content
): ListenableFuture<GenerateContentResponse> =
Expand All @@ -79,6 +82,8 @@ abstract class GenerativeModelFutures internal constructor() {
override fun startChat(): ChatFutures = startChat(emptyList())

override fun startChat(history: List<Content>): ChatFutures = from(model.startChat(history))

override fun getGenerativeModel(): GenerativeModel = model
}

companion object {
Expand Down

0 comments on commit 440f960

Please sign in to comment.