-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.py
More file actions
47 lines (31 loc) · 982 Bytes
/
chat.py
File metadata and controls
47 lines (31 loc) · 982 Bytes
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
#!/usr/bin/env python3
from openai import OpenAI
from dotenv import load_dotenv
import os, time
load_dotenv()
API_KEY = os.getenv("API_KEY")
client = OpenAI(
api_key=API_KEY,
base_url="https://openrouter.ai/api/v1"
)
print("💬 OpenRouter Chat Terminal (type 'exit' to quit)\n")
messages = []
while True:
try:
user_input = input("You: ").strip()
if user_input.lower() in ["exit", "quit"]:
print("👋 Goodbye!")
break
messages.append({"role": "user", "content": user_input})
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=messages
)
reply = response.choices[0].message.content
messages.append({"role": "assistant", "content": reply})
print(f"\n\nGPT: {reply}\n\n")
except KeyboardInterrupt:
print("\n👋 Session ended.")
break
except Exception as e:
print(f"⚠️ Error: {e}\n")