-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathai.ts
57 lines (51 loc) · 2.1 KB
/
ai.ts
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
import { executeFunction } from '@/tasks/ai/executeFunction'
import { createOpenAI } from '@ai-sdk/openai'
type AIFunctionSettings = {
_model?: string
_temperature?: number
_maxTokens?: number
_topP?: number
_topK?: number
}
type AIFunctionProperty = string | string[] | Record<string, string> | Record<string, string[]>
type AIFunction = Record<string, AIFunctionProperty>
type AIWorkflow = (args: any) => Promise<any> // TODO: Get this strongly typed with generics on ai and db
type AIConfig = AIFunctionSettings & {
[key: string]: AIFunction | AIWorkflow
}
export const AI = (config: AIConfig) => {
return new Proxy(
{},
{
get: (target: any, functionName: string) => {
if (config[functionName]) {
// Strip out _* properties
const schema = Object.fromEntries(Object.entries(config[functionName]).filter(([key]) => !key.startsWith('_')))
const settings = Object.fromEntries(Object.entries(config[functionName]).filter(([key]) => key.startsWith('_')))
// TODO: if functionName is a workflow, call it directly wrapped in try/catch, and maybe queued in a job
return (args: any) => {
if (!functionName) {
console.error('Missing functionName in AI function call')
throw new Error('Invalid function call: missing functionName')
}
return executeFunction({ functionName, schema, settings, args }).catch((error) => {
console.error(`Error executing function ${functionName}:`, error)
// TODO: log error in db ... maybe also create a task to retry
throw error
})
}
}
throw new Error(`Function ${functionName} not found in AI config`)
},
},
)
}
export const model = createOpenAI({
compatibility: 'compatible',
apiKey: process.env.AI_GATEWAY_TOKEN!,
baseURL: process.env.AI_GATEWAY_URL!,
headers: {
'HTTP-Referer': 'https://workflows.do', // Optional. Site URL for rankings on openrouter.ai.
'X-Title': 'Workflows.do Business-as-Code', // Optional. Site title for rankings on openrouter.ai.
},
})