BomKhung.Com Đã Quay Trở Lại
Hoạt Động Dưới Tên Miền Hung.Pro.VN
00 Days
00 Hours
00 Minutes
00 Seconds
Hiện website đang cập nhật nội dung bài viết, nếu có lỗi gì mọi người có thể thông báo cho mình Tại đây!

[C#] How to make chat with Gemini AI Google Winform

Xin chào các bạn, bài viết hôm nay mình tiếp tục hướng dẫn các bạn cách kết nối trò chuyện Chat Gemini AI Google bằng C#, Winform.

[C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform

[C#] How to make chat with Gemini AI Google Winform

Gemini AI là gì?

Gemini AI dòng mô hình ngôn ngữ lớn mới nhất của Google, cung cấp tất cả thông tin cần thiết để bắt đầu tạo ứng dụng bằng Gemini.

Đầu tiên, các bạn cần tạo một tài khoản API key.

Các bạn truy cập vào địa chỉ sau: https://aistudio.google.com/

Tiếp đến, các bạn tạo một API key để sử dụng như hình bên dưới:
[C#] How to make chat with Gemini AI Google Winform

Tiếp đến, các bạn tạo project C#, vào cài đặt thư viện Google_GenerativeAI từ nuget:
PM> Install-Package Google_GenerativeAI

Thiết kế giao diện chat Winform như hình bên dưới:
[C#] How to make chat with Gemini AI Google Winform

Full source code C#:

using GenerativeAI.Methods;
using GenerativeAI.Models;
using GenerativeAI.Types;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GeminiAIDemo
{
    public partial class Form1 : Form
    {
        ChatSession chatSession;
        public string apiKey = "AIzaSyCjhe255nXjOQwst6NM6RrXCbz03xl66HM";
        public Form1()
        {
            InitializeComponent();
        }

        private async void btnSend_Click(object sender, EventArgs e)
        {
            var message = txtMessage.Text.Trim();
            await SendMessageAsync(message);

        }

        private async Task SendMessageAsync(string message)
        {
            if (btnSend.Text != "Send message") return;

            if (message.Length > 0)
            {
                txtMessage.Text = "";
                btnSend.Text = "Waiting...";

                txtBody.SelectionAlignment = HorizontalAlignment.Left;
                txtBody.SelectionColor = Color.Blue;
                txtBody.AppendText($"Me: {message}\n");

                txtBody.SelectionAlignment = HorizontalAlignment.Right;
                txtBody.SelectionColor = Color.Red;
                var result = await chatSession.SendMessageAsync(message);
                txtBody.AppendText($"Gemini AI: {result}\n");

                txtBody.SelectionAlignment = HorizontalAlignment.Left;
                txtBody.SelectionColor = txtBody.ForeColor;
               
                btnSend.Text = "Send message";
                txtBody.ScrollToCaret();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {    
            var model = new GenerativeModel(apiKey);
            chatSession = model.StartChat(new StartChatParams());

        }

        private async  void txtMessage_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                var message = txtMessage.Text.Trim();
                await SendMessageAsync(message);
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("https://hung.pro.vn/");
        }
    }
}


Chúc các bạn thành công với thủ thuật trên, để tạo cho mình một ứng dụng chat AT tốt nhất nhé
 

DOWNLOAD SOURCE CODE


PASSWORD UNZIP: HUNG.PRO.VN 

إرسال تعليق

Đồng ý sữ dụng cookie
Chúng tôi sử dụng cookie trên trang web này để phân tích lưu lượng truy cập, ghi nhớ tùy chọn của bạn và tối ưu hóa trải nghiệm của bạn.
Xem thêm
Oops!
Có vẻ như kết nối internet của bạn có vấn đề. Vui lòng kết nối lại và duyệt web.
AdBlock Detected!
Chúng tôi phát hiện bạn đang sử dụng plugin chặn quảng cáo trong trình duyệt của mình.
Doanh thu chúng tôi kiếm được từ quảng cáo được sử dụng để quản lý trang web này, chúng tôi yêu cầu bạn đưa trang web của chúng tôi vào danh sách trắng trong plugin chặn quảng cáo của bạn.
Site is Blocked
Sorry! This site is not available in your country.
Kỹ thuật số thế hệ tiếp theo Chào mừng bạn đến với trò chuyện WhatsApp
Xin chào! Chúng tôi có thể giúp gì cho bạn hôm nay?
Nhập vào đây...