├── .gitignore ├── README.md ├── helper └── openai_api.py ├── requirements.txt ├── run.py └── src └── app.py /.gitignore: -------------------------------------------------------------------------------- 1 | /venv 2 | **/__pycache__ 3 | .env 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![GitHub User's stars](https://img.shields.io/github/stars/RajKKapadia?style=for-the-badge) 2 | ![GitHub followers](https://img.shields.io/github/followers/RajKKapadia?style=for-the-badge) 3 | 4 | # Connect OpenAI to Dialogflow CX/ES 5 | Hey everyone, if you are looking for a connection between OpenAI and Dialogflow CX/ES, then read more. You will learn how to connect your existing Dialogflow CX/ES agent to openAI. 6 | 7 | # Youtube 8 | I have recorded a quick tutorial on this topic, you can watch it [here](). 9 | 10 | ### Things you will need 11 | * Dialogflow CX and ES agent 12 | > some knowledge of Dialogflow CX and ES as well 13 | * OpenAI account and API Key 14 | > create an account [here](https://openai.com/) 15 | * NGROK for exposing our local server to internet for testing 16 | > get it from [here](https://ngrok.com/) 17 | * Python as a programing tool 18 | > install it from [here](https://www.python.org/downloads/) 19 | 20 | ### How to use it 21 | To replicate the work of this repository and run it locally, you need to follow these steps: 22 | * create a `.env` file inside the root directory, create these environmental variables: 23 | ``` 24 | OPENAI_API_KEY=YOUR OPENAI API KEY 25 | ``` 26 | * create a virtual environment and activate it before installing the packages 27 | * install all the required dependencies from the `requirements.txt` file 28 | ```python 29 | pip install -r requirements.txt 30 | ``` 31 | * run the server with either of the following commands 32 | ```python 33 | python run.py 34 | ``` 35 | * add the webhook url to Dialogflow 36 | > CX: YOUR NGROK URL/dialogflow/cx/receiveMessage 37 | 38 | > ES: YOUR NGROK URL/dialogflow/es/receiveMessage 39 | 40 | # About me 41 | I am `Raj Kapadia`, I am passionate about `AI/ML/DL` and their use in different domains, I also love to build `chatbots` using `Google Dialogflow ES/CX`, I have backend development experience with Python[Flask], and NodeJS[Express] For any work, you can reach out to me at... 42 | 43 | * [LinkedIn](https://www.linkedin.com/in/rajkkapadia/) 44 | * [Fiverr](https://www.fiverr.com/rajkkapadia​) 45 | * [Upwork](https://www.upwork.com/freelancers/~0176aeacfcff7f1fc2) 46 | * [Youtube](https://www.youtube.com/channel/UCOT01XvBSj12xQsANtTeAcQ) -------------------------------------------------------------------------------- /helper/openai_api.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | 4 | import openai 5 | from dotenv import load_dotenv 6 | load_dotenv() 7 | 8 | 9 | openai.api_key = os.getenv('OPENAI_API_KEY') 10 | 11 | 12 | def text_complition(prompt: str) -> dict: 13 | ''' 14 | Call Openai API for text completion 15 | 16 | Parameters: 17 | - prompt: user query (str) 18 | 19 | Returns: 20 | - dict 21 | ''' 22 | try: 23 | response = openai.Completion.create( 24 | model='text-davinci-003', 25 | prompt=f'Human: {prompt}\nAI: ', 26 | temperature=0.9, 27 | max_tokens=150, 28 | top_p=1, 29 | frequency_penalty=0, 30 | presence_penalty=0.6, 31 | stop=['Human:', 'AI:'] 32 | ) 33 | return { 34 | 'status': 1, 35 | 'response': response['choices'][0]['text'] 36 | } 37 | except: 38 | return { 39 | 'status': 0, 40 | 'response': '' 41 | } 42 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | certifi==2022.12.7 2 | charset-normalizer==2.1.1 3 | click==8.1.3 4 | et-xmlfile==1.1.0 5 | Flask==2.2.2 6 | idna==3.4 7 | importlib-metadata==6.0.0 8 | itsdangerous==2.1.2 9 | Jinja2==3.1.2 10 | MarkupSafe==2.1.1 11 | numpy==1.24.1 12 | openai==0.25.0 13 | openpyxl==3.0.10 14 | pandas==1.5.2 15 | pandas-stubs==1.5.2.230105 16 | python-dateutil==2.8.2 17 | python-dotenv==0.21.0 18 | pytz==2022.7 19 | requests==2.28.1 20 | six==1.16.0 21 | tqdm==4.64.1 22 | types-pytz==2022.7.0.0 23 | typing_extensions==4.4.0 24 | urllib3==1.26.13 25 | Werkzeug==2.2.2 26 | zipp==3.11.0 27 | -------------------------------------------------------------------------------- /run.py: -------------------------------------------------------------------------------- 1 | from src.app import app 2 | 3 | 4 | if __name__ == '__main__': 5 | app.run(debug=True) 6 | -------------------------------------------------------------------------------- /src/app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, request, jsonify 2 | 3 | 4 | from helper.openai_api import text_complition 5 | 6 | 7 | app = Flask(__name__) 8 | 9 | 10 | @app.route('/') 11 | def home(): 12 | return 'All is well...' 13 | 14 | 15 | @app.route('/dialogflow/es/receiveMessage', methods=['POST']) 16 | def esReceiveMessage(): 17 | try: 18 | data = request.get_json() 19 | # You can use this action to do different things 20 | # action = data['queryResult']['action'] 21 | query_text = data['queryResult']['queryText'] 22 | 23 | result = text_complition(query_text) 24 | 25 | if result['status'] == 1: 26 | return jsonify( 27 | { 28 | 'fulfillmentText': result['response'] 29 | } 30 | ) 31 | except: 32 | pass 33 | return jsonify( 34 | { 35 | 'fulfillmentText': 'Something went wrong.' 36 | } 37 | ) 38 | 39 | 40 | @app.route('/dialogflow/cx/receiveMessage', methods=['POST']) 41 | def cxReceiveMessage(): 42 | try: 43 | data = request.get_json() 44 | # Use this tag peoperty to choose the action 45 | # tag = data['fulfillmentInfo']['tag'] 46 | query_text = data['text'] 47 | 48 | result = text_complition(query_text) 49 | 50 | if result['status'] == 1: 51 | return jsonify( 52 | { 53 | 'fulfillment_response': { 54 | 'messages': [ 55 | { 56 | 'text': { 57 | 'text': [result['response']], 58 | 'redactedText': [result['response']] 59 | }, 60 | 'responseType': 'HANDLER_PROMPT', 61 | 'source': 'VIRTUAL_AGENT' 62 | } 63 | ] 64 | } 65 | } 66 | ) 67 | except: 68 | pass 69 | return jsonify( 70 | { 71 | 'fulfillment_response': { 72 | 'messages': [ 73 | { 74 | 'text': { 75 | 'text': ['Something went wrong.'], 76 | 'redactedText': ['Something went wrong.'] 77 | }, 78 | 'responseType': 'HANDLER_PROMPT', 79 | 'source': 'VIRTUAL_AGENT' 80 | } 81 | ] 82 | } 83 | } 84 | ) 85 | --------------------------------------------------------------------------------