File size: 843 Bytes
7e41497
2af20fb
0ed4567
b1714cd
0ed4567
 
b1714cd
0ed4567
 
7e41497
0ed4567
7e41497
0ed4567
 
 
7e41497
0ed4567
7e41497
 
b1714cd
0ed4567
 
 
 
 
 
 
 
7e41497
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
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()