Best answer

How can I remove ChatGPT-4’s sources from the text?

  • 4 December 2023
  • 16 replies
  • 1199 views

Userlevel 1

Hi there. I have a customer support gpt assistant integration, how can I remove gpt-4’s sources from the text, can’t get it to work in formatter, they apper as: 【1†source】,【2†source】,【3†source】...【15†source】need a formatter replace with blank (text), how to do it? Thank you.

icon

Best answer by Troy Tessalone 4 December 2023, 18:21

View original

16 replies

Userlevel 1

If there is nothing better, I’d have to say in one go to search for 【1†source】OR【2†source】OR … 【20†source】 and remove any of those with blank… Or is it? 

Userlevel 7
Badge +14

Hi @looka 

Good question.

You may have to try using a Code step: https://zapier.com/apps/code/help

Userlevel 1

I believe this will be one of the most requested features..., could someone hack up additional option for that in text formatter? If not, I’ll give it a try. Thank you...

Userlevel 7
Badge +6

Hi there @looka,

I’ve been doing some tests on my end here, and I can’t remove the【1†source】data using Formatter. Maybe try adding an additional prompt to your “Message” field in the ChatGPT step will work here.

Could you please try adding “Don’t include source citation” in the “Message” field?

Please let me know if that works. Thanks!

Userlevel 1

I’ve tried that first in several ways, chatgpt-4 also says it’s unoverridable with prompt, default part of the response (to improve accuracy and justify replies).


Seems like it has to be done in formatter or similar. I guess many people will hit that wall, probably best formatter gets expanded with this option by Zapier. The easiest way would be to support OR syntax in formatter - text - replace: to kick them out like that (【1†source】OR【2†source】OR … 【20†source】), coz’ regex doesn’t work for this sources syntax in formatter either (I guess this is on purpose by openAI).

However what currently works for me is formatter - text - replace 【1†source】with blank. So if one is willing to use 50 formatter blocks for each number of source (depends on length of knowledgebase, number of sources), could be done ;)

 

Userlevel 7
Badge +14

Hi @looka 

Did you try asking ChatGPT for the custom Code (javascript) to do the replace using regex?

 

Userlevel 1

Nope, not yet. Somewhat out of my skillset, probably doable, would have to put in time. I will if you guys can’t fix it... If you could give it a try, probably break it up first, remove by chars / parts with regex, wrap in formatter text - replace option.

Userlevel 7
Badge +14

@looka 

I wouldn’t wait on Zapier to implement a fix for this specific request.

 

Code to try (may need adjustments)

// Sample text with bracketed sources
const text = inputData.text;

// Replace all bracketed sources with nothing
const newText = text.replace(/.\d+†source./g, '');

output = [{text, newText}];

 

Userlevel 1

Slightly different, but it works, yeah!

Userlevel 7
Badge +6

That’s awesome news @looka! Big thanks Troy for providing the code here!

If you have any other questions, please don’t hesitate to reach out to the Community. We’re always happy to help! 🤗

Userlevel 1

This is now in place to also handle special case of when URL from gpt’s sources is provided, to clean up everything, except the url.

// Extract the 'text' property from the inputData object const text = inputData.text; // Define a regular expression to match the pattern of square brackets with or without a hyperlink // The regular expression is divided into three parts: // 1. \[ - This matches the opening square bracket // 2. (?:\d+†)? - This is a non-capturing group that matches one or more digits followed by †. The group is optional. // 3. [^\[\]]+ - This matches one or more characters that are not square brackets // 4. \] - This matches the closing square bracket const regex = /\[(?:\d+†)?[^\[\]]+\]/g; // Replace all occurrences of the matched pattern with a function const result = text.replace(regex, (match) => { // If the match includes a hyperlink (indicated by †), return only the hyperlink if (match.includes("†")) { // Extract the hyperlink by splitting the match at † and taking the second part const link = match.split("†")[1]; // Return the hyperlink wrapped in square brackets return "[" + link + "]"; } else { // If the match does not include a hyperlink, return an empty string return ""; } }); // Set the output to be an array containing an object with the processed text output = [{ text: result }];

Userlevel 1

Currently followed by another code block, rechecking if all instances were fixed, had to do it due to various formats those source citations could be delivered by gpt assistant depending on hyperlink presence in the previos step (above), couldn’t get it to work for all cases in single js block for some reason, probably could be done though (object vs array issue). Will give it another try soon, meanwhile the combo of the one above and this one below works for all variations:

// Extract the 'text' property from the inputData objectconst text = inputData.text;// Use a regular expression to match any text within brackets, including the brackets themselves// The 'g' flag at the end of the regular expression is used to perform a global search, finding all matches rather than stopping after the first match// The 'm' flag is not necessary in this case, as it is used for multi-line matching, but it doesn't affect the resultconst regex = /\【.*?\】/g;// Use the replace method of the text string to replace all matches of the regular expression with an empty string// This effectively deletes all brackets and their contents from the textconst result = text.replace(regex, "");// Store the result in an object with the key 'output' and assign it to the variable 'output'output = { output: result };

Slightly different, but it works, yeah!

Hi Looka, thank you for providing the code. Can you please explain where I would add this code? Somewhere in Zapier or OpenAi. Thank you for your help.

That’s awesome news @looka! Big thanks Troy for providing the code here!

If you have any other questions, please don’t hesitate to reach out to the Community. We’re always happy to help! 🤗

Hi Ken, where do we add the code? In Zapier or OpenAi?

That’s awesome news @looka! Big thanks Troy for providing the code here!

If you have any other questions, please don’t hesitate to reach out to the Community. We’re always happy to help! 🤗

Hi Ken, where do we add the code? In Zapier or OpenAi?

That’s awesome news @looka! Big thanks Troy for providing the code here!

If you have any other questions, please don’t hesitate to reach out to the Community. We’re always happy to help! 🤗

Hi Ken, can you please help me to get this working?

After the step ‘Conversation with Assistant in ChatGPT’ i added a new step ‘Run Javascript in Code by Zapier’

I added the code, although not sure what to write in the ‘Input Data’ above? I tried to add the ‘ChatGPT Full Response’ there although it is not working.

Any help would be much appreciated. Thank you

Userlevel 7
Badge +11

Hi @Green Square! 👋

Not sure if you’ve already managed to solve this but thought I’d pop by to offer some help just in case you’re still stuck.

You’ve added the code action into the correct place in the Zap so you’re nearly there! For the Input Data section you’d want to set the name of the input field to be text (as that’s the input field name what was referenced in the previous code examples shared in this thread). Then, select the Full Response field from your ChatGPT action. For example:

a3e9f7e8fea51a79719eea49dde1fccc.png

Hope that helps. If you run into any further trouble on this or you managed to find an alternative solution please let us know, would love to hear from you! 

Reply