Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
GoとServer-Sent Events
Search
shuntaka
June 30, 2023
0
8.9k
GoとServer-Sent Events
shuntaka
June 30, 2023
Tweet
Share
More Decks by shuntaka
See All by shuntaka
話題のMCPとは何か? 基礎知識と活用事例をご紹介 / What is the Trending MCP? Introduction to Basic Knowledge and Practical Applications
shuntaka
0
660
MCPに入門する/ Introduction to MCP
shuntaka
101
63k
RustでGoogle CalendarのMCPを実装した話 / Implementing Google Calendar MCP in Rust
shuntaka
4
1.9k
サーバーレスAPI(API Gateway+Lambda)とNext.jsで 個人ブログを作ろう!
shuntaka
0
1.5k
NestJS Prisma on Fargate構成で作るWeb API開発Tips
shuntaka
1
16k
GoでDynamoDBのCLI(便利)を作った
shuntaka
0
130
20190827_Auth0_Rules_使ってみた.pdf
shuntaka
0
300
AWSのCI/CDサービスを試してみた!
shuntaka
0
260
Goでブラウザ業務を自動化する
shuntaka
0
2.3k
Featured
See All Featured
The Cult of Friendly URLs
andyhume
78
6.4k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
137
33k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
30
2.4k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.3k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
41
2.3k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.2k
Designing for humans not robots
tammielis
253
25k
GraphQLの誤解/rethinking-graphql
sonatard
71
11k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.6k
Facilitating Awesome Meetings
lara
54
6.4k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
24
2.7k
Transcript
GoとServer-Sent Events shuntaka 2022/04/12 Let's Go Talk #9 (GoのLT会)
自己紹介 宜しくお願いします😌 埼玉からお送りしています! GitHub: https://github.com/shuntaka9576 blog: https://shuntaka.dev 仕事: AWS,IoT,サーバサイド,TypeScript,Pythonなど Go歴:
仕事ではほぼなく、ときどき趣味で書きます、、
目次 ・そもそもServer-Sent Events ・Server-Sent Eventsを調査した経緯 ・Goで実装する ・作ったツールの紹介
そもそも Server-Sent Events
Server-Sent Events(出典,詳細: MDN) リアルタイム通信をWebアプリケーションに実装するための技術 ・HTTP/1.1とHTTP/2に対応 ・単方向通信 WebSocketは双方向で、専用プロトコル
None
None
フィールドは複数ある(出典,詳細: MDN) ・event … イベントの種別 ・data … メッセージ内容(今回はdataだけ知っていればOK) ・id …
EventSourceに設定するイベント ID ・retry … 指定時間待機後再接続要求
Server-Sent Eventsを調査した 経緯
OpenAI API ・OpenAIのAPIのサービス ・ChatGPTのWeb版(chat.openai.com)の機能がAPIとして提供 されている
ChatGPTの課金体系 ・ChatGPTのサブスクとAPIは課金体系が**別** ・ChatGPT Plusはサブスク$20 ・OpenAI APIは従量課金 ※ Open AI APIのGPT-4は、waitlistに登録しないと使えない。
仮説 使い方次第ではサブスクよりAPIを使った方が安くなるケースがあ りそう。
CLIを作ろう Gopher CLI作りがち問題...
CLI設計 ・Web版のような徐々に出力される体験にしたい 🤔
参考
Goで実装する
実装方法 1. github.com/sashabaranov/go-openai 2. github.com/r3labs/sse 3. 自前
3を選択 1. github.com/sashabaranov/go-openai => 多分一番穏便(ライブラリ内でSSEは自前実装) 2. github.com/r3labs/sse => GET以外のメソッドで未対応。パッチ当てればOK 3.
自前 => 2.を参考にしつつ実装した
None
None
GitHubコード Response.Bodyをリアルタイム逐次読み出し
None
*bufio.Scanner *Response.Body (io.Reader) ストリーム読み 取りgoroutine ① ReadEvent ② Response GitHubコード
改行\n\nで逐次読み出し scanner.Split(splitFunc) メインgoroutine channel ② Response ③ ④ 標準出力 Server-Sent Event
None
作ったツールの紹介 github.com/shuntaka9576/oax
詳細はZennのブログ記事へ
oax(OpenAI eXecuter) 動作 その1 所要2分くらいあれば実演
oax(OpenAI eXecuter) 動作 その1
oax(OpenAI eXecuter) 動作 その2
使ってよかったライブラリ github.com/alecthomas/kong github.com/itchyny/timefmt-go github.com/ktr0731/go-fuzzyfinder(2つ目Gifは99.9%こちらのラ イブラリのおかげ)
まとめ SSEのリアルタイムイベントハンドリングは、Goの得意分野 ・net/httpの豊富な機能 ・io.Readerのリアルタイムストリームを読み出し ・goroutineやchannelを使った並行処理
ご静聴ありがとうございました。