-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
138 lines (103 loc) · 3.29 KB
/
app.py
File metadata and controls
138 lines (103 loc) · 3.29 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import os
from dotenv import load_dotenv
from openai import OpenAI
# =========================
# LOAD ENV VARIABLES
# =========================
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
raise ValueError("❌ API key not found. Please set OPENAI_API_KEY in .env")
# =========================
# INITIALIZE CLIENT
# =========================
client = OpenAI(api_key=api_key)
# =========================
# CORE FUNCTION
# =========================
def generate_response(user_input, temperature=0.7, max_tokens=200, system_message=None):
if system_message is None:
system_message = "You are a helpful AI assistant."
try:
response = client.chat.completions.create(
model="gpt-5.3",
messages=[
{"role": "system", "content": system_message},
{"role": "user", "content": user_input}
],
temperature=temperature,
max_tokens=max_tokens
)
return response.choices[0].message.content
except Exception as e:
return f"❌ Error: {str(e)}"
# =========================
# TEMPERATURE COMPARISON
# =========================
def compare_temperatures(prompt):
temps = [0.2, 0.5, 0.9]
print("\n🔍 Comparing Outputs:\n")
for temp in temps:
print(f"\n--- Temperature: {temp} ---")
response = generate_response(prompt, temperature=temp)
print(response)
# =========================
# INTERACTIVE CHAT
# =========================
def chat_mode():
print("\n🤖 Chat Mode (type 'exit' to quit)\n")
while True:
user_input = input("You: ")
if user_input.lower() == "exit":
print("👋 Goodbye!")
break
temp = input("Set temperature (0.0–1.0, default=0.7): ")
max_tok = input("Set max_tokens (default=200): ")
temperature = float(temp) if temp else 0.7
max_tokens = int(max_tok) if max_tok else 200
response = generate_response(
user_input,
temperature=temperature,
max_tokens=max_tokens
)
print(f"\nAI: {response}\n")
# =========================
# SYSTEM MESSAGE MODE
# =========================
def system_mode():
print("\n🧠 Custom System Role Mode\n")
system_message = input("Enter system behavior (e.g., 'You are a strict teacher'): ")
user_input = input("Enter your prompt: ")
response = generate_response(
user_input,
system_message=system_message
)
print(f"\nAI: {response}\n")
# =========================
# MAIN MENU
# =========================
def main():
while True:
print("\n===== OpenAI Python Playground =====")
print("1. Chat Mode")
print("2. Compare Temperatures")
print("3. Custom System Role")
print("4. Exit")
choice = input("Select option: ")
if choice == "1":
chat_mode()
elif choice == "2":
prompt = input("Enter prompt: ")
compare_temperatures(prompt)
elif choice == "3":
system_mode()
elif choice == "4":
print("👋 Exiting...")
break
else:
print("❌ Invalid choice")
# =========================
# RUN APP
# =========================
if __name__ == "__main__":
main()