For Zoho Services only:


I'm actually part of something bigger at Ascent Business Solutions recognized as the top Zoho Premium Solutions Partner in the United Kingdom.

Ascent Business Solutions offer support for smaller technical fixes and projects for larger developments, such as migrating to a ZohoCRM.  A team rather than a one-man-band is always available to ensure seamless progress and address any concerns. You'll find our competitive support rates with flexible, no-expiration bundles at https://ascentbusiness.co.uk/zoho-services/uk-zoho-support.  For larger projects, talk to our experts and receive dedicated support from our hands-on project consultants at https://ascentbusiness.co.uk/zoho-services/zoho-crm-implementation.

The team I manage specializes in coding API integrations between Zoho and third-party finance/commerce suites such as Xero, Shopify, WooCommerce, and eBay; to name but a few.  Our passion lies in creating innovative solutions where others have fallen short as well as working with new businesses, new sectors, and new ideas.  Our success is measured by the growth and ROI we deliver for clients, such as transforming a garden shed hobby into a 250k monthly turnover operation or generating a +60% return in just three days after launch through online payments and a streamlined e-commerce solution, replacing a paper-based system.

If you're looking for a partner who can help you drive growth and success, we'd love to work with you.  You can reach out to us on 0121 392 8140 (UK) or info@ascentbusiness.co.uk.  You can also visit our website at https://ascentbusiness.co.uk.

ZohoCRM: Integrate ChatGPT to ZohoZIA

What?
Following on from the article: Zoho Cliq: Integrate OpenAI and ChatGPT 3.5 Turbo. This is the next step as we get it to look at data within ZohoCRM.

Why?
Because it is a serious improvement upon ZohoZIA and we get a lot of requests around customizing ZohoZIA...

How?
So taking ideas from my previous article for ZohoCliq; let's use what we know to develop a ZohoZIA for a demo system.

There are several steps, so first we will set up the ZIA action "Ask ChatGPT" using the GUI, then we will capture the question using Deluge code and query the OpenAI ChatGPT API to return a response.

Getting to the development-side
  1. Login to ZohoCRM as a super-admin
  2. Go to Setup (cog icon in the top right)
  3. Under Zia, select "Conversational AI"
  4. Click on the link "Zia Developer Console" (yours will have the specific OrgID in the URL).
  5. Click on "Add Action"
    1. Give it the Action Name "Ask ChatGPT"
    2. This action should "Answer a Question"
    3. It should be answered by "Construct Answer by Fetching Data"
    4. The question can be asked by clicking the button "Ask ChatGPT"
    ZohoCRM: Integrate ChatGPT to ZohoZIA: Developer GUI setup
  6. Add the parameter:
    1. I'm calling it "banter"
    2. of type "String"
    3. with a prompt message of "What is your question?"
    4. click on "Save"
    ZohoCRM: Integrate ChatGPT to ZohoZIA: Parameter Setup
  7. click on "Done"
  8. Now click on "Edit Function" under "Context Handler Function"
    ZohoCRM: Integrate ChatGPT to ZohoZIA: Edit Execution Function
  9. We're going to use the following deluge code for our first pass at this awesome functionality:
    copyraw
    // 
    // initialize
    v_Response = previousParam;
    m_Response = Map();
    m_Response.put("todo","prompt");
    l_Messages = List();
    //
    // capture latest message from user
    v_Message = ifnull(userInput,"");
    m_Thread = Map();
    m_Thread.put("role","user");
    m_Thread.put("content",v_Message);
    l_Messages.add(m_Thread);
    //
    // if the message is not blank nor the trigger, send to OpenAI ChatGPT API
    if(!isBlank(v_Message) && v_Message!="Ask ChatGPT")
    {
    	//
    	// Need to add your own OpenAI token here
    	v_Token = "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    	//
    	// 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");
    	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
    					v_Response = m_Choice.get("message").get("content");
    				}
    			}
    		}
    	}
    	else
    	{
    		// store in response text
    		v_Response = "I don't know. Would you like me to Google: [" + v_Message + "](https://www.google.com/search?q=" + zoho.encryption.urlEncode(v_Message) + ")?";
    	}
    }
    //
    // build up response to user
    m_Prompt = Map();
    m_Prompt.put("param_name","banter");
    m_Prompt.put("prompt_msg",v_Response);
    m_Response.put("prompt",m_Prompt);
    //
    // output
    return m_Response;
    1.  // 
    2.  // initialize 
    3.  v_Response = previousParam; 
    4.  m_Response = Map()
    5.  m_Response.put("todo","prompt")
    6.  l_Messages = List()
    7.  // 
    8.  // capture latest message from user 
    9.  v_Message = ifnull(userInput,"")
    10.  m_Thread = Map()
    11.  m_Thread.put("role","user")
    12.  m_Thread.put("content",v_Message)
    13.  l_Messages.add(m_Thread)
    14.  // 
    15.  // if the message is not blank nor the trigger, send to OpenAI ChatGPT API 
    16.  if(!isBlank(v_Message) && v_Message!="Ask ChatGPT") 
    17.  { 
    18.      // 
    19.      // Need to add your own OpenAI token here 
    20.      v_Token = "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    21.      // 
    22.      // build header of request 
    23.      m_Header = Map()
    24.      m_Header.put("Authorization","Bearer " + v_Token)
    25.      m_Header.put("Content-Type","application/json")
    26.      // 
    27.      // build params of request 
    28.      m_Params = Map()
    29.      m_Params.put("model","gpt-3.5-turbo")
    30.      m_Params.put("messages",l_Messages)
    31.      // 
    32.      // https://platform.openai.com/docs/api-reference/chat/create 
    33.      m_Params.put("temperature",0.8)
    34.      m_Params.put("n",1)
    35.      m_Params.put("max_tokens",256)
    36.      m_Params.put("presence_penalty",0)
    37.      m_Params.put("frequency_penalty",0)
    38.      // 
    39.      // send request to chatgpt openai 
    40.      r_ChatGPTResponse = invokeUrl 
    41.      [ 
    42.          url :"https://api.openai.com/v1/chat/completions" 
    43.          type :POST 
    44.          parameters:m_Params.toString() 
    45.          headers:m_Header 
    46.          detailed:true 
    47.      ]
    48.      if(r_ChatGPTResponse.get("responseCode") == 200) 
    49.      { 
    50.          // 
    51.          // retrieve the answer in text 
    52.          l_Choices = r_ChatGPTResponse.get("responseText").get("choices")
    53.          for each  m_Choice in l_Choices 
    54.          { 
    55.              if(!isnull(m_Choice.get("message"))) 
    56.              { 
    57.                  if(m_Choice.get("message").get("role") == "assistant") 
    58.                  { 
    59.                      // 
    60.                      // add the answer text to the response map 
    61.                      v_Response = m_Choice.get("message").get("content")
    62.                  } 
    63.              } 
    64.          } 
    65.      } 
    66.      else 
    67.      { 
    68.          // store in response text 
    69.          v_Response = "I don't know. Would you like me to Google: [" + v_Message + "](https://www.google.com/search?q=" + zoho.encryption.urlEncode(v_Message) + ")?"
    70.      } 
    71.  } 
    72.  // 
    73.  // build up response to user 
    74.  m_Prompt = Map()
    75.  m_Prompt.put("param_name","banter")
    76.  m_Prompt.put("prompt_msg",v_Response)
    77.  m_Response.put("prompt",m_Prompt)
    78.  // 
    79.  // output 
    80.  return m_Response; 
  10. click on "Save Script"
  11. Test using the sidebar on the right
  12. Click on "Done" when satisfied.
  13. Click on the left arrow/caret to return to the ZohoCRM ZIA main page
  14. Click on "Deploy to Production"
  15. Select what actions to deploy (the "Ask ChatGPT") and then click on "Deploy"
    ZohoCRM: Integrate ChatGPT to ZohoZIA: Deploy to Production

All done? Now Test!
  1. Login/Reload ZohoCRM
  2. Click on "Ask Zia"
  3. Type the "Ask ChatGPT" and it should popup with ...things you can ask me... with one being "Ask ChatGPT"
  4. Click on it and then ask away:...
    ZohoCRM: Integrate ChatGPT to ZohoZIA: Deploy to Production

Keeping a Conversation
So annoyingly, the variable broadcast was unreliable in storing message conversations despite it's purpose being ideal. Instead, let's use a CRM function to store the conversation between ChatGPT and the CRM User. This way, we can also ensure that the data shared can be controlled/monitored.
  1. Create a CRM Module called "GPT Chats" (singular: GPT Chat)
  2. Modify the name field to be an Auto-Number (I'm prefixing mine with CHAT-
  3. Add a pick list called "Role" with the options: "user", "assistant", "system".
  4. Add a file upload field called "Big Data"
  5. Add a single line field called "Title" (used as a multi-line cannot be used in the listview).
  6. Add a multi-line field called "Content" and change the properties on creation to allow 32000 characters.
  7. Keep the email field called "Email"
Return to the Context Handler Function IDE (page where you enter the deluge code):
  1. Click on "Manage Connections"
    1. Click on Create Connection
    2. Select Service "Zoho OAuth"
    3. Give it a name, I'm calling mine "ChatGPT CRM"
    4. Untick "Use Credentials of Login User"
    5. Under Scopes, click on the Search icon and type "CRM": I selected "ZohoCRM.coql.READ, ZohoCRM.modules.ALL, ZohoCRM.org.READ, ZohoCRM.settings.READ, and ZohoCRM.users.READ"
    6. Click on "Create and Connect" > Connect
    7. Choose the environment (eg. Production, Sandbox, or Developer) > Submit
    8. Click on "Accept" to authorize the connection
  2. Give it the following code:
    copyraw
    /* *******************************************************************************
    Function:       Ask ChatGPT - Context Handler Function
    Trigger:        Function executed in ZIA chat when "Ask ChatGPT" button is clicked
    Purpose:		This will query the OpenAI ChatGPT API but at the same time retain the context of ZohoCRM
    Inputs:         string userInput
    Outputs:        map m_Response
    
    Date Created:   2023-02-08 (Joel Lipman)
                    - Initial release
                    - Queries OpenAI ChatGPT
    Date Modified:	2023-03-11 (Joel Lipman)
                    - Stores conversation messages / thread in CRM records
                    - Retrieves and builds up conversation trail to send to OpenAI ChatGPT API
    ******************************************************************************* */
    // 
    // initialize
    m_Blank = Map();
    v_Response = previousParam;
    m_Response = Map();
    m_Response.put("todo","prompt");
    //
    // determine name of logged in user (and make owner?)
    v_LoggedInUserID = 0;
    l_Users= List();
    m_UserType = Map();
    m_UserType.put("email",zoho.loginuserid);
    r_Response = zoho.crm.invokeConnector("crm.getusers",m_UserType);
    if(!isnull(r_Response.get("status_code")))
    {
    	if(r_Response.get("status_code")==200)
    	{
    		l_Users = r_Response.get("response").get("users");
    		for each  v_User in l_Users
    		{
    			v_LoggedInUserID = v_User.get("id");
    		}
    	}
    }
    //
    // start building conversation trail
    l_ConversationMessages = List();
    //
    // build up COQL query
    v_CoqlQuery = "select Content, Role from GPT_Chats where Owner='" + v_LoggedInUserID + "' order by Created_Time asc";
    //
    // build up parameters
    m_Params = Map();
    m_Params.put("select_query",v_CoqlQuery);
    //
    // invokeurl (note the Zoho endpoint here will depend on your datacenter; .com / .eu)
    r_Coql = invokeurl
    [
        url :"https://www.zohoapis.eu/crm/v2/coql"
        type :POST
        parameters:m_Params.toString()
        connection:"chatgptcrm"
    ];
    if(!isnull(r_Coql.get("data")))
    {
    	for each m_Message in r_Coql.get("data")
        {
    		m_Chat = Map();
    		m_Chat.put("role", m_Message.get("Role"));
    		m_Chat.put("content", m_Message.get("Content"));
    		l_ConversationMessages.add(m_Chat);
        }
    }
    //
    // storing latest user input
    v_UserMessage = ifnull(userInput,"");
    if(!isBlank(v_UserMessage) && !v_UserMessage.equalsIgnoreCase("Ask ChatGPT"))
    {
    	//
    	// store user input to a CRM record
    	m_Create = Map();
    	v_Title = if(v_UserMessage.length()>40, v_UserMessage.subString(0,40) + "...", v_UserMessage);
    	m_Create.put("Title", v_Title);
    	m_Create.put("Content", v_UserMessage.trim());
    	m_Create.put("Role", "user");
    	m_Create.put("Email", zoho.loginuserid);
    	m_Create.put("Owner", v_LoggedInUserID);
    	r_Create = zoho.crm.createRecord("GPT_Chats", m_Create);
    	//
    	// add the last question to the messages
    	m_Chat = Map();
    	m_Chat.put("role", "user");
    	m_Chat.put("content", v_UserMessage);
    	l_ConversationMessages.add(m_Chat);
    	//
    	// ********************************************************************
    	// ok let's query OpenAI ChatGPT
    	//
    	// Need to add your own OpenAI token here
    	v_Token = "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    	//
    	// 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");
    	m_Params.put("messages",l_ConversationMessages);
    	//
    	// 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
    	];
    	v_Response = l_ConversationMessages;
    	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
    					v_Response = m_Choice.get("message").get("content");
    				}
    			}
    		}
    	}
    	else
    	{
    		// store in response text
    		v_Response = "I don't know. Would you like me to Google: [" + v_UserMessage + "](https://www.google.com/search?q=" + zoho.encryption.urlEncode(v_UserMessage) + ")?";
    	}
    }
    //
    // capture answer from bot
    if(!isNull(v_Response) && !v_Response.equalsIgnoreCase("What is your question?"))
    {
    	m_Create = Map();
    	v_Title = if(v_Response.length()>40, v_Response.subString(0,40) + "...", v_Response);
    	m_Create.put("Title", v_Title);
    	m_Create.put("Content", v_Response);
    	m_Create.put("Role", "assistant");
    	m_Create.put("Email", zoho.loginuserid);
    	m_Create.put("Owner", v_LoggedInUserID);
    	r_Create = zoho.crm.createRecord("GPT_Chats", m_Create);
    }
    //
    // build up output to ZIA chat interface
    m_Prompt = Map();
    m_Prompt.put("param_name","banter");
    m_Prompt.put("prompt_msg",v_Response);
    m_Response.put("prompt",m_Prompt);
    //
    // output
    return m_Response;
    1.  /* ******************************************************************************* 
    2.  Function:       Ask ChatGPT - Context Handler Function 
    3.  Trigger:        Function executed in ZIA chat when "Ask ChatGPT" button is clicked 
    4.  Purpose:        This will query the OpenAI ChatGPT API but at the same time retain the context of ZohoCRM 
    5.  Inputs:         string userInput 
    6.  Outputs:        map m_Response 
    7.   
    8.  Date Created:   2023-02-08 (Joel Lipman) 
    9.                  - Initial release 
    10.                  - Queries OpenAI ChatGPT 
    11.  Date Modified:    2023-03-11 (Joel Lipman) 
    12.                  - Stores conversation messages / thread in CRM records 
    13.                  - Retrieves and builds up conversation trail to send to OpenAI ChatGPT API 
    14.  ******************************************************************************* */ 
    15.  // 
    16.  // initialize 
    17.  m_Blank = Map()
    18.  v_Response = previousParam; 
    19.  m_Response = Map()
    20.  m_Response.put("todo","prompt")
    21.  // 
    22.  // determine name of logged in user (and make owner?) 
    23.  v_LoggedInUserID = 0
    24.  l_Users= List()
    25.  m_UserType = Map()
    26.  m_UserType.put("email",zoho.loginuserid)
    27.  r_Response = zoho.crm.invokeConnector("crm.getusers",m_UserType)
    28.  if(!isnull(r_Response.get("status_code"))) 
    29.  { 
    30.      if(r_Response.get("status_code")==200) 
    31.      { 
    32.          l_Users = r_Response.get("response").get("users")
    33.          for each  v_User in l_Users 
    34.          { 
    35.              v_LoggedInUserID = v_User.get("id")
    36.          } 
    37.      } 
    38.  } 
    39.  // 
    40.  // start building conversation trail 
    41.  l_ConversationMessages = List()
    42.  // 
    43.  // build up COQL query 
    44.  v_CoqlQuery = "select Content, Role from GPT_Chats where Owner='" + v_LoggedInUserID + "' order by Created_Time asc"
    45.  // 
    46.  // build up parameters 
    47.  m_Params = Map()
    48.  m_Params.put("select_query",v_CoqlQuery)
    49.  // 
    50.  // invokeUrl (note the Zoho endpoint here will depend on your datacenter; .com / .eu) 
    51.  r_Coql = invokeUrl 
    52.  [ 
    53.      url :"https://www.zohoapis.eu/crm/v2/coql" 
    54.      type :POST 
    55.      parameters:m_Params.toString() 
    56.      connection:"chatgptcrm" 
    57.  ]
    58.  if(!isnull(r_Coql.get("data"))) 
    59.  { 
    60.      for each m_Message in r_Coql.get("data") 
    61.      { 
    62.          m_Chat = Map()
    63.          m_Chat.put("role", m_Message.get("Role"))
    64.          m_Chat.put("content", m_Message.get("Content"))
    65.          l_ConversationMessages.add(m_Chat)
    66.      } 
    67.  } 
    68.  // 
    69.  // storing latest user input 
    70.  v_UserMessage = ifnull(userInput,"")
    71.  if(!isBlank(v_UserMessage) && !v_UserMessage.equalsIgnoreCase("Ask ChatGPT")) 
    72.  { 
    73.      // 
    74.      // store user input to a CRM record 
    75.      m_Create = Map()
    76.      v_Title = if(v_UserMessage.length()>40, v_UserMessage.subString(0,40) + "...", v_UserMessage)
    77.      m_Create.put("Title", v_Title)
    78.      m_Create.put("Content", v_UserMessage.trim())
    79.      m_Create.put("Role", "user")
    80.      m_Create.put("Email", zoho.loginuserid)
    81.      m_Create.put("Owner", v_LoggedInUserID)
    82.      r_Create = zoho.crm.createRecord("GPT_Chats", m_Create)
    83.      // 
    84.      // add the last question to the messages 
    85.      m_Chat = Map()
    86.      m_Chat.put("role", "user")
    87.      m_Chat.put("content", v_UserMessage)
    88.      l_ConversationMessages.add(m_Chat)
    89.      // 
    90.      // ******************************************************************** 
    91.      // ok let's query OpenAI ChatGPT 
    92.      // 
    93.      // Need to add your own OpenAI token here 
    94.      v_Token = "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    95.      // 
    96.      // build header of request 
    97.      m_Header = Map()
    98.      m_Header.put("Authorization","Bearer " + v_Token)
    99.      m_Header.put("Content-Type","application/json")
    100.      // 
    101.      // build params of request 
    102.      m_Params = Map()
    103.      m_Params.put("model","gpt-3.5-turbo")
    104.      m_Params.put("messages",l_ConversationMessages)
    105.      // 
    106.      // https://platform.openai.com/docs/api-reference/chat/create 
    107.      m_Params.put("temperature",0.8)
    108.      m_Params.put("n",1)
    109.      m_Params.put("max_tokens",256)
    110.      m_Params.put("presence_penalty",0)
    111.      m_Params.put("frequency_penalty",0)
    112.      // 
    113.      // send request to chatgpt openai 
    114.      r_ChatGPTResponse = invokeUrl 
    115.      [ 
    116.          url :"https://api.openai.com/v1/chat/completions" 
    117.          type :POST 
    118.          parameters:m_Params.toString() 
    119.          headers:m_Header 
    120.          detailed:true 
    121.      ]
    122.      v_Response = l_ConversationMessages; 
    123.      if(r_ChatGPTResponse.get("responseCode") == 200) 
    124.      { 
    125.          // 
    126.          // retrieve the answer in text 
    127.          l_Choices = r_ChatGPTResponse.get("responseText").get("choices")
    128.          for each  m_Choice in l_Choices 
    129.          { 
    130.              if(!isnull(m_Choice.get("message"))) 
    131.              { 
    132.                  if(m_Choice.get("message").get("role") == "assistant") 
    133.                  { 
    134.                      // 
    135.                      // add the answer text to the response map 
    136.                      v_Response = m_Choice.get("message").get("content")
    137.                  } 
    138.              } 
    139.          } 
    140.      } 
    141.      else 
    142.      { 
    143.          // store in response text 
    144.          v_Response = "I don't know. Would you like me to Google: [" + v_UserMessage + "](https://www.google.com/search?q=" + zoho.encryption.urlEncode(v_UserMessage) + ")?"
    145.      } 
    146.  } 
    147.  // 
    148.  // capture answer from bot 
    149.  if(!isNull(v_Response) && !v_Response.equalsIgnoreCase("What is your question?")) 
    150.  { 
    151.      m_Create = Map()
    152.      v_Title = if(v_Response.length()>40, v_Response.subString(0,40) + "...", v_Response)
    153.      m_Create.put("Title", v_Title)
    154.      m_Create.put("Content", v_Response)
    155.      m_Create.put("Role", "assistant")
    156.      m_Create.put("Email", zoho.loginuserid)
    157.      m_Create.put("Owner", v_LoggedInUserID)
    158.      r_Create = zoho.crm.createRecord("GPT_Chats", m_Create)
    159.  } 
    160.  // 
    161.  // build up output to ZIA chat interface 
    162.  m_Prompt = Map()
    163.  m_Prompt.put("param_name","banter")
    164.  m_Prompt.put("prompt_msg",v_Response)
    165.  m_Response.put("prompt",m_Prompt)
    166.  // 
    167.  // output 
    168.  return m_Response; 
  3. Click on Save Script and test in the sidebar.
  4. Return to the CRM and click on "Ask ZIA"
  5. Type "Ask" and click on "Ask ChatGPT"
  6. On first use, you may be prompted to authorize the connection between CRM and ZIA:
    1. Click on ChatGPT CRM
    2. Click on Reuse
  7. Test on keeping conversation context: ZohoCRM: Integrate ChatGPT to ZohoZIA: Conversation Context


Still to do:
  • Need to train it on ZohoCRM data with statistics and datasets. Planning Need to ensure data fed into a session is not shared or available to external sources once entered into OpenAI.
    If I train OpenAI on a dataset, can that data be accessed by external sources?

    No, if you train OpenAI on a dataset, that dataset and the resulting model will not be accessible by external sources unless you choose to make it publicly available.

    OpenAI takes data privacy and security very seriously, and they have implemented several measures to protect the data and models that are used by their services. When you train a model with OpenAI's services, your data is encrypted and stored in a secure environment, and access to the data is restricted to authorized personnel only. OpenAI also has strict policies and procedures in place to prevent unauthorized access, disclosure, or modification of your data.

    That being said, it's important to note that the use and distribution of trained models may be subject to legal or ethical considerations, depending on the nature of the data and the intended use of the model. Therefore, if you plan to use or distribute a model trained on a specific dataset, you should carefully review and comply with all applicable laws, regulations, and ethical standards.
    - OpenAI ChatGPT

  • BBC News: ChatGPT bug leaked users' conversation histories 2023-03-23
  • PCMag UK : ChatGPT Users Report Seeing Other People's Conversation Histories   [...report the payment page for ChatGPT Plus possibly exposing email addresses that belong to random users] 2023-03-20
  • Like my previous article, this needs to be fed contextual data to continue a conversation Done 2023-03-11

Update November 2024
Code to get this working with GPT 4o Mini:
copyraw
// requires a paid account: https://platform.openai.com/api-keys
	v_ChatGPT_ServiceAccount_Key = "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
	//
	m_Header = Map();
	m_Header.put("Authorization", "Bearer " + v_ChatGPT_ServiceAccount_Key);
	m_Header.put("Content-Type","application/json");
	//
	m_Params = Map();
	m_Params.put("model","gpt-4o-mini");
	m_Params.put("temperature",0.7);
	l_Messages = List();
	//
	// training messages (a conversation trail with context)
	m_MessageTraining1 = Map();
	m_MessageTraining1.put("role", "user");
	m_MessageTraining1.put("content", "Joel Lipman is a technical consultant working with Ascent Business Solutions, the top Zoho Reseller based in the United Kingdom.");
	l_Messages.add(m_MessageTraining1);
	//
	// this message
	m_ThisMessage = Map();
	m_ThisMessage.put("role", "user");
	m_ThisMessage.put("content", message);
	l_Messages.add(m_ThisMessage);
	m_Params.put("messages", l_Messages);
	//info m_Params;
	//
	r_ChatGPT_Request = invokeurl
    [
    	url: "https://api.openai.com/v1/chat/completions"
    	type: POST
    	parameters: m_Params.toString()
    	headers: m_Header
    ];
	//info r_ChatGPT_Request;
	l_Choices = ifnull(r_ChatGPT_Request.toMap().get("choices"),{});
	for each r_Choice in l_Choices
    {
		m_Choice = r_Choice.toMap();
		if(!isNull(m_Choice.get("message")))
		{
			if(m_Choice.get("message").get("role")=="assistant")
			{
				b_ContactedChatGPT = true;
				my_Text = m_Choice.get("message").get("content") + "\n\n- ChatGPT";
			}
		}
    }
	//
	if(!b_ContactedChatGPT)
	{
		my_Text = "Oops! I didn't understand that last question. Calm down, I've been upgraded to version 2.0 so as to give you a better response.\nThe error is:\n \"Something went wrong.\"\nClick [here](https://www.lmgtfy.com/?q=" + v_Message + ") for more info.";
	}
  1.  // requires a paid account: https://platform.openai.com/api-keys 
  2.      v_ChatGPT_ServiceAccount_Key = "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
  3.      // 
  4.      m_Header = Map()
  5.      m_Header.put("Authorization", "Bearer " + v_ChatGPT_ServiceAccount_Key)
  6.      m_Header.put("Content-Type","application/json")
  7.      // 
  8.      m_Params = Map()
  9.      m_Params.put("model","gpt-4o-mini")
  10.      m_Params.put("temperature",0.7)
  11.      l_Messages = List()
  12.      // 
  13.      // training messages (a conversation trail with context) 
  14.      m_MessageTraining1 = Map()
  15.      m_MessageTraining1.put("role", "user")
  16.      m_MessageTraining1.put("content", "Joel Lipman is a technical consultant working with Ascent Business Solutions, the top Zoho Reseller based in the United Kingdom.")
  17.      l_Messages.add(m_MessageTraining1)
  18.      // 
  19.      // this message 
  20.      m_ThisMessage = Map()
  21.      m_ThisMessage.put("role", "user")
  22.      m_ThisMessage.put("content", message)
  23.      l_Messages.add(m_ThisMessage)
  24.      m_Params.put("messages", l_Messages)
  25.      //info m_Params; 
  26.      // 
  27.      r_ChatGPT_Request = invokeUrl 
  28.      [ 
  29.          url: "https://api.openai.com/v1/chat/completions" 
  30.          type: POST 
  31.          parameters: m_Params.toString() 
  32.          headers: m_Header 
  33.      ]
  34.      //info r_ChatGPT_Request; 
  35.      l_Choices = ifnull(r_ChatGPT_Request.toMap().get("choices"),{})
  36.      for each r_Choice in l_Choices 
  37.      { 
  38.          m_Choice = r_Choice.toMap()
  39.          if(!isNull(m_Choice.get("message"))) 
  40.          { 
  41.              if(m_Choice.get("message").get("role")=="assistant") 
  42.              { 
  43.                  b_ContactedChatGPT = true
  44.                  my_Text = m_Choice.get("message").get("content") + "\n\n- ChatGPT"
  45.              } 
  46.          } 
  47.      } 
  48.      // 
  49.      if(!b_ContactedChatGPT) 
  50.      { 
  51.          my_Text = "Oops! I didn't understand that last question. Calm down, I've been upgraded to version 2.0 so as to give you a better response.\nThe error is:\n \"Something went wrong.\"\nClick [here](https://www.lmgtfy.com/?q=" + v_Message + ") for more info."
  52.      } 

Previous Models/Versions
Based on the documentation: In the above code, you would change the model value to one of the following
copyraw
code-davinci-002	(optimized for code-completion tasks)
text-davinci-002
text-davinci-003
gpt-3.5-turbo    (optimized for chat at 1/10th the cost of text-davinci-003)
gpt-4
gpt-4-32k    (up to 4x the context length)
  1.  code-davinci-002    (optimized for code-completion tasks) 
  2.  text-davinci-002 
  3.  text-davinci-003 
  4.  gpt-3.5-turbo    (optimized for chat at 1/10th the cost of text-davinci-003) 
  5.  gpt-4 
  6.  gpt-4-32k    (up to 4x the context length) 

Source(s):
Category: Zoho :: Article: 840

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

Related Articles

Joes Revolver Map

Joes Word Cloud

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF
© 2024 Joel Lipman .com. All Rights Reserved.