Skip to content

Commit b457ccb

Browse files
committed
Added news and ip address
1 parent 495c40e commit b457ccb

File tree

6 files changed

+178
-43
lines changed

6 files changed

+178
-43
lines changed

cmd/agent/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
kong "github.com/alecthomas/kong"
1212
client "github.com/mutablelogic/go-client"
1313
agent "github.com/mutablelogic/go-client/pkg/agent"
14+
"github.com/mutablelogic/go-client/pkg/ipify"
1415
"github.com/mutablelogic/go-client/pkg/newsapi"
1516
ollama "github.com/mutablelogic/go-client/pkg/ollama"
1617
openai "github.com/mutablelogic/go-client/pkg/openai"
@@ -95,6 +96,10 @@ func main() {
9596
cmd.FatalIfErrorf(err)
9697
cli.Globals.tools = append(cli.Globals.tools, news.Tools()...)
9798
}
99+
// Add ipify
100+
ipify, err := ipify.New(clientOpts(&cli)...)
101+
cmd.FatalIfErrorf(err)
102+
cli.Globals.tools = append(cli.Globals.tools, ipify.Tools()...)
98103

99104
// Create a context
100105
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)

pkg/agent/tool.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package agent
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"strconv"
78

89
// Namespace imports
@@ -68,11 +69,20 @@ func (t *ToolResult) Role() string {
6869
return "tool"
6970
}
7071

72+
// Return parameter as a string
73+
func (t *ToolCall) String(name string) (string, error) {
74+
v, ok := t.Args[name]
75+
if !ok {
76+
return "", ErrBadParameter.Withf("%q not found", name)
77+
}
78+
return fmt.Sprint(v), nil
79+
}
80+
7181
// Return parameter as an integer
7282
func (t *ToolCall) Int(name string) (int, error) {
7383
v, ok := t.Args[name]
7484
if !ok {
75-
return 0, ErrBadParameter.Withf("'%s' not found", name)
85+
return 0, ErrBadParameter.Withf("%q not found", name)
7686
}
7787
switch v := v.(type) {
7888
case int:

pkg/ipify/agent.go

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,73 @@ import (
44
"context"
55

66
// Packages
7-
schema "github.com/mutablelogic/go-client/pkg/openai/schema"
8-
9-
// Namespace imports
10-
. "github.com/djthorpe/go-errors"
7+
agent "github.com/mutablelogic/go-client/pkg/agent"
118
)
129

1310
///////////////////////////////////////////////////////////////////////////////
1411
// TYPES
1512

13+
type tool struct {
14+
name string
15+
description string
16+
params []agent.ToolParameter
17+
run func(context.Context, *agent.ToolCall) (*agent.ToolResult, error)
18+
}
19+
20+
// Ensure tool satisfies the agent.Tool interface
21+
var _ agent.Tool = (*tool)(nil)
22+
1623
///////////////////////////////////////////////////////////////////////////////
1724
// PUBLIC METHODS
1825

19-
// Return the tools available
20-
func (c *Client) Tools() []*schema.Tool {
21-
if get_ip_address, err := schema.NewToolEx("get_ip_address", "Get the current IP address.", nil); err != nil {
22-
panic(err)
23-
} else {
24-
return []*schema.Tool{get_ip_address}
26+
// Return all the agent tools for the weatherapi
27+
func (c *Client) Tools() []agent.Tool {
28+
return []agent.Tool{
29+
&tool{
30+
name: "get_ip_address",
31+
description: "Return your IP address",
32+
run: c.agentGetAddress,
33+
},
2534
}
2635
}
2736

28-
// Run a tool and return the result
29-
func (c *Client) Run(ctx context.Context, name string, _ any) (any, error) {
30-
switch name {
31-
case "get_ip_address":
32-
return c.Get()
33-
default:
34-
return nil, ErrInternalAppError.With(name)
37+
///////////////////////////////////////////////////////////////////////////////
38+
// PRIVATE METHODS - TOOL
39+
40+
func (*tool) Provider() string {
41+
return "ipify"
42+
}
43+
44+
func (t *tool) Name() string {
45+
return t.name
46+
}
47+
48+
func (t *tool) Description() string {
49+
return t.description
50+
}
51+
52+
func (t *tool) Params() []agent.ToolParameter {
53+
return t.params
54+
}
55+
56+
func (t *tool) Run(ctx context.Context, call *agent.ToolCall) (*agent.ToolResult, error) {
57+
return t.run(ctx, call)
58+
}
59+
60+
///////////////////////////////////////////////////////////////////////////////
61+
// PRIVATE METHODS - TOOL
62+
63+
// Return the current general headlines
64+
func (c *Client) agentGetAddress(_ context.Context, call *agent.ToolCall) (*agent.ToolResult, error) {
65+
response, err := c.Get()
66+
if err != nil {
67+
return nil, err
3568
}
69+
return &agent.ToolResult{
70+
Id: call.Id,
71+
Result: map[string]any{
72+
"type": "text",
73+
"ip_address": response,
74+
},
75+
}, nil
3676
}

pkg/ipify/agent_test.go

Lines changed: 0 additions & 23 deletions
This file was deleted.

pkg/newsapi/agent.go

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package newsapi
22

33
import (
44
"context"
5+
"strings"
56

67
// Packages
78
agent "github.com/mutablelogic/go-client/pkg/agent"
@@ -30,6 +31,39 @@ func (c *Client) Tools() []agent.Tool {
3031
name: "current_headlines",
3132
description: "Return the current news headlines",
3233
run: c.agentCurrentHeadlines,
34+
}, &tool{
35+
name: "current_headlines_country",
36+
description: "Return the current news headlines for a country",
37+
run: c.agentCountryHeadlines,
38+
params: []agent.ToolParameter{
39+
{
40+
Name: "countrycode",
41+
Description: "The two-letter country code to return headlines for",
42+
Required: true,
43+
},
44+
},
45+
}, &tool{
46+
name: "current_headlines_category",
47+
description: "Return the current news headlines for a business, entertainment, health, science, sports or technology",
48+
run: c.agentCategoryHeadlines,
49+
params: []agent.ToolParameter{
50+
{
51+
Name: "category",
52+
Description: "business, entertainment, health, science, sports, technology",
53+
Required: true,
54+
},
55+
},
56+
}, &tool{
57+
name: "search_news",
58+
description: "Return the news headlines with a search query",
59+
run: c.agentSearchNews,
60+
params: []agent.ToolParameter{
61+
{
62+
Name: "query",
63+
Description: "A phrase used to search for news headlines",
64+
Required: true,
65+
},
66+
},
3367
},
3468
}
3569
}
@@ -60,7 +94,7 @@ func (t *tool) Run(ctx context.Context, call *agent.ToolCall) (*agent.ToolResult
6094
///////////////////////////////////////////////////////////////////////////////
6195
// PRIVATE METHODS - TOOL
6296

63-
// Return the current weather
97+
// Return the current general headlines
6498
func (c *Client) agentCurrentHeadlines(_ context.Context, call *agent.ToolCall) (*agent.ToolResult, error) {
6599
response, err := c.Headlines(OptCategory("general"))
66100
if err != nil {
@@ -74,3 +108,65 @@ func (c *Client) agentCurrentHeadlines(_ context.Context, call *agent.ToolCall)
74108
},
75109
}, nil
76110
}
111+
112+
// Return the headlines for a specific country
113+
func (c *Client) agentCountryHeadlines(_ context.Context, call *agent.ToolCall) (*agent.ToolResult, error) {
114+
country, err := call.String("countrycode")
115+
if err != nil {
116+
return nil, err
117+
}
118+
country = strings.ToLower(country)
119+
response, err := c.Headlines(OptCountry(country))
120+
if err != nil {
121+
return nil, err
122+
}
123+
return &agent.ToolResult{
124+
Id: call.Id,
125+
Result: map[string]any{
126+
"type": "text",
127+
"country": country,
128+
"headlines": response,
129+
},
130+
}, nil
131+
}
132+
133+
// Return the headlines for a specific category
134+
func (c *Client) agentCategoryHeadlines(_ context.Context, call *agent.ToolCall) (*agent.ToolResult, error) {
135+
category, err := call.String("category")
136+
if err != nil {
137+
return nil, err
138+
}
139+
category = strings.ToLower(category)
140+
response, err := c.Headlines(OptCategory(category), OptLimit(5))
141+
if err != nil {
142+
return nil, err
143+
}
144+
return &agent.ToolResult{
145+
Id: call.Id,
146+
Result: map[string]any{
147+
"type": "text",
148+
"category": category,
149+
"headlines": response,
150+
},
151+
}, nil
152+
}
153+
154+
// Return the headlines for a specific query
155+
func (c *Client) agentSearchNews(_ context.Context, call *agent.ToolCall) (*agent.ToolResult, error) {
156+
query, err := call.String("query")
157+
if err != nil {
158+
return nil, err
159+
}
160+
response, err := c.Articles(OptQuery(query), OptLimit(5))
161+
if err != nil {
162+
return nil, err
163+
}
164+
return &agent.ToolResult{
165+
Id: call.Id,
166+
Result: map[string]any{
167+
"type": "text",
168+
"query": query,
169+
"headlines": response,
170+
},
171+
}, nil
172+
}

pkg/openai/schema/tool.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,14 @@ func NewToolResult(id string, result map[string]any) *Message {
8989
var message Message
9090
message.Role = "tool"
9191
message.ToolCallId = id
92-
message.Content = []any{result}
92+
93+
data, err := json.Marshal(result)
94+
if err != nil {
95+
return nil
96+
} else {
97+
message.Content = string(data)
98+
}
99+
93100
return &message
94101
}
95102

0 commit comments

Comments
 (0)