Skip to content

Commit

Permalink
update readme.d
Browse files Browse the repository at this point in the history
  • Loading branch information
mojocn authored Mar 18, 2024
1 parent d60eaf6 commit cf442bc
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 165 deletions.
88 changes: 88 additions & 0 deletions cloudflare_ai_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package sseread

import (
"bytes"
"encoding/json"
"io"
"net/http"
"os"
"testing"
)

// https://developers.cloudflare.com/workers-ai/models/zephyr-7b-beta-awq/#using-streaming
type llamaMsg struct {
Response string `json:"response"`
P string `json:"p"`
}

// TestReadFromCloudflareLama2 is a test function for the ReadCh function in the sseread package.
// It sends a POST request to the Cloudflare API and reads the response body as Server-Sent Events.
// For each event, it parses the JSON object from the event data and appends the response to the fulltext string.
// If an error occurs during the POST request, the reading of the events, or the JSON unmarshalling, it fails the test.
func TestReadFromCloudflareLama2(t *testing.T) {
// Retrieve the account ID and API token from the environment variables
accountID := os.Getenv("CF_ACCOUNT_ID")
apiToken := os.Getenv("CF_API_TOKEN")
if accountID == "" || apiToken == "" {
t.Fatal("CF_ACCOUNT_ID and CF_API_TOKEN environment variables are required")
}
// Create a buffer with the request body
buff := bytes.NewBufferString(`{ "stream":true,"messages": [{ "role": "system", "content": "You are a friendly assistant" }, { "role": "user", "content": "Why is pizza so good" }]}`)

// Create a new POST request to the Cloudflare API
req, err := http.NewRequest("POST", "https://api.cloudflare.com/client/v4/accounts/"+accountID+"/ai/run/@cf/meta/llama-2-7b-chat-int8", buff)
if err != nil {
t.Fatal(err)
}

// Set the Authorization header with the API token
req.Header.Set("Authorization", "Bearer "+apiToken)

// Send the POST request
response, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}

// 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 {
t.Error(err)
}
t.Log(string(all))
return
}

// Read the response body as Server-Sent Events
channel, err := ReadCh(response.Body)
if err != nil {
t.Fatal(err)
}

// 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(llamaMsg)
err := json.Unmarshal(event.Data, e)
if err != nil {
t.Error(err, string(event.Data))
} else {
// Append the response to the fulltext string
fulltext += e.Response
}
}

// Log the full text of the responses
t.Log(fulltext)
}
84 changes: 0 additions & 84 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -1,96 +1,12 @@
package sseread_test

import (
"bytes"
"encoding/json"
"fmt"
"github.com/mojocn/sseread"
"io"
"net/http"
"strings"
)

// ExampleReadCloudflareAI is a function that demonstrates how to interact with the Cloudflare AI API.
// It sends a POST request to the API and reads the response body as Server-Sent Events (SSE).
// For each event, it parses the JSON object from the event data and appends the response to the fulltext string.
// If an error occurs during the POST request, the reading of the events, or the JSON unmarshalling, it prints the error and returns from the function.
func ExampleReadCloudflareAI() {
// Replace these with your actual account ID and API token
accountID := "xxxx"
apiToken := "yyy"

// Create a buffer with the request body
buff := bytes.NewBufferString(`{ "stream":true,"messages": [{ "role": "system", "content": "You are a friendly assistant" }, { "role": "user", "content": "Why is pizza so good" }]}`)

// Create a new POST request to the Cloudflare AI API
req, err := http.NewRequest("POST", "https://api.cloudflare.com/client/v4/accounts/"+accountID+"/ai/run/@cf/meta/llama-2-7b-chat-int8", buff)
if err != nil {
fmt.Println(err)
return
}

// Set the Authorization header with the API token
req.Header.Set("Authorization", "Bearer "+apiToken)

// Send the POST request
response, err := http.DefaultClient.Do(req)
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)
}
fmt.Println(string(all))
fmt.Println("response status code is not 200")
return
}

// Read the response body as Server-Sent Events
channel, err := sseread.ReadCh(response.Body)
if err != nil {
fmt.Println(err)
return
}

// Define a struct to unmarshal the JSON object from the event data
type llamaMsg struct {
Response string `json:"response"`
P string `json:"p"`
}

// 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 the event is nil, skip it
if event == nil {
continue
}

// Parse the JSON object from the event data
e := new(llamaMsg)
err := json.Unmarshal(event.Data, e)
if err != nil {
fmt.Println(err)
} else {
// Append the response to the fulltext string
fulltext += e.Response
}
}

// Print the full text of the responses
fmt.Printf(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.
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ simple examples of how to use the library.

1. [read SSE by callback](https://pkg.go.dev/github.com/mojocn/sseread#example-Read)
2. [read SSE by channel](https://pkg.go.dev/github.com/mojocn/sseread#example-ReadCh)
3. [cloudflare AI text generation example](cloudflare_ai_test.go)

```go

## Testing

Expand Down
81 changes: 0 additions & 81 deletions sseread_test.go
Original file line number Diff line number Diff line change
@@ -1,94 +1,13 @@
package sseread

import (
"bytes"
"encoding/json"
"io"
"log"
"net/http"
"os"
"strings"
"testing"
)

// https://developers.cloudflare.com/workers-ai/models/zephyr-7b-beta-awq/#using-streaming
type llamaMsg struct {
Response string `json:"response"`
P string `json:"p"`
}

// TestReadFromCloudflareLama2 is a test function for the ReadCh function in the sseread package.
// It sends a POST request to the Cloudflare API and reads the response body as Server-Sent Events.
// For each event, it parses the JSON object from the event data and appends the response to the fulltext string.
// If an error occurs during the POST request, the reading of the events, or the JSON unmarshalling, it fails the test.
func TestReadFromCloudflareLama2(t *testing.T) {
// Retrieve the account ID and API token from the environment variables
accountID := os.Getenv("CF_ACCOUNT_ID")
apiToken := os.Getenv("CF_API_TOKEN")
if accountID == "" || apiToken == "" {
t.Fatal("CF_ACCOUNT_ID and CF_API_TOKEN environment variables are required")
}
// Create a buffer with the request body
buff := bytes.NewBufferString(`{ "stream":true,"messages": [{ "role": "system", "content": "You are a friendly assistant" }, { "role": "user", "content": "Why is pizza so good" }]}`)

// Create a new POST request to the Cloudflare API
req, err := http.NewRequest("POST", "https://api.cloudflare.com/client/v4/accounts/"+accountID+"/ai/run/@cf/meta/llama-2-7b-chat-int8", buff)
if err != nil {
t.Fatal(err)
}

// Set the Authorization header with the API token
req.Header.Set("Authorization", "Bearer "+apiToken)

// Send the POST request
response, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}

// 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 {
t.Error(err)
}
t.Log(string(all))
return
}

// Read the response body as Server-Sent Events
channel, err := ReadCh(response.Body)
if err != nil {
t.Fatal(err)
}

// 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(llamaMsg)
err := json.Unmarshal(event.Data, e)
if err != nil {
t.Error(err, string(event.Data))
} else {
// Append the response to the fulltext string
fulltext += e.Response
}
}

// Log the full text of the responses
t.Log(fulltext)
}

// TestRead is a test function for the Read function in the sseread package.
// It sends a GET request to the specified URL and reads the response body as Server-Sent Events.
// For each event, it appends it to the messages slice and logs the event ID, event type, and event data.
Expand Down

0 comments on commit cf442bc

Please sign in to comment.