Spaces:
Paused
Paused
File size: 8,249 Bytes
8c741f6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | /**
* @license
* 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.
*/
import { expect, use } from "chai";
import * as chaiAsPromised from "chai-as-promised";
import { FunctionDeclarationSchemaType, GoogleGenerativeAI } from "../..";
import { Content } from "../../types";
use(chaiAsPromised);
/**
* Integration tests against live backend.
*/
describe("generateContent - tools", function () {
this.timeout(60e3);
this.slow(10e3);
// This test can be flaky
// eslint-disable-next-line no-restricted-properties
it("non-streaming, tools usage", async () => {
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY || "");
const model = genAI.getGenerativeModel(
{
model: "gemini-1.5-flash-latest",
tools: [
{
functionDeclarations: [
{
name: "find_movies",
description:
"find movie titles currently playing in theaters based on any description, genre, title words, etc.",
parameters: {
type: FunctionDeclarationSchemaType.OBJECT,
properties: {
location: {
type: FunctionDeclarationSchemaType.STRING,
description:
"The city and state, e.g. San Francisco, CA or a zip code e.g. 95616",
},
description: {
type: FunctionDeclarationSchemaType.STRING,
description:
"Any kind of description including category or genre, title words, attributes, etc.",
},
},
required: ["description"],
},
},
{
name: "find_theaters",
description:
"find theaters based on location and optionally movie title which are is currently playing in theaters",
parameters: {
type: FunctionDeclarationSchemaType.OBJECT,
properties: {
location: {
type: FunctionDeclarationSchemaType.STRING,
description:
"The city and state, e.g. San Francisco, CA or a zip code e.g. 95616",
},
movie: {
type: FunctionDeclarationSchemaType.STRING,
description: "Any movie title",
},
},
required: ["location"],
},
},
{
name: "get_showtimes",
description:
"Find the start times for movies playing in a specific theater",
parameters: {
type: FunctionDeclarationSchemaType.OBJECT,
properties: {
location: {
type: FunctionDeclarationSchemaType.STRING,
description:
"The city and state, e.g. San Francisco, CA or a zip code e.g. 95616",
},
movie: {
type: FunctionDeclarationSchemaType.STRING,
description: "Any movie title",
},
theater: {
type: FunctionDeclarationSchemaType.STRING,
description: "Name of the theater",
},
date: {
type: FunctionDeclarationSchemaType.STRING,
description: "Date for requested showtime",
},
},
required: ["location", "movie", "theater", "date"],
},
},
],
},
],
},
{ apiVersion: "v1beta" },
);
const src1 = {
role: "user",
parts: [
{
text: "Which theaters in Mountain View show Barbie movie?",
},
],
};
const exp1 = {
role: "model",
parts: [
{
functionCall: {
name: "find_theaters",
args: {
location: "Mountain View, CA",
movie: "Barbie",
},
},
},
],
};
const src2 = {
role: "function",
parts: [
{
functionResponse: {
name: "find_theaters",
response: {
name: "find_theaters",
content: {
movie: "Barbie",
theaters: [
{
name: "AMC Mountain View 16",
address: "2000 W El Camino Real, Mountain View, CA 94040",
},
{
name: "Regal Edwards 14",
address: "245 Castro St, Mountain View, CA 94040",
},
],
},
},
},
},
],
};
const result1 = await model.generateContentStream({
contents: [src1],
});
const response1 = await result1.response;
expect(response1.candidates.length).to.equal(1);
expect(response1.candidates[0].content.role).to.equal("model");
expect(response1.candidates[0].content.parts.length).to.equal(1);
expect(response1.candidates[0].content).to.deep.equal(exp1);
const result3 = await model.generateContent({
contents: [src1, exp1, src2],
});
const response3 = result3.response;
expect(response3.text()).include("AMC Mountain View 16");
expect(response3.text()).include("Regal Edwards 14");
});
it("streaming, tools usage", async () => {
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY || "");
const model = genAI.getGenerativeModel(
{
model: "gemini-1.5-flash-latest",
tools: [
{
functionDeclarations: [
{
name: "getTemperature",
description:
"Get current temperature in degrees Celsius in a given city",
parameters: {
type: FunctionDeclarationSchemaType.OBJECT,
properties: {
city: { type: FunctionDeclarationSchemaType.STRING },
},
required: ["city"],
},
},
],
},
],
},
{ apiVersion: "v1beta" },
);
const src1: Content = {
role: "user",
parts: [
{
text: "Is the temperature the same in New York and San Jose right now?",
},
],
};
const src2: Content = {
role: "model",
parts: [
{
functionCall: {
name: "getTemperature",
args: { city: "New York" },
},
},
],
};
const src3: Content = {
role: "model",
parts: [
{
functionCall: {
name: "getTemperature",
args: { city: "San Jose" },
},
},
],
};
const fn1 = {
role: "function",
parts: [
{
functionResponse: {
name: "getTemperature",
response: {
name: "getTemperature",
content: {
temperature: "30",
},
},
},
},
],
};
const result = await model.generateContentStream({
contents: [src1, src2, fn1, src3, fn1],
});
const response = await result.response;
console.log(response.text());
expect(response.text()).to.match(/(\bsame\b|\byes\b)/i);
});
});
|