File size: 1,450 Bytes
f268dce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
import openai
import gradio as gr
import os

openai.api_key = os.getenv("OPENAI_API_KEY")  # Set this in your HF Space secret settings

def chat_with_gpt4(user_question):
    # Few-shot prompt context
    prompt = f"""
You are a helpful customer support assistant.

Q: What is the return policy?
A: You can return items within 30 days if they are in original condition.

Q: How do I track my order?
A: You will receive a tracking number via email once your order is shipped.

Q: What payment methods do you accept?
A: We accept credit cards, PayPal, and bank transfers.

Q: {user_question}
A:
"""

    try:
        response = openai.ChatCompletion.create(
            model="gpt-4",
            messages=[
                {"role": "system", "content": "You are a helpful customer support assistant."},
                {"role": "user", "content": prompt}
            ],
            temperature=0.3,
            max_tokens=150,
        )
        return response['choices'][0]['message']['content'].strip()

    except Exception as e:
        return f"Error: {str(e)}"

iface = gr.Interface(
    fn=chat_with_gpt4,
    inputs=gr.Textbox(lines=2, placeholder="Ask a customer service question..."),
    outputs=gr.Textbox(label="GPT-4 Response"),
    title="Few-Shot Customer Support Chatbot (GPT-4)",
    description="This chatbot uses GPT-4 with few-shot prompting to answer customer support questions."
)

if __name__ == "__main__":
    iface.launch()