-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
60 lines (46 loc) · 1.58 KB
/
Copy pathmain.py
File metadata and controls
60 lines (46 loc) · 1.58 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
#!/usr/bin/env python3
"""
Main file for FireGuard demo using OpenAI API with a command-line interaction.
"""
import os
from dotenv import load_dotenv
from openai_client import OpenAIClient
# Load environment variables from .env file
load_dotenv()
def main():
"""Main function to run the chat interface."""
print("Type your message and press Enter to chat with the AI!")
# Check for API key
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
print("Error: OPENAI_API_KEY not found.")
return
try:
# Initialize OpenAI client
client = OpenAIClient(api_key=api_key)
# Main chat loop
while True:
try:
# Get user input
user_input = input("You: ").strip()
# Handle empty input
if not user_input:
continue
# Handle quit command
if user_input.lower() in ['quit', 'exit', 'bye']:
break
# Get AI response
print("AI: ", end="", flush=True)
response = client.get_response(user_input)
print(response)
print()
except KeyboardInterrupt:
break
except Exception as e:
print(f"\nError: {str(e)}")
print("Please try again.")
except Exception as e:
print(f"Failed to initialize OpenAI client: {str(e)}")
return
if __name__ == "__main__":
main()