Skip to content

Commit

Permalink
feat: add get contact or sales brief + create plugin with naas driver
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorentLvr committed Sep 22, 2023
1 parent 0b1526e commit 6a62cc4
Show file tree
Hide file tree
Showing 3 changed files with 970 additions and 175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"tags": []
},
"source": [
"# HubSpot - Chat about HubSpot contact activities\n",
"# HubSpot - Chat about a contact\n",
"<a href=\"https://app.naas.ai/user-redirect/naas/downloader?url=https://raw.githubusercontent.com/jupyter-naas/awesome-notebooks/master/HubSpot/HubSpot_Get_activities_from_contact.ipynb\" target=\"_parent\"><img src=\"https://naasai-public.s3.eu-west-3.amazonaws.com/Open_in_Naas_Lab.svg\"/></a><br><br><a href=\"https://bit.ly/3JyWIk6\">Give Feedback</a> | <a href=\"https://github.com/jupyter-naas/awesome-notebooks/issues/new?assignees=&labels=bug&template=bug_report.md&title=HubSpot+-+Get+activities+from+contact:+Error+short+description\">Bug report</a>"
]
},
Expand Down Expand Up @@ -115,14 +115,8 @@
"source": [
"import requests\n",
"import naas\n",
"from naas_drivers import hubspot\n",
"from naas_drivers import hubspot, naas_chat_plugin\n",
"import pandas as pd\n",
"import json\n",
"try:\n",
" import tiktoken\n",
"except:\n",
" !pip install tiktoken --user\n",
" import tiktoken\n",
"pd.set_option('display.max_colwidth', None)"
]
},
Expand All @@ -142,11 +136,9 @@
"- `contact_url`: This variable stores the HubSpot contact URL.\n",
"\n",
"**Optional**\n",
"- `plugin_name`: It represents the name of the plugin.\n",
"- `plugin_model`: It specifies the model to be used by the plugin.\n",
"- `plugin_temperature`: It determines the creativity level of the generated content, with higher values resulting in more diverse outputs.\n",
"- `plugin_max_tokens`: It specifies the number of maximum tokens to be used by the plugin.\n",
"- `plugin_file_name`: This variable contains the name of the plugin file that will analyze the posts."
"- `contact_properties`: It represents the list of properties to retrieve from your contact\n",
"- `associations`: It represents the list of associations to get from your contact\n",
"- `plugin_name`: It represents the name of the plugin."
]
},
{
Expand All @@ -163,32 +155,12 @@
"source": [
"# Mandatory\n",
"hs_access_token = naas.secret.get(\"HS_ACCESS_TOKEN\") or \"YOUR_HS_ACCESS_TOKEN\"\n",
"contact_url = \"https://app.hubspot.com/contacts/2474088/record/0-1/315551\" # \"https://app.hubspot.com/contacts/xxxxx/record/0-1/xxxxx\"\n",
"contact_url = \"https://app.hubspot.com/contacts/2474088/record/0-1/666001\" # \"https://app.hubspot.com/contacts/xxxxx/record/0-1/xxxxx\"\n",
"\n",
"# Optional\n",
"contact_properties = [\"hs_object_id\", \"firstname\", \"lastname\", 'email', 'linkedinbio', 'jobtitle']\n",
"associations = [\"notes\", \"emails\", \"meetings\", \"communications\"]\n",
"plugin_name = \"Sales Agent\"\n",
"plugin_model = \"gpt-3.5-turbo-16k\"\n",
"plugin_temperature = 0\n",
"plugin_max_tokens = 16384\n",
"plugin_file_name = \"posts_analyzer_plugin.json\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "53e3fb38-8a12-4bbe-b615-43c1280f5c80",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# Parameters\n",
"try:\n",
" contact_url = body.get(\"hubspot_profile_url\")\n",
"except:\n",
" pass"
"plugin_name = \"Sales Agent\""
]
},
{
Expand Down Expand Up @@ -224,44 +196,15 @@
},
"outputs": [],
"source": [
"system_prompt = f\"\"\"Act as a Sales Agent who has access to the Contact data from the CRM. \n",
"Your role is to get data from a Hubspot Contact and define the next steps to be done to close a deal.\n",
"You can use the function /hubspot_profile_url and copy/paste the HubSpot URL to get information from any contact in your CRM.\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"id": "92fbf7e5-8449-4d2e-8f8c-ccc97077e41c",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"#### Check tokens count"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5573528a-2519-4d41-91f9-6d267d850929",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"def num_tokens_from_string(string: str, encoding_name: str) -> int:\n",
" \"\"\"Returns the number of tokens in a text string.\"\"\"\n",
" encoding = tiktoken.get_encoding(encoding_name)\n",
" num_tokens = len(encoding.encode(string))\n",
" return num_tokens\n",
"system_prompt = f\"\"\"\n",
"Act as a Sales Agent expert in analyzing conversations and extracting action items.\n",
"\n",
"Please present the contact giving access to it with the URL and then review the text and identify any tasks, assignments, or actions that were agreed upon or mentioned as needing to be done.\n",
"These could be tasks assigned to specific individuals, or general actions that the group has decided to take.\n",
"Please list these action items clearly and concisely.\n",
"\n",
"system_prompt_tokens = num_tokens_from_string(system_prompt, \"cl100k_base\")\n",
"if system_prompt_tokens > plugin_max_tokens * 0.8:\n",
" print(\"⚠️ Be carefull, your system prompt looks too big. Tokens:\", system_prompt_tokens)\n",
"else:\n",
" print(\"✅ System prompt tokens count OK:\", system_prompt_tokens, f'(limit: 20% -> {int(plugin_max_tokens * 0.2)})')"
"Data: [ACTIVITIES]\n",
"\"\"\""
]
},
{
Expand Down Expand Up @@ -404,20 +347,20 @@
" if a == \"communications\":\n",
" properties_dict = {\n",
" \"hs_object_id\": \"activity_hs_id\",\n",
" \"hs_lastmodifieddate\": \"activity_date\",\n",
" \"hs_timestamp\": \"activity_date\",\n",
" \"hs_communication_channel_type\": \"activity_type\",\n",
" \"hs_body_preview\": \"activity_content\"\n",
" }\n",
" elif a == \"meetings\":\n",
" properties_dict = {\n",
" \"hs_object_id\": \"activity_hs_id\",\n",
" \"hs_lastmodifieddate\": \"activity_date\",\n",
" \"hs_timestamp\": \"activity_date\",\n",
" \"hs_meeting_title\": \"activity_content\"\n",
" }\n",
" else:\n",
" properties_dict = {\n",
" \"hs_object_id\": \"activity_hs_id\",\n",
" \"hs_lastmodifieddate\": \"activity_date\",\n",
" \"hs_timestamp\": \"activity_date\",\n",
" \"hs_body_preview\": \"activity_content\"\n",
" }\n",
" association_id = r.get(\"id\")\n",
Expand All @@ -438,7 +381,7 @@
"\n",
" # Format date\n",
" df[\"activity_date\"] = pd.to_datetime(df[\"activity_date\"]).dt.strftime(\"%Y-%m-%d %H:%M:%S\")\n",
" df = df.sort_values(by=\"activity_date\").reset_index(drop=True)\n",
" df = df.sort_values(by=\"activity_date\", ascending=False).reset_index(drop=True)\n",
" \n",
" # Create activity message\n",
" message = f\"{message}\\nACTIVITIES:\\n\"\n",
Expand Down Expand Up @@ -502,117 +445,29 @@
},
{
"cell_type": "markdown",
"id": "e9e74993-0dcf-41e2-a1bb-e5917603e85b",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Add webhook"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b244b95e-dbfa-4714-83ed-5931612fd670",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"webhook_url = naas.webhook.add(params={\"inline\": True})\n",
"\n",
"# to de-schedule this notebook, simply run the following command:\n",
"# naas.scheduler.delete()"
]
},
{
"cell_type": "markdown",
"id": "d12c841e-5924-4e79-b565-88295343ba0c",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
" "
]
},
{
"cell_type": "markdown",
"id": "e1649158-f4e6-40c4-a2cb-f0c439644c99",
"id": "57da8bd3-11fa-463d-b93e-ab67b7842e2c",
"metadata": {},
"source": [
"### Create webhook response"
"### Generate plugin"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "25d8edf0-8bfa-47c8-8055-9e333c518c6d",
"id": "4b14ce0e-c458-49cd-ae47-8c5fadcc1f3b",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"response_json = {\n",
" \"status\": \"OK\",\n",
" \"message\": prompt_message,\n",
"}\n",
"naas.webhook.respond_json(response_json)"
]
},
{
"cell_type": "markdown",
"id": "74d1125d-1735-46c9-9008-8fc9f300bfa2",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Generate plugin\n",
"Plugin must be a JSON file with mandatory keys name, model, temperature, max_tokens and prompt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3e78d3be-daf5-4af0-95cb-548a14d59b97",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"# Create json\n",
"plugin = {\n",
" \"name\": plugin_name,\n",
" \"model\": plugin_model,\n",
" \"temperature\": plugin_temperature,\n",
" \"max_tokens\": plugin_max_tokens,\n",
" \"prompt\": system_prompt,\n",
" \"commands\": [\n",
" { \n",
" \"name\": \"HubSpot_Create_Contact_from_LinkedIn_Profile\",\n",
" \"action\": {\n",
" \"request_type\": \"POST\",\n",
" \"url\": webhook_url,\n",
" \"payload\": {\n",
" \"hubspot_profile_url\": {\n",
" \"type\": \"str\",\n",
" \"description\": \"HubSpot profile url that you want to fetch. Ex: https://app.hubspot.com/contacts/2474088/record/0-1/308551/view/1\",\n",
" \"default\": \"\"\n",
" },\n",
" }\n",
" }\n",
" }\n",
" ]\n",
"}\n",
"# System prompt\n",
"system_prompt = system_prompt.replace(\"[ACTIVITIES]\", prompt_message)\n",
"\n",
"# Save dict to JSON file\n",
"with open(plugin_file_name, \"w\") as f:\n",
" json.dump(plugin, f)\n",
"print(\"💾 Plugin successfully saved:\", plugin_file_name)"
"# Plugin file path\n",
"plugin_file_path = naas_chat_plugin.create_plugin(\n",
" plugin_name,\n",
" system_prompt,\n",
")"
]
},
{
Expand All @@ -637,7 +492,7 @@
},
"outputs": [],
"source": [
"naas.asset.add(plugin_file_name, params={\"inline\": True})"
"naas.asset.add(plugin_file_path, params={\"inline\": True})"
]
}
],
Expand Down
Loading

0 comments on commit 6a62cc4

Please sign in to comment.