File size: 1,330 Bytes
5943c56
9a6b3b4
 
 
 
 
 
 
 
 
 
 
 
86dc7b3
 
 
 
9a6b3b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Optional
from huggingface_hub import InferenceClient
import uvicorn

app = FastAPI()
client = InferenceClient("scb10x/llama-3-typhoon-v1.5-8b-instruct")

class Query(BaseModel):
    queryResult: Optional[dict] = None
    queryText: Optional[str] = None

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

@app.post("/webhook")
async def webhook(query: Query):
    try:
        user_query = query.queryResult.get('queryText') if query.queryResult else query.queryText
        
        if not user_query:
            raise HTTPException(status_code=400, detail="No query text provided")
        
        messages = [
            {"role": "system", "content": "You are a friendly Chatbot."},
            {"role": "user", "content": user_query}
        ]
        
        response = client.chat_completion(
            messages,
            max_tokens=512,
            temperature=0.7,
            top_p=0.95,
        )
        
        model_reply = response.choices[0].message.content.strip()
        
        return {"fulfillmentText": model_reply}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)