-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
162 lines (142 loc) · 4.94 KB
/
example_test.go
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package sseread_test
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"github.com/mojocn/sseread"
)
func ExampleCloudflareAI_Do() {
// Retrieve the account ID and API token from the environment variables
accountID := os.Getenv("CF_ACCOUNT_ID")
apiToken := os.Getenv("CF_API_TOKEN")
cf := &sseread.CloudflareAI{
AccountID: accountID,
APIToken: apiToken,
}
// Send the POST request
response, err := cf.Do("@cf/meta/llama-2-7b-chat-fp8b", &sseread.CfTextGenerationArg{
Stream: true,
Messages: []sseread.CfTextGenerationMsg{
{Role: "system", Content: "You are a chatbot."},
{Role: "user", Content: "What is your name?"},
}})
if err != nil {
fmt.Println(err)
return
}
// Ensure the response body is closed after the function returns
defer response.Body.Close()
// Check the response status code
if response.StatusCode != http.StatusOK {
all, err := io.ReadAll(response.Body)
if err != nil {
fmt.Println(err)
return
}
log.Fatal(string(all))
return
}
// Read the response body as Server-Sent Events
channel, err := sseread.ReadCh(response.Body)
if err != nil {
fmt.Println(err)
return
}
// Initialize an empty string to store the full text of the responses
fulltext := ""
// Iterate over the events from the channel
for event := range channel {
if event == nil || event.IsSkip() {
continue
}
// Parse the JSON object from the event data
e := new(sseread.CfTextGenerationResponse)
err := json.Unmarshal(event.Data, e)
if err != nil {
log.Fatal(err, string(event.Data))
} else {
// Append the response to the fulltext string
fulltext += e.Response
}
}
// Log the full text of the responses
fmt.Println(fulltext)
}
// ExampleRead is a function that demonstrates how to read Server-Sent Events (SSE) from a specific URL.
func ExampleRead() {
// Send a GET request to the specified URL.
response, err := http.Get("https://mojotv.cn/api/sse") //API source code from https://github.com/mojocn/gptchat/blob/main/app/api/sse/route.ts
// If an error occurs during the GET request, print the error and return from the function.
if err != nil {
fmt.Println(err)
return
}
// Get the Content-Type header from the response and convert it to lowercase.
ct := strings.ToLower(response.Header.Get("Content-Type"))
// If the Content-Type is not "text/event-stream", print a message and continue.
if !strings.Contains(ct, "text/event-stream") {
fmt.Println("expect content-type: text/event-stream, but actual", ct)
}
// Ensure the response body is closed when the function returns.
defer response.Body.Close() //don't forget to close the response body
// Declare a slice to store the SSE messages.
var messages []sseread.Event
// Read the SSE messages from the response body.
err = sseread.Read(response.Body, func(msg *sseread.Event) {
// If the message is not nil, append it to the messages slice.
if msg != nil {
messages = append(messages, *msg)
}
// Print the ID, event type, and data of the message.
fmt.Println(msg.ID, msg.Event, string(msg.Data))
})
// If an error occurs while reading the SSE messages, print the error and return from the function.
if err != nil {
fmt.Println(err)
return
}
// Print the number of messages read.
fmt.Printf("length of messages is %d", len(messages))
}
// ExampleReadCh is a function that demonstrates how to read Server-Sent Events (SSE) from a specific URL using channels.
func ExampleReadCh() {
// Send a GET request to the specified URL.
response, err := http.Get("https://mojotv.cn/api/sse") //API source code from https://github.com/mojocn/gptchat/blob/main/app/api/sse/route.ts
// If an error occurs during the GET request, print the error and return from the function.
if err != nil {
fmt.Println(err)
return
}
// Get the Content-Type header from the response and convert it to lowercase.
ct := strings.ToLower(response.Header.Get("Content-Type"))
// If the Content-Type is not "text/event-stream", print a message and continue.
if !strings.Contains(ct, "text/event-stream") {
fmt.Println("expect content-type: text/event-stream, but actual", ct)
}
// Ensure the response body is closed when the function returns.
defer response.Body.Close() //don't forget to close the response body
// Read the SSE messages from the response body into a channel.
channel, err := sseread.ReadCh(response.Body)
// If an error occurs while reading the SSE messages, print the error and return from the function.
if err != nil {
fmt.Println(err)
return
}
// Declare a slice to store the SSE messages.
var messages []sseread.Event
// Loop over the channel to receive the SSE messages.
for msg := range channel {
// If the message is not nil, append it to the messages slice.
if msg != nil {
messages = append(messages, *msg)
}
// Print the ID, event type, and data of the message.
fmt.Println(msg.ID, msg.Event, string(msg.Data))
}
// Print the number of messages read.
fmt.Printf("length of messages is %d", len(messages))
}