agent / app.py
smithaneka's picture
Update app.py
0ed4567 verified
Raw History Blame Contribute Delete
843 Bytes
import gradio as gr
import os
from openai import OpenAI
# Load your OpenAI API key from Hugging Face Secrets or environment
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# Chat function using new v1 OpenAI SDK
def simple_agent(message, history):
try:
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."}
] + [{"role": "user", "content": m} for m, _ in history] + [{"role": "user", "content": message}]
)
return response.choices[0].message.content
except Exception as e:
return f"Error: {str(e)}"
# Gradio chat interface
iface = gr.ChatInterface(
fn=simple_agent,
title="Simple AI Agent",
theme="default",
type="messages"
)
iface.launch()