Skip to content

Commit f4b9b85

Browse files
AkhileshNegiAkhilesh Negi
andauthored
Response API: Support non vector store assistants (#242)
* check if vector store is linked or not * added testcases * cleanups * refactor --------- Co-authored-by: Akhilesh Negi <[email protected]>
1 parent e5d09e1 commit f4b9b85

File tree

2 files changed

+85
-10
lines changed

2 files changed

+85
-10
lines changed

backend/app/api/routes/responses.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,27 @@ def process_response(
106106
f"Starting generating response for assistant_id={request.assistant_id}, project_id={request.project_id}, organization_id={organization_id}"
107107
)
108108
try:
109-
response = client.responses.create(
110-
model=assistant.model,
111-
previous_response_id=request.response_id,
112-
instructions=assistant.instructions,
113-
tools=[
109+
# Create response with or without tools based on vector_store_id
110+
params = {
111+
"model": assistant.model,
112+
"previous_response_id": request.response_id,
113+
"instructions": assistant.instructions,
114+
"temperature": assistant.temperature,
115+
"input": [{"role": "user", "content": request.question}],
116+
}
117+
118+
if assistant.vector_store_id:
119+
params["tools"] = [
114120
{
115121
"type": "file_search",
116122
"vector_store_ids": [assistant.vector_store_id],
117123
"max_num_results": assistant.max_num_results,
118124
}
119-
],
120-
temperature=assistant.temperature,
121-
input=[{"role": "user", "content": request.question}],
122-
include=["file_search_call.results"],
123-
)
125+
]
126+
params["include"] = ["file_search_call.results"]
127+
128+
response = client.responses.create(**params)
129+
124130
response_chunks = get_file_search_results(response)
125131
logger.info(
126132
f"Successfully generated response: response_id={response.id}, assistant={request.assistant_id}, project_id={request.project_id}, organization_id={organization_id}"

backend/app/tests/api/routes/test_responses.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,72 @@ def test_responses_endpoint_success(
7070
assert response_json["success"] is True
7171
assert response_json["data"]["status"] == "processing"
7272
assert response_json["data"]["message"] == "Response creation started"
73+
74+
75+
@patch("app.api.routes.responses.OpenAI")
76+
@patch("app.api.routes.responses.get_provider_credential")
77+
@patch("app.api.routes.responses.get_assistant_by_id")
78+
def test_responses_endpoint_without_vector_store(
79+
mock_get_assistant,
80+
mock_get_credential,
81+
mock_openai,
82+
db,
83+
):
84+
"""Test the /responses endpoint when assistant has no vector store configured."""
85+
# Setup mock credentials
86+
mock_get_credential.return_value = {"api_key": "test_api_key"}
87+
88+
# Setup mock assistant without vector store
89+
mock_assistant = MagicMock()
90+
mock_assistant.model = "gpt-4"
91+
mock_assistant.instructions = "Test instructions"
92+
mock_assistant.temperature = 0.1
93+
mock_assistant.vector_store_id = None # No vector store configured
94+
mock_get_assistant.return_value = mock_assistant
95+
96+
# Setup mock OpenAI client
97+
mock_client = MagicMock()
98+
mock_openai.return_value = mock_client
99+
100+
# Setup the mock response object
101+
mock_response = MagicMock()
102+
mock_response.id = "mock_response_id"
103+
mock_response.output_text = "Test output"
104+
mock_response.model = "gpt-4"
105+
mock_response.usage.input_tokens = 10
106+
mock_response.usage.output_tokens = 5
107+
mock_response.usage.total_tokens = 15
108+
# No output attribute since there are no tool calls
109+
mock_client.responses.create.return_value = mock_response
110+
111+
# Get the Glific project ID
112+
glific_project = db.exec(select(Project).where(Project.name == "Glific")).first()
113+
if not glific_project:
114+
pytest.skip("Glific project not found in the database")
115+
116+
# Use the original API key from seed data
117+
original_api_key = "ApiKey No3x47A5qoIGhm0kVKjQ77dhCqEdWRIQZlEPzzzh7i8"
118+
119+
headers = {"X-API-KEY": original_api_key}
120+
request_data = {
121+
"project_id": glific_project.id,
122+
"assistant_id": "assistant_123",
123+
"question": "What is Glific?",
124+
"callback_url": "http://example.com/callback",
125+
}
126+
127+
response = client.post("/responses", json=request_data, headers=headers)
128+
assert response.status_code == 200
129+
response_json = response.json()
130+
assert response_json["success"] is True
131+
assert response_json["data"]["status"] == "processing"
132+
assert response_json["data"]["message"] == "Response creation started"
133+
134+
# Verify OpenAI client was called without tools
135+
mock_client.responses.create.assert_called_once_with(
136+
model=mock_assistant.model,
137+
previous_response_id=None,
138+
instructions=mock_assistant.instructions,
139+
temperature=mock_assistant.temperature,
140+
input=[{"role": "user", "content": "What is Glific?"}],
141+
)

0 commit comments

Comments
 (0)