-
Notifications
You must be signed in to change notification settings - Fork 0
/
makers-api.js
81 lines (69 loc) · 2.51 KB
/
makers-api.js
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
const {TextServiceClient} = require('@google-ai/generativelanguage').v1beta2;
const {GoogleAuth} = require('google-auth-library');
const {DiscussServiceClient} = require('@google-ai/generativelanguage');
const chat = async () => {
const MODEL_NAME = 'models/chat-bison-001';
const API_KEY = process.env.GOOGLE_PALM_API_KEY;
const client = new DiscussServiceClient({
authClient: new GoogleAuth().fromAPIKey(API_KEY),
});
const result = await client.generateMessage({
model: MODEL_NAME,
temperature: 0.5, // Optional. Value `0.0` always uses the highest-probability result.
candidateCount: 1, // Optional. The number of candidate results to generate.
prompt: {
// optional, preamble context to prime responses
context: 'Respond to all questions with a rhyming poem.',
// Optional. Examples for further fine-tuning of responses.
examples: [
{
input: {content: 'What is the capital of California?'},
output: {
content:
`If the capital of California is what you seek,
Sacramento is where you ought to peek.`,
},
},
],
// Required. Alternating prompt/response messages.
messages: [{content: 'How tall is the Eiffel Tower?'}],
},
});
console.log(result[0].candidates[0].content);
return result;
};
const prompt = async (prompt, config) => {
const MODEL_NAME = 'models/text-bison-001';
const API_KEY = process.env.GOOGLE_PALM_API_KEY;
const client = new TextServiceClient({
authClient: new GoogleAuth().fromAPIKey(API_KEY),
});
console.log(`Calling MakerSuite API with prompt: ${prompt}`);
const result = await client.generateText({
model: MODEL_NAME,
prompt: {
text: prompt,
},
...config,
});
console.log('Result:', JSON.stringify(result));
return result;
};
const embeddings = async (text) => {
const MODEL_NAME = 'models/embedding-gecko-001';
const API_KEY = process.env.GOOGLE_PALM_API_KEY;
const client = new TextServiceClient({
authClient: new GoogleAuth().fromAPIKey(API_KEY),
});
const result = await client.embedText({
model: MODEL_NAME,
text: text,
});
// console.log(JSON.stringify(result[0].embedding.value));
return result[0].embedding;
};
module.exports = {
prompt,
embeddings,
chat,
};