forked from tmc/langchaingo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneratecontent.go
More file actions
189 lines (159 loc) · 5.42 KB
/
generatecontent.go
File metadata and controls
189 lines (159 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package llms
import (
"encoding/base64"
"fmt"
"io"
)
// MessageContent is the content of a message sent to a LLM. It has a role and a
// sequence of parts. For example, it can represent one message in a chat
// session sent by the user, in which case Role will be
// ChatMessageTypeHuman and Parts will be the sequence of items sent in
// this specific message.
type MessageContent struct {
Role ChatMessageType
Name string
Parts []ContentPart
}
// TextPart creates TextContent from a given string.
func TextPart(s string) TextContent {
return TextContent{Text: s}
}
// BinaryPart creates a new BinaryContent from the given MIME type (e.g.
// "image/png" and binary data).
func BinaryPart(mime string, data []byte) BinaryContent {
return BinaryContent{
MIMEType: mime,
Data: data,
}
}
// ImageURLPart creates a new ImageURLContent from the given URL.
func ImageURLPart(url string) ImageURLContent {
return ImageURLContent{
URL: url,
}
}
// ImageURLWithDetailPart creates a new ImageURLContent from the given URL and detail.
func ImageURLWithDetailPart(url string, detail string) ImageURLContent {
return ImageURLContent{
URL: url,
Detail: detail,
}
}
// ContentPart is an interface all parts of content have to implement.
type ContentPart interface {
isPart()
}
// TextContent is content with some text.
type TextContent struct {
Text string
}
func (tc TextContent) String() string {
return tc.Text
}
func (TextContent) isPart() {}
// ImageURLContent is content with an URL pointing to an image.
type ImageURLContent struct {
URL string `json:"url"`
Detail string `json:"detail,omitempty"` // Detail is the detail of the image, e.g. "low", "high".
}
func (iuc ImageURLContent) String() string {
return iuc.URL
}
func (ImageURLContent) isPart() {}
// BinaryContent is content holding some binary data with a MIME type.
type BinaryContent struct {
MIMEType string
Data []byte
}
func (bc BinaryContent) String() string {
base64Encoded := base64.StdEncoding.EncodeToString(bc.Data)
return "data:" + bc.MIMEType + ";base64," + base64Encoded
}
func (BinaryContent) isPart() {}
// FunctionCall is the name and arguments of a function call.
type FunctionCall struct {
// The name of the function to call.
Name string `json:"name"`
// The arguments to pass to the function, as a JSON string.
Arguments string `json:"arguments"`
}
// ToolCall is a call to a tool (as requested by the model) that should be executed.
type ToolCall struct {
// ID is the unique identifier of the tool call.
ID string `json:"id"`
// Type is the type of the tool call. Typically, this would be "function".
Type string `json:"type"`
// FunctionCall is the function call to be executed.
FunctionCall *FunctionCall `json:"function,omitempty"`
}
func (ToolCall) isPart() {}
// ToolCallResponse is the response returned by a tool call.
type ToolCallResponse struct {
// ToolCallID is the ID of the tool call this response is for.
ToolCallID string `json:"tool_call_id"`
// Name is the name of the tool that was called.
Name string `json:"name"`
// Content is the textual content of the response.
Content string `json:"content"`
}
func (ToolCallResponse) isPart() {}
// ContentResponse is the response returned by a GenerateContent call.
// It can potentially return multiple content choices.
type ContentResponse struct {
Choices []*ContentChoice
Provider string
}
// ContentChoice is one of the response choices returned by GenerateContent
// calls.
type ContentChoice struct {
// Content is the textual content of a response
Content string
// StopReason is the reason the model stopped generating output.
StopReason string
// GenerationInfo is arbitrary information the model adds to the response.
GenerationInfo map[string]any
// FuncCall is non-nil when the model asks to invoke a function/tool.
// If a model invokes more than one function/tool, this field will only
// contain the first one.
FuncCall *FunctionCall
// ToolCalls is a list of tool calls the model asks to invoke.
ToolCalls []ToolCall
// This field is only used with the deepseek-reasoner model and represents the reasoning contents of the assistant message before the final answer.
ReasoningContent string
}
// TextParts is a helper function to create a MessageContent with a role and a
// list of text parts.
func TextParts(role ChatMessageType, parts ...string) MessageContent {
result := MessageContent{
Role: role,
Parts: []ContentPart{},
}
for _, part := range parts {
result.Parts = append(result.Parts, TextPart(part))
}
return result
}
// ShowMessageContents is a debugging helper for MessageContent.
func ShowMessageContents(w io.Writer, msgs []MessageContent) {
fmt.Fprintf(w, "MessageContent (len=%v)\n", len(msgs))
for i, mc := range msgs {
fmt.Fprintf(w, "[%d]: Role=%s\n", i, mc.Role)
for j, p := range mc.Parts {
fmt.Fprintf(w, " Parts[%v]: ", j)
switch pp := p.(type) {
case TextContent:
fmt.Fprintf(w, "TextContent %q\n", pp.Text)
case ImageURLContent:
fmt.Fprintf(w, "ImageURLPart %q\n", pp.URL)
case BinaryContent:
fmt.Fprintf(w, "BinaryContent MIME=%q, size=%d\n", pp.MIMEType, len(pp.Data))
case ToolCall:
fmt.Fprintf(w, "ToolCall ID=%v, Type=%v, Func=%v(%v)\n", pp.ID, pp.Type, pp.FunctionCall.Name, pp.FunctionCall.Arguments)
case ToolCallResponse:
fmt.Fprintf(w, "ToolCallResponse ID=%v, Name=%v, Content=%v\n", pp.ToolCallID, pp.Name, pp.Content)
default:
fmt.Fprintf(w, "unknown type %T\n", pp)
}
}
}
}