Spaces:
Sleeping
Sleeping
File size: 971 Bytes
8e19f18 |
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 |
import gradio as gr
from dotenv import load_dotenv
from openai import OpenAI
load_dotenv()
client = OpenAI()
# Backend: Python
def echo(message, history):
# Convert Gradio history format to OpenAI messages format
messages = [
{"role": "system", "content": "You are a helpful LLM teacher."}
]
# Add chat history
for user_msg, bot_msg in history:
messages.append({"role": "user", "content": user_msg})
messages.append({"role": "assistant", "content": bot_msg})
# Add current message
messages.append({"role": "user", "content": message})
# Get response from OpenAI
completion = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages
)
return completion.choices[0].message.content
# Frontend: Gradio
demo = gr.ChatInterface(
fn=echo,
examples=["I want to learn about LLMs", "What is NLP", "What is RAG"],
title="LLM Mentor"
)
demo.launch() |