14
14
15
15
package backend
16
16
17
- import "context"
17
+ import (
18
+ "context"
19
+ )
18
20
19
21
// Backend defines the interface for interacting with various LLM backends.
20
22
type Backend interface {
23
+ Converse (ctx context.Context , prompt * Prompt ) (PromptResponse , error )
21
24
Generate (ctx context.Context , prompt * Prompt ) (string , error )
22
25
Embed (ctx context.Context , input string ) ([]float32 , error )
23
26
}
24
27
25
28
// Message represents a single role-based message in the conversation.
26
29
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"`
29
33
}
30
34
31
35
// Parameters defines generation settings for LLM completions.
@@ -41,11 +45,17 @@ type Parameters struct {
41
45
type Prompt struct {
42
46
Messages []Message `json:"messages"`
43
47
Parameters Parameters `json:"parameters"`
48
+ // ToolRegistry is a map of tool names to their corresponding wrapper functions.
49
+ Tools * ToolRegistry
44
50
}
45
51
46
52
// NewPrompt creates and returns a new Prompt.
47
53
func NewPrompt () * Prompt {
48
- return & Prompt {}
54
+ return & Prompt {
55
+ Messages : make ([]Message , 0 ),
56
+ Parameters : Parameters {},
57
+ Tools : NewToolRegistry (),
58
+ }
49
59
}
50
60
51
61
// AddMessage adds a message with a specific role to the prompt.
@@ -54,8 +64,50 @@ func (p *Prompt) AddMessage(role, content string) *Prompt {
54
64
return p
55
65
}
56
66
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
+
57
73
// SetParameters sets the generation parameters for the prompt.
58
74
func (p * Prompt ) SetParameters (params Parameters ) * Prompt {
59
75
p .Parameters = params
60
76
return p
61
77
}
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
+ }
0 commit comments