Skip to content

Commit fef2899

Browse files
committed
Improve logging, documentation, and response handling in assistant views and settings configuration
1 parent 3342224 commit fef2899

File tree

2 files changed

+167
-102
lines changed

2 files changed

+167
-102
lines changed

server/api/views/assistant/views.py

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def invoke_functions_from_response(
7373
}
7474
)
7575
elif response_item.type == "reasoning":
76-
logger.debug("Reasoning step")
76+
logger.debug(f"Reasoning step: {response_item.summary}")
7777
return intermediate_messages
7878

7979

@@ -88,15 +88,16 @@ def post(self, request):
8888
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
8989

9090
TOOL_DESCRIPTION = """
91-
Search through the user's uploaded documents using semantic similarity matching.
92-
This function finds the most relevant document chunks based on the input query and
93-
returns contextual information including page numbers, chunk locations, and similarity scores.
94-
Use this to answer the user's questions.
91+
Search the user's uploaded documents for information relevant to answering their question.
92+
Call this function when you need to find specific information from the user's documents
93+
to provide an accurate, citation-backed response. Always search before answering questions
94+
about document content.
9595
"""
9696

9797
TOOL_PROPERTY_DESCRIPTION = """
98-
The search query to find semantically similar content in uploaded documents.
99-
Should be a natural language question or keyword phrase.
98+
A specific search query to find relevant information in the user's documents.
99+
Use keywords, phrases, or questions related to what the user is asking about.
100+
Be specific rather than generic - use terms that would appear in the relevant documents.
100101
"""
101102

102103
tools = [
@@ -119,7 +120,7 @@ def post(self, request):
119120

120121
def search_documents(query: str, user=user) -> str:
121122
"""
122-
Search through user's uploaded documents using semantic similarity.
123+
Search through user's uploaded documents using semantic similarity.
123124
124125
This function performs vector similarity search against the user's document corpus
125126
and returns formatted results with context information for the LLM to use.
@@ -162,25 +163,58 @@ def search_documents(query: str, user=user) -> str:
162163
except Exception as e:
163164
return f"Error searching documents: {str(e)}. Please try again if the issue persists."
164165

166+
INSTRUCTIONS = """
167+
You are an AI assistant that helps users find and understand information about bipolar disorder
168+
from their uploaded bipolar disorder research documents using semantic search.
169+
170+
SEMANTIC SEARCH STRATEGY:
171+
- Always perform semantic search using the search_documents function when users ask questions
172+
- Use conceptually related terms and synonyms, not just exact keyword matches
173+
- Search for the meaning and context of the user's question, not just literal words
174+
- Consider medical terminology, lay terms, and related conditions when searching
175+
176+
FUNCTION USAGE:
177+
- When a user asks about information that might be in their documents ALWAYS use the search_documents function first
178+
- Perform semantic searches using concepts, symptoms, treatments, and related terms from the user's question
179+
- Only provide answers based on information found through document searches
180+
181+
RESPONSE FORMAT:
182+
After gathering information through semantic searches, provide responses that:
183+
1. Answer the user's question directly using only the found information
184+
2. Structure responses with clear sections and paragraphs
185+
3. Include citations after EACH sentence using this exact format: ***[File ID {file_id}, Page {page_number}, Chunk {chunk_number}]***
186+
4. Only cite information that directly supports your statements
187+
188+
If no relevant information is found in the documents, clearly state that the information is not available in the uploaded documents.
189+
"""
190+
165191
MODEL_DEFAULTS = {
192+
"instructions": INSTRUCTIONS,
166193
"model": "gpt-5-nano", # 400,000 token context window
167-
"reasoning": {"effort": "medium"},
194+
"reasoning": {"effort": "medium", "summary": "auto"},
168195
"tools": tools,
169196
}
170197

171198
# We fetch a response and then kick off a loop to handle the response
172199

173-
request_data = request.data.get("message", None)
174-
if not request_data:
175-
return Response(
176-
{"error": "Message data is required."},
177-
status=status.HTTP_400_BAD_REQUEST,
178-
)
179-
message = str(request_data)
200+
message = request.data.get("message", None)
201+
previous_response_id = request.data.get("previous_response_id", None)
180202

181-
response = client.responses.create(
182-
input=[{"type": "text", "text": message}], **MODEL_DEFAULTS
183-
)
203+
if not previous_response_id:
204+
response = client.responses.create(
205+
input=[
206+
{"type": "message", "role": "user", "content": str(message)}
207+
],
208+
**MODEL_DEFAULTS,
209+
)
210+
else:
211+
response = client.responses.create(
212+
input=[
213+
{"type": "message", "role": "user", "content": str(message)}
214+
],
215+
previous_response_id=str(previous_response_id),
216+
**MODEL_DEFAULTS,
217+
)
184218

185219
# Open AI Cookbook: Handling Function Calls with Reasoning Models
186220
# https://cookbook.openai.com/examples/reasoning_function_calls
@@ -190,10 +224,11 @@ def search_documents(query: str, user=user) -> str:
190224
response, tool_mapping={"search_documents": search_documents}
191225
)
192226
if len(function_responses) == 0: # We're done reasoning
193-
logger.info(f"Reasoning completed for user {user.id}")
194-
final_response = response.output_text
227+
logger.info("Reasoning completed")
228+
final_response_output_text = response.output_text
229+
final_response_id = response.id
195230
logger.debug(
196-
f"Final response length: {len(final_response)} characters"
231+
f"Final response length: {len(final_response_output_text)} characters"
197232
)
198233
break
199234
else:
@@ -204,7 +239,13 @@ def search_documents(query: str, user=user) -> str:
204239
**MODEL_DEFAULTS,
205240
)
206241

207-
return Response({"response": final_response}, status=status.HTTP_200_OK)
242+
return Response(
243+
{
244+
"response_output_text": final_response_output_text,
245+
"final_response_id": final_response_id,
246+
},
247+
status=status.HTTP_200_OK,
248+
)
208249

209250
except Exception as e:
210251
logger.error(

0 commit comments

Comments
 (0)