Spaces:
Runtime error
Runtime error
import os | |
import requests | |
from dotenv import load_dotenv | |
from typing import List, Dict | |
import json | |
# Load environment variables (e.g., API keys) | |
load_dotenv() | |
class CSLAssistant: | |
def __init__(self): | |
self.api_token = os.getenv("TOKEN") | |
self.api_url = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct" | |
self.headers = {"Authorization": f"Bearer {self.api_token}"} | |
def get_system_prompt(self) -> str: | |
return """ | |
Identity & Purpose: | |
You are CSL Assistant, the official AI assistant for Cochin Shipyard Limited. | |
You specialize in providing information about shipbuilding, marine engineering, | |
and Cochin Shipyard's capabilities and services. | |
Guidelines: | |
1. Provide accurate information about CSL's shipbuilding capabilities | |
2. Be professional and courteous in all interactions | |
3. If unsure about specific details, acknowledge limitations | |
4. Focus on publicly available information about CSL | |
""" | |
def get_prompt_template(self, customer_message: str) -> str: | |
return f"{self.get_system_prompt()}\n\nCustomer Query: {customer_message}" | |
def get_response(self, prompt: str) -> str: | |
try: | |
# Prepare the payload | |
payload = { | |
"inputs": self.get_prompt_template(prompt), | |
"parameters": { | |
"max_new_tokens": 3000, | |
"temperature": 0.5, | |
"return_full_text": False | |
} | |
} | |
# Make the API request | |
response = requests.post( | |
self.api_url, | |
headers=self.headers, | |
json=payload | |
) | |
# Check if the request was successful | |
response.raise_for_status() | |
# Parse the response | |
result = response.json() | |
if isinstance(result, list) and len(result) > 0: | |
return result[0].get('generated_text', '').strip() | |
return "Sorry, I couldn't generate a response right now." | |
except requests.exceptions.RequestException as e: | |
return f"API Request Error: {str(e)}" | |
except json.JSONDecodeError: | |
return "Error: Invalid response from the API" | |
except Exception as e: | |
return f"An unexpected error occurred: {str(e)}" | |
def main(): | |
assistant = CSLAssistant() | |
# Example queries | |
test_queries = [ | |
"What types of ships do you build?", | |
"What is your shipyard's capacity?", | |
"Tell me about your recent projects" | |
] | |
for query in test_queries: | |
print(f"\nQuery: {query}") | |
print("Response:", assistant.get_response(query)) | |
if __name__ == "__main__": | |
main() |