File size: 2,890 Bytes
8550cf4
 
127e0c7
8550cf4
127e0c7
cfed78a
127e0c7
73e4535
127e0c7
 
8550cf4
285a245
8550cf4
 
 
 
 
285a245
8550cf4
 
 
285a245
8550cf4
 
285a245
8550cf4
 
285a245
8550cf4
 
f898eb9
 
 
 
 
8550cf4
 
285a245
8550cf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
285a245
cfed78a
 
 
 
8550cf4
 
 
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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import os
from openai import OpenAI
from dotenv import load_dotenv
from typing import List


load_dotenv()

app = FastAPI(title="Debyez Chatbot API")

# Initialize OpenAI client
client = OpenAI(
    base_url="https://api-inference.huggingface.co/v1",
    api_key=os.getenv("TOKEN")
)

class Message(BaseModel):
    role: str
    content: str

class ChatRequest(BaseModel):
    messages: List[Message]

class ChatResponse(BaseModel):
    response: str

def get_debyez_prompt_template(customer_message: str) -> str:
    return f"""
    You are an AI-powered technical assistant integrated into an Interactive Electronic Technical Manual (IETM) Level 4, designed for the operators and maintenance personnel of an advanced underwater vehicle. Your primary role is to provide precise, context-aware guidance on setup, operation, troubleshooting, and maintenance, strictly based on the structured S1000D-compliant technical manual. You should deliver step-by-step procedures, safety precautions, part specifications, and diagnostic recommendations while avoiding hallucinations or speculative answers.

If a user asks about fault diagnosis, provide structured troubleshooting steps along with possible causes and solutions. For operational queries, guide them through detailed procedures, ensuring adherence to safety protocols. When discussing maintenance, offer insights into periodic servicing schedules, component replacement guidelines, and regulatory compliance. If a query requires additional reference material, retrieve relevant sections from the technical manual and cite the exact source within the IETM.

Maintain a professional, concise, and user-friendly tone, ensuring clarity for both novice and experienced personnel. If a query falls outside the scope of the manual, politely redirect the user to appropriate human support or official documentation.: '{customer_message}'
    Respond in a way that is warm, professional, and relevant to the customer's needs.
    """

@app.post("/chat", response_model=ChatResponse)
async def chat_endpoint(request: ChatRequest):
    try:
        formatted_messages = [
            {"role": msg.role, "content": get_debyez_prompt_template(msg.content)}
            for msg in request.messages
        ]
        
        completion = client.chat.completions.create(
            model="meta-llama/Meta-Llama-3-8B-Instruct",
            messages=formatted_messages,
            temperature=0.5,
            max_tokens=3000,
        )
        
        return ChatResponse(response=completion.choices[0].message.content)
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/")
async def root():
    return {"message": "Welcome to Debyez Chatbot API"}

@app.get("/health")
async def health_check():
    return {"status": "healthy"}