Spaces:
Sleeping
Sleeping
File size: 1,031 Bytes
951e9a2 |
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 |
# Goal: Build an AI powered chat bot
import gradio as gr
from dotenv import load_dotenv
from openai import OpenAI
import json
load_dotenv()
client = OpenAI()
def save_history(history):
with open("data.json", "w") as data:
json.dump(history, data)
def load_history():
with open("data.json", "r") as data:
return json.load(data)
# Backend: Python
def echo(message, history):
# LLM: OpenAI
converstation_history = load_history()
converstation_history.append({"role": "user", "content": message})
completion = client.chat.completions.create(
model="gpt-4o-mini",
messages=converstation_history
)
converstation_history.append({"role": "assistant", "content": completion.choices[0].message.content})
save_history(converstation_history)
return completion.choices[0].message.content
# Frontend: Gradio
demo = gr.ChatInterface(fn=echo, type="messages", examples=["I want to lear about LLMs", "What is NLP", "what is RAG"], title="LLM Mentor")
demo.launch() |