File size: 2,834 Bytes
127e0c7
285a245
127e0c7
285a245
 
127e0c7
09ec474
127e0c7
 
285a245
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127e0c7
285a245
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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()