Using Golang to complete ChatGPT streaming, making it easier to join your own project

chatGPT_streaming

go语言使用openai的ChatGPT接口实践,使用流式传输,类似ChatGPT网页效果,并且可以让ChatGPT的服务单独部署(单独部署到非大中华区的服务器),并用grpc streaming 做中间层,保证不受墙的影响。使用websocket最终通信,后端考虑多种情况,健壮可用。 非常方便移植到你自己的项目,赶紧来看看吧!

演示

日志 GIF演示

前置知识

  • http text/event-stream
  • Streaming with gRPC
  • websocket
  • go routine and channel

event_stream

访问后端使用的是text/event-stream 配置:

  • BaseURL默认是:https://api.openai.com/v1
  • token: 使用自己注册的openai的token,注意不要泄漏自己的token
  • 可以选择代理请求,方便本地测试,详细逻辑见具体代码
      proxyUrl, err := url.Parse("http://127.0.0.1:10887")
      if err != nil {
          log.Fatal().Msgf("failed to init proxy: %v", err)
          return
      }
      http.DefaultTransport = &http.Transport{Proxy: http.ProxyURL(proxyUrl)}
      config.HTTPClient.Transport = http.DefaultTransport
      client := goopenai.NewClientWithConfig(config)

启动:

  • 直接执行main()函数
  • 然后打开ws_demo.html
  • 愉快的对话了,注意我使用了context上下文传递,超过4090token数量会接口抛错,具体逻辑请自行修改

grpc_stream

配置:

  • 在grpc_stream/grpc.go 或者 grpc_stream/grpc/grpc_server.go 配置常量Token
  • 代理设置grpc_stream/grpc/grpc_server.go StreamChatCompletion方法里 启动
  • 先启动grpc服务:grpc_stream/grpc/grpc_server.go main()
  • 再启动webscket服务端:grpc_stream/grpc.go main()
  • 然后打开ws_demo.html
  • 愉快的对话了,注意我使用了context上下文传递,超过4090token数量会接口抛错,具体逻辑请自行修改
Similar Resources

A simple api built in Go that facilitates directly sending email from your client side html to your inbox

go-email-service A simple api built in Go that facilitates directly sending emai

Dec 28, 2021

Sync your bank transactions with google sheets using Open Banking APIs

Sync your bank transactions with google sheets using Open Banking APIs

Jul 22, 2022

Sync your bank transactions with google sheets using Open Banking APIs

this is a markdown version of the copy on the site landing page: https://youneedaspreadsheet.com You need a spreadsheet 📊 Get on top of your finances

Jul 22, 2022

Simple CRUD API written in Go, built using AWS SAM tool and using the AWS' infrastructure.

Simple CRUD API written in Go, built using AWS SAM tool and using the AWS' infrastructure.

tutor-pet API Simple CRUD API written in Go, built using AWS SAM tool and using the AWS' infrastructure. Macro architecture: Code architecture: Pre-Re

Aug 17, 2022

To-Do list in GoLang with Redis as a backend -- Sample project for proof of concepts

🗒 GoLang To-Do List Sample App w/ Redis 📝 Description A simple to-do list app written in GoLang with Redis as a backend showing a few GoLang feature

Dec 14, 2021

This project implements a Go client library for the Hipchat API.

Hipchat This project implements a Go client library for the Hipchat API (API version 2 is not supported). Pull requests are welcome as the API is limi

Jan 3, 2023

Display last GitLab project git commit. Page is optimized for an e-paper device.

Display last GitLab project git commit. Page is optimized for an e-paper device.

git-on-epaper A gitlab webhook for push notifications on a project. The webhook serves a HTML that shows the last push on the project with the followi

Dec 12, 2022

A project template for a weather client in Go

A project template for a weather client in Go

Sep 17, 2022

Handle any SQS use case, monitor any queue. Reusable for any project! Invoke in a goroutine to process SQS messages.

GOSQS This package is intended to be a Go SQS listener that can be imported and invoked as a goroutine handled by the life cycle of your service. It's

Dec 22, 2021
Go api infra - Infrastructure for making api on golang so easy

Go Infra Api Infrastructre methods and types for make api simple Response abstra

Jun 18, 2022
Morecontext - Context.Context helpers to make your life slightly easier

morecontext context.Context helpers to make your life slightly easier. -- import

Mar 5, 2022
A complete and simple wrapper for the Hypixel API

Gopixel A simple and complete1 wrapper for the hypixel API Key features Full API coverage1 Autocomplete for fields Near complete structs MIT license I

Apr 26, 2022
Go Twitter REST and Streaming API v1.1

go-twitter go-twitter is a Go client library for the Twitter API. Check the usage section or try the examples to see how to access the Twitter API. Fe

Dec 28, 2022
This is a single-instance streaming server with chat.

Table of Contents AlbertoBroadcast stream server Build requirements Older Go Versions Compile and install Docker build Building the Container Running

Dec 24, 2021
StreamWall - WIP demo application which streams music in exchange for streaming sats

Stream Wall Music examples borrowed from https://ableandthewolf.com/ check them

Jul 9, 2022
Service that wrap up different movies-related APIs like IMDB and match it to streaming services
Service that wrap up different movies-related APIs like IMDB and match it to streaming services

Service that wrap up different movies-related APIs like IMDB and match it to streaming services. That way you can check in which platforms you can find your favorite movies.

Feb 10, 2022
Todo-list - In this project using golang and mySql to create todo-list to Add and remove
Todo-list - In this project using golang and mySql to create todo-list to Add and remove

TODO-Fullstack-App-Go-Gin-Postgres-React This fullstack application creates a TODO List Web Page using the Go/Gin/Postgres/React Stack. Starting the a

Apr 7, 2022
A very simple local client-server calculator project built using Cobra and socket programming, written in Go.

Golculator Introduction A very simple local client-server calculator project built using Cobra and socket programming, written in Go. Setup and run Fi

Apr 3, 2022
Asteroids written in Golang using raylib using an ECS system.

Asteroids written in Golang using Raylib using an ECS system.

Feb 12, 2022