I’m really new to programming and I’m trying to implement Llama 3.1 8B into my Python scirpt, but for some reason the error in the title pops up.
Because I didn’t find any documentation on how to use the huggingface API:s, I used ai to create the following script which takes input from the user which the Llama model should answer:
<code>import os
import requests
from dotenv import load_dotenv, find_dotenv
from time import sleep
# Load environment variables from .env file
dotenv_path = find_dotenv()
if not dotenv_path:
raise FileNotFoundError(".env file not found. Please ensure it is located in the project root.")
load_dotenv(dotenv_path)
# Get API key from environment variable
api_key = os.getenv('HF_API_KEY')
# Check if API key is available
if api_key is None:
raise ValueError("API key not found. Please set it in the .env file.")
# Correct Hugging Face API URL for the specified model
api_url = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3.1-8B-Instruct"
# Function to get response from the model with retry logic
def get_model_response(prompt, retries=3, delay=5):
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
data = {
"inputs": prompt
}
for attempt in range(retries):
try:
response = requests.post(api_url, headers=headers, json=data)
response.raise_for_status() # Raise an exception for HTTP errors
return response.json()
except requests.exceptions.RequestException as e:
print(f"Request error: {e}")
if attempt < retries - 1: # If not the last attempt
print(f"Retrying in {delay} seconds...")
sleep(delay)
else:
raise # Re-raise the exception if all retries failed
# Main function
def main():
print("Welcome to the SizeBetweenGame!")
user_input = input("Enter your prompt: ")
try:
response = get_model_response(user_input)
# Print the full response for debugging
print("Full Response:", response)
# Extract and print the response text if available
response_text = response.get('choices', [{}])[0].get('text', 'No response text')
print("Model Response:", response_text)
except requests.exceptions.RequestException as e:
print(f"An unexpected error occurred: {e}")
if __name__ == "__main__":
main()
</code>
<code>import os
import requests
from dotenv import load_dotenv, find_dotenv
from time import sleep
# Load environment variables from .env file
dotenv_path = find_dotenv()
if not dotenv_path:
raise FileNotFoundError(".env file not found. Please ensure it is located in the project root.")
load_dotenv(dotenv_path)
# Get API key from environment variable
api_key = os.getenv('HF_API_KEY')
# Check if API key is available
if api_key is None:
raise ValueError("API key not found. Please set it in the .env file.")
# Correct Hugging Face API URL for the specified model
api_url = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3.1-8B-Instruct"
# Function to get response from the model with retry logic
def get_model_response(prompt, retries=3, delay=5):
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
data = {
"inputs": prompt
}
for attempt in range(retries):
try:
response = requests.post(api_url, headers=headers, json=data)
response.raise_for_status() # Raise an exception for HTTP errors
return response.json()
except requests.exceptions.RequestException as e:
print(f"Request error: {e}")
if attempt < retries - 1: # If not the last attempt
print(f"Retrying in {delay} seconds...")
sleep(delay)
else:
raise # Re-raise the exception if all retries failed
# Main function
def main():
print("Welcome to the SizeBetweenGame!")
user_input = input("Enter your prompt: ")
try:
response = get_model_response(user_input)
# Print the full response for debugging
print("Full Response:", response)
# Extract and print the response text if available
response_text = response.get('choices', [{}])[0].get('text', 'No response text')
print("Model Response:", response_text)
except requests.exceptions.RequestException as e:
print(f"An unexpected error occurred: {e}")
if __name__ == "__main__":
main()
</code>
import os
import requests
from dotenv import load_dotenv, find_dotenv
from time import sleep
# Load environment variables from .env file
dotenv_path = find_dotenv()
if not dotenv_path:
raise FileNotFoundError(".env file not found. Please ensure it is located in the project root.")
load_dotenv(dotenv_path)
# Get API key from environment variable
api_key = os.getenv('HF_API_KEY')
# Check if API key is available
if api_key is None:
raise ValueError("API key not found. Please set it in the .env file.")
# Correct Hugging Face API URL for the specified model
api_url = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3.1-8B-Instruct"
# Function to get response from the model with retry logic
def get_model_response(prompt, retries=3, delay=5):
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
data = {
"inputs": prompt
}
for attempt in range(retries):
try:
response = requests.post(api_url, headers=headers, json=data)
response.raise_for_status() # Raise an exception for HTTP errors
return response.json()
except requests.exceptions.RequestException as e:
print(f"Request error: {e}")
if attempt < retries - 1: # If not the last attempt
print(f"Retrying in {delay} seconds...")
sleep(delay)
else:
raise # Re-raise the exception if all retries failed
# Main function
def main():
print("Welcome to the SizeBetweenGame!")
user_input = input("Enter your prompt: ")
try:
response = get_model_response(user_input)
# Print the full response for debugging
print("Full Response:", response)
# Extract and print the response text if available
response_text = response.get('choices', [{}])[0].get('text', 'No response text')
print("Model Response:", response_text)
except requests.exceptions.RequestException as e:
print(f"An unexpected error occurred: {e}")
if __name__ == "__main__":
main()
New contributor
user26527821 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.