Botcopy support

Dialogflow + OpenAI 🔌

Dustin Dye the AI Guy | CEO Botcopy
Botcopy Blog
Published in
3 min readJan 24, 2023

--

Botcopy’s Step-by-Step Guide to Integrating Google Cloud Dialogflow with OpenAI’s GPT (davinci) technology for use in Botcopy’s Web Messenger.

Connect an LLM to your Dialogflow agent! Before we dive into how to connect two of the most potent conversational AI tools currently on the market, let’s dive into why. (Also, please note, Botcopy, as an exclusive Google Cloud Partner, is eagerly awaiting early access to the Bard API for use in Dialogflow, and we will let you know as soon as it’s tested and ready. We expect it to be better in every way and easier to connect because it’s native to Google. Meanwhile, let’s move onto why came to this post – the OpenAI integration.)

Google Dialogflow CX can handle natural language understanding and processing tasks, such as identifying the user’s intent and extracting entities.

OpenAI can help with the language generation piece. Combining Dialogflow with an LLM or NLG engine like OpenAI’s GPT API provides a complete natural language processing and understanding solution.

Plus, Botcopy allows your new Dialogflow chatbot to communicate seamlessly on your website and or mobile apps.

Here’s one example:

Dialogflow helps a utility company automate receiving and processing power outage reports by identifying the intent and extracting the location information. The info then goes to the external system, which processes the power outage report and updates the status in the database. Lastly, Dialogflow uses this information to generate a personalized response for the customer who initially requested the info.

Now for the fun part: Let’s dive into how it works.

Create a webhook to OpenAI in Dialogflow.

To use Dialogflow to call GPT (davinci), you will need to create an Intent in Dialogflow that is set up to call an external webhook.

We recommend Google Cloud function as the webhook. It can also be a server you set up to handle the communication between Dialogflow and GPT (davinci).

Here’s the step-by-step process:

  1. Create a new Intent in Dialogflow and give it a name.
  2. Under the “Action and parameters” section, add a parameter with the name “text” to capture the user’s input.
  3. Under the “Fulfillment” section, enable “Webhook” and save the changes.
  4. Set up a server that can handle the communication between Dialogflow and GPT (davinci). The server should be able to receive a request from Dialogflow with the user’s input, pass that input to GPT (davinci), and return the response to Dialogflow.
  5. In the Dialogflow Intent, configure the webhook to send a request to your server with the user’s input as a parameter.
  6. Your server should receive the request, pass the user’s input to GPT (davinci), and return the response to Dialogflow.
  7. Dialogflow will receive the response from the server and use it to respond to the user.

You can use various libraries available for Python, NodeJS, Java, etc., to write the server-side code.

Here is an example of a simple server-side script that can handle the communication between Dialogflow and GPT (davinci) using Node.js and the axios library for making HTTP requests.

const express = require('express')
const bodyParser = require('body-parser')
const axios = require('axios')
const app = express()
app.use(bodyParser.json())
app.post('/', (req, res) => {
const userInput = req.body.queryResult.parameters.text

// Make a request to the GPT API with the user's input
axios.post('https://api.openai.com/v1/engines/davinci/completions', {
prompt: userInput,
engine: 'davinci',
max_tokens:2048
}, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer <YOUR_API_KEY>`
}
}).then(response => {
const GPTResponse = response.data.choices[0].text
// Send the response from GPT back to Dialogflow
res.json({ fulfillmentText: GPTResponse })
}).catch(error => {
console.log(error)
res.json({ fulfillmentText: 'Error Occured' })
})
})
const PORT = process.env.PORT || 8080
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`)
})

In the above code,

  • The expresslibrary is used to set up a simple server.
  • body-parser is used to parse the JSON data sent by Dialogflow.
  • axios is used to make a request to the GPT API with the user's input.
  • Replace <YOUR_API_KEY> with your API Key from OpenAI.

Please note that this is just a basic example, and you may need to modify it depending on your specific use case and requirements.

Give this setup a try for yourself at www.Botcopy.com.

See you there!

--

--