Kunal commited on
Commit
e6f0bb0
·
1 Parent(s): 6001123

updated score model

Browse files
Files changed (2) hide show
  1. Agent.py +2 -2
  2. test.py +96 -0
Agent.py CHANGED
@@ -217,7 +217,7 @@ def generate_environmental_score(state: EnvironmentalAnalysisState):
217
  6. End-of-life disposal
218
 
219
  Provide:
220
- - Overall score (0-100, where 100 is most sustainable)
221
  - Category breakdown
222
  - Improvement recommendations
223
  """)
@@ -235,7 +235,7 @@ def generate_environmental_score(state: EnvironmentalAnalysisState):
235
  ]
236
 
237
  result = llm.chat.completions.create(
238
- model="deepseek-ai/DeepSeek-R1",
239
  messages=messages,
240
  max_tokens=1500,
241
  temperature=0.2
 
217
  6. End-of-life disposal
218
 
219
  Provide:
220
+ - Overall score (0-100, where 100 is most sustainable and 0 is least sustainable)
221
  - Category breakdown
222
  - Improvement recommendations
223
  """)
 
235
  ]
236
 
237
  result = llm.chat.completions.create(
238
+ model="meta-llama/Meta-Llama-3.1-70B-Instruct",
239
  messages=messages,
240
  max_tokens=1500,
241
  temperature=0.2
test.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from dotenv import load_dotenv
3
+ from huggingface_hub import InferenceClient
4
+ from langchain.prompts import ChatPromptTemplate
5
+ import time
6
+ from huggingface_hub import InferenceClient
7
+ from huggingface_hub.errors import HfHubHTTPError # Corrected import path
8
+
9
+ # --- Setup for environment variables and client ---
10
+ # 1. Create a .env file in the same directory as this script.
11
+ # 2. Add your Hugging Face Access Token to it:
12
+ # HF_TOKEN="hf_YOUR_ACTUAL_HUGGING_FACE_TOKEN"
13
+ # (The 'hf_' prefix is important for Hugging Face tokens)
14
+ load_dotenv()
15
+
16
+ # Initialize llm with your standard Hugging Face Token
17
+ # The InferenceClient automatically looks for HF_TOKEN if not explicitly provided
18
+ try:
19
+ hf_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJra3VuYWxnZ3VwdGEyMDBAZ21haWwuY29tIiwiaWF0IjoxNzQ5MDI5MzE2fQ.FyaF9EEw5MlkVNjq3SxjfzIiFqGCm8Z-glIqGEuL8ac"
20
+ if not hf_token:
21
+ print("Warning: HF_TOKEN not found in .env file or environment variables.")
22
+ print("The InferenceClient might work for public models, but private models or higher rate limits may require a token.")
23
+ # If no token is found, try to proceed without it (might work for public models)
24
+ llm = InferenceClient()
25
+ else:
26
+ llm = InferenceClient(token=hf_token) # Explicitly pass the token if you want to be sure
27
+
28
+ except Exception as e:
29
+ print(f"Error initializing InferenceClient: {e}")
30
+ print("Please ensure your .env file has HF_TOKEN set correctly.")
31
+ exit()
32
+
33
+ # --- Sample Prompt and API Call ---
34
+ def test_llm_timeout():
35
+ # Use a simple prompt for testing
36
+ test_prompt = ChatPromptTemplate.from_template(
37
+ "Explain the concept of neural networks in a simple way."
38
+ )
39
+ rendered_prompt_content = test_prompt.format()
40
+
41
+ messages = [
42
+ {
43
+ "role": "user",
44
+ "content": rendered_prompt_content
45
+ }
46
+ ]
47
+
48
+ # The model you specified in your Agent.py
49
+ model_name = "deepseek-ai/DeepSeek-R1"
50
+ max_retries = 3
51
+
52
+ print(f"Attempting to call model: {model_name}")
53
+ print(f"Prompt: '{rendered_prompt_content[:50]}...'")
54
+
55
+ for attempt in range(max_retries):
56
+ print(f"\n--- Attempt {attempt + 1}/{max_retries} ---")
57
+ try:
58
+ # Make the API call
59
+ result = llm.chat.completions.create(
60
+ model=model_name,
61
+ messages=messages,
62
+ max_tokens=200, # Keep max_tokens reasonable for testing
63
+ temperature=0.7,
64
+ # Explicitly specify the router if you know it, though usually not needed with token
65
+ # router="https://router.huggingface.co/hyperbolic/v1/" # This might be the actual endpoint you need
66
+ )
67
+ # If successful, print the result and break
68
+ print("API call successful!")
69
+ print("Response:", result.choices[0].message.content)
70
+ return
71
+
72
+ except HfHubHTTPError as e:
73
+ if e.response.status_code == 504:
74
+ print(f"Caught 504 Gateway Time-out on attempt {attempt + 1}.")
75
+ if attempt < max_retries - 1:
76
+ wait_time = 2 ** (attempt + 1) # Exponential backoff: 2, 4 seconds
77
+ print(f"Retrying in {wait_time} seconds...")
78
+ time.sleep(wait_time)
79
+ else:
80
+ print(f"Max retries ({max_retries}) reached. Still encountering 504.")
81
+ print("This indicates a persistent issue with the API or model availability.")
82
+ print(f"Full error: {e}")
83
+ return
84
+ else:
85
+ # Re-raise other HTTP errors
86
+ print(f"Caught unexpected HTTP error: {e.response.status_code} - {e.response.reason}")
87
+ print(f"Full error response: {e.response.text}")
88
+ raise
89
+ except Exception as e:
90
+ # Catch any other unexpected errors
91
+ print(f"An unexpected error occurred: {e}")
92
+ return
93
+
94
+ # Run the test
95
+ if __name__ == "__main__":
96
+ test_llm_timeout()