For Zoho services only


I'm currently part of a wider delivery team at Ascent Business Solutions, recognised as a leading Zoho Premium Solutions Partner in the United Kingdom.

Ascent Business Solutions support organisations with everything from targeted technical fixes through to full Zoho CRM implementations and long-term platform adoption. Working as a team rather than a one-person consultancy allows projects to move forward consistently, with access to the right skills at each stage.

The team I manage specialises in API integrations between Zoho and third-party finance and commerce platforms such as Xero, Shopify, WooCommerce, and eBay. Much of our work involves solving integration challenges that fall outside standard documentation, supporting new ideas, new sectors, and evolving business models.

Success is measured through practical outcomes and return on investment, ranging from scaling small operations into high-turnover businesses to delivering rapid gains through online payments, automation, and streamlined digital workflows.

If you are looking for structured Zoho expertise backed by an established consultancy, you can contact Ascent Business Solutions on 0121 392 8140 (UK), email info@ascentbusiness.co.uk, or visit https://www.ascentbusiness.co.uk.

Zoho Cliq: Integrate OpenAI ChatGPT with Conversation Threads

What?
A quick follow-on article to my previous article on Zoho Cliq: Integrate OpenAI and ChatGPT 3.5 Turbo.

Why?
Connecting to OpenAI ChatGPT is all very well and good, but this forgets what you were talking about after each chat. So it's good for 1 question and 1 answer. What we need is to train it a little a bit like the standard OpenAI Chat website does in terms of context.

How?
Just a few changes to the previous code of connecting to OpenAI ChatGPT but the exception here is that there needs to be code to loop through all the messages of the chat/conversation thread so that we send the OpenAI ChatGPT bot the full conversation thread.


First getting the messages relating to this chat:
You will need a connection that has the necessary scope(s). Note that you have to do this as the super admin to get organization level messages. But in ths example below, we're going to query a message to the ChatGPT bot:

copyraw
// 
// initialize
m_Response = Map();
v_Message = message.trim();
v_ChatID = ifnull(chat.getJSON("id"),0);
//
// get conversations from last 10 minutes
v_MinutesAgo = 10;
v_FromTime = zoho.currenttime.subMinutes(v_MinutesAgo).toLong();
v_TillTime = zoho.currenttime.toLong() / 1000;
//
// by default gets last 100 messages (doesn't return anything if no parameters specified)
v_Endpoint = "https://cliq.zoho.eu/api/v2/chats/"+v_ChatID + "/messages?fromtime=" + v_FromTime + "&limit=50";
r_ChatDetails = invokeurl
[
	url: v_Endpoint
	type: GET
	connection: "ab_cliq"
];
//
// let's build up the conversation thread
l_Messages = List();
if(!isnull(r_ChatDetails.get("data")))
{
	for each r_MessageData in r_ChatDetails.get("data")
    {
		v_Role = "user";
		if(!isnull(r_MessageData.get("message_source")))
		{
			v_UserType = r_MessageData.get("message_source").get("type");
			v_Role = if(v_UserType=="bot", "assistant", "user");
		}
		if(!isnull(r_MessageData.get("content")))
		{
			v_Content = r_MessageData.get("content").get("text");
		}
		//
		m_Message = Map();
		m_Message.put("role",v_Role);
		m_Message.put("content",v_Content);
		l_Messages.add(m_Message);
    }
}
//
// if the message contains a question mark, send it to OpenAI
if(!isBlank(v_Message))
{
	v_Question = v_Message;
	//
	// Need to add openAI token
	v_Token = "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";
	//
	// build header of request
	m_Header = Map();
	m_Header.put("Authorization","Bearer " + v_Token);
	m_Header.put("Content-Type","application/json");
	//
	// build params of request
	m_Params = Map();
	m_Params.put("model","gpt-3.5-turbo");
	//
	// add latest message to conversation thread
	m_Message = Map();
	m_Message.put("role","user");
	m_Message.put("content",v_Question);
	l_Messages.add(m_Message);
	m_Params.put("messages",l_Messages);
	//
	// https://platform.openai.com/docs/api-reference/chat/create
	m_Params.put("temperature",0.8);
	m_Params.put("n",1);
	m_Params.put("max_tokens",256);
	m_Params.put("presence_penalty",0);
	m_Params.put("frequency_penalty",0);
	// 
	// send request to chatgpt openai
	r_ChatGPTResponse = invokeurl
	[
		url :"https://api.openai.com/v1/chat/completions"
		type :POST
		parameters:m_Params.toString()
		headers:m_Header
		detailed:true
	];
	if(r_ChatGPTResponse.get("responseCode") == 200)
	{
		//
		// retrieve the answer in text
		l_Choices = r_ChatGPTResponse.get("responseText").get("choices");
		for each  m_Choice in l_Choices
		{
			if(!isnull(m_Choice.get("message")))
			{
				if(m_Choice.get("message").get("role") == "assistant")
				{
					//
					// add the answer text to the response map
					m_Response.put("text",m_Choice.get("message").get("content"));
					//break;
				}
			}
		}
	}
	else if(r_ChatGPTResponse.get("responseCode") == 429)
	{
		// store in response text
		m_Response = {"text":"I dont have any knowledge on this. Consider doing a Google search."};
	}
}
return m_Response;
  1.  // 
  2.  // initialize 
  3.  m_Response = Map()
  4.  v_Message = message.trim()
  5.  v_ChatID = ifnull(chat.getJSON("id"),0)
  6.  // 
  7.  // get conversations from last 10 minutes 
  8.  v_MinutesAgo = 10
  9.  v_FromTime = zoho.currenttime.subMinutes(v_MinutesAgo).toLong()
  10.  v_TillTime = zoho.currenttime.toLong() / 1000
  11.  // 
  12.  // by default gets last 100 messages (doesn't return anything if no parameters specified) 
  13.  v_Endpoint = "https://cliq.zoho.eu/api/v2/chats/"+v_ChatID + "/messages?fromtime=" + v_FromTime + "&limit=50"
  14.  r_ChatDetails = invokeurl 
  15.  [ 
  16.      url: v_Endpoint 
  17.      type: GET 
  18.      connection: "ab_cliq" 
  19.  ]
  20.  // 
  21.  // let's build up the conversation thread 
  22.  l_Messages = List()
  23.  if(!isnull(r_ChatDetails.get("data"))) 
  24.  { 
  25.      for each r_MessageData in r_ChatDetails.get("data") 
  26.      { 
  27.          v_Role = "user"
  28.          if(!isnull(r_MessageData.get("message_source"))) 
  29.          { 
  30.              v_UserType = r_MessageData.get("message_source").get("type")
  31.              v_Role = if(v_UserType=="bot", "assistant", "user")
  32.          } 
  33.          if(!isnull(r_MessageData.get("content"))) 
  34.          { 
  35.              v_Content = r_MessageData.get("content").get("text")
  36.          } 
  37.          // 
  38.          m_Message = Map()
  39.          m_Message.put("role",v_Role)
  40.          m_Message.put("content",v_Content)
  41.          l_Messages.add(m_Message)
  42.      } 
  43.  } 
  44.  // 
  45.  // if the message contains a question mark, send it to OpenAI 
  46.  if(!isBlank(v_Message)) 
  47.  { 
  48.      v_Question = v_Message; 
  49.      // 
  50.      // Need to add openAI token 
  51.      v_Token = "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"
  52.      // 
  53.      // build header of request 
  54.      m_Header = Map()
  55.      m_Header.put("Authorization","Bearer " + v_Token)
  56.      m_Header.put("Content-Type","application/json")
  57.      // 
  58.      // build params of request 
  59.      m_Params = Map()
  60.      m_Params.put("model","gpt-3.5-turbo")
  61.      // 
  62.      // add latest message to conversation thread 
  63.      m_Message = Map()
  64.      m_Message.put("role","user")
  65.      m_Message.put("content",v_Question)
  66.      l_Messages.add(m_Message)
  67.      m_Params.put("messages",l_Messages)
  68.      // 
  69.      // https://platform.openai.com/docs/api-reference/chat/create 
  70.      m_Params.put("temperature",0.8)
  71.      m_Params.put("n",1)
  72.      m_Params.put("max_tokens",256)
  73.      m_Params.put("presence_penalty",0)
  74.      m_Params.put("frequency_penalty",0)
  75.      // 
  76.      // send request to chatgpt openai 
  77.      r_ChatGPTResponse = invokeurl 
  78.      [ 
  79.          url :"https://api.openai.com/v1/chat/completions" 
  80.          type :POST 
  81.          parameters:m_Params.toString() 
  82.          headers:m_Header 
  83.          detailed:true 
  84.      ]
  85.      if(r_ChatGPTResponse.get("responseCode") == 200) 
  86.      { 
  87.          // 
  88.          // retrieve the answer in text 
  89.          l_Choices = r_ChatGPTResponse.get("responseText").get("choices")
  90.          for each  m_Choice in l_Choices 
  91.          { 
  92.              if(!isnull(m_Choice.get("message"))) 
  93.              { 
  94.                  if(m_Choice.get("message").get("role") == "assistant") 
  95.                  { 
  96.                      // 
  97.                      // add the answer text to the response map 
  98.                      m_Response.put("text",m_Choice.get("message").get("content"))
  99.                      //break; 
  100.                  } 
  101.              } 
  102.          } 
  103.      } 
  104.      else if(r_ChatGPTResponse.get("responseCode") == 429) 
  105.      { 
  106.          // store in response text 
  107.          m_Response = {"text":"I dont have any knowledge on this. Consider doing a Google search."}
  108.      } 
  109.  } 
  110.  return m_Response; 
Category: Zoho Other :: Article: 64

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com