sagar008 commited on
Commit
7ea8d7c
·
verified ·
1 Parent(s): bd4e7fa

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +39 -13
main.py CHANGED
@@ -82,29 +82,55 @@ async def analyze_document(data: AnalyzeDocumentInput):
82
  return {"error": str(e)}
83
 
84
  async def generate_response_with_context(user_question: str, relevant_context: str, document_id: str):
85
- """Send relevant chunks to Gemini for response generation"""
86
- try:
87
- prompt = f"""You are a legal document assistant. Answer the user's question based ONLY on the provided context from their legal document.
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
- Context from document {document_id}:
90
  {relevant_context}
91
 
92
- User Question: {user_question}
93
 
94
- Instructions:
95
- - Provide a clear, accurate answer based on the context above
96
- - If the answer isn't in the context, say "I cannot find information about this in the provided document"
97
- - Include specific quotes from the document when relevant
98
- - Keep your answer focused on legal implications and key details
99
 
100
  Answer:"""
101
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  model = genai.GenerativeModel('gemini-1.5-flash')
103
  response = model.generate_content(prompt)
104
  return response.text
105
-
106
  except Exception as e:
107
- return f"Error generating response: {str(e)}"
108
 
109
  @app.post("/chat")
110
  async def chat_with_document(data: ChatInput):
 
82
  return {"error": str(e)}
83
 
84
  async def generate_response_with_context(user_question: str, relevant_context: str, document_id: str):
85
+ """Let Gemini handle intent classification for better flexibility"""
86
+
87
+ user_question_lower = user_question.lower().strip()
88
+
89
+ # Only check for very simple conversational patterns
90
+ simple_conversational = user_question_lower in ["thank you", "thanks", "hello", "hi", "bye", "ok", "okay"]
91
+
92
+ if simple_conversational:
93
+ return "You're welcome! I'm here to help you understand your legal document. Feel free to ask about any terms, risks, or clauses you'd like clarified."
94
+
95
+ # Check if context is available
96
+ has_relevant_context = relevant_context and len(relevant_context.strip()) > 30
97
+
98
+ if has_relevant_context:
99
+ prompt = f"""You are NyayAI, a legal document analysis assistant.
100
 
101
+ DOCUMENT CONTEXT:
102
  {relevant_context}
103
 
104
+ USER QUESTION: "{user_question}"
105
 
106
+ RESPONSE RULES:
107
+ 1. If the question is related to the document content above: Answer using ONLY the provided context
108
+ 2. If the question is about legal documents but the context doesn't contain that information: Say "I don't see information about that specific topic in this document"
109
+ 3. If the question is completely unrelated to legal documents (like weather, cooking, sports, etc.): Say "Please ask me questions related to your legal document analysis only"
110
+ 4. If you're unsure whether it's document-related: Ask for clarification about which aspect of the document they want to know about
111
 
112
  Answer:"""
113
+
114
+ else:
115
+ # No context available - let Gemini decide if it's document-related
116
+ prompt = f"""You are NyayAI, a legal document analysis assistant.
117
+
118
+ USER QUESTION: "{user_question}"
119
+
120
+ The user is asking about a legal document, but I don't have relevant information about their specific question in the document analysis.
121
+
122
+ RESPONSE RULES:
123
+ 1. If the question seems related to legal document analysis: Say "I don't see information about that specific topic in this document. Please ask about the terms, clauses, risks, or other content from the analysis."
124
+ 2. If the question is completely unrelated to legal documents: Say "Please ask me questions related to your legal document analysis only"
125
+
126
+ Answer:"""
127
+
128
+ try:
129
  model = genai.GenerativeModel('gemini-1.5-flash')
130
  response = model.generate_content(prompt)
131
  return response.text
 
132
  except Exception as e:
133
+ return "I encountered an error processing your question. Please try asking about your document analysis again."
134
 
135
  @app.post("/chat")
136
  async def chat_with_document(data: ChatInput):