Skip to content

Commit

Permalink
fixed some failing jigsawstack funcs and added some tests to prevent …
Browse files Browse the repository at this point in the history
…regressions
  • Loading branch information
conneroisu committed Nov 4, 2024
1 parent 9a549d1 commit 1baaa92
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 25 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- Supports [Toolhouse](https://app.toolhouse.ai/) function calling. [Extention](https://github.com/conneroisu/groq-go/tree/main/extensions/toolhouse)
- Supports [E2b](https://e2b.dev/) function calling. [Extention](https://github.com/conneroisu/groq-go/tree/main/extensions/e2b)
- Supports [Composio](https://composio.dev/) function calling. [Extention](https://github.com/conneroisu/groq-go/tree/main/extensions/composio)
- Supports [Jigsaw Stack](https://jigsawstack.com/) function calling. [Extention](https://github.com/conneroisu/groq-go/tree/main/extensions/jigsawstack)

## Installation

Expand Down
14 changes: 12 additions & 2 deletions agents.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type (
ToolGetter interface {
Get(
ctx context.Context,
params ToolGetParams,
) ([]tools.Tool, error)
}
// ToolRunner is an interface for a tool runner.
Expand All @@ -30,8 +31,17 @@ type (
response ChatCompletionResponse,
) ([]ChatCompletionMessage, error)
}
// GetToolsParams is the parameters for getting tools.
getToolsParams struct {
// ToolGetParams are the parameters for getting tools.
ToolGetParams struct {
}
// Router is an agent router.
//
// It is used to route messages to the appropriate model.
Router struct {
// Agents is the agents of the router.
Agents []Agent
// Logger is the logger of the router.
Logger *slog.Logger
}
)

Expand Down
1 change: 0 additions & 1 deletion extensions/jigsawstack/natural_language.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ const (
)

type (

// Language is a language.
Language string
// TranslateRequest represents a request structure for translate API.
Expand Down
2 changes: 1 addition & 1 deletion extensions/jigsawstack/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

const (
promptCreateEndpoint Endpoint = "v1/prompt_engine"
promptCreateEndpoint Endpoint = "/v1/prompt_engine"
)

type (
Expand Down
58 changes: 58 additions & 0 deletions extensions/jigsawstack/prompt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package jigsawstack_test

import (
"context"
"testing"

"github.com/conneroisu/groq-go/extensions/jigsawstack"
"github.com/conneroisu/groq-go/pkg/test"
"github.com/stretchr/testify/assert"
)

func TestJigsawStack_PromptCreate(t *testing.T) {
if !test.IsIntegrationTest() {
t.Skip("Skipping integration test")
}
a := assert.New(t)
apiKey, err := test.GetAPIKey("JIGSAWSTACK_API_KEY")
a.NoError(err)
j, err := jigsawstack.NewJigsawStack(apiKey)
a.NoError(err)
resp, err := j.PromptCreate(context.Background(), jigsawstack.PromptCreateRequest{
Prompt: `

Check warning on line 22 in extensions/jigsawstack/prompt_test.go

View check run for this annotation

Codeac.io / Codeac Code Quality

CodeDuplication

This block of 10 lines is too similar to extensions/jigsawstack/prompt_test.go:46

Check warning on line 22 in extensions/jigsawstack/prompt_test.go

View check run for this annotation

Codeac.io / Codeac Code Quality

CodeDuplication

This block of 10 lines is too similar to extensions/jigsawstack/natural_language_test.go:12
You are a helpful assistant that answers questions based on the provided context.
Your job is to provide code completions based on the provided context.
`,
Inputs: []jigsawstack.PromptCreateInput{
{
Key: "context",
Optional: false,
InitialValue: `
<file name="context.py">
def main():
print("Hello, World!")
if __name__ == "__main__":
main()
</file>
`,
},
},
})
a.NoError(err)
t.Logf("response: %v", resp)
t.Fail()
}
func TestJigsawStack_PromptGet(t *testing.T) {
if !test.IsIntegrationTest() {
t.Skip("Skipping integration test")
}
a := assert.New(t)
apiKey, err := test.GetAPIKey("JIGSAWSTACK_API_KEY")
a.NoError(err)
j, err := jigsawstack.NewJigsawStack(apiKey)
a.NoError(err)
resp, err := j.PromptGet(context.Background(), "test")
a.NoError(err)

Check warning on line 56 in extensions/jigsawstack/prompt_test.go

View check run for this annotation

Codeac.io / Codeac Code Quality

CodeDuplication

This block of 10 lines is too similar to extensions/jigsawstack/prompt_test.go:12
a.NotEmpty(resp.Prompt)
}
17 changes: 10 additions & 7 deletions extensions/jigsawstack/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ const (
)

type (
// TextToSQLRequest represents a request structure for text to SQL API.
TextToSQLRequest struct {
Prompt string `json:"prompt"`
SQLSchema string `json:"sql_schema"`
}
// TextToSQLResponse represents a response structure for text to SQL API.
TextToSQLResponse struct {
Success bool `json:"success"`
Expand All @@ -29,14 +24,22 @@ type (
// Max text character is 5000.
func (j *JigsawStack) TextToSQL(
ctx context.Context,
request TextToSQLRequest,
prompt string,
sqlSchema string,
) (response TextToSQLResponse, err error) {
body := struct {
Prompt string `json:"prompt"`
SQLSchema string `json:"sql_schema"`
}{
Prompt: prompt,
SQLSchema: sqlSchema,
}
req, err := builders.NewRequest(
ctx,
j.header,
http.MethodPost,
j.baseURL+string(textToSQLEndpoint),
builders.WithBody(request),
builders.WithBody(body),
)
if err != nil {
return
Expand Down
33 changes: 33 additions & 0 deletions extensions/jigsawstack/sql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package jigsawstack_test

import (
"context"
"testing"

"github.com/conneroisu/groq-go/extensions/jigsawstack"
"github.com/conneroisu/groq-go/pkg/test"
"github.com/stretchr/testify/assert"
)

// TestJigsawStack_TextToSQL tests the TextToSQL method of the JigsawStack client.
func TestJigsawStack_TextToSQL(t *testing.T) {
if !test.IsIntegrationTest() {
t.Skip("Skipping unit test")
}
a := assert.New(t)
ctx := context.Background()
apiKey, err := test.GetAPIKey("JIGSAWSTACK_API_KEY")
a.NoError(err)
j, err := jigsawstack.NewJigsawStack(apiKey)
a.NoError(err)
resp, err := j.TextToSQL(ctx, "select all users", `
CREATE TABLE users (

Check warning on line 24 in extensions/jigsawstack/sql_test.go

View check run for this annotation

Codeac.io / Codeac Code Quality

CodeDuplication

This block of 11 lines is too similar to extensions/jigsawstack/web_test.go:13
id INT PRIMARY KEY,
name VARCHAR(255),
email VARCHAR(255),
age INT
);
`)
a.NoError(err)
a.NotEmpty(resp.SQL)
}
13 changes: 7 additions & 6 deletions extensions/jigsawstack/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package jigsawstack
import (
"context"
"fmt"
"mime"
"net/http"

"github.com/conneroisu/groq-go/pkg/builders"
Expand Down Expand Up @@ -36,14 +35,16 @@ type (
func (j *JigsawStack) FileAdd(
ctx context.Context,
key string,
body string,
contentType string,
content string,
) (string, error) {
// TODO: may need to santize the key
url := j.baseURL + string(uploadEndpoint) + "?key=" + key
contentType, _, err := mime.ParseMediaType(body)
if err != nil {
return "", err
var body = struct {
Blob string `json:"blob"`
}{
Blob: content,
}

req, err := builders.NewRequest(
ctx,
j.header,
Expand Down
26 changes: 26 additions & 0 deletions extensions/jigsawstack/storage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package jigsawstack_test

import (
"context"
"testing"

"github.com/conneroisu/groq-go/extensions/jigsawstack"
"github.com/conneroisu/groq-go/pkg/test"
"github.com/stretchr/testify/assert"
)

// TestJigsawStack_FileAdd tests the FileAdd method of the JigsawStack client.
func TestJigsawStack_FileAdd(t *testing.T) {
if !test.IsIntegrationTest() {
t.Skip("Skipping unit test")
}
a := assert.New(t)
ctx := context.Background()
apiKey, err := test.GetAPIKey("JIGSAWSTACK_API_KEY")
a.NoError(err)
j, err := jigsawstack.NewJigsawStack(apiKey)
a.NoError(err)
resp, err := j.FileAdd(ctx, "test", "text/plain", "hello world")
a.NoError(err)

Check warning on line 24 in extensions/jigsawstack/storage_test.go

View check run for this annotation

Codeac.io / Codeac Code Quality

CodeDuplication

This block of 11 lines is too similar to extensions/jigsawstack/web_test.go:13
a.NotEmpty(resp)
}
Binary file added extensions/jigsawstack/tts.mp3
Binary file not shown.
15 changes: 7 additions & 8 deletions extensions/jigsawstack/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
)

const (
webSearchEndpoint Endpoint = "v1/web/search"
webSearchEndpoint Endpoint = "/v1/web/search"
webSuggestEndpoint Endpoint = "/v1/web/search/suggest"
)

type (
Expand Down Expand Up @@ -51,19 +52,18 @@ type (

// WebSearch performs a web search api call over a query string.
//
// POST https://api.jigsawstack.com/v1/web/search
// GET https://api.jigsawstack.com/v1/web/search
//
// https://docs.jigsawstack.com/api-reference/web/search
func (j *JigsawStack) WebSearch(
ctx context.Context,
query string,
) (response WebSearchResponse, err error) {
// TODO: may need to santize the query
uri := j.baseURL + string(webSearchEndpoint) + "?query=" + query
req, err := builders.NewRequest(
ctx,
j.header,
http.MethodPost,
http.MethodGet,
uri,
)
if err != nil {
Expand All @@ -80,19 +80,18 @@ func (j *JigsawStack) WebSearch(
// WebSearchSuggestions performs a web search suggestions api call over a query
// string.
//
// POST https://api.jigsawstack.com/v1/web/search
// GET https://api.jigsawstack.com/v1/web/search/suggest
//
// https://docs.jigsawstack.com/api-reference/web/search
func (j *JigsawStack) WebSearchSuggestions(
ctx context.Context,
query string,
) (response WebSearchSuggestions, err error) {
// TODO: may need to santize the query
uri := j.baseURL + string(webSearchEndpoint) + "?query=" + query
uri := j.baseURL + string(webSuggestEndpoint) + "?query=" + query
req, err := builders.NewRequest(
ctx,
j.header,
http.MethodPost,
http.MethodGet,
uri,
)
if err != nil {
Expand Down
42 changes: 42 additions & 0 deletions extensions/jigsawstack/web_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package jigsawstack_test

import (
"context"
"testing"

"github.com/conneroisu/groq-go/extensions/jigsawstack"
"github.com/conneroisu/groq-go/pkg/test"
"github.com/stretchr/testify/assert"
)

// TestJigsawStack_WebSearch tests the WebSearch method of the JigsawStack client.
func TestJigsawStack_WebSearch(t *testing.T) {
if !test.IsIntegrationTest() {
t.Skip("Skipping unit test")
}
a := assert.New(t)
ctx := context.Background()
apiKey, err := test.GetAPIKey("JIGSAWSTACK_API_KEY")
a.NoError(err)
j, err := jigsawstack.NewJigsawStack(apiKey)
a.NoError(err)
resp, err := j.WebSearch(ctx, "hello world golang")
a.NoError(err)

Check warning on line 24 in extensions/jigsawstack/web_test.go

View check run for this annotation

Codeac.io / Codeac Code Quality

CodeDuplication

This block of 11 lines is too similar to extensions/jigsawstack/storage_test.go:13

Check warning on line 24 in extensions/jigsawstack/web_test.go

View check run for this annotation

Codeac.io / Codeac Code Quality

CodeDuplication

This block of 11 lines is too similar to extensions/jigsawstack/sql_test.go:13

Check warning on line 24 in extensions/jigsawstack/web_test.go

View check run for this annotation

Codeac.io / Codeac Code Quality

CodeDuplication

This block of 11 lines is too similar to extensions/jigsawstack/web_test.go:29
a.NotEmpty(resp.Results)
}

// TestJigsawStack_WebSearchSuggestions tests the WebSearchSuggestions method of the JigsawStack client.
func TestJigsawStack_WebSearchSuggestions(t *testing.T) {
if !test.IsIntegrationTest() {
t.Skip("Skipping unit test")
}
a := assert.New(t)
ctx := context.Background()
apiKey, err := test.GetAPIKey("JIGSAWSTACK_API_KEY")
a.NoError(err)
j, err := jigsawstack.NewJigsawStack(apiKey)
a.NoError(err)
resp, err := j.WebSearchSuggestions(ctx, "hello")
a.NoError(err)

Check warning on line 40 in extensions/jigsawstack/web_test.go

View check run for this annotation

Codeac.io / Codeac Code Quality

CodeDuplication

This block of 11 lines is too similar to extensions/jigsawstack/web_test.go:13
a.NotEmpty(resp.Suggestions)
}

0 comments on commit 1baaa92

Please sign in to comment.