Upload 2 files
Browse files
app.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
from utils import generate_response
|
| 4 |
+
|
| 5 |
+
with gr.Blocks() as demo:
|
| 6 |
+
|
| 7 |
+
chatbot = gr.Chatbot(label='Network Design Chatbot', height=600)
|
| 8 |
+
msg = gr.Textbox()
|
| 9 |
+
clear = gr.ClearButton([msg, chatbot])
|
| 10 |
+
|
| 11 |
+
msg.submit(generate_response, [msg, chatbot], [msg, chatbot])
|
| 12 |
+
|
| 13 |
+
demo.launch()
|
utils.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import random
|
| 3 |
+
import time
|
| 4 |
+
|
| 5 |
+
import openai
|
| 6 |
+
from dotenv import load_dotenv, find_dotenv
|
| 7 |
+
|
| 8 |
+
load_dotenv(find_dotenv())
|
| 9 |
+
|
| 10 |
+
openai.api_key = os.getenv('OPENAI_API_KEY')
|
| 11 |
+
|
| 12 |
+
def chat_completion(messages: list) -> str:
|
| 13 |
+
try:
|
| 14 |
+
completion = openai.ChatCompletion.create(
|
| 15 |
+
model='gpt-3.5-turbo',
|
| 16 |
+
messages=messages
|
| 17 |
+
)
|
| 18 |
+
return completion['choices'][0]['message']['content']
|
| 19 |
+
except:
|
| 20 |
+
return 'We are facing a technical issue at this moment.'
|
| 21 |
+
|
| 22 |
+
def generate_messages(messages: list, query: str) -> list:
|
| 23 |
+
formated_messages = [
|
| 24 |
+
{
|
| 25 |
+
'role': 'system',
|
| 26 |
+
'content': 'You are a helpful Network Design assistant.'
|
| 27 |
+
}
|
| 28 |
+
]
|
| 29 |
+
for m in messages:
|
| 30 |
+
formated_messages.append({
|
| 31 |
+
'role': 'user',
|
| 32 |
+
'content': m[0]
|
| 33 |
+
})
|
| 34 |
+
formated_messages.append({
|
| 35 |
+
'role': 'assistant',
|
| 36 |
+
'content': m[1]
|
| 37 |
+
})
|
| 38 |
+
formated_messages.append(
|
| 39 |
+
{
|
| 40 |
+
'role': 'user',
|
| 41 |
+
'content': query
|
| 42 |
+
}
|
| 43 |
+
)
|
| 44 |
+
return formated_messages
|
| 45 |
+
|
| 46 |
+
def generate_response(query: str, chat_history: list) -> tuple:
|
| 47 |
+
messages = generate_messages(chat_history, query)
|
| 48 |
+
bot_message = chat_completion(messages)
|
| 49 |
+
chat_history.append((query, bot_message))
|
| 50 |
+
time.sleep(random.randint(0, 5))
|
| 51 |
+
return '', chat_history
|