-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenAi.ts
More file actions
123 lines (107 loc) · 4.32 KB
/
openAi.ts
File metadata and controls
123 lines (107 loc) · 4.32 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
import { settings } from '$lib/config'
import { fetchRelevantHistory } from './history'
import { logger } from './logger'
import { openAiPrompt, systemContext } from './prompts'
import type { Message, OpenAIConfig, ToneType } from './types'
import { formatMessagesAsText } from './utils'
const API_URL = 'https://api.openai.com/v1/chat/completions'
const DEFAULT_MODEL = 'gpt-4'
const DEFAULT_TEMPERATURE = 0.5
const getConfig = (): OpenAIConfig => ({
model: settings.OPENAI_MODEL || DEFAULT_MODEL,
temperature: Number(settings.OPENAI_TEMPERATURE || DEFAULT_TEMPERATURE),
topP: settings.OPENAI_TOP_P ? Number(settings.OPENAI_TOP_P) : undefined,
frequencyPenalty: settings.OPENAI_FREQUENCY_PENALTY ? Number(settings.OPENAI_FREQUENCY_PENALTY) : undefined,
presencePenalty: settings.OPENAI_PRESENCE_PENALTY ? Number(settings.OPENAI_PRESENCE_PENALTY) : undefined,
apiUrl: API_URL,
apiKey: settings.OPENAI_API_KEY,
})
if (!settings.OPENAI_API_KEY) {
logger.warn('⚠️ OPENAI_API_KEY is not set. OpenAI integration will not work.')
}
const summaryFunction = {
type: 'function',
function: {
name: 'draft_replies',
description: 'Generate a short summary and three suggested replies',
parameters: {
type: 'object',
properties: {
summary: {
type: 'string',
description: 'Brief summary of the conversation',
},
replies: {
type: 'array',
items: { type: 'string' },
description: 'Suggested replies for me',
},
},
required: ['summary', 'replies'],
},
},
}
export const getOpenaiReply = async (
messages: Message[],
tone: ToneType,
context: string
): Promise<{ summary: string; replies: string[] }> => {
const config = getConfig()
if (!config.apiKey) {
return {
summary: 'OpenAI API key is not configured.',
replies: ['Please set up your OpenAI API key in the .env file.'],
}
}
const historyContext = await fetchRelevantHistory(messages)
const prompt = openAiPrompt(tone, historyContext) + '\n\n' + context
const body = {
model: config.model,
messages: [
{ role: 'system', content: systemContext() },
{ role: 'user', content: formatMessagesAsText(messages) },
{ role: 'user', content: prompt },
],
temperature: config.temperature,
...(config.topP && { top_p: config.topP }),
...(config.frequencyPenalty && { frequency_penalty: config.frequencyPenalty }),
...(config.presencePenalty && { presence_penalty: config.presencePenalty }),
tools: [summaryFunction],
tool_choice: { type: 'function', function: { name: 'draft_replies' } },
}
logger.debug({ body }, 'Sending request to OpenAI')
logger.info('Sending request to OpenAI')
try {
const response = await fetch(API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${config.apiKey}`,
},
body: JSON.stringify(body),
})
logger.debug(
{ status: response.status, statusText: response.statusText },
'OpenAI API response received'
)
if (!response.ok) {
logger.error({ status: response.status }, 'OpenAI API error')
throw new Error(`OpenAI API error code ${response.status}: ${response.statusText}`)
}
const { choices } = await response.json()
const functionCall = choices[0]?.message?.tool_calls?.[0]?.function
if (!functionCall) throw new Error('No function call in response')
const { summary, replies } = JSON.parse(functionCall.arguments)
if (!summary || !Array.isArray(replies)) {
logger.error({ summary, replies }, 'Invalid response format from OpenAI')
throw new Error('Invalid response format from OpenAI')
}
return { summary, replies }
} catch (error) {
logger.error({ error }, 'Error in getOpenaiReply (fetching or parsing OpenAI response)')
return {
summary: '',
replies: ['(AI API error. Check your key and usage.)'],
}
}
}