|
1 | | -# multimodal-live-api-web-console |
| 1 | +# Multimodal Live API - Web console |
| 2 | + |
| 3 | +This repository contains a react-based starter app for using the [Multimodal Live API](https://ai.google.dev/gemini-api) over a websocket. It provides modules for streaming audio playback, recording user media such as from a microphone, webcam or screen capture as well as a unified log view to aid in development of your application. |
| 4 | + |
| 5 | +We have provided several example applications on other branches of this repository: |
| 6 | + |
| 7 | +- [demos/GenExplainer](https://github.com/google-gemini/multimodal-live-api-web-console/tree/demos/genexplainer) |
| 8 | +- [demos/GenWeather](https://github.com/google-gemini/multimodal-live-api-web-console/tree/demos/genweather) |
| 9 | + |
| 10 | +Below is an example of an entire application that will use Google Search grounding and then render graphs using [vega-embed](https://github.com/vega/vega-embed): |
| 11 | + |
| 12 | +```typescript |
| 13 | +import { type FunctionDeclaration, SchemaType } from "@google/generative-ai"; |
| 14 | +import { useEffect, useRef, useState, memo } from "react"; |
| 15 | +import vegaEmbed from "vega-embed"; |
| 16 | +import { useLiveAPIContext } from "../../contexts/LiveAPIContext"; |
| 17 | + |
| 18 | +export const declaration: FunctionDeclaration = { |
| 19 | + name: "render_altair", |
| 20 | + description: "Displays an altair graph in json format.", |
| 21 | + parameters: { |
| 22 | + type: SchemaType.OBJECT, |
| 23 | + properties: { |
| 24 | + json_graph: { |
| 25 | + type: SchemaType.STRING, |
| 26 | + description: |
| 27 | + "JSON STRING representation of the graph to render. Must be a string, not a json object", |
| 28 | + }, |
| 29 | + }, |
| 30 | + required: ["json_graph"], |
| 31 | + }, |
| 32 | +}; |
| 33 | + |
| 34 | +export function Altair() { |
| 35 | + const [jsonString, setJSONString] = useState<string>(""); |
| 36 | + const { client, setConfig } = useLiveAPIContext(); |
| 37 | + |
| 38 | + useEffect(() => { |
| 39 | + setConfig({ |
| 40 | + model: "models/gemini-v3-s-test", |
| 41 | + systemInstruction: { |
| 42 | + parts: [ |
| 43 | + { |
| 44 | + text: 'You are my helpful assistant. Any time I ask you for a graph call the "render_altair" function I have provided you. Dont ask for additional information just make your best judgement.', |
| 45 | + }, |
| 46 | + ], |
| 47 | + }, |
| 48 | + tools: [{ googleSearch: {} }, { functionDeclarations: [declaration] }], |
| 49 | + }); |
| 50 | + }, [setConfig]); |
| 51 | + |
| 52 | + useEffect(() => { |
| 53 | + client.on("toolcall", (toolCall) => { |
| 54 | + console.log(`got toolcall`, toolCall); |
| 55 | + const fc = toolCall.functionCalls.find( |
| 56 | + (fc) => fc.name === declaration.name |
| 57 | + ); |
| 58 | + if (fc) { |
| 59 | + const str = (fc.args as any).json_graph; |
| 60 | + setJSONString(str); |
| 61 | + } |
| 62 | + }); |
| 63 | + }, [client]); |
| 64 | + |
| 65 | + const embedRef = useRef<HTMLDivElement>(null); |
| 66 | + |
| 67 | + useEffect(() => { |
| 68 | + if (embedRef.current && jsonString) { |
| 69 | + vegaEmbed(embedRef.current, JSON.parse(jsonString)); |
| 70 | + } |
| 71 | + }, [embedRef, jsonString]); |
| 72 | + return <div className="vega-embed" ref={embedRef} />; |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +## development |
| 77 | + |
| 78 | +This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). |
| 79 | +Project consists of: |
| 80 | + |
| 81 | +- an Event-emitting websocket-client to ease communication between the websocket and the front-end |
| 82 | +- communication layer for processing audio in and out |
| 83 | +- a boilerplate view for starting to build your apps and view logs |
| 84 | + |
| 85 | +## Available Scripts |
| 86 | + |
| 87 | +In the project directory, you can run: |
| 88 | + |
| 89 | +### `npm start` |
| 90 | + |
| 91 | +Runs the app in the development mode.\ |
| 92 | +Open [http://localhost:3000](http://localhost:3000) to view it in the browser. |
| 93 | + |
| 94 | +The page will reload if you make edits.\ |
| 95 | +You will also see any lint errors in the console. |
| 96 | + |
| 97 | +### `npm run build` |
| 98 | + |
| 99 | +Builds the app for production to the `build` folder.\ |
| 100 | +It correctly bundles React in production mode and optimizes the build for the best performance. |
| 101 | + |
| 102 | +The build is minified and the filenames include the hashes.\ |
| 103 | +Your app is ready to be deployed! |
| 104 | + |
| 105 | +See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. |
0 commit comments