Sidd0316 commited on
Commit
5271feb
·
1 Parent(s): 7664a92

Fixed API authentication issue and finalized AI assistant

Browse files
Files changed (2) hide show
  1. app.py +2 -1
  2. test_openrouter.py +12 -11
app.py CHANGED
@@ -3,7 +3,8 @@ import requests
3
  import os
4
 
5
  # ✅ Retrieve API Key from HF中国镜像站 Secrets or Environment
6
- HF_AUTH_TOKEN = os.getenv("HF_AUTH_TOKEN")
 
7
 
8
  # ✅ OpenRouter API Endpoint
9
  API_URL = "https://openrouter.ai/api/v1/chat/completions"
 
3
  import os
4
 
5
  # ✅ Retrieve API Key from HF中国镜像站 Secrets or Environment
6
+ HF_AUTH_TOKEN = "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"
7
+
8
 
9
  # ✅ OpenRouter API Endpoint
10
  API_URL = "https://openrouter.ai/api/v1/chat/completions"
test_openrouter.py CHANGED
@@ -1,25 +1,26 @@
1
- import os
2
  import requests
3
 
4
- # ✅ Fetch API Key from Environment Variables (Corrected Key Name)
5
- HF_AUTH_TOKEN = os.getenv("HF_AUTH_TOKEN1")
6
 
7
  # ✅ OpenRouter API Endpoint
8
  API_URL = "https://openrouter.ai/api/v1/chat/completions"
9
 
10
- # Check if the API key is properly loaded
11
- if not HF_AUTH_TOKEN:
12
- raise ValueError("Missing OpenRouter API Key. Make sure 'HF_AUTH_TOKEN1' is set in environment variables.")
13
-
14
- # Example API Request
15
  headers = {
16
  "Authorization": f"Bearer {HF_AUTH_TOKEN}",
17
  "Content-Type": "application/json"
18
  }
 
 
19
  data = {
20
- "model": "openrouter/latest",
21
- "messages": [{"role": "user", "content": "How do solar panels work?"}]
 
22
  }
23
 
 
24
  response = requests.post(API_URL, headers=headers, json=data)
25
- print(response.json())
 
 
 
 
1
  import requests
2
 
3
+ # ✅ Replace this with your new OpenRouter API Key
4
+ HF_AUTH_TOKEN = "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"
5
 
6
  # ✅ OpenRouter API Endpoint
7
  API_URL = "https://openrouter.ai/api/v1/chat/completions"
8
 
9
+ # Headers with API Key
 
 
 
 
10
  headers = {
11
  "Authorization": f"Bearer {HF_AUTH_TOKEN}",
12
  "Content-Type": "application/json"
13
  }
14
+
15
+ # ✅ Example API Request
16
  data = {
17
+ "model": "cognitivecomputations/dolphin3.0-r1-mistral-24b:free",
18
+
19
+ "messages": [{"role": "user", "content": "What is solar energy?"}]
20
  }
21
 
22
+ # ✅ Make API Request
23
  response = requests.post(API_URL, headers=headers, json=data)
24
+
25
+ # ✅ Print Response
26
+ print(response.json()) # This should return the AI's response