Skip to content

Commit c23c465

Browse files
committed
Add the Converse method to the back end interface
Adds Converse as a back end method, making it usable by library clients.
1 parent 9561cd3 commit c23c465

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
lines changed

pkg/backend/backend.go

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,22 @@
1414

1515
package backend
1616

17-
import "context"
17+
import (
18+
"context"
19+
)
1820

1921
// Backend defines the interface for interacting with various LLM backends.
2022
type Backend interface {
23+
Converse(ctx context.Context, prompt *Prompt) (PromptResponse, error)
2124
Generate(ctx context.Context, prompt *Prompt) (string, error)
2225
Embed(ctx context.Context, input string) ([]float32, error)
2326
}
2427

2528
// Message represents a single role-based message in the conversation.
2629
type Message struct {
27-
Role string `json:"role"`
28-
Content string `json:"content"`
30+
Role string `json:"role"`
31+
Content string `json:"content"`
32+
Fields map[string]any `json:"fields,omitempty"`
2933
}
3034

3135
// Parameters defines generation settings for LLM completions.
@@ -41,11 +45,17 @@ type Parameters struct {
4145
type Prompt struct {
4246
Messages []Message `json:"messages"`
4347
Parameters Parameters `json:"parameters"`
48+
// ToolRegistry is a map of tool names to their corresponding wrapper functions.
49+
Tools *ToolRegistry
4450
}
4551

4652
// NewPrompt creates and returns a new Prompt.
4753
func NewPrompt() *Prompt {
48-
return &Prompt{}
54+
return &Prompt{
55+
Messages: make([]Message, 0),
56+
Parameters: Parameters{},
57+
Tools: NewToolRegistry(),
58+
}
4959
}
5060

5161
// AddMessage adds a message with a specific role to the prompt.
@@ -54,8 +64,50 @@ func (p *Prompt) AddMessage(role, content string) *Prompt {
5464
return p
5565
}
5666

67+
// AppendMessage adds a message with a specific role to the prompt.
68+
func (p *Prompt) AppendMessage(msg Message) *Prompt {
69+
p.Messages = append(p.Messages, msg)
70+
return p
71+
}
72+
5773
// SetParameters sets the generation parameters for the prompt.
5874
func (p *Prompt) SetParameters(params Parameters) *Prompt {
5975
p.Parameters = params
6076
return p
6177
}
78+
79+
// AsMap returns the conversation's messages as a list of maps.
80+
func (p *Prompt) AsMap() ([]map[string]any, error) {
81+
messageList := make([]map[string]any, 0, len(p.Messages))
82+
for _, message := range p.Messages {
83+
msgMap := map[string]any{
84+
"role": message.Role,
85+
"content": message.Content,
86+
}
87+
for k, v := range message.Fields {
88+
msgMap[k] = v
89+
}
90+
messageList = append(messageList, msgMap)
91+
}
92+
93+
return messageList, nil
94+
}
95+
96+
// FunctionCall represents a call to a function.
97+
type FunctionCall struct {
98+
Name string `json:"name"`
99+
Arguments map[string]any `json:"arguments"`
100+
Result any `json:"result"`
101+
}
102+
103+
// ToolCall represents a call to a tool.
104+
type ToolCall struct {
105+
Function FunctionCall `json:"function"`
106+
}
107+
108+
// PromptResponse represents a response from the AI in a conversation.
109+
type PromptResponse struct {
110+
Role string `json:"role"`
111+
Content string `json:"content"`
112+
ToolCalls []ToolCall `json:"tool_calls"`
113+
}

pkg/backend/openai_backend_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,18 @@ func TestGenerate(t *testing.T) {
3333
Choices: []struct {
3434
Index int `json:"index"`
3535
Message struct {
36-
Role string `json:"role"`
37-
Content string `json:"content"`
36+
Role string `json:"role"`
37+
Content string `json:"content"`
38+
ToolCalls []OpenAIToolCall `json:"tool_calls"`
3839
} `json:"message"`
3940
FinishReason string `json:"finish_reason"`
4041
}{
4142
{
4243
Index: 0,
4344
Message: struct {
44-
Role string `json:"role"`
45-
Content string `json:"content"`
45+
Role string `json:"role"`
46+
Content string `json:"content"`
47+
ToolCalls []OpenAIToolCall `json:"tool_calls"`
4648
}{
4749
Role: "assistant",
4850
Content: "This is a test response.",

0 commit comments

Comments
 (0)