Skip to content

Commit

Permalink
Merge pull request #11 from gradientlabs-ai/add-resource
Browse files Browse the repository at this point in the history
Add `AddResource` method
  • Loading branch information
boxofrad authored Jun 19, 2024
2 parents a3472b1 + 495633e commit d483898
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 4 deletions.
37 changes: 37 additions & 0 deletions add_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package client

import (
"context"
"fmt"
"net/http"
)

// AddResource adds (or updates) a resource to the conversation (e.g. the
// customer's order details) so the AI agent can handle customer-specific
// queries.
//
// A resource can be any JSON document, as long it is smaller than 1MB. There
// are no strict requirements on the format/structure of the document, but we
// recommend making attribute names as descriptive as possible.
//
// Over time, the AI agent will learn the structure of your resources - so while
// it's fine to add new attributes, you may want to consider using new resource
// names when removing attributes or changing the structure of your resources
// significantly.
//
// Resource names can be anything consisting of letters, numbers, or any of the
// following characters: _ - + =. Names should be descriptive handles that are
// the same for all conversations (e.g. "order-details" and "user-profile") not
// unique identifiers.
func (c *Client) AddResource(ctx context.Context, conversationID string, name string, resource any) error {
rsp, err := c.makeRequest(ctx, http.MethodPut, fmt.Sprintf("conversations/%s/resources/%s", conversationID, name), resource)
if err != nil {
return err
}
defer rsp.Body.Close()

if err := responseError(rsp); err != nil {
return err
}
return nil
}
2 changes: 1 addition & 1 deletion conversation_assign.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type AssignmentParams struct {

// AssignConversation assigns a conversation to a participant.
func (c *Client) AssignConversation(ctx context.Context, conversationID string, p *AssignmentParams) error {
rsp, err := c.makeRequest(ctx, http.MethodPut, fmt.Sprintf("/conversations/%s/assignee", conversationID), p)
rsp, err := c.makeRequest(ctx, http.MethodPut, fmt.Sprintf("conversations/%s/assignee", conversationID), p)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion conversation_end.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type EndParams struct {

// EndConversation ends a conversation.
func (c *Client) EndConversation(ctx context.Context, conversationID string, p EndParams) error {
rsp, err := c.makeRequest(ctx, http.MethodPut, fmt.Sprintf("/conversations/%s/end", conversationID), p)
rsp, err := c.makeRequest(ctx, http.MethodPut, fmt.Sprintf("conversations/%s/end", conversationID), p)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion conversation_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type EventParams struct {

// AddConversationEvent records an event such as the customer started typing.
func (c *Client) AddConversationEvent(ctx context.Context, conversationID string, p *EventParams) error {
rsp, err := c.makeRequest(ctx, http.MethodPost, fmt.Sprintf("/conversations/%s/events", conversationID), p)
rsp, err := c.makeRequest(ctx, http.MethodPost, fmt.Sprintf("conversations/%s/events", conversationID), p)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion conversation_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

// ReadConversation returns a conversation.
func (c *Client) ReadConversation(ctx context.Context, conversationID string) (*Conversation, error) {
rsp, err := c.makeRequest(ctx, http.MethodGet, fmt.Sprintf("/conversations/%s", conversationID), nil)
rsp, err := c.makeRequest(ctx, http.MethodGet, fmt.Sprintf("conversations/%s", conversationID), nil)
if err != nil {
return nil, err
}
Expand Down
17 changes: 17 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ func run(client *glabs.Client) error {
}
fmt.Printf("Message: %#v\n", msg)

err = client.AddResource(ctx, conv.ID, "order-details", struct {
ID string `json:"id"`
Status string `json:"status"`
}{
ID: "1234",
Status: "shipped",
})
if err != nil {
return err
}

err = client.AssignConversation(ctx, conv.ID, &glabs.AssignmentParams{
AssigneeType: glabs.ParticipantTypeAIAgent,
})
Expand All @@ -83,6 +94,12 @@ func run(client *glabs.Client) error {
return err
}

readRsp, err := client.ReadConversation(ctx, conv.ID)
if err != nil {
return err
}
fmt.Printf("%#v\n", readRsp)

return nil
}

Expand Down

0 comments on commit d483898

Please sign in to comment.