mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-13 15:14:49 -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
|
||||
* OwnerJid passed to typebot
|
||||
* Function for openai assistant added
|
||||
|
||||
# 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)
|
||||
botType OpenaiBotType
|
||||
assistantId String? @db.VarChar(255)
|
||||
functionUrl String? @db.VarChar(500)
|
||||
model String? @db.VarChar(100)
|
||||
systemMessages Json? @db.JsonB
|
||||
assistantMessages Json? @db.JsonB
|
||||
|
@ -19,6 +19,7 @@ export class OpenaiDto {
|
||||
openaiCredsId: string;
|
||||
botType?: string;
|
||||
assistantId?: string;
|
||||
functionUrl?: string;
|
||||
model?: string;
|
||||
systemMessages?: string[];
|
||||
assistantMessages?: string[];
|
||||
|
@ -242,6 +242,7 @@ export class OpenaiService {
|
||||
openaiCredsId: data.openaiCredsId,
|
||||
botType: data.botType,
|
||||
assistantId: data.assistantId,
|
||||
functionUrl: data.functionUrl,
|
||||
model: data.model,
|
||||
systemMessages: data.systemMessages,
|
||||
assistantMessages: data.assistantMessages,
|
||||
@ -407,6 +408,7 @@ export class OpenaiService {
|
||||
openaiCredsId: data.openaiCredsId,
|
||||
botType: data.botType,
|
||||
assistantId: data.assistantId,
|
||||
functionUrl: data.functionUrl,
|
||||
model: data.model,
|
||||
systemMessages: data.systemMessages,
|
||||
assistantMessages: data.assistantMessages,
|
||||
@ -1315,7 +1317,7 @@ export class OpenaiService {
|
||||
|
||||
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);
|
||||
|
||||
@ -1345,18 +1347,73 @@ export class OpenaiService {
|
||||
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);
|
||||
let toolCalls;
|
||||
|
||||
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':
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
return this.getAIResponse(threadId, runId);
|
||||
return this.getAIResponse(threadId, runId, functionUrl);
|
||||
case 'in_progress':
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
return this.getAIResponse(threadId, runId);
|
||||
case 'requires_action':
|
||||
return null;
|
||||
return this.getAIResponse(threadId, runId, functionUrl);
|
||||
case 'completed':
|
||||
return await this.client.beta.threads.messages.list(threadId, {
|
||||
run_id: runId,
|
||||
@ -1489,7 +1546,7 @@ export class OpenaiService {
|
||||
|
||||
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);
|
||||
|
||||
|
@ -29,6 +29,7 @@ export const openaiSchema: JSONSchema7 = {
|
||||
openaiCredsId: { type: 'string' },
|
||||
botType: { type: 'string', enum: ['assistant', 'chatCompletion'] },
|
||||
assistantId: { type: 'string' },
|
||||
functionUrl: { type: 'string' },
|
||||
model: { type: 'string' },
|
||||
systemMessages: { type: 'array', items: { type: 'string' } },
|
||||
assistantMessages: { type: 'array', items: { type: 'string' } },
|
||||
|
@ -3345,6 +3345,11 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
try {
|
||||
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);
|
||||
|
||||
return {
|
||||
|
Loading…
Reference in New Issue
Block a user