File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed
Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ config.py
2+ .env
Original file line number Diff line number Diff line change 1+ import requests
2+ import config
3+
4+ API_URL = "https://api.openai.com/v1/chat/completions"
5+
6+ def chat_with_ai (user_message ):
7+ headers = {
8+ "Authorization" : f"Bearer { config .API_KEY } " ,
9+ "Content-Type" : "application/json"
10+ }
11+
12+ data = {
13+ "model" : "gpt-4" ,
14+ "messages" : [{"role" : "user" , "content" : user_message }]
15+ }
16+
17+ response = requests .post (API_URL , json = data , headers = headers )
18+
19+ if response .status_code == 200 :
20+ return response .json ()["choices" ][0 ]["message" ]["content" ]
21+ else :
22+ return f"Error: { response .status_code } , { response .text } "
23+
24+ # Greeting message
25+ print ("Hi there! I am Markhor, the AI chatbot powered by ChatGPT-4, represented by Coding Moves." )
26+ print ("How can I assist you today?\n " )
27+
28+ # Example usage
29+ while True :
30+ user_input = input ("You: " )
31+ if user_input .lower () in ["exit" , "quit" , "bye" ]:
32+ print ("Markhor AI: Goodbye! Have a great day." )
33+ break
34+ bot_response = chat_with_ai (user_input )
35+ print ("Markhor AI:" , bot_response )
You can’t perform that action at this time.
0 commit comments