File size: 1,271 Bytes
7110ce1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f10e9d4
7110ce1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from dotenv import load_dotenv
from langchain_groq import ChatGroq
from langchain_core.prompts import ChatPromptTemplate
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel


load_dotenv() 


groq_api_key = os.getenv('GROQ_API_KEY')


llm_model = ChatGroq(
    groq_api_key=groq_api_key,
    model_name="Llama3-8b-8192"
)


app = FastAPI()


origins = ["*"]


app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


class textFromFrontendModel(BaseModel):
    textFromNextJSFrontend: str



@app.get('/')
def welcome():
    return {
        'success': True,
        'message': 'server of "fitbites is up and running successfully '
    }


@app.post('/generateanswer')
async def predict(incomingTextFromFrontend: textFromFrontendModel):

    prompt_text = incomingTextFromFrontend.textFromNextJSFrontend

    prompt_template = ChatPromptTemplate.from_template(
        """
        {text}
        """
    )

    chain = prompt_template | llm_model
    
    response_from_model = chain.invoke({"text": prompt_text})

    return {
        'success': True,
        'response_from_model': response_from_model
    }