Jaita commited on
Commit
7ff3496
·
verified ·
1 Parent(s): 16f19be

Update app/main.py

Browse files
Files changed (1) hide show
  1. app/main.py +30 -47
app/main.py CHANGED
@@ -5,7 +5,7 @@ from fastapi import FastAPI, HTTPException
5
  from fastapi.middleware.cors import CORSMiddleware
6
  from pydantic import BaseModel
7
  from dotenv import load_dotenv
8
- from kb_embed import search_knowledge_base
9
  import logging
10
 
11
 
@@ -21,14 +21,14 @@ load_dotenv()
21
  app = FastAPI()
22
 
23
  # --- 2. Configure CORS ---
24
- origins = [
25
- "http://localhost:5173",
26
- "http://localhost:3000",
27
- ]
28
 
29
  app.add_middleware(
30
  CORSMiddleware,
31
- allow_origins=origins,
32
  allow_credentials=True,
33
  allow_methods=["*"],
34
  allow_headers=["*"],
@@ -47,57 +47,40 @@ GEMINI_URL = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.
47
  async def health_check():
48
  return {"status": "ok"}
49
 
 
50
  @app.post("/chat")
51
- async def chat_with_gemini(input_data: ChatInput):
 
52
  try:
53
- # Retrieve relevant documents from knowledge base
54
-
55
- kb_results = search_knowledge_base(input_data.user_message, top_k=10)
56
- logging.info(f"kb_results are: {kb_results}")
57
-
58
- # Extract relevant context from search results
59
- context = ""
60
- if kb_results and kb_results.get('documents'):
61
- # Limit context to avoid token limits - take top 2 most relevant
62
- relevant_docs = kb_results['documents'][0][:2]
63
- context = "\n\n".join(relevant_docs)
64
-
65
- # Construct enhanced prompt with context
66
- if context:
67
- enhanced_prompt = f"""Use the following knowledge base context to answer the user's question accurately.
68
- If the context contains relevant information, base your answer on it.
69
- If the context doesn't help, say you're raising a ticket and provide a ticket number.
70
-
71
- Knowledge Base Context:
72
- {context}
73
-
74
- User Question: {input_data.user_message}
75
-
76
- Answer:"""
77
- else:
78
- enhanced_prompt = f"User Question: {input_data.user_message}\n\nAnswer:"
79
-
80
- headers = {"Content-Type": "application/json"}
81
  payload = {
82
  "contents": [
83
  {
84
- "parts": [{"text": enhanced_prompt}]
 
85
  }
86
- ]
 
 
 
 
87
  }
88
 
89
- response = requests.post(GEMINI_URL, headers=headers, json=payload, verify=False) # SSL disabled for testing
90
- result = response.json()
 
 
91
 
92
- # Extract Gemini's response
93
- bot_response = result["candidates"][0]["content"]["parts"][0]["text"]
 
 
 
 
 
94
 
95
- # Include debug info in response
96
- debug_info = f"Context found: {'Yes' if context else 'No'}"
97
- if context:
98
- debug_info += f" (Top {len(relevant_docs)} documents used)"
99
 
100
- return {"bot_response": bot_response, "debug": debug_info}
101
 
102
  except Exception as e:
103
- raise HTTPException(status_code=500, detail=str(e))
 
5
  from fastapi.middleware.cors import CORSMiddleware
6
  from pydantic import BaseModel
7
  from dotenv import load_dotenv
8
+ #from kb_embed import search_knowledge_base
9
  import logging
10
 
11
 
 
21
  app = FastAPI()
22
 
23
  # --- 2. Configure CORS ---
24
+ #origins = [
25
+ # "http://localhost:5173",
26
+ # "http://localhost:3000",
27
+ #]
28
 
29
  app.add_middleware(
30
  CORSMiddleware,
31
+ allow_origins=["*"],
32
  allow_credentials=True,
33
  allow_methods=["*"],
34
  allow_headers=["*"],
 
47
  async def health_check():
48
  return {"status": "ok"}
49
 
50
+
51
  @app.post("/chat")
52
+ async def chat_with_ai(input_data: ChatInput):
53
+ """Handle chat interactions using Google Generative AI via requests."""
54
  try:
55
+ # Gemini expects contents array with role and parts
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  payload = {
57
  "contents": [
58
  {
59
+ "role": "user",
60
+ "parts": [{"text": input_data.user_message}],
61
  }
62
+ ],
63
+ #"generationConfig": {
64
+ # "temperature": 0.7,
65
+ # "maxOutputTokens": 512
66
+ #}
67
  }
68
 
69
+ # Make POST request to Gemini API
70
+ response = requests.post(GEMINI_URL, json=payload,verify=False)
71
+ response.raise_for_status()
72
+ data = response.json()
73
 
74
+ # Extract text from response
75
+ bot_response = ""
76
+ if "candidates" in data and data["candidates"]:
77
+ parts = data["candidates"][0].get("content", {}).get("parts", [])
78
+ for part in parts:
79
+ if "text" in part:
80
+ bot_response += part["text"]
81
 
82
+ return {"bot_response": bot_response or "No response text."}
 
 
 
83
 
 
84
 
85
  except Exception as e:
86
+ raise HTTPException(status_code=500, detail=str(e))