File size: 1,369 Bytes
d9882b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
######################################
##
##  ChatBot using OpenAI
##
######################################

import os
import openai as opai
import gradio as gr

opai.api_key = os.getenv("OPENAI_API_KEY")

# make prompt accept command line argument
# prompt = input("Enter prompt: ")
# setup a function that accepts propmpt and returns a response using "gpt-3.5-turbo" model

def chatbot(prompt, temperature = 0.2):
    response = opai.ChatCompletion.create(
        model = "gpt-3.5-turbo",
        max_tokens = 100,
        temperature = temperature,
        frequency_penalty = 0,
        presence_penalty = 0.6,
        messages = [
            {"role": "user", "content": prompt}
]
    )
    return response['choices'][0]['message']['content']

# test the function
demo = gr.Interface(
    fn = chatbot, 
    inputs = [gr.Textbox(lines=5, label="Prompt", placeholder="Enter prompt here", info = "Enter a prompt and the chatbot will generate a response."), gr.Slider(0, 1, 0.2, step = 0.1, label="Temperature", info = "The higher the temperature, the more creative the response will be.")],
    outputs = gr.Textbox(label="Response", lines=10), 
    title = "ChatGenius - An OpenAI self-tuned chatbot based on GPT-3.5 Turbo", 
    description = "Enter a prompt and the chatbot will generate a response.",
    theme=gr.themes.Monochrome()
)
demo.launch(share=True)