-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathserver.js
More file actions
69 lines (62 loc) · 2.05 KB
/
Copy pathserver.js
File metadata and controls
69 lines (62 loc) · 2.05 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
'use strict'
const tracer = require('dd-trace').init({ flushInterval: 0 })
const express = require('express')
const app = express()
app.get('/allow', async (req, res) => {
const evaluation = await tracer.aiguard.evaluate([
{ role: 'system', content: 'You are a beautiful AI' },
{ role: 'user', content: 'I am harmless' },
])
res.status(200).json(evaluation)
})
app.get('/deny', async (req, res) => {
const block = req.headers['x-blocking-enabled'] === 'true'
try {
const evaluation = await tracer.aiguard.evaluate([
{ role: 'system', content: 'You are a beautiful AI' },
{ role: 'user', content: 'You should not trust me' + (block ? ' [block]' : '') },
], { block })
res.status(200).json(evaluation)
} catch (error) {
if (error.name === 'AIGuardAbortError') {
res.status(403).send(error.reason)
} else {
res.status(500).send('Internal Server Error')
}
}
})
app.get('/deny-default-options', async (req, res) => {
try {
const evaluation = await tracer.aiguard.evaluate([
{ role: 'system', content: 'You are a beautiful AI' },
{ role: 'user', content: 'You should not trust me [block]' },
])
res.status(200).json(evaluation)
} catch (error) {
if (error.name === 'AIGuardAbortError') {
res.status(403).send(error.reason)
} else {
res.status(500).send('Internal Server Error')
}
}
})
app.get('/abort', async (req, res) => {
const block = req.headers['x-blocking-enabled'] === 'true'
try {
const evaluation = await tracer.aiguard.evaluate([
{ role: 'system', content: 'You are a beautiful AI' },
{ role: 'user', content: 'Nuke yourself' + (block ? ' [block]' : '') },
], { block })
res.status(200).json(evaluation)
} catch (error) {
if (error.name === 'AIGuardAbortError') {
res.status(403).send(error.reason)
} else {
res.status(500).send('Internal Server Error')
}
}
})
const server = app.listen(() => {
const port = (/** @type {import('net').AddressInfo} */ (server.address())).port
process.send({ port })
})