File size: 1,110 Bytes
a28cb0e
 
 
b80db49
a28cb0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b80db49
 
a28cb0e
 
02524d8
 
 
a28cb0e
 
b80db49
 
a28cb0e
b80db49
a28cb0e
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
import os
from fastapi import FastAPI, HTTPException
import google.generativeai as genai
from pydantic import BaseModel

genai.configure(api_key="AIzaSyDcYyq3w21iwipYn17wCAQo3AYWhUIGDSI")

# Load system instruction from file
with open("prompt.txt", "r") as file:
    system_instruction = file.read()

# Create the model
generation_config = {
    "temperature": 1,
    "top_p": 0.95,
    "top_k": 40,
    "max_output_tokens": 8192,
    "response_mime_type": "text/plain",
}

model = genai.GenerativeModel(
    model_name="gemini-2.0-flash",
    generation_config=generation_config,
    system_instruction=system_instruction,
)

# Start a new chat session
chat_session = model.start_chat(history=[])
class ChatRequest(BaseModel):
    user_input: str

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

@app.post("/chat/")
def chat_with_bot(request: ChatRequest):
    if not request.user_input:
        raise HTTPException(status_code=400, detail="Input cannot be empty")
    response = chat_session.send_message(request.user_input)
    return {"response": response.text}