Skip to content

Commit

Permalink
[#75] Fix: set default value for role in public Content class and b…
Browse files Browse the repository at this point in the history
…uilder (#76)

# Summary

- Specify default value for `role` as a `"user"` in a public API class
Content.
- Added tests for the verification of conversion functions (_from public
class to internal class and vice versa_).

Fixes: #75

---------

Co-authored-by: Rodrigo Lazo <[email protected]>
  • Loading branch information
PatilShreyas and rlazo authored Mar 8, 2024
1 parent 9bd6231 commit 5bb04e4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import android.graphics.Bitmap
*
* @see content
*/
class Content @JvmOverloads constructor(val role: String? = null, val parts: List<Part>) {
class Content @JvmOverloads constructor(val role: String? = "user", val parts: List<Part>) {

class Builder {
var role: String? = null
var role: String? = "user"

var parts: MutableList<Part> = arrayListOf()

Expand Down Expand Up @@ -56,7 +56,7 @@ class Content @JvmOverloads constructor(val role: String? = null, val parts: Lis
* )
* ```
*/
fun content(role: String? = null, init: Content.Builder.() -> Unit): Content {
fun content(role: String? = "user", init: Content.Builder.() -> Unit): Content {
val builder = Content.Builder()
builder.role = role
builder.init()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.ai.client.generativeai.internal.util

import com.google.ai.client.generativeai.internal.api.shared.Content
import com.google.ai.client.generativeai.internal.api.shared.TextPart
import com.google.ai.client.generativeai.type.content
import io.kotest.matchers.shouldBe
import org.junit.Test

class ConversionsTest {

@Test
fun `test content conversion toInternal (role not mentioned)`() {
val content = content { text("test") }.toInternal()
content.run {
// default role should be a "user"
role shouldBe "user"

// only one part should be present
parts.size shouldBe 1
parts[0].run { (this as TextPart).text shouldBe "test" }
}
}

@Test
fun `test content conversion toInternal (role mentioned)`() {
val content = content(role = "model") { text("test") }.toInternal()
content.run {
// Role should be a "model"
role shouldBe "model"

// only one part should be present
parts.size shouldBe 1
parts[0].run { (this as TextPart).text shouldBe "test" }
}
}

@Test
fun `test content conversion toPublic (role not mentioned)`() {
val content = Content(parts = listOf(TextPart("test"))).toPublic()
content.role shouldBe "user"
}

@Test
fun `test content conversion toPublic (role mentioned)`() {
val content = Content(role = "model", parts = listOf(TextPart("test"))).toPublic()
content.role shouldBe "model"
}
}

0 comments on commit 5bb04e4

Please sign in to comment.