File size: 1,779 Bytes
2e51161
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import google.generativeai as genai
import google.ai.generativelanguage as glm
import os

genai.configure(api_key='AIzaSyALFCivW9GP25mbxL3W7Fv6u7m2ZHVlC8w')


def multiply(a: float, b: float):
    """returns a * b."""
    return a*b


def add(a: float, b: float):
    """returns a + b."""
    return a+b


calculator = {
    'function_declarations': [
        {
            'name': 'multiply',
            'description': 'Returns the product of two numbers.',
            'parameters': {
                'type_': 'OBJECT',
                'properties': {
                    'a': {'type_': 'NUMBER'},
                    'b': {'type_': 'NUMBER'}
                },
                'required': ['a', 'b']
            }
        },
        {
            'name': 'add',
            'description': 'Returns the sum of two numbers.',
            'parameters': {
                'type_': 'OBJECT',
                'properties': {
                    'a': {'type_': 'NUMBER'},
                    'b': {'type_': 'NUMBER'}
                },
                'required': ['a', 'b']
            }
        }
    ]
}


model = genai.GenerativeModel(model_name='gemini-1.0-pro', tools=calculator)

chat = model.start_chat()
response = chat.send_message('I have 57 cats and 44 dogs, how many pets I have?')
fc = response.candidates[0].content.parts[0].function_call
assert fc.name == 'add'

result = add(fc.args['a'], fc.args['b'])

response = chat.send_message(
    glm.Content(
        parts=[
            glm.Part(
                function_response=glm.FunctionResponse(
                    name='add',
                    response={'result': result}
                )
            )
        ]
    )
)
print(response.text)