forked from Joao208/alexa-chatgpt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaiClient.js
More file actions
96 lines (82 loc) · 2.36 KB
/
aiClient.js
File metadata and controls
96 lines (82 loc) · 2.36 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
const axios = require("axios");
const OPENAI_RESPONSES_URL = "https://api.openai.com/v1/responses";
const OPENAI_TIMEOUT_MS = Number(process.env.OPENAI_TIMEOUT_MS || 12000);
function getProvider() {
return (process.env.AI_PROVIDER || "responses").toLowerCase();
}
async function askWithResponses(prompt) {
const response = await axios.post(
OPENAI_RESPONSES_URL,
{
model: process.env.OPENAI_MODEL || "gpt-4o-mini",
input: prompt,
max_output_tokens: Number(process.env.OPENAI_MAX_OUTPUT_TOKENS || 150),
temperature: Number(process.env.OPENAI_TEMPERATURE || 0.5),
},
{
timeout: OPENAI_TIMEOUT_MS,
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
},
}
);
const output = response.data.output || [];
const outputText = output
.flatMap((item) => item.content || [])
.filter((content) => content.type === "output_text" && content.text)
.map((content) => content.text)
.join(" ")
.trim();
if (outputText) return outputText;
return response.data.choices?.[0]?.text || "";
}
async function askWithRealtime2Broker(prompt, context = {}) {
const brokerUrl = process.env.REALTIME2_BROKER_URL;
if (!brokerUrl) {
throw new Error("AI_PROVIDER=realtime2 requires REALTIME2_BROKER_URL");
}
const response = await axios.post(
brokerUrl,
{
input_text: prompt,
session: {
session_id: context.sessionId,
user_id: context.userId,
locale: context.locale,
},
metadata: {
source: "alexa-skill",
},
},
{
timeout: OPENAI_TIMEOUT_MS,
headers: {
"Content-Type": "application/json",
...(process.env.REALTIME2_BROKER_API_KEY
? { Authorization: `Bearer ${process.env.REALTIME2_BROKER_API_KEY}` }
: {}),
},
}
);
const text =
response.data?.output_text ||
response.data?.text ||
response.data?.response?.text ||
"";
return String(text).trim();
}
async function ask(prompt, context = {}) {
if (!prompt || !prompt.trim()) {
return "Não entendi sua pergunta. Pode repetir de outro jeito?";
}
const provider = getProvider();
if (provider === "realtime2") {
return askWithRealtime2Broker(prompt, context);
}
return askWithResponses(prompt);
}
module.exports = {
ask,
getProvider,
};