import { ask_gpt } from 'backend/openai' import wixChatBackend from 'wix-chat-backend'; import wixData from 'wix-data'; export function wixChat_onMessage(event) { if (event.direction === 'VisitorToBusiness') { start(event) } } async function start(event){ wixData.query('gpt_chat').eq('channelId', event.channelId).find() .then(async (res) => { if (res.items.length > 0) { console.log('CHAT EXISTENTE') let msg_array = res.items[0].messageHistory await msg_array.push({ role: 'user', 'content': event.payload.text/*+ 'me responda estritamente no contexto: Marketing Juridico, atender mas não passar ideia de valores'*/ }) //Chat existente ask_gpt(msg_array, event.sender.id) .then(async (resGpt) => { let gpt_res = JSON.parse(resGpt) console.log(gpt_res) let ai_reply = gpt_res.choices[0].message.content.replace(/\n /g, ''); await msg_array.push({ role: 'assistant', 'content': ai_reply }) gpt_response(ai_reply, event.channelId, msg_array) }) } else { console.log('NOVO CHAT') //Novo chat let msg_array = [ { role: 'system', 'content': 'Você está falando com o Thiago, (IA) inteligência artificial da Thuim Jurídica' }, { role: 'user', 'content': event.payload.text /*+'me responda est ritamente no contexto: atendimento básico juridico não falar sobre valoresl'*/}, ] let obj = { channelId: event.channelId, userId: event.sender.id, messageHistory: msg_array, } wixData.insert('gpt_chat', obj) ask_gpt(msg_array, event.sender.id) .then(async (resGpt2) => { let gpt_res2 = JSON.parse(resGpt2) console.log(gpt_res2) let ai_reply = gpt_res2.choices[0].message.content.replace(/\ n/g, ''); await msg_array.push({ role: 'assistant', 'content': ai_reply }) gpt_response(ai_reply, event.channelId, msg_array) }) } }) } async function gpt_response (gpt, chid, array){ let obj = { "messageText": gpt, "channelId": chid, "metadata": {}, "sendAsVisitor": false, } wixChatBackend.sendMessage(obj) .then(() => { wixData.query('gpt_chat').eq('channelId', chid).find() .then((res) => { let item = res.items[0]; item.messageHistory = array; wixData.update('gpt_chat', item) }) }) .catch((e) => { console.log(e) }) } const axios = require('axios'); //LEMBRAR DE IMPORTAR O PACOTE NPM: AXIOS // ... export async function ask_gpt(array, user) { let data = JSON.stringify({ model: 'gpt-3.5-turbo', messages: array, max_tokens: 2500, n: 1, temperature: 0.2, user: user, }); let config = { method: 'post', maxBodyLength: Infinity, url: 'https://api.openai.com/v1/chat/completions', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + 'sk-a_FYuPA_pK3dg88pNRdV06YS6tlREuQAKnCkMdncERT3BlbkFJ_nJD8tUW9_h8T86e48C_xAfDEp7bQ1BFYw3xuaK5kA’ }, data: data }; return await axios.request(config) .then(async(response) => { console.log(response.data) console.log(typeof response) return await JSON.stringify(response.data) }) .catch((error) => { console.log(error); }); }
top of page

GRAZIE MILLE

per visitare

il nostro sito web.

CONTATTO

Grazie per aver visitato il nostro sito. Per programmare un incontro introduttivo con uno dei nostri esperti, contattaci oggi stesso.

Rua Venceslau Brás, 16 Conj. 104 - Sé - San Paolo - SP

Dott.ssa Suselei Perri Gonzales (Coordinatore)

Rubrica fuori rete/SP 423.323

thuimjuridica@gmail.com

(11) 91226-3958

  • Facebook ícone social
  • Instagram
bottom of page