Views
No views yet
pip install --upgrade google-genaigcloud auth application-default login1from google.genai import types
2import base64def
3generate(): client = genai.Client( vertexai=True, project="903806786624", location="us-central1", )
4 model = "projects/903806786624/locations/us-central1/endpoints/2455637174847012864"
5 contents = [ types.Content( role="user", parts=[ ] ) ]
6 generate_content_config = types.GenerateContentConfig(
7 temperature = 1,
8 top_p = 0.95,
9 max_output_tokens = 8192,
10 safety_settings = [types.SafetySetting(
11 category="HARM_CATEGORY_HATE_SPEECH",
12 threshold="OFF" ),
13 types.SafetySetting(
14 category="HARM_CATEGORY_DANGEROUS_CONTENT",
15 threshold="OFF"
16 ),types.SafetySettin
17 category="HARM_CATEGORY_SEXUALLY_EXPLICIT"
18 threshold="O
19 ),types.SafetySetting(
20 category="HARM_CATEGORY_HARASSMENT",
21 threshold="OFF"
22 )]
23 )1 FunctionCallingConfigMode,
2 FunctionDeclaration,
3 GoogleGenAI,
4 Type,
5 GenerateContentResponse,
6} from '@google/genai';
7
8// --- Configuration and Function Declaration ---
9
10const API_KEY = process.env.API_KEY;
11
12const getFramerUpdateInfoFunctionDeclaration: FunctionDeclaration = {
13 name: 'getFramerUpdateInfo',
14 description: `Retrieves update information or code examples from specific sections of the Framer developer documentation or the Motion animation library documentation.
15This function helps to check for the latest information on Framer Components, Overrides, and the Motion animation library.
16Relevant documentation sections include:
17- Framer Components:
18 - https://www.framer.com/developers/component-examples
19 - https://www.framer.com/developers/auto-sizing
20 - https://www.framer.com/developers/property-controls
21 - https://www.framer.com/developers/components-reference
22- Framer Overrides:
23 - https://www.framer.com/developers/overrides-introduction
24 - https://www.framer.com/developers/overrides-examples
25- Motion Animation Library:
26 - https://motion.dev/docs/
27Use this function to find out about recent changes, validate against training data, or to get code snippets.`,
28 parameters: {
29 type: Type.OBJECT,
30 properties: {
31 documentationArea: {
32 type: Type.STRING,
33 description: "The primary area of documentation to query. Supported areas: 'Components', 'Overrides', 'Motion'.",
34 },
35 specificTopic: {
36 type: Type.STRING,
37 description: "Optional. A more specific topic or sub-section within the documentation area. Examples: 'auto-sizing', 'property-controls', 'overrides-introduction', 'animation types', 'component-examples'.",
38 },
39 query: {
40 type: Type.STRING,
41 description: "The specific question about updates, conflicts with training data, or the code example being sought. E.g., 'latest changes to property controls', 'example for tap gesture animation', 'any new override examples'.",
42 },
43 },
44 required: ['documentationArea', 'query'],
45 },
46};
47
48
49// --- The Core Function for Calling the AI ---
50
51async function generateContentWithFunctions(prompt: string) {
52 if (!API_KEY) {
53 console.error('API_KEY is not configured. Please set the environment variable.');
54 return;
55 }
56
57 try {
58 const ai = new GoogleGenAI({ apiKey: API_KEY });
59
60 const response: GenerateContentResponse = await ai.models.generateContent({
61 model: 'projects/903806786624/locations/us-central1/endpoints/2455637174847012864',
62 contents: [{role: "user", parts: [{text: prompt}]}],
63 config: {
64 tools: [
65 { functionDeclarations: [getFramerUpdateInfoFunctionDeclaration] }
66 ],
67 toolConfig: {
68 functionCallingConfig: {
69 mode: FunctionCallingConfigMode.ANY,
70 allowedFunctionNames: ['getFramerUpdateInfo'],
71 },
72 },
73 },
74 });
75
76 // log response
77 if (response.functionCalls && response.functionCalls.length > 0) {
78 console.log('Function call(s) requested:');
79 response.functionCalls.forEach(fc => {
80 console.log(`Function: ${fc.name}\nArguments: ${JSON.stringify(fc.args, null, 2)}`);
81 });
82 } else if (response.text) {
83 console.log(response.text);
84 } else {
85 console.log('Received a response, but it contained no text or function calls.');
86 }
87
88 } catch (e) {
89 console.error(e);
90 }
91}