-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenai.service.js
More file actions
138 lines (110 loc) · 2.91 KB
/
Copy pathopenai.service.js
File metadata and controls
138 lines (110 loc) · 2.91 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
import 'dotenv/config';
import OpenAI from 'openai';
class OpenAIService {
static openai;
static init(key) {
this.openai = new OpenAI(process.env.OPENAI_API_KEY || key);
}
static async sendMessage(data) {
//region Parameters
const {
model = 'gpt-4o',
frequencyPenalty = 0,
system = null,
prompt = null,
messages = [],
maxTokens = null,
maxCompletionTokens = null,
presencePenalty = 0,
responseFormat = null,
seed = null,
stop = null,
stream = false,
temperature = 1,
topP = 1,
tools = null,
toolChoice = null,
parallelToolCalls = null,
} = data;
//endregion
try {
// prepare messages
const preparedMessages = [];
if(system) preparedMessages.push({ role: 'system', content: system });
preparedMessages.push(...messages);
if(prompt) preparedMessages.push({ role: 'user', content: prompt });
const body = {
messages: preparedMessages,
model,
frequency_penalty: frequencyPenalty,
presence_penalty: presencePenalty,
stop,
stream,
temperature,
top_p: topP,
max_tokens: 2048,
};
if(maxTokens) body.max_tokens = maxTokens;
if(maxCompletionTokens) body.max_completion_tokens = maxCompletionTokens;
if(seed) body.seed = seed;
if(responseFormat) {
if(responseFormat === 'json') body.response_format = { type: 'json_object' };
else body.response_format = responseFormat;
}
if(tools) {
body.tools = this.prepareTools(tools);
if(toolChoice) body.tool_choice = toolChoice;
if(parallelToolCalls) body.parallel_tool_calls = parallelToolCalls;
}
console.log('Body:', body);
if(stream) {
return this.openai.chat.completions.create(body);
} else {
const completion = await this.openai.chat.completions.create(body);
if(tools) {
return this.prepareToolCalls(completion.choices[0].message.tool_calls);
}
if(responseFormat === 'json') {
try {
return JSON.parse(completion.choices[0].message.content);
} catch(e) {
console.error('Error parsing response:', e);
return completion.choices[0];
}
}
return completion.choices[0];
}
} catch(error) {
console.error('Error sending message:', error);
}
}
static prepareTools(tools) {
const preparedTools = [];
for(const tool of Object.values(tools)) {
preparedTools.push({
type: 'function',
function: tool,
});
}
return preparedTools;
}
static prepareToolCalls(toolCalls) {
const preparedToolCalls = [];
for(const toolCall of toolCalls) {
preparedToolCalls.push({
name: toolCall.function.name,
arguments: JSON.parse(toolCall.function.arguments),
});
}
return preparedToolCalls;
}
static async generateEmbedding(text) {
const embedding = await this.openai.embeddings.create({
model: 'text-embedding-3-small',
input: text,
encoding_format: 'float',
});
return embedding.data[0].embedding;
}
}
export default OpenAIService;