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
"""
At the command line, only need to run once to install the package via pip:
$ pip install google-generativeai
"""
import google.generativeai as genai
genai.configure(api_key="YOUR API KEY")
defaults = {
'model': 'models/chat-bison-001',
'temperature': 0.25,
'candidate_count': 1,
'top_k': 40,
'top_p': 0,
}
context = "We're playing a game. I'm thinking of a word, and I need to get you to guess that word. But I can't say the word itself. I'll give you clues, and you'll respond with a guess. Your guess should be a single word only."
examples = [
[
"This is a thing into which feathers go, and it makes it more comfortable to sit.",
"A pillow?"
],
[
"You might find this on a couch.",
"A cushion?"
],
[
"Yes. Okay next. This is something you might use when drops are falling from the sky",
"An umbrella?"
],
[
"Next: Oops",
"A mistake?"
],
[
"Next: booboo",
"A boo-boo?"
],
[
"No, another word for that",
"A wound?"
]
]
messages = [
"This is a type of creature that lived a very long time ago",
"A dinosaur?",
"Next: This is a food that has leaves",
"Lettuce?",
"You might use that to make what I'm thinking of",
"A salad?"
]
messages.append("NEXT REQUEST")
response = genai.chat(
**defaults,
context=context,
examples=examples,
messages=messages
)
print(response.last) # Response of the AI to your most recent request
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
const { DiscussServiceClient } = require("@google-ai/generativelanguage");
const { GoogleAuth } = require("google-auth-library");
const MODEL_NAME = "models/chat-bison-001";
const API_KEY = "YOUR API KEY";
const client = new DiscussServiceClient({
authClient: new GoogleAuth().fromAPIKey(API_KEY),
});
const context = "We're playing a game. I'm thinking of a word, and I need to get you to guess that word. But I can't say the word itself. I'll give you clues, and you'll respond with a guess. Your guess should be a single word only.";
const examples = [
{
"input": {
"content": "This is a thing into which feathers go, and it makes it more comfortable to sit."
},
"output": {
"content": "A pillow?"
}
},
{
"input": {
"content": "You might find this on a couch."
},
"output": {
"content": "A cushion?"
}
},
{
"input": {
"content": "Yes. Okay next. This is something you might use when drops are falling from the sky"
},
"output": {
"content": "An umbrella?"
}
},
{
"input": {
"content": "Next: Oops"
},
"output": {
"content": "A mistake?"
}
},
{
"input": {
"content": "Next: booboo"
},
"output": {
"content": "A boo-boo?"
}
},
{
"input": {
"content": "No, another word for that"
},
"output": {
"content": "A wound?"
}
}
];
const messages = [
{
"content": "This is a type of creature that lived a very long time ago"
},
{
"content": "A dinosaur?"
},
{
"content": "Next: This is a food that has leaves"
},
{
"content": "Lettuce?"
},
{
"content": "You might use that to make what I'm thinking of"
},
{
"content": "A salad?"
}
];
messages.push({ "content": "NEXT REQUEST" });
client.generateMessage({
// required, which model to use to generate the result
model: MODEL_NAME,
// optional, 0.0 always uses the highest-probability result
temperature: 0.25,
// optional, how many candidate results to generate
candidateCount: 1,
// optional, number of most probable tokens to consider for generation
top_k: 40,
// optional, for nucleus sampling decoding strategy
top_p: 0,
prompt: {
// optional, sent on every request and prioritized over history
context: context,
// optional, examples to further finetune responses
examples: examples,
// required, alternating prompt/response messages
messages: messages,
},
}).then(result => {
console.log(JSON.stringify(result, null, 2));
});