Skip to content

Commit

Permalink
remake support readme and further implement agents
Browse files Browse the repository at this point in the history
  • Loading branch information
conneroisu committed Nov 17, 2024
1 parent e86aa76 commit 9fc2ac8
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 59 deletions.
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@
[![Coverage Status](https://coveralls.io/repos/github/conneroisu/groq-go/badge.svg?branch=main)](https://coveralls.io/github/conneroisu/groq-go?branch=main)
[![PhormAI](https://img.shields.io/badge/Phorm-Ask_AI-%23F2777A.svg?&logo=data:image/svg+xml)](https://www.phorm.ai/query?projectId=0634251d-5a98-4c37-ac2f-385b588ce3d3)

<a href="https://groq.com" target="_blank" rel="noopener noreferrer" style="width: 10px; height: 30px;">
<img
src="https://groq.com/wp-content/uploads/2024/03/PBG-mark1-color.svg"
alt="Powered by Groq for fast inference."
/>
</a>

## Features

- Supports all models from [Groq](https://wow.groq.com/) in a type-safe way.
Expand Down Expand Up @@ -1203,3 +1196,18 @@ Generated by [gomarkdoc](<https://github.com/princjef/gomarkdoc>)


<!-- gomarkdoc:embed:end -->

## Support

<a href="https://groq.com" target="_blank" rel="noopener noreferrer" style="width: 10px; height: 30px;">
<img
src="https://groq.com/wp-content/uploads/2024/03/PBG-mark1-color.svg"
alt="Powered by Groq for fast inference."
/>
</a>
<a href="https://jigsawstack.com/?ref=powered-by" rel="follow">
<img
src="https://jigsawstack.com/badge.svg"
alt="Powered by JigsawStack. The One API for your next big thing."
/>
</a>
75 changes: 38 additions & 37 deletions agents.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,38 @@ import (
)

type (
ToolsIdx struct {
Start int
End int
// Agency is a collection of agents.
Agency struct {
client *Client
agents []Agent
logger *slog.Logger
}
// Agent is an agent.
Agent struct {
client *Client
logger *slog.Logger
tools []ToolProvider
hist []ChatCompletionMessage
client *Client
logger *slog.Logger
providers []ToolProvider
history []ChatCompletionMessage
inbox chan ChatCompletionMessage
}
// ToolProvider is an interface for a tool provider.
ToolProvider interface {
ToolRunner
ToolGetter
ToolResolver
}
// ToolRunner is an interface for a tool manager.
ToolRunner interface {
// Run runs responded tool calls.
Run(
ctx context.Context,
response ChatCompletionResponse,
) ([]ChatCompletionMessage, error)
}
// ToolResolver is an interface for a tool resolver.
//
// Implementations must not return an error if the tool is not found.
ToolResolver interface {
Resolve(
ctx context.Context,
call tools.ToolCall,
) (ToolRunner, error)
}
// ToolGetter is an interface for a tool getter.
ToolGetter interface {
// Get gets the tools for the provider.
Get(
ctx context.Context,
) ([]tools.Tool, error)
// Resolve resolves a tool call.
//
// Implementations must not return an error if the tool is not found.
Resolve(
ctx context.Context,
call tools.ToolCall,
) (bool, error)
}
)

Expand All @@ -57,37 +51,38 @@ func NewAgent(
tools ...ToolProvider,
) *Agent {
return &Agent{
client: client,
logger: logger,
client: client,
logger: logger,
providers: tools,
}
}

func (a *Agent) resolveTool(
func (agent *Agent) resolveTool(
ctx context.Context,
call tools.ToolCall,
) (ToolRunner, error) {
for _, provider := range a.tools {
runner, err := provider.Resolve(ctx, call)
) (ToolProvider, error) {
for _, provider := range agent.providers {
ok, err := provider.Resolve(ctx, call)
if err != nil {
return nil, err
}
if runner != nil {
if !ok {
continue
}
return runner, nil
return provider, nil
}
return nil, fmt.Errorf("tool not found")
}

// Run runs the agent on a chat completion response.
// run runs the agent on a chat completion response.
//
// More specifically, it runs the tool calls made by the model.
func (a *Agent) Run(
func (agent *Agent) run(
ctx context.Context,
response ChatCompletionResponse,
) (hist []ChatCompletionMessage, err error) {
for _, tool := range response.Choices[0].Message.ToolCalls {
runner, err := a.resolveTool(ctx, tool)
runner, err := agent.resolveTool(ctx, tool)
if err != nil {
return nil, err
}
Expand All @@ -105,9 +100,15 @@ func (a *Agent) Run(
//
// It basically resets the agent's history by having a new conversation with
// the model prefixed with a summary of the previous conversation/history.
func (a *Agent) refresh(
func (agent *Agent) refresh(
ctx context.Context,

Check warning on line 104 in agents.go

View check run for this annotation

Codeac.io / Codeac Code Quality

unused-parameter

parameter 'ctx' seems to be unused, consider removing or renaming it as _
) error {
// TODO: refresh agent context
return nil
}

// Start starts the agency within the given context.
func (agency *Agency) Start(ctx context.Context) error {

Check warning on line 111 in agents.go

View check run for this annotation

Codeac.io / Codeac Code Quality

unused-parameter

parameter 'ctx' seems to be unused, consider removing or renaming it as _
// TODO: start agents
return nil
}
21 changes: 9 additions & 12 deletions extensions/composio/composio.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

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

const (
Expand Down Expand Up @@ -86,27 +87,24 @@ func (c *Composio) doRequest(req *http.Request, v interface{}) error {
}
}

type (
request struct {
// Run runs the composio client on a chat completion response.
func (c *Composio) Run(
ctx context.Context,
user ConnectedAccount,
response groq.ChatCompletionResponse,
) ([]groq.ChatCompletionMessage, error) {
type request struct {
ConnectedAccountID string `json:"connectedAccountId"`
EntityID string `json:"entityId"`
AppName string `json:"appName"`
Input map[string]any `json:"input"`
Text string `json:"text,omitempty"`
AuthConfig map[string]any `json:"authConfig,omitempty"`
}
)

// Run runs the composio client on a chat completion response.
func (c *Composio) Run(
ctx context.Context,
user ConnectedAccount,
response groq.ChatCompletionResponse,
) ([]groq.ChatCompletionMessage, error) {
var respH []groq.ChatCompletionMessage
if response.Choices[0].FinishReason != groq.ReasonFunctionCall &&
response.Choices[0].FinishReason != "tool_calls" {
return nil, fmt.Errorf("not a function call")
return nil, groqerr.ErrNonFunctionCall{}
}
for _, toolCall := range response.Choices[0].Message.ToolCalls {
var args map[string]any
Expand All @@ -115,7 +113,6 @@ func (c *Composio) Run(
if err != nil {
return nil, err
}
c.logger.Debug("arguments", "args", args)
}
req, err := builders.NewRequest(
ctx,
Expand Down
6 changes: 3 additions & 3 deletions extensions/composio/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ func (c *Composio) GetTools(
}
return groqTools(items.Tools), nil
}
func groqTools(localTools []Tool) []tools.Tool {
groqTools := make([]tools.Tool, 0, len(localTools))
for _, tool := range localTools {
func groqTools(tooling []Tool) []tools.Tool {
groqTools := make([]tools.Tool, 0, len(tooling))
for _, tool := range tooling {
groqTools = append(groqTools, tools.Tool{
Function: tools.Defintion{
Name: tool.Name,
Expand Down
8 changes: 8 additions & 0 deletions pkg/groqerr/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ type (
ToolName string
ArgName string
}
// ErrNonFunctionCall is returned when a expected toolcall is not a function call.
ErrNonFunctionCall struct {
}
)

// Error implements the error interface for ErrToolArgument.
func (e ErrToolArgument) Error() string {
return fmt.Sprintf("invalid argument %s for tool %s", e.ArgName, e.ToolName)
}

// Error implements the error interface for ErrNonFunctionCall.
func (e ErrNonFunctionCall) Error() string {
return "ran on response without a function call"
}

0 comments on commit 9fc2ac8

Please sign in to comment.