-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOT_API.py
More file actions
49 lines (39 loc) · 1.17 KB
/
BOT_API.py
File metadata and controls
49 lines (39 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from flask import Flask, request
import openai
from twilio.twiml.messaging_response import MessagingResponse
import os
# Init the Flask App
app = Flask(__name__)
# Initialize the OpenAI API key
# export OPENAI_API_KEY=YOUR API KEY
openai.api_key = os.environ.get("OPENAI_API_KEY")
# Define a function to generate answers using GPT-3
def generate_answer(question):
model_engine = "text-davinci-002"
prompt = (f"Q: {question}\n"
"A:")
response = openai.Completion.create(
engine=model_engine,
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
temperature=0.7,
)
answer = response.choices[0].text.strip()
return answer
# Define a route to handle incoming requests
@app.route('/chatgpt', methods=['POST'])
def chatgpt():
incoming_que = request.values.get('Body', '').lower()
print("Question: ", incoming_que)
# Generate the answer using GPT-3
answer = generate_answer(incoming_que)
print("BOT Answer: ", answer)
bot_resp = MessagingResponse()
msg = bot_resp.message()
msg.body(answer)
return str(bot_resp)
# Run the Flask app
if __name__ == '__main__':
app.run()