Upgrade to Pro — share decks privately, control downloads, hide ads and more …

從 Gemini API 到 LINE Bot

從 Gemini API 到 LINE Bot

隨著AI工具的出現,各種應用迅速崛起,我們將展示如何低成本打造個人QA機器人,建立客戶支援生態系統的AI原型服務。在當前技術迅速發展的背景下,自動化與智慧型客戶服務解決方案日益顯得重要。Gemini API與Bot為開發者提供了一條低成本、高效率建立QA機器人、打造客戶支援生態系統AI原型的途徑。

此次分享聚焦兩大主題:

了解Gemini API和Line Bot基礎:Gemini API提供廣泛的數據和服務存取能力,Line Bot則是亞洲市場上受歡迎的溝通平台。開發者需要先瞭解這些技術的功能、限制與最佳實踐。

設計用戶體驗:開發QA機器人時,考慮用戶互動方式極為關鍵。這包括創建自然直觀的對話、友好的歡迎信息、理解自然語言的能力,以及提供快速準確的回應。透過第三方服務與Gemini的結合,可以讓開發者連接到公司數據庫或其他API,擴展機器人的功能和回答範圍。

The advent of AI tools has led to a surge in applications, including the creation of low-cost, self-built QA Bots for customer support ecosystems. In this fast-evolving tech landscape, the importance of automated and smart customer service solutions is growing. Gemini API and Bot present a unique chance for developers to create efficient, cost-effective QA robots, establishing prototype AI customer service systems.

This presentation focuses on two main areas:

Understanding Gemini API and Line Bot Basics: Gemini API provides extensive access to data and services, and Line Bot is a popular communication platform in Asia. Developers must first grasp these technologies' capabilities, limitations, and best practices.

Designing User Experience: When developing QA robots, it's essential to consider user interaction. This involves creating natural and intuitive dialogues, friendly greetings, understanding natural language, and delivering quick, accurate responses. Leveraging third-party services with Gemini can extend the robot's functionalities and response range by connecting to databases and other APIs.

Caesar Chi

March 30, 2024
Tweet

More Decks by Caesar Chi

Other Decks in Technology

Transcript

  1. Caesar Chi 2024/03 從 Gemini API 到 Line Bot Build

    with AI 2024 Taichung https://gdg.community.dev/events/details/google-gdg-taichung-presents-build-with-ai-2024-taichung-3/
  2. 個⼈經歷 • Career • 2024 - EXMA-Square • 2021 -

    TransIot — CTO • 2020 - Undercover - Tech infra • 2018 - Awoo Tech Manager • 2017 - EXMA-Square • 2016 - Hiiir Tech Manager • 2014 - Mitac full-stack developer • 2012 - Dlink Front end developer • Community • JSDC core-team Caesar
  3. Gemini 在 Node.js const { GoogleGenerativeAI } = require("@google/generative-ai"); //

    Access your API key as an environment variable (see "Set up your API key" above) const genAI = new GoogleGenerativeAI(process.env.API_KEY);
  4. const { GoogleGenerativeAI } = require("@google/generative-ai"); // Access your API

    key as an environment variable (see "Set up your API key" above) const genAI = new GoogleGenerativeAI(process.env.API_KEY); async function run() { // For text-only input, use the gemini-pro model const model = genAI.getGenerativeModel({ model: "gemini-pro"}); const prompt = "Write a story about a magic backpack." const result = await model.generateContent(prompt); const response = await result.response; const text = response.text(); console.log(text); } run();
  5. // Converts local file information to a GoogleGenerativeAI.Part object. function

    fileToGenerativePart(path, mimeType) { return { inlineData: { data: Buffer.from(fs.readFileSync(path)).toString("base64"), mimeType }, }; } async function run() { // For text-and-image input (multimodal), use the gemini-pro-vision model const model = genAI.getGenerativeModel({ model: "gemini-pro-vision" }); const prompt = "What's different between these pictures?"; const imageParts = [ fileToGenerativePart("image1.png", "image/png"), fileToGenerativePart("image2.jpeg", "image/jpeg"), ]; const result = await model.generateContent([prompt, ...imageParts]); const response = await result.response; const text = response.text(); console.log(text);
  6. // Converts local file information to a GoogleGenerativeAI.Part object. function

    fileToGenerativePart(path, mimeType) { return { inlineData: { data: Buffer.from(fs.readFileSync(path)).toString("base64"), mimeType }, }; } async function run() { // For text-and-image input (multimodal), use the gemini-pro-vision model const model = genAI.getGenerativeModel({ model: "gemini-pro-vision" }); const prompt = "What's different between these pictures?"; const imageParts = [ fileToGenerativePart("image1.png", "image/png"), fileToGenerativePart("image2.jpeg", "image/jpeg"), ]; const result = await model.generateContent([prompt, ...imageParts]); const response = await result.response; const text = response.text(); console.log(text);
  7. async function run() { // For text-only input, use the

    gemini-pro model const model = genAI.getGenerativeModel({ model: "gemini-pro"}); const chat = model.startChat({ history: [ { role: "user", parts: [{ text: "Hello, I have 2 dogs in my house." }], }, { role: "model", parts: [{ text: "Great to meet you. What would you like to know?" }], }, ], generationConfig: { maxOutputTokens: 100, }, }); const msg = "How many paws are in my house?"; const result = await chat.sendMessage(msg); const response = await result.response; const text = response.text();
  8. //... const result = await model.generateContentStream([prompt, ...imageParts]); let text =

    ''; for await (const chunk of result.stream) { const chunkText = chunk.text(); console.log(chunkText); text += chunkText; } //...
  9. // Initialize Vertex with your Cloud project and location const

    vertexAI = new VertexAI({project: projectId, location: location}); // Instantiate the model const generativeModel = vertexAI.getGenerativeModel({ model: model, // The following parameters are optional // They can also be passed to individual content generation requests safety_settings: [ { category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, threshold: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE, }, ], generation_config: {max_output_tokens: 256}, }); const request = { contents: [{role: 'user', parts: [{text: 'Tell me something dangerous.'}]}], };
  10. 純⽂字模式 if (event.message.type === "text") { const msg = await

    gemini.textOnly(event.message.text); await line.reply(event.replyToken, [{ type: "text", text: msg }]); return res.end(); }
  11. 多模態輸入 if (event.message.type === "image") { const imageBinary = await

    line.getImageBinary(event.message.id); const msg = await gemini.multimodal(imageBinary); await line.reply(event.replyToken, [{ type: "text", text: msg }]); return res.end(); } JavaScript
  12. 聊天模式 if (event.message.type === "text") { const msg = await

    gemini.chat(event.message.text); await line.reply(event.replyToken, [{ type: "text", text: msg }]); return res.end(); }
  13. TAR