-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodelsocket.go
More file actions
61 lines (61 loc) · 1.66 KB
/
Copy pathmodelsocket.go
File metadata and controls
61 lines (61 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Package modelsocket provides a Go client for the ModelSocket protocol.
//
// ModelSocket is a WebSocket-based protocol for efficiently integrating with
// Large Language Models (LLMs). It provides streaming text generation, tool
// calling, and sequence forking capabilities.
//
// # Thread Safety
//
// [Client] and [Seq] are safe for concurrent use by multiple goroutines.
// However, only one [Seq.Generate] call can be active per sequence at a time.
// [GenStream] should only be consumed by a single goroutine.
//
// # Basic Usage
//
// ctx := context.Background()
//
// // Connect to server
// client, err := modelsocket.Connect(ctx, "wss://example.com/ws", "api-key")
// if err != nil {
// log.Fatal(err)
// }
// defer client.Close(ctx)
//
// // Open a sequence
// seq, err := client.Open(ctx, "model-name")
// if err != nil {
// log.Fatal(err)
// }
// defer seq.Close(ctx)
//
// // Append user message
// err = seq.Append(ctx, "Hello!", modelsocket.AsUser())
// if err != nil {
// log.Fatal(err)
// }
//
// // Generate response using iterator
// stream, err := seq.Generate(ctx, modelsocket.GenerateAsAssistant())
// if err != nil {
// log.Fatal(err)
// }
//
// for chunk, err := range stream.Chunks(ctx) {
// if err != nil {
// log.Fatal(err)
// }
// fmt.Print(chunk.Text)
// }
//
// # Observability
//
// Use [WithLogger], [WithOnSend], and [WithOnReceive] to add logging and
// monitoring to the client:
//
// client, err := modelsocket.Connect(ctx, url, apiKey,
// modelsocket.WithLogger(slog.Default()),
// modelsocket.WithOnSend(func(req *modelsocket.MSRequest) {
// metrics.RequestsSent.Inc()
// }),
// )
package modelsocket