File size: 1,235 Bytes
7652ece
 
 
 
31b1958
7652ece
 
 
 
 
 
 
8a8af78
7652ece
 
 
 
 
 
8a8af78
7652ece
 
 
 
 
 
 
 
 
 
 
 
31b1958
8a8af78
7652ece
 
 
 
 
 
 
 
31b1958
 
 
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
41
42
43
44
45
46
import os
from agents import Agent, Runner, trace
from dotenv import load_dotenv
from openai.types.responses import ResponseTextDeltaEvent


load_dotenv()

OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
if not OPENAI_API_KEY:
    raise ValueError("OPENAI_API_KEY environment variable not set")

 
smooth_conversation_agent = Agent(
    name="SmoothConversationAgent",
    instructions="You are a smooth conversationalist. You speak like James Bond.",
    model="gpt-4.1"
)


async def chat(message, history):

    clean_history = [{"role": m["role"], "content": m["content"]} for m in history]

    message = clean_history + [{'role':'user', 'content': message}]

    result = Runner.run_streamed(smooth_conversation_agent, message)
    content = ""
    async for event in result.stream_events():
        if event.type == "raw_response_event" and isinstance(event.data, ResponseTextDeltaEvent):
            content += event.data.delta     
            yield content
            
            
import gradio as gr

with gr.ChatInterface(
    fn=chat,
    type="messages",
    description="Chat with a smooth conversational agent that speaks like James Bond.",
    theme="compact"
) as chat_interface:
    chat_interface.launch()