Skip to content

Commit

Permalink
added initial jigsawstack extension
Browse files Browse the repository at this point in the history
  • Loading branch information
conneroisu committed Oct 30, 2024
1 parent 765c44c commit 594ad68
Show file tree
Hide file tree
Showing 25 changed files with 660 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .go-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.23.1
1.23.2
2 changes: 1 addition & 1 deletion chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ func (stream *streamReader[T]) processLines() (T, error) {
return response, nil
}
}
func (stream *streamReader[T]) unmarshalError() (errResp *errorResponse) {
func (stream *streamReader[T]) unmarshalError() (errResp *ErrorResponse) {
errBytes := stream.errAccumulator.Bytes()
if len(errBytes) == 0 {
return
Expand Down
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func withModel(model model) fullURLOption {
}

func (c *Client) handleErrorResp(resp *http.Response) error {
var errRes errorResponse
var errRes ErrorResponse
err := json.NewDecoder(resp.Body).Decode(&errRes)
if err != nil || errRes.Error == nil {
reqErr := &requestError{
Expand Down
1 change: 1 addition & 0 deletions extensions/jigsawstack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# jigsawstack
13 changes: 13 additions & 0 deletions extensions/jigsawstack/audio_tts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package jigsawstack

// https://api.jigsawstack.com/v1/ai/tts

// https://docs.jigsawstack.com/api-reference/ai/text-to-speech

// TODO: implement audio tts

// https://api.jigsawstack.com/v1/ai/tts

// https://docs.jigsawstack.com/api-reference/audio/speaker-voice-accents

// TODO: implement get audio speaker voice accents
6 changes: 6 additions & 0 deletions extensions/jigsawstack/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package jigsawstack

// https://docs.jigsawstack.com/api-reference/store/file/add
// https://docs.jigsawstack.com/api-reference/store/file/get
// https://docs.jigsawstack.com/api-reference/store/file/delete
// Upload Retrieve Delete
13 changes: 13 additions & 0 deletions extensions/jigsawstack/geo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package jigsawstack

// https://api.jigsawstack.com/v1/geo/search

// https://docs.jigsawstack.com/api-reference/geo/search

// TODO: Add geo search

// https://api.jigsawstack.com/v1/geo/geocode

// https://docs.jigsawstack.com/api-reference/geo/geocode

// TODO: Add geocode
62 changes: 62 additions & 0 deletions extensions/jigsawstack/image_generation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package jigsawstack

import (
"context"
"net/http"

"github.com/conneroisu/groq-go/pkg/builders"
)

const (
imageGenerationEndpoint = "v1/ai/image_generation"
)

// model
// string
// default: "sdxl"
//
// The model to use for the generation. Default is sdxl
//
// sd1.5 - Stable Diffusion v1.5
// sdxl - Stable Diffusion XL
// ead1.0 - Anime Diffusion
// rv1.3 - Realistic Vision v1.3
// rv3 - Realistic Vision v3
// rv5.1 - Realistic Vision v5.1
// ar1.8 - AbsoluteReality v1.8.1
type (
ImageGenerationRequest struct {
Prompt string `json:"prompt"`
Model string `json:"model,omitempty"`
Size string `json:"size"`
Width int `json:"width"`
Height int `json:"height"`
}
ImageGenerationResponse struct {
Success bool `json:"success"`
Image string `json:"image"`
}
)

// Default is sdxl
func (j *JigsawStack) ImageGeneration(
ctx context.Context,
request ImageGenerationRequest,
) (response ImageGenerationResponse, err error) {
req, err := builders.NewRequest(
ctx,
j.header,
http.MethodPost,
j.baseURL+imageGenerationEndpoint,
builders.WithBody(request),
)
if err != nil {
return
}
var resp ImageGenerationResponse
err = j.sendRequest(req, &resp)
if err != nil {
return
}
return resp, nil
}
98 changes: 98 additions & 0 deletions extensions/jigsawstack/jigsawstack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package jigsawstack

import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"

"github.com/conneroisu/groq-go/pkg/builders"
)

const (
defaultBaseURL = "https://api.jigsawstack.com"
)

type (
// JigsawStack is a JigsawStack extension.
JigsawStack struct {
baseURL string
client *http.Client
logger *slog.Logger
header builders.Header
}
// Option is an option for the JigsawStack client.
Option func(*JigsawStack)
// Endpoint is the endpoint for the JigsawStack api.
Endpoint string
)

// NewJigsawStack creates a new JigsawStack extension.
func NewJigsawStack(apiKey string, opts ...Option) (*JigsawStack, error) {
j := &JigsawStack{
baseURL: defaultBaseURL,
client: http.DefaultClient,
logger: slog.Default(),
}
for _, opt := range opts {
opt(j)
}
j.header.SetCommonHeaders = func(req *http.Request) {
req.Header.Set("x-api-key", apiKey)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
}
return j, nil
}

// WithBaseURL sets the base URL for the JigsawStack extension.
func WithBaseURL(baseURL string) Option {
return func(j *JigsawStack) { j.baseURL = baseURL }
}

// WithClient sets the client for the JigsawStack extension.
func WithClient(client *http.Client) Option {
return func(j *JigsawStack) { j.client = client }
}

// WithLogger sets the logger for the JigsawStack extension.
func WithLogger(logger *slog.Logger) Option {
return func(j *JigsawStack) { j.logger = logger }
}

func (j *JigsawStack) sendRequest(req *http.Request, v any) error {
j.header.SetCommonHeaders(req)
resp, err := j.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < http.StatusOK ||
resp.StatusCode >= http.StatusBadRequest {
return nil
}
if v == nil {
return nil
}
switch o := v.(type) {
case *string:
b, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
*o = string(b)
return nil
default:
err = json.NewDecoder(resp.Body).Decode(v)
if err != nil {
read, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
j.logger.Debug("failed to decode response", "response", string(read))
return fmt.Errorf("failed to decode response: %w\nbody: %s", err, string(read))
}
return nil
}
}
1 change: 1 addition & 0 deletions extensions/jigsawstack/jigsawstack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package jigsawstack
7 changes: 7 additions & 0 deletions extensions/jigsawstack/kv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package jigsawstack

// https://docs.jigsawstack.com/api-reference/store/kv/get
// https://docs.jigsawstack.com/api-reference/store/kv/add
// https://docs.jigsawstack.com/api-reference/store/kv/delete

// Add Get Delete
55 changes: 55 additions & 0 deletions extensions/jigsawstack/prediction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package jigsawstack

import (
"context"
"net/http"
"time"

"github.com/conneroisu/groq-go/pkg/builders"
)

const (
predictEndpoint Endpoint = "v1/ai/prediction"
)

type (
// DatasetEntry represents a dataset entry.
DatasetEntry struct {
Date time.Time `json:"date"`
Value float64 `json:"value"`
}
// PredictRequest represents a request structure for prediction API.
PredictRequest struct {
Dataset []DatasetEntry `json:"dataset"`
}
// PredictResponse represents a response structure for prediction API.
PredictResponse struct {
Success bool `json:"success"`
Answer []DatasetEntry `json:"answer"`
}
)

// Predict predicts the text.
//
// Max text character is 5000.
func (j *JigsawStack) Predict(
ctx context.Context,
request PredictRequest,
) (response PredictResponse, err error) {
req, err := builders.NewRequest(
ctx,
j.header,
http.MethodPost,
j.baseURL+string(predictEndpoint),
builders.WithBody(request),
)
if err != nil {
return
}
var resp PredictResponse
err = j.sendRequest(req, &resp)
if err != nil {
return
}
return resp, nil
}
5 changes: 5 additions & 0 deletions extensions/jigsawstack/prompt_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package jigsawstack

// https://docs.jigsawstack.com/api-reference/prompt-engine/create

// TODO: implement prompt create
7 changes: 7 additions & 0 deletions extensions/jigsawstack/prompt_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package jigsawstack

// https://api.jigsawstack.com/v1/prompt_engine/{id}

// https://docs.jigsawstack.com/api-reference/prompt-engine/delete

// TODO: add support for deleting a prompt
11 changes: 11 additions & 0 deletions extensions/jigsawstack/prompt_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package jigsawstack

// https://api.jigsawstack.com/v1/prompt_engine/{id}

// https://docs.jigsawstack.com/api-reference/prompt-engine/retrieve

// TODO: add support for retrieving a specific prompt

// https://docs.jigsawstack.com/api-reference/prompt-engine/list

// TODO: add support for listing prompts
11 changes: 11 additions & 0 deletions extensions/jigsawstack/prompt_run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package jigsawstack

// https://api.jigsawstack.com/v1/prompt_engine/{id}

// https://docs.jigsawstack.com/api-reference/prompt-engine/run

// TODO: implement prompt run

// TODO: implement prompt run direct
// https://docs.jigsawstack.com/api-reference/prompt-engine/run-direct
// https://api.jigsawstack.com/v1/prompt_engine/run
Loading

0 comments on commit 594ad68

Please sign in to comment.