-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optimize composio and e2b extensions
- Loading branch information
1 parent
ba4965c
commit a7ca26c
Showing
9 changed files
with
310 additions
and
304 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package groq | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
) | ||
|
||
type ( | ||
// Agent is an agent. | ||
Agent struct { | ||
client *Client | ||
logger *slog.Logger | ||
runners []ToolRunner | ||
} | ||
// ToolRunner is an interface for a tool manager. | ||
ToolRunner interface { | ||
Run( | ||
ctx context.Context, | ||
response ChatCompletionResponse, | ||
) ([]ChatCompletionMessage, error) | ||
} | ||
) | ||
|
||
// Run runs the agent on a chat completion response. | ||
func (a *Agent) Run( | ||
ctx context.Context, | ||
response ChatCompletionResponse, | ||
) ([]ChatCompletionMessage, error) { | ||
for _, runner := range a.runners { | ||
messages, err := runner.Run(ctx, response) | ||
if err == nil { | ||
return messages, nil | ||
} | ||
} | ||
return nil, fmt.Errorf("no runners found for response") | ||
} | ||
|
||
// NewAgent creates a new agent. | ||
func NewAgent( | ||
client *Client, | ||
logger *slog.Logger, | ||
runners ...ToolRunner, | ||
) *Agent { | ||
return &Agent{ | ||
client: client, | ||
logger: logger, | ||
runners: runners, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,115 @@ | ||
package composio | ||
package composio_test | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"os" | ||
"testing" | ||
|
||
"github.com/conneroisu/groq-go" | ||
"github.com/conneroisu/groq-go/extensions/composio" | ||
"github.com/conneroisu/groq-go/pkg/models" | ||
"github.com/conneroisu/groq-go/pkg/test" | ||
"github.com/conneroisu/groq-go/pkg/tools" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRun(t *testing.T) { | ||
a := assert.New(t) | ||
ctx := context.Background() | ||
ts := test.NewTestServer() | ||
ts.RegisterHandler("/v1/connectedAccounts", func(w http.ResponseWriter, _ *http.Request) { | ||
var items struct { | ||
Items []composio.ConnectedAccount `json:"items"` | ||
} | ||
items.Items = append(items.Items, composio.ConnectedAccount{ | ||
IntegrationID: "INTEGRATION_ID", | ||
ID: "ID", | ||
MemberID: "MEMBER_ID", | ||
ClientUniqueUserID: "CLIENT_UNIQUE_USER_ID", | ||
Status: "STATUS", | ||
AppUniqueID: "APP_UNIQUE_ID", | ||
AppName: "APP_NAME", | ||
InvocationCount: "INVOCATION_COUNT", | ||
}) | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
jsonBytes, err := json.Marshal(items) | ||
a.NoError(err) | ||
_, err = w.Write(jsonBytes) | ||
a.NoError(err) | ||
}) | ||
ts.RegisterHandler("/v2/actions/TOOL/execute", func(w http.ResponseWriter, _ *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
_, err := w.Write([]byte(`response1`)) | ||
a.NoError(err) | ||
}) | ||
testS := ts.ComposioTestServer() | ||
testS.Start() | ||
client, err := composio.NewComposer( | ||
test.GetTestToken(), | ||
composio.WithLogger(test.DefaultLogger), | ||
composio.WithBaseURL(testS.URL), | ||
) | ||
a.NoError(err) | ||
ca, err := client.GetConnectedAccounts(ctx, composio.WithShowActiveOnly(true)) | ||
a.NoError(err) | ||
resp, err := client.Run(ctx, ca[0], groq.ChatCompletionResponse{ | ||
Choices: []groq.ChatCompletionChoice{{ | ||
Message: groq.ChatCompletionMessage{ | ||
Role: groq.ChatMessageRoleUser, | ||
Content: "Hello!", | ||
ToolCalls: []tools.ToolCall{{ | ||
Function: tools.FunctionCall{ | ||
Name: "TOOL", | ||
Arguments: `{ "foo": "bar", }`, | ||
}}}}, | ||
FinishReason: groq.ReasonFunctionCall, | ||
}}}) | ||
a.NoError(err) | ||
assert.Equal(t, "response1", resp[0].Content) | ||
} | ||
|
||
func TestUnitRun(t *testing.T) { | ||
if !test.IsIntegrationTest() { | ||
t.Skip() | ||
} | ||
a := assert.New(t) | ||
ctx := context.Background() | ||
key, err := test.GetAPIKey("COMPOSIO_API_KEY") | ||
a.NoError(err) | ||
client, err := composio.NewComposer( | ||
key, | ||
composio.WithLogger(test.DefaultLogger), | ||
) | ||
a.NoError(err) | ||
ts, err := client.GetTools( | ||
ctx, composio.WithApp("GITHUB"), composio.WithUseCase("StarRepo")) | ||
a.NoError(err) | ||
a.NotEmpty(ts) | ||
groqClient, err := groq.NewClient( | ||
os.Getenv("GROQ_KEY"), | ||
) | ||
a.NoError(err, "NewClient error") | ||
response, err := groqClient.CreateChatCompletion(ctx, groq.ChatCompletionRequest{ | ||
Model: models.ModelLlama3Groq8B8192ToolUsePreview, | ||
Messages: []groq.ChatCompletionMessage{ | ||
{ | ||
Role: groq.ChatMessageRoleUser, | ||
Content: "Star the facebookresearch/spiritlm repository on GitHub", | ||
}, | ||
}, | ||
MaxTokens: 2000, | ||
Tools: ts, | ||
}) | ||
a.NoError(err) | ||
a.NotEmpty(response.Choices[0].Message.ToolCalls) | ||
users, err := client.GetConnectedAccounts(ctx) | ||
a.NoError(err) | ||
resp2, err := client.Run(ctx, users[0], response) | ||
a.NoError(err) | ||
a.NotEmpty(resp2) | ||
t.Logf("%+v\n", resp2) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.