mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-22 20:12:02 -06:00
feat: adicionada capacidade de OpenAI para interpretar o contexto de contatos de maneira mais precisa
This commit is contained in:
parent
a77fa414e5
commit
0cfd143db0
@ -452,7 +452,7 @@ export class OpenaiService {
|
|||||||
|
|
||||||
const openaiBots = await this.prismaRepository.openaiBot.findMany({
|
const openaiBots = await this.prismaRepository.openaiBot.findMany({
|
||||||
where: {
|
where: {
|
||||||
instanceId: instanceId,
|
instanceId,
|
||||||
},
|
},
|
||||||
include: {
|
include: {
|
||||||
sessions: true,
|
sessions: true,
|
||||||
@ -1350,7 +1350,7 @@ export class OpenaiService {
|
|||||||
|
|
||||||
await instance.client.sendPresenceUpdate('composing', remoteJid);
|
await instance.client.sendPresenceUpdate('composing', remoteJid);
|
||||||
|
|
||||||
const response = await this.getAIResponse(data.session.sessionId, runAssistant.id, openaiBot.functionUrl);
|
const response = await this.getAIResponse(data.session.sessionId, runAssistant.id, openaiBot.functionUrl, remoteJid);
|
||||||
|
|
||||||
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
||||||
|
|
||||||
@ -1426,12 +1426,20 @@ export class OpenaiService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getAIResponse(threadId: string, runId: string, functionUrl: string) {
|
private async getAIResponse(threadId: string, runId: string, functionUrl: string, remoteJid: string) {
|
||||||
const getRun = await this.client.beta.threads.runs.retrieve(threadId, runId);
|
const getRun = await this.client.beta.threads.runs.retrieve(threadId, runId);
|
||||||
let toolCalls;
|
let toolCalls;
|
||||||
|
|
||||||
switch (getRun.status) {
|
switch (getRun.status) {
|
||||||
case 'requires_action':
|
case 'requires_action':
|
||||||
|
const contactPushName = await this.prismaRepository.contact.findFirst({
|
||||||
|
where: {
|
||||||
|
remoteJid,
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
pushName: true,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.then((contact) => contact.pushName);
|
||||||
toolCalls = getRun?.required_action?.submit_tool_outputs?.tool_calls;
|
toolCalls = getRun?.required_action?.submit_tool_outputs?.tool_calls;
|
||||||
|
|
||||||
if (toolCalls) {
|
if (toolCalls) {
|
||||||
@ -1447,7 +1455,7 @@ export class OpenaiService {
|
|||||||
try {
|
try {
|
||||||
const { data } = await axios.post(functionUrl, {
|
const { data } = await axios.post(functionUrl, {
|
||||||
name: functionName,
|
name: functionName,
|
||||||
arguments: functionArgument,
|
arguments: { ...functionArgument, remoteJid, contactPushName },
|
||||||
});
|
});
|
||||||
|
|
||||||
output = JSON.stringify(data)
|
output = JSON.stringify(data)
|
||||||
@ -1476,13 +1484,13 @@ export class OpenaiService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.getAIResponse(threadId, runId, functionUrl);
|
return this.getAIResponse(threadId, runId, functionUrl, remoteJid);
|
||||||
case 'queued':
|
case 'queued':
|
||||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||||
return this.getAIResponse(threadId, runId, functionUrl);
|
return this.getAIResponse(threadId, runId, functionUrl, remoteJid);
|
||||||
case 'in_progress':
|
case 'in_progress':
|
||||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||||
return this.getAIResponse(threadId, runId, functionUrl);
|
return this.getAIResponse(threadId, runId, functionUrl, remoteJid);
|
||||||
case 'completed':
|
case 'completed':
|
||||||
return await this.client.beta.threads.messages.list(threadId, {
|
return await this.client.beta.threads.messages.list(threadId, {
|
||||||
run_id: runId,
|
run_id: runId,
|
||||||
@ -1628,16 +1636,28 @@ export class OpenaiService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await this.client.beta.threads.messages.create(threadId, messageData);
|
await this.client.beta.threads.messages.create(threadId, messageData);
|
||||||
|
const contactPushName = await this.prismaRepository.contact.findFirst({
|
||||||
|
where: {
|
||||||
|
remoteJid,
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
pushName: true,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.then((contact) => contact.pushName);
|
||||||
const runAssistant = await this.client.beta.threads.runs.create(threadId, {
|
const runAssistant = await this.client.beta.threads.runs.create(threadId, {
|
||||||
assistant_id: openaiBot.assistantId,
|
assistant_id: openaiBot.assistantId,
|
||||||
|
additional_instructions: `WhatsappApiInfo:
|
||||||
|
Name: ${contactPushName}
|
||||||
|
RemoteJid: ${remoteJid}
|
||||||
|
`
|
||||||
});
|
});
|
||||||
|
|
||||||
await instance.client.presenceSubscribe(remoteJid);
|
await instance.client.presenceSubscribe(remoteJid);
|
||||||
|
|
||||||
await instance.client.sendPresenceUpdate('composing', remoteJid);
|
await instance.client.sendPresenceUpdate('composing', remoteJid);
|
||||||
|
|
||||||
const response = await this.getAIResponse(threadId, runAssistant.id, openaiBot.functionUrl);
|
const response = await this.getAIResponse(threadId, runAssistant.id, openaiBot.functionUrl, remoteJid);
|
||||||
|
|
||||||
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user