Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Follow publication

Building an AI Chatbot with Go

Create an AI assistant using Gemini and Websockets.

Cheikh seck
Dev Genius
Published in
7 min readJan 17, 2025

--

Chat GPT changed the world when it was released. It spawned new niches, driving the development of innovative courses, platforms, and businesses all centered around the capabilities of generative AI.

I like to use Chat GPT for text analysis, proofreading and code generation. When it comes to generating code, I usually know the code I need ahead of time and prompt Chat GPT until I have the desired output.

Maximalfocus — unsplash.com

In this post, I’ll be building a Go-based web server that enables access to a large language model (LLM) via websocket connection. I’ll name this project “Terminal GPT,” and it will be powered by Gemini AI.

GPT Interface

To start, I’ll need an interface that can manage the lifecycle of a chat session and — most importantly — send user messages to Gemini’s API.

To communicate with the API, I’ll use the following Go package:

github.com/google/generative-ai-go/genai

With the requirements outlined above, here is the code of the interface:

import "github.com/gorilla/websocket"
// GPT interface
type GPT interface {
// Register a client session by ID. Store websocket connection
// so that LLM responses are streamed back to client
Register(clientID string, c *websocket.Conn)
// DeRegister will remove the client connection and close
// the chat session
DeRegister(clientID string)
// SendMessage sends a user message to the LLM.
// The response is handled internaly since
// connection is stored on register function. The clientID
// field will be used to locate the connection and stream
// LLM responses back to the connection.
SendMessage(clientID, message string) error
}

Once this is in place, I’ll start with the first implementation of this interface.

GPT Implementation

To track a chat session, I’ll add a new type called Session and it will have two fields:

  1. One with type genai.ChatSession from the Gemini Go package. This type abstracts chat session management.
  2. One with type websocket.Conn from the Gorilla…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

--

--

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Written by Cheikh seck

[Beta] Checkout my AI agents: https://zeroaigency.web.app/ Available for hire as a technical writer or software developer: cheeikhseck@gmail.com

Responses (1)

Write a response