|
|
import os |
|
|
from openai import Client |
|
|
import gradio as gr |
|
|
|
|
|
|
|
|
api_key = os.environ.get("OPENAI_API_KEY") |
|
|
client = Client(api_key=api_key) |
|
|
|
|
|
|
|
|
def generate_response(user_input): |
|
|
if "who created you" in user_input.lower(): |
|
|
chat_response = "I was created by Ram.V" |
|
|
elif "who is superstar" in user_input.lower(): |
|
|
chat_response = "The one and only * Superstar Rajinikanth * Thalaiva!" |
|
|
else: |
|
|
|
|
|
response = client.chat.completions.create( |
|
|
model="gpt-3.5-turbo-1106", |
|
|
response_format={"type": "json_object"}, |
|
|
messages=[ |
|
|
{"role": "system", "content": "You are a helpful assistant designed to output JSON."}, |
|
|
{"role": "user", "content": user_input} |
|
|
] |
|
|
) |
|
|
chat_response = response.choices[0].message.content |
|
|
|
|
|
return chat_response |
|
|
|
|
|
|
|
|
def openai_chat_history(input, history): |
|
|
history = history or [] |
|
|
if input.strip() != "": |
|
|
s = list(sum(history, ())) |
|
|
s.append(input) |
|
|
inp = ' '.join(s) |
|
|
output = generate_response(inp) |
|
|
history.append((input, output)) |
|
|
return history[-1][1] |
|
|
else: |
|
|
return "" |
|
|
|
|
|
|
|
|
conversation_prompt = "Welcome to ChatRobo, kindly type in your enquiries: " |
|
|
|
|
|
|
|
|
block = gr.Interface( |
|
|
fn=openai_chat_history, |
|
|
inputs=[gr.inputs.Textbox(placeholder=conversation_prompt)], |
|
|
outputs=[gr.outputs.Textbox(label="ChatRobo Output")] |
|
|
) |
|
|
|
|
|
|
|
|
block.launch() |
|
|
|
|
|
|