Skip to content

Commit

Permalink
add xai
Browse files Browse the repository at this point in the history
  • Loading branch information
seefs001 committed Sep 27, 2024
1 parent ec2f54a commit 2c47af6
Show file tree
Hide file tree
Showing 5 changed files with 1,045 additions and 107 deletions.
76 changes: 76 additions & 0 deletions examples/xai_example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import (
"context"

"github.com/seefs001/xox/xai"
"github.com/seefs001/xox/xenv"
"github.com/seefs001/xox/xlog"
)

func main() {
xenv.Load()
client := xai.NewOpenAIClient()

// Text generation (non-streaming)
response, err := client.QuickGenerateText(context.Background(), []string{"Hello, world!"}, xai.WithTextModel(xai.ModelClaude35Sonnet))
if err != nil {
xlog.Error("Error generating text", "error", err)
return
}
xlog.Info("Text generation response:")
xlog.Info(response)

// Text generation (streaming)
xlog.Info("Streaming text generation:")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
textChan, errChan := client.QuickGenerateTextStream(ctx, []string{"Hello, world!"}, xai.WithTextModel(xai.ModelClaude35Sonnet))

streamFinished := make(chan struct{})
go func() {
defer close(streamFinished)
for {
select {
case text, ok := <-textChan:
if !ok {
xlog.Info("Stream finished")
return
}
xlog.Info(text)
case err, ok := <-errChan:
if !ok {
return
}
if err != nil {
xlog.Error("Error generating text stream", "error", err)
return
}
case <-ctx.Done():
return
}
}
}()

<-streamFinished

// Image generation
xlog.Info("Image generation:")
imageURLs, err := client.GenerateImage(context.Background(), "A beautiful sunset over the ocean", xai.WithImageModel(xai.DefaultImageModel))
if err != nil {
xlog.Error("Error generating image", "error", err)
return
}
for i, url := range imageURLs {
xlog.Infof("Image %d URL: %s", i+1, url)
}

// Embedding generation
xlog.Info("Embedding generation:")
embeddings, err := client.CreateEmbeddings(context.Background(), []string{"Hello, world!"}, xai.DefaultEmbeddingModel)
if err != nil {
xlog.Error("Error creating embeddings", "error", err)
return
}
xlog.Infof("Embedding for 'Hello, world!': %v", embeddings[0][:5]) // Print first 5 values of the embedding
}
27 changes: 27 additions & 0 deletions x/x.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package x
import (
"context"
"crypto/rand"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"math/big"
"reflect"
"runtime/debug"
"strings"
"sync"
"time"
)
Expand Down Expand Up @@ -1010,3 +1012,28 @@ func IsZero[T any](value T) bool {
}
return false
}

// IsImageURL checks if a string is a valid image URL
func IsImageURL(s string) bool {
// Check if the URL starts with http:// or https://
if !strings.HasPrefix(s, "http://") && !strings.HasPrefix(s, "https://") {
return false
}

// Simple check for common image file extensions
// You might want to implement a more robust check based on your requirements
extensions := []string{".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp"}
lowered := strings.ToLower(s)
for _, ext := range extensions {
if strings.HasSuffix(lowered, ext) {
return true
}
}
return false
}

// IsBase64 checks if a string is a valid base64 encoded string
func IsBase64(s string) bool {
_, err := base64.StdEncoding.DecodeString(s)
return err == nil
}
Loading

0 comments on commit 2c47af6

Please sign in to comment.