Spaces:
Runtime error
Runtime error
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. | |
""" | |
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)) | |
async def root(): | |
return {"message": "Welcome to Debyez Chatbot API"} | |
async def health_check(): | |
return {"status": "healthy"} |