mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-16 12:12:55 -06:00
feat: function for openai assistant added
This commit is contained in:
parent
e7ca3cf254
commit
26a974a239
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
* Variables passed to the input in dify
|
* Variables passed to the input in dify
|
||||||
* OwnerJid passed to typebot
|
* OwnerJid passed to typebot
|
||||||
|
* Function for openai assistant added
|
||||||
|
|
||||||
# 2.0.7-rc (2024-08-03 14:04)
|
# 2.0.7-rc (2024-08-03 14:04)
|
||||||
|
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "Instance" ADD COLUMN "disconnectionAt" TIMESTAMP,
|
||||||
|
ADD COLUMN "disconnectionObject" JSONB,
|
||||||
|
ADD COLUMN "disconnectionReasonCode" INTEGER;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "OpenaiBot" ADD COLUMN "functionUrl" VARCHAR(500);
|
@ -374,6 +374,7 @@ model OpenaiBot {
|
|||||||
description String? @db.VarChar(255)
|
description String? @db.VarChar(255)
|
||||||
botType OpenaiBotType
|
botType OpenaiBotType
|
||||||
assistantId String? @db.VarChar(255)
|
assistantId String? @db.VarChar(255)
|
||||||
|
functionUrl String? @db.VarChar(500)
|
||||||
model String? @db.VarChar(100)
|
model String? @db.VarChar(100)
|
||||||
systemMessages Json? @db.JsonB
|
systemMessages Json? @db.JsonB
|
||||||
assistantMessages Json? @db.JsonB
|
assistantMessages Json? @db.JsonB
|
||||||
|
@ -19,6 +19,7 @@ export class OpenaiDto {
|
|||||||
openaiCredsId: string;
|
openaiCredsId: string;
|
||||||
botType?: string;
|
botType?: string;
|
||||||
assistantId?: string;
|
assistantId?: string;
|
||||||
|
functionUrl?: string;
|
||||||
model?: string;
|
model?: string;
|
||||||
systemMessages?: string[];
|
systemMessages?: string[];
|
||||||
assistantMessages?: string[];
|
assistantMessages?: string[];
|
||||||
|
@ -242,6 +242,7 @@ export class OpenaiService {
|
|||||||
openaiCredsId: data.openaiCredsId,
|
openaiCredsId: data.openaiCredsId,
|
||||||
botType: data.botType,
|
botType: data.botType,
|
||||||
assistantId: data.assistantId,
|
assistantId: data.assistantId,
|
||||||
|
functionUrl: data.functionUrl,
|
||||||
model: data.model,
|
model: data.model,
|
||||||
systemMessages: data.systemMessages,
|
systemMessages: data.systemMessages,
|
||||||
assistantMessages: data.assistantMessages,
|
assistantMessages: data.assistantMessages,
|
||||||
@ -407,6 +408,7 @@ export class OpenaiService {
|
|||||||
openaiCredsId: data.openaiCredsId,
|
openaiCredsId: data.openaiCredsId,
|
||||||
botType: data.botType,
|
botType: data.botType,
|
||||||
assistantId: data.assistantId,
|
assistantId: data.assistantId,
|
||||||
|
functionUrl: data.functionUrl,
|
||||||
model: data.model,
|
model: data.model,
|
||||||
systemMessages: data.systemMessages,
|
systemMessages: data.systemMessages,
|
||||||
assistantMessages: data.assistantMessages,
|
assistantMessages: data.assistantMessages,
|
||||||
@ -1315,7 +1317,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);
|
const response = await this.getAIResponse(data.session.sessionId, runAssistant.id, openaiBot.functionUrl);
|
||||||
|
|
||||||
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
||||||
|
|
||||||
@ -1345,18 +1347,73 @@ export class OpenaiService {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getAIResponse(threadId: string, runId: string) {
|
private isJSON(str: string): boolean {
|
||||||
|
try {
|
||||||
|
JSON.parse(str);
|
||||||
|
return true;
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async getAIResponse(threadId: string, runId: string, functionUrl: 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;
|
||||||
|
|
||||||
switch (getRun.status) {
|
switch (getRun.status) {
|
||||||
|
case 'requires_action':
|
||||||
|
toolCalls = getRun?.required_action?.submit_tool_outputs?.tool_calls;
|
||||||
|
|
||||||
|
if (toolCalls) {
|
||||||
|
for (const toolCall of toolCalls) {
|
||||||
|
const id = toolCall.id;
|
||||||
|
const functionName = toolCall?.function?.name;
|
||||||
|
const functionArgument = this.isJSON(toolCall?.function?.arguments)
|
||||||
|
? JSON.parse(toolCall?.function?.arguments)
|
||||||
|
: toolCall?.function?.arguments;
|
||||||
|
|
||||||
|
let output = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { data } = await axios.post(functionUrl, {
|
||||||
|
name: functionName,
|
||||||
|
arguments: functionArgument,
|
||||||
|
});
|
||||||
|
|
||||||
|
output = JSON.stringify(data)
|
||||||
|
.replace(/\\/g, '\\\\')
|
||||||
|
.replace(/"/g, '\\"')
|
||||||
|
.replace(/\n/g, '\\n')
|
||||||
|
.replace(/\r/g, '\\r')
|
||||||
|
.replace(/\t/g, '\\t');
|
||||||
|
} catch (error) {
|
||||||
|
output = JSON.stringify(error)
|
||||||
|
.replace(/\\/g, '\\\\')
|
||||||
|
.replace(/"/g, '\\"')
|
||||||
|
.replace(/\n/g, '\\n')
|
||||||
|
.replace(/\r/g, '\\r')
|
||||||
|
.replace(/\t/g, '\\t');
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.client.beta.threads.runs.submitToolOutputs(threadId, runId, {
|
||||||
|
tool_outputs: [
|
||||||
|
{
|
||||||
|
tool_call_id: id,
|
||||||
|
output,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
|
||||||
case 'queued':
|
case 'queued':
|
||||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||||
return this.getAIResponse(threadId, runId);
|
return this.getAIResponse(threadId, runId, functionUrl);
|
||||||
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);
|
return this.getAIResponse(threadId, runId, functionUrl);
|
||||||
case 'requires_action':
|
|
||||||
return null;
|
|
||||||
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,
|
||||||
@ -1489,7 +1546,7 @@ export class OpenaiService {
|
|||||||
|
|
||||||
await instance.client.sendPresenceUpdate('composing', remoteJid);
|
await instance.client.sendPresenceUpdate('composing', remoteJid);
|
||||||
|
|
||||||
const response = await this.getAIResponse(threadId, runAssistant.id);
|
const response = await this.getAIResponse(threadId, runAssistant.id, openaiBot.functionUrl);
|
||||||
|
|
||||||
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
||||||
|
|
||||||
|
@ -29,6 +29,7 @@ export const openaiSchema: JSONSchema7 = {
|
|||||||
openaiCredsId: { type: 'string' },
|
openaiCredsId: { type: 'string' },
|
||||||
botType: { type: 'string', enum: ['assistant', 'chatCompletion'] },
|
botType: { type: 'string', enum: ['assistant', 'chatCompletion'] },
|
||||||
assistantId: { type: 'string' },
|
assistantId: { type: 'string' },
|
||||||
|
functionUrl: { type: 'string' },
|
||||||
model: { type: 'string' },
|
model: { type: 'string' },
|
||||||
systemMessages: { type: 'array', items: { type: 'string' } },
|
systemMessages: { type: 'array', items: { type: 'string' } },
|
||||||
assistantMessages: { type: 'array', items: { type: 'string' } },
|
assistantMessages: { type: 'array', items: { type: 'string' } },
|
||||||
|
@ -3345,6 +3345,11 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
try {
|
try {
|
||||||
const group = await this.client.groupMetadata(id.groupJid);
|
const group = await this.client.groupMetadata(id.groupJid);
|
||||||
|
|
||||||
|
if (!group) {
|
||||||
|
this.logger.error('Group not found');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
const picture = await this.profilePicture(group.id);
|
const picture = await this.profilePicture(group.id);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
Loading…
Reference in New Issue
Block a user