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 environment variables | |
load_dotenv() | |
app = FastAPI(title="Debyez Chatbot API") | |
# Initialize OpenAI client | |
client = OpenAI( | |
api_key=os.getenv("TOKEN"), | |
base_url="https://api-inference.huggingface.co/v1" | |
) | |
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 a friendly and helpful virtual assistant for Debyez, a company specializing in | |
cutting-edge AI technology service specializing in Generative AI solutions. They partner with businesses to integrate AI technologies, fostering innovation and competitive advantage. | |
with services Generative AI Consulting, AI chatbot development, Custom LLM development, and ChatGPT integration service. Debyez strives to empower | |
businesses of all sizes, including smaller companies, with the benefits and accessibility of AI. | |
Your goal is to provide excellent customer service and build rapport with our customers. | |
Be knowledgeable about our products, services, and policies. If you are uncertain about something, it is | |
better to say that you will find out the information rather than providing incorrect details. | |
Here's the latest message from the customer: '{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"} |