-
Notifications
You must be signed in to change notification settings - Fork 10
OpenAI Conversation: Lookup #297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
77619b5
aeebc1f
a2b001c
42d1d2c
7df16e8
9432e0d
c02b35d
10ccbb3
bafa85c
5e8ab64
29e7655
08eadcd
6a5333d
c40a8e9
468a812
1fd09b5
751dbe5
e69cc32
a796c3c
6b7b559
afb0ce7
947546e
bb5308a
1aaf4f1
db0d2f7
e5234a7
dcb40b7
adc41c3
f8e38e5
0df228a
aeb224c
3ef0162
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,7 +11,12 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||
| from app.api.routes.threads import send_callback | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| from app.crud.assistants import get_assistant_by_id | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| from app.crud.credentials import get_provider_credential | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| from app.models import UserProjectOrg | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| from app.crud.openai_conversation import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| create_conversation, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| get_ancestor_id_from_response, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| get_conversation_by_ancestor_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| from app.models import UserProjectOrg, OpenAIConversationCreate | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| from app.utils import APIResponse, mask_string | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| from app.core.langfuse.langfuse import LangfuseTracer | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -21,8 +26,20 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| def handle_openai_error(e: openai.OpenAIError) -> str: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Extract error message from OpenAI error.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if isinstance(e.body, dict) and "message" in e.body: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Try to get error message from different possible attributes | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if hasattr(e, "body") and isinstance(e.body, dict) and "message" in e.body: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return e.body["message"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| elif hasattr(e, "message"): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return e.message | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| elif hasattr(e, "response") and hasattr(e.response, "json"): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| error_data = e.response.json() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if isinstance(error_data, dict) and "error" in error_data: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| error_info = error_data["error"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if isinstance(error_info, dict) and "message" in error_info: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return error_info["message"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| except: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| pass | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return str(e) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -98,6 +115,8 @@ def process_response( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| assistant, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| tracer: LangfuseTracer, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| project_id: int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| organization_id: int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| session: Session, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Process a response and send callback with results, with Langfuse tracing.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.info( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -117,9 +136,21 @@ def process_response( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Get the latest conversation by ancestor ID to use as previous_response_id | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ancestor_id = request.response_id | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| latest_conversation = None | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ancestor_id: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| latest_conversation = get_conversation_by_ancestor_id( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| session=session, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ancestor_response_id=ancestor_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| project_id=project_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if latest_conversation: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
avirajsingh7 marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| ancestor_id = latest_conversation.response_id | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+148
to
+149
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential logic error in ancestor_id reassignment. Line 149 reassigns Apply this fix: - if latest_conversation:
- ancestor_id = latest_conversation.response_id
+ # ancestor_id should remain as request.response_id (the root)
+ # latest_conversation.response_id will be used as previous_response_id insteadThen update line 153 to use the correct previous response ID: - "previous_response_id": ancestor_id,
+ "previous_response_id": latest_conversation.response_id if latest_conversation else ancestor_id,📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| params = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "model": assistant.model, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "previous_response_id": request.response_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "previous_response_id": ancestor_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "instructions": assistant.instructions, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "temperature": assistant.temperature, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "input": [{"role": "user", "content": request.question}], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -165,6 +196,35 @@ def process_response( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| "error": None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Set ancestor_response_id using CRUD function | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ancestor_response_id = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| latest_conversation.ancestor_response_id | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if latest_conversation | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| else get_ancestor_id_from_response( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
avirajsingh7 marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| session=session, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| current_response_id=response.id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| previous_response_id=response.previous_response_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| project_id=project_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Create conversation record in database | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| conversation_data = OpenAIConversationCreate( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| response_id=response.id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| previous_response_id=response.previous_response_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ancestor_response_id=ancestor_response_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| user_question=request.question, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| response=response.output_text, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| model=response.model, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| assistant_id=request.assistant_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| create_conversation( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| session=session, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| conversation=conversation_data, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| project_id=project_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| organization_id=organization_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| request_dict = request.model_dump() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| callback_response = ResponsesAPIResponse.success_response( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -264,6 +324,8 @@ async def responses( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| assistant, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| tracer, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| project_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| organization_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| _session, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.info( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improve exception handling specificity
The enhanced error message extraction logic is good, but the bare
exceptclause should be more specific to avoid catching system exceptions.if isinstance(error_info, dict) and "message" in error_info: return error_info["message"] - except: + except (AttributeError, TypeError, ValueError): pass📝 Committable suggestion
🧰 Tools
🪛 Ruff (0.12.2)
40-40: Do not use bare
except(E722)
🤖 Prompt for AI Agents