FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
function-openai/fn.go at main · upbound/function-openai · GitHub
upbound
/
function-openai
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
2
Code
Issues
0
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
function-openai
/
fn.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
480 lines (400 loc) · 14.2 KB
Breadcrumbs
function-openai
/
fn.go
Copy path
File metadata and controls
480 lines (400 loc) · 14.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
Copyright 2025 The Upbound Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
main
import
(
"context"
"encoding/json"
"html/template"
"strings"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
"github.com/tmc/langchaingo/agents"
"github.com/tmc/langchaingo/chains"
openaillm
"github.com/tmc/langchaingo/llms/openai"
"github.com/tmc/langchaingo/tools"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/structpb"
"sigs.k8s.io/yaml"
"github.com/crossplane/function-sdk-go/errors"
"github.com/crossplane/function-sdk-go/logging"
fnv1
"github.com/crossplane/function-sdk-go/proto/v1"
"github.com/crossplane/function-sdk-go/request"
"github.com/crossplane/function-sdk-go/resource"
"github.com/crossplane/function-sdk-go/response"
"github.com/upbound/function-openai/input/v1alpha1"
"github.com/upbound/function-openai/internal/tool"
)
const
(
credName
=
"gpt"
credKey
=
"OPENAI_API_KEY"
credBaseURLKey
=
"OPENAI_BASE_URL"
credModelKey
=
"OPENAI_MODEL"
defaultModel
=
"gpt-4"
)
// Variables used to form the prompt.
type
Variables
struct
{
// Observed composite resource, as a YAML manifest.
Composite
string
// Observed composed resources, as a stream of YAML manifests.
Composed
string
}
// Function asks GPT to compose resources.
type
Function
struct
{
fnv1.
UnimplementedFunctionRunnerServiceServer
ai
agentInvoker
log
logging.
Logger
}
// agentInvoker is a consumer interface for working with agents. Notably this
// is helpful for writing tests that mock the agent invocations.
type
agentInvoker
interface
{
Invoke
(
ctx
context.
Context
,
key
,
system
,
prompt
,
baseURL
,
modelName
string
) (
string
,
error
)
}
// Option modifies the underlying Function.
type
Option
func
(
*
Function
)
// WithLogger overrides the default logger.
func
WithLogger
(
log
logging.
Logger
)
Option
{
return
func
(
f
*
Function
) {
f
.
log
=
log
}
}
// NewFunction creates a new function powered by GPT.
func
NewFunction
(
opts
...
Option
)
*
Function
{
f
:=
&
Function
{
log
:
logging
.
NewNopLogger
(),
}
for
_
,
o
:=
range
opts
{
o
(
f
)
}
f
.
ai
=
&
agent
{
log
:
f
.
log
,
res
:
tool
.
NewResolver
(
tool
.
WithLogger
(
f
.
log
)),
}
return
f
}
// RunFunction runs the Function.
func
(
f
*
Function
)
RunFunction
(
ctx
context.
Context
,
req
*
fnv1.
RunFunctionRequest
) (
*
fnv1.
RunFunctionResponse
,
error
) {
log
:=
f
.
log
.
WithValues
(
"tag"
,
req
.
GetMeta
().
GetTag
())
log
.
Info
(
"Running function"
,
"tag"
,
req
.
GetMeta
().
GetTag
())
rsp
:=
response
.
To
(
req
,
response
.
DefaultTTL
)
if
f
.
shouldIgnore
(
req
) {
response
.
ConditionTrue
(
rsp
,
"FunctionSuccess"
,
"Success"
).
TargetCompositeAndClaim
()
response
.
Normal
(
rsp
,
"received an ignored resource, skipping"
)
return
rsp
,
nil
}
in
:=
&
v1alpha1.
Prompt
{}
if
err
:=
request
.
GetInput
(
req
,
in
);
err
!=
nil
{
response
.
Fatal
(
rsp
,
errors
.
Wrapf
(
err
,
"cannot get Function input from %T"
,
req
))
return
rsp
,
err
}
c
,
err
:=
request
.
GetCredentials
(
req
,
credName
)
if
err
!=
nil
{
response
.
Fatal
(
rsp
,
errors
.
Wrapf
(
err
,
"cannot get OPENAI_API_KEY from credential %q"
,
credName
))
return
rsp
,
err
}
if
c
.
Type
!=
resource
.
CredentialsTypeData
{
response
.
Fatal
(
rsp
,
errors
.
Errorf
(
"expected credential %q to be %q, got %q"
,
credName
,
resource
.
CredentialsTypeData
,
c
.
Type
))
return
rsp
,
err
}
b
,
ok
:=
c
.
Data
[
credKey
]
if
!
ok
{
response
.
Fatal
(
rsp
,
errors
.
Errorf
(
"credential %q is missing required key %q"
,
credName
,
credKey
))
return
rsp
,
err
}
// TODO(negz): Where the heck is the newline at the end of this key
// coming from? Bug in crossplane render?
key
:=
strings
.
Trim
(
string
(
b
),
"
\n
"
)
// Extract optional base URL from credentials
var
baseURL
string
if
baseURLBytes
,
ok
:=
c
.
Data
[
credBaseURLKey
];
ok
{
baseURL
=
strings
.
Trim
(
string
(
baseURLBytes
),
"
\n
"
)
}
// Extract optional model from credentials, default to gpt-4
model
:=
defaultModel
if
modelBytes
,
ok
:=
c
.
Data
[
credModelKey
];
ok
{
model
=
strings
.
Trim
(
string
(
modelBytes
),
"
\n
"
)
}
d
:=
pipelineDetails
{
req
:
req
,
rsp
:
rsp
,
in
:
in
,
cred
:
key
,
baseURL
:
baseURL
,
model
:
model
,
}
// If we're in a composition pipeline we want to do things with the
// composed resources.
if
inCompositionPipeline
(
req
) {
return
f
.
compositionPipeline
(
ctx
,
log
,
d
)
}
// Handle operation pipeline separately.
return
f
.
operationPipeline
(
ctx
,
log
,
d
)
}
// CompositeToYAML returns the XR as YAML.
func
CompositeToYAML
(
xr
*
fnv1.
Resource
) (
string
,
error
) {
j
,
err
:=
protojson
.
Marshal
(
xr
.
GetResource
())
if
err
!=
nil
{
return
""
,
errors
.
Wrap
(
err
,
"cannot convert XR to JSON"
)
}
y
,
err
:=
yaml
.
JSONToYAML
(
j
)
return
string
(
y
),
errors
.
Wrap
(
err
,
"cannot convert XR to YAML"
)
}
// ComposedToYAML returns the supplied composed resources as a YAML stream. The
// resources are annotated with their upbound.io/name annotations.
func
ComposedToYAML
(
cds
map
[
string
]
*
fnv1.
Resource
) (
string
,
error
) {
composed
:=
&
strings.
Builder
{}
for
name
,
ocd
:=
range
cds
{
jocd
,
err
:=
protojson
.
Marshal
(
ocd
.
GetResource
())
if
err
!=
nil
{
return
""
,
errors
.
Wrap
(
err
,
"cannot convert composed resource to JSON"
)
}
jocd
,
err
=
sjson
.
SetBytes
(
jocd
,
"metadata.annotations.upbound
\\
.io/name"
,
name
)
if
err
!=
nil
{
return
""
,
errors
.
Wrapf
(
err
,
"cannot set upbound.io/name annotation"
)
}
yocd
,
err
:=
yaml
.
JSONToYAML
(
jocd
)
if
err
!=
nil
{
return
""
,
errors
.
Wrap
(
err
,
"cannot convert composed resource to YAML"
)
}
composed
.
WriteString
(
"---
\n
"
)
composed
.
Write
(
yocd
)
}
return
composed
.
String
(),
nil
}
// ComposedFromYAML parses the supplied YAML stream as desired composed
// resources. The resource names are extracted from the upbound.io/name
// annotation.
func
ComposedFromYAML
(
y
string
) (
map
[
string
]
*
fnv1.
Resource
,
error
) {
out
:=
make
(
map
[
string
]
*
fnv1.
Resource
)
for
doc
:=
range
strings
.
SplitSeq
(
y
,
"---"
) {
if
doc
==
""
{
continue
}
j
,
err
:=
yaml
.
YAMLToJSON
([]
byte
(
doc
))
if
err
!=
nil
{
return
nil
,
errors
.
Wrap
(
err
,
"cannot parse YAML"
)
}
s
:=
&
structpb.
Struct
{}
if
err
:=
protojson
.
Unmarshal
(
j
,
s
);
err
!=
nil
{
return
nil
,
errors
.
Wrap
(
err
,
"cannot parse JSON"
)
}
name
:=
gjson
.
GetBytes
(
j
,
"metadata.annotations.upbound
\\
.io/name"
).
String
()
if
name
==
""
{
return
nil
,
errors
.
New
(
"missing 'upbound.io/name' annotation"
)
}
if
_
,
seen
:=
out
[
name
];
seen
{
return
nil
,
errors
.
Errorf
(
"'upbound.io/name' annotation %q must be unique within the YAML stream"
,
name
)
}
out
[
name
]
=
&
fnv1.
Resource
{
Resource
:
s
}
}
return
out
,
nil
}
// removeYAMLMarkdown is a helper function for cleaning the output from GPT.
// The responses can be inconsitent with markdown being returned at times.
// This function takes a multi-line string containing YAML tags and cleans
// those for future processing of the YAML stream.
func
removeYAMLMarkdown
(
in
string
)
string
{
wsRemoved
:=
strings
.
TrimSpace
(
in
)
yamlPrefix
:=
strings
.
TrimPrefix
(
wsRemoved
,
"```yaml"
)
return
strings
.
TrimSuffix
(
yamlPrefix
,
"```"
)
}
// resourceFrom produces a map of resource name to resources derived from the
// given string. If the string is neither JSON nor YAML, an error is returned.
func
(
f
*
Function
)
resourceFrom
(
i
string
) (
map
[
string
]
*
fnv1.
Resource
,
error
) {
out
:=
make
(
map
[
string
]
*
fnv1.
Resource
)
b
:=
[]
byte
(
i
)
// Is i YAML?
jb
,
err
:=
yaml
.
YAMLToJSON
(
b
)
if
err
!=
nil
{
f
.
log
.
Debug
(
"error seen while attempting to convert YAML to JSON"
,
"error"
,
err
)
// i doesn't appear to be YAML, maybe it's JSON...
jb
=
b
}
s
:=
&
structpb.
Struct
{}
if
err
:=
protojson
.
Unmarshal
(
jb
,
s
);
err
!=
nil
{
return
nil
,
errors
.
Wrap
(
err
,
"cannot parse JSON"
)
}
name
:=
gjson
.
GetBytes
(
jb
,
"metadata.name"
).
String
()
out
[
name
]
=
&
fnv1.
Resource
{
Resource
:
s
}
return
out
,
nil
}
// attempts to identify if the function is operating within a composition
// pipeline or not by looking to see if a composite was sent with the request.
func
inCompositionPipeline
(
req
*
fnv1.
RunFunctionRequest
)
bool
{
return
req
.
GetObserved
().
GetComposite
()
!=
nil
}
// pipelineDetails wraps the inputs and outputs for the given function run.
type
pipelineDetails
struct
{
// FunctionRequest
req
*
fnv1.
RunFunctionRequest
// FunctionResponse
rsp
*
fnv1.
RunFunctionResponse
// marshalled input
in
*
v1alpha1.
Prompt
// LLM API credential
cred
string
// Optional base URL for OpenAI API
baseURL
string
// Optional model name, defaults to gpt-4
model
string
}
// compositionPipeline processes the given pipelineDetails with the assumption
// that the function is defined in a composition pipeline and will be working
// with composites and desired resources.
func
(
f
*
Function
)
compositionPipeline
(
ctx
context.
Context
,
log
logging.
Logger
,
d
pipelineDetails
) (
*
fnv1.
RunFunctionResponse
,
error
) {
userPrompt
,
err
:=
template
.
New
(
"prompt"
).
Parse
(
d
.
in
.
UserPrompt
)
if
err
!=
nil
{
response
.
Fatal
(
d
.
rsp
,
errors
.
Wrap
(
err
,
"cannot parse userPrompt"
))
return
d
.
rsp
,
err
}
// TODO(ththornton): possibly switch to just JSON to remove the double encode.
xr
,
err
:=
CompositeToYAML
(
d
.
req
.
GetObserved
().
GetComposite
())
if
err
!=
nil
{
response
.
Fatal
(
d
.
rsp
,
errors
.
Wrap
(
err
,
"cannot convert observed XR to YAML"
))
return
d
.
rsp
,
err
}
cds
,
err
:=
ComposedToYAML
(
d
.
req
.
GetObserved
().
GetResources
())
if
err
!=
nil
{
response
.
Fatal
(
d
.
rsp
,
errors
.
Wrap
(
err
,
"cannot convert observed composed resources to YAML"
))
return
d
.
rsp
,
err
}
pb
:=
&
strings.
Builder
{}
if
err
:=
userPrompt
.
Execute
(
pb
,
&
Variables
{
Composite
:
xr
,
Composed
:
cds
});
err
!=
nil
{
response
.
Fatal
(
d
.
rsp
,
errors
.
Wrapf
(
err
,
"cannot build prompt from template"
))
return
d
.
rsp
,
err
}
log
.
Debug
(
"Using prompt"
,
"prompt"
,
pb
.
String
())
resp
,
err
:=
f
.
ai
.
Invoke
(
ctx
,
d
.
cred
,
d
.
in
.
SystemPrompt
,
pb
.
String
(),
d
.
baseURL
,
d
.
model
)
if
err
!=
nil
{
response
.
Fatal
(
d
.
rsp
,
errors
.
Wrap
(
err
,
"failed to run chain"
))
return
d
.
rsp
,
err
}
result
:=
""
dcds
,
err
:=
ComposedFromYAML
(
removeYAMLMarkdown
(
resp
))
if
err
!=
nil
{
result
=
err
.
Error
()
log
.
Debug
(
"Submitted YAML stream"
,
"result"
,
result
,
"isError"
,
true
)
response
.
Fatal
(
d
.
rsp
,
errors
.
Wrap
(
err
,
"did not receive a YAML stream from GPT"
))
return
d
.
rsp
,
err
}
log
.
Debug
(
"Received YAML manifests from GPT"
,
"resourceCount"
,
len
(
dcds
))
d
.
rsp
.
Desired
.
Resources
=
dcds
return
d
.
rsp
,
nil
}
// OperationVariables used to form the prompt.
type
OperationVariables
struct
{
Input
string
`json:"input"`
Resources
string
`json:"resources"`
}
// operationPipeline processes the given pipelineDetails with the assumption
// that the function is defined in an operations pipeline.
func
(
f
*
Function
)
operationPipeline
(
ctx
context.
Context
,
log
logging.
Logger
,
d
pipelineDetails
) (
*
fnv1.
RunFunctionResponse
,
error
) {
prompt
,
err
:=
template
.
New
(
"prompt"
).
Parse
(
d
.
in
.
UserPrompt
)
if
err
!=
nil
{
response
.
Fatal
(
d
.
rsp
,
errors
.
New
(
"failed to parse UserPrompt as a go-template"
))
return
d
.
rsp
,
err
}
rr
,
err
:=
request
.
GetRequiredResources
(
d
.
req
)
if
err
!=
nil
{
response
.
Fatal
(
d
.
rsp
,
errors
.
Wrapf
(
err
,
"cannot get Function extra resources from %T"
,
d
.
req
))
return
d
.
rsp
,
err
}
// TODO(tnthornton) reference const from c/c instead. Currently too many
// conflicting dependencies are pulled in when updating c/c in this repo.
rs
,
ok
:=
rr
[
"ops.crossplane.io/watched-resource"
]
if
!
ok
{
f
.
log
.
Debug
(
"no resource to process"
)
response
.
ConditionTrue
(
d
.
rsp
,
"FunctionSuccess"
,
"Success"
).
TargetCompositeAndClaim
()
return
d
.
rsp
,
nil
}
if
len
(
rs
)
!=
1
{
response
.
Fatal
(
d
.
rsp
,
errors
.
New
(
"too many resources sent to the function. expected 1"
))
return
d
.
rsp
,
err
}
rb
,
err
:=
json
.
MarshalIndent
(
rs
[
0
].
Resource
.
UnstructuredContent
(),
""
,
" "
)
if
err
!=
nil
{
response
.
Fatal
(
d
.
rsp
,
errors
.
New
(
"failed to unmarshal required resource"
))
return
d
.
rsp
,
err
}
vars
:=
&
strings.
Builder
{}
if
err
:=
prompt
.
Execute
(
vars
,
&
OperationVariables
{
Input
:
d
.
in
.
UserPrompt
,
Resources
:
string
(
rb
)});
err
!=
nil
{
response
.
Fatal
(
d
.
rsp
,
errors
.
Wrapf
(
err
,
"cannot build prompt from template"
))
return
d
.
rsp
,
err
}
log
.
Debug
(
"Using prompt"
,
"prompt"
,
vars
.
String
())
resp
,
err
:=
f
.
ai
.
Invoke
(
ctx
,
d
.
cred
,
d
.
in
.
SystemPrompt
,
vars
.
String
(),
d
.
baseURL
,
d
.
model
)
if
err
!=
nil
{
response
.
Fatal
(
d
.
rsp
,
errors
.
Wrap
(
err
,
"failed to run chain"
))
return
d
.
rsp
,
err
}
desired
,
err
:=
f
.
resourceFrom
(
resp
)
if
err
!=
nil
{
// we didn't get a JSON based response from GPT
log
.
Debug
(
"failed to get a JSON response back, no desired resources will be sent back to crossplane"
)
}
response
.
ConditionTrue
(
d
.
rsp
,
"FunctionSuccess"
,
"Success"
).
TargetCompositeAndClaim
()
response
.
Normal
(
d
.
rsp
,
resp
)
d
.
rsp
.
Desired
.
Resources
=
desired
return
d
.
rsp
,
nil
}
// shouldIgnore returns true if the caller has communicated that the resource
// is an ignored resource. False otherwise.
func
(
f
*
Function
)
shouldIgnore
(
req
*
fnv1.
RunFunctionRequest
)
bool
{
fctx
:=
req
.
GetContext
()
ignored
,
ok
:=
fctx
.
AsMap
()[
"ops.upbound.io/ignored-resource"
]
if
ok
{
i
,
ok
:=
ignored
.(
bool
)
if
ok
&&
i
{
return
true
}
}
return
false
}
type
agent
struct
{
log
logging.
Logger
res
*
tool.
Resolver
}
// Invoke makes an external call to the configured LLM with the supplied
// credential key, system and user prompts.
func
(
a
*
agent
)
Invoke
(
ctx
context.
Context
,
key
,
system
,
prompt
,
baseURL
,
modelName
string
) (
string
,
error
) {
opts
:=
[]openaillm.
Option
{
openaillm
.
WithToken
(
key
),
openaillm
.
WithModel
(
modelName
),
}
// Add custom base URL if provided
if
baseURL
!=
""
{
opts
=
append
(
opts
,
openaillm
.
WithBaseURL
(
baseURL
))
}
model
,
err
:=
openaillm
.
New
(
opts
...
)
if
err
!=
nil
{
return
""
,
errors
.
Wrap
(
err
,
"failed to build model"
)
}
agent
:=
agents
.
NewOpenAIFunctionsAgent
(
model
,
a
.
tools
(
ctx
),
agents
.
WithMaxIterations
(
20
),
agents
.
NewOpenAIOption
().
WithSystemMessage
(
system
),
)
return
chains
.
Run
(
ctx
,
agents
.
NewExecutor
(
agent
),
prompt
,
chains
.
WithTemperature
(
float64
(
0
)),
)
}
func
(
a
*
agent
)
tools
(
ctx
context.
Context
) []tools.
Tool
{
cfgs
:=
a
.
res
.
FromEnvVars
()
if
len
(
cfgs
)
==
0
{
a
.
log
.
Debug
(
"no valid mcp server configurations found"
)
}
return
a
.
res
.
Resolve
(
ctx
,
cfgs
)
}
Back
|
FazBrowse Home
|
New Git URL