-
Notifications
You must be signed in to change notification settings - Fork 212
Davidmotson.null function fix #166
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
Open
davidmotson
wants to merge
8
commits into
main
Choose a base branch
from
davidmotson.null_function_fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c876aad
make args nullable in parsing and add test, also specify that we expe…
039b66c
ktfmt
87e6270
update test file
f9b9c7c
new test
21e4773
Merge branch 'main' into davidmotson.null_function_fix
davidmotson 309e070
add extra schema to the test
9a726c4
ktfmt
b04c05c
Merge branch 'main' into davidmotson.null_function_fix
davidmotson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
77 changes: 77 additions & 0 deletions
77
generativeai/src/test/java/com/google/ai/client/generativeai/FunctionCallingTests.kt
This file contains hidden or 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,77 @@ | ||
/* | ||
* 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 | ||
|
||
import com.google.ai.client.generativeai.type.FunctionCallPart | ||
import com.google.ai.client.generativeai.type.Schema | ||
import com.google.ai.client.generativeai.type.Tool | ||
import com.google.ai.client.generativeai.type.defineFunction | ||
import io.kotest.assertions.throwables.shouldThrowWithMessage | ||
import io.kotest.matchers.shouldBe | ||
import org.json.JSONObject | ||
import org.junit.Test | ||
|
||
internal class FunctionCallingTests { | ||
|
||
@Test | ||
fun `function calls with valid args should succeed`() = doBlocking { | ||
val functionDeclaration = | ||
defineFunction( | ||
"name", | ||
"description", | ||
Schema.str("param1", "description"), | ||
Schema.num("param2", "description"), | ||
Schema.bool("param3", "description"), | ||
Schema.int("param4", "description"), | ||
) { param1, param2, param3, param4 -> | ||
JSONObject(mapOf("result" to "success")) | ||
} | ||
val model = GenerativeModel("model", "key", tools = listOf(Tool(listOf(functionDeclaration)))) | ||
|
||
val functionCall = | ||
FunctionCallPart( | ||
"name", | ||
mapOf( | ||
("param1" to "valid parameter"), | ||
("param2" to "2.2"), | ||
("param3" to "false"), | ||
("param4" to "2"), | ||
), | ||
) | ||
|
||
val result = model.executeFunction(functionCall) | ||
|
||
result["result"] shouldBe "success" | ||
} | ||
|
||
@Test | ||
fun `function calls with invalid args should fail`() = doBlocking { | ||
val functionDeclaration = | ||
defineFunction("name", "description", Schema.str("param1", "description")) { param1 -> | ||
JSONObject(mapOf("result" to "success")) | ||
} | ||
val model = GenerativeModel("model", "key", tools = listOf(Tool(listOf(functionDeclaration)))) | ||
|
||
val functionCall = FunctionCallPart("name", mapOf("param1" to null)) | ||
|
||
shouldThrowWithMessage<RuntimeException>( | ||
"Missing argument for parameter \"param1\" for function \"name\"" | ||
) { | ||
model.executeFunction(functionCall) | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.