Skip to content

Commit 3444ca6

Browse files
style: ruff autofix (auto)
1 parent fcc365d commit 3444ca6

5 files changed

Lines changed: 75 additions & 31 deletions

File tree

src/api/v1/chat.py

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Provides chat functionality with streaming support and conversation history.
55
Uses API key authentication. Routes through Langflow (chat_service.langflow_chat).
66
"""
7+
78
import json
89
from typing import Any
910

@@ -38,13 +39,15 @@ def _extract_sources(item: dict) -> list[dict]:
3839
sources = []
3940
for result in item.get("results", []):
4041
if isinstance(result, dict) and "text" in result:
41-
sources.append({
42-
"filename": result.get("filename", ""),
43-
"text": result.get("text", ""),
44-
"score": result.get("score", 0),
45-
"page": result.get("page"),
46-
"mimetype": result.get("mimetype"),
47-
})
42+
sources.append(
43+
{
44+
"filename": result.get("filename", ""),
45+
"text": result.get("text", ""),
46+
"score": result.get("score", 0),
47+
"page": result.get("page"),
48+
"mimetype": result.get("mimetype"),
49+
}
50+
)
4851
return sources
4952

5053

@@ -121,6 +124,7 @@ async def chat_create_endpoint(
121124
jwt_token = user.jwt_token
122125
if body.chat_id:
123126
from api.chat import _assert_owns
127+
124128
await _assert_owns(body.chat_id, storage_user_id)
125129

126130
if body.filters:
@@ -146,7 +150,11 @@ async def chat_create_endpoint(
146150
return StreamingResponse(
147151
_transform_stream_to_sse(raw_stream, chat_id_container),
148152
media_type="text/event-stream",
149-
headers={"Cache-Control": "no-cache", "Connection": "keep-alive", "X-Accel-Buffering": "no"},
153+
headers={
154+
"Cache-Control": "no-cache",
155+
"Connection": "keep-alive",
156+
"X-Accel-Buffering": "no",
157+
},
150158
)
151159
else:
152160
result = await chat_service.langflow_chat(
@@ -161,11 +169,13 @@ async def chat_create_endpoint(
161169
owner_email=user.email,
162170
storage_user_id=storage_user_id,
163171
)
164-
return JSONResponse({
165-
"response": result.get("response", ""),
166-
"chat_id": result.get("response_id"),
167-
"sources": result.get("sources", []),
168-
})
172+
return JSONResponse(
173+
{
174+
"response": result.get("response", ""),
175+
"chat_id": result.get("response_id"),
176+
"sources": result.get("sources", []),
177+
}
178+
)
169179

170180

171181
async def chat_list_endpoint(
@@ -218,20 +228,28 @@ async def chat_get_endpoint(
218228
"timestamp": msg.get("timestamp"),
219229
}
220230
# Include token usage if available (from Responses API)
221-
usage = msg.get("response_data", {}).get("usage") if isinstance(msg.get("response_data"), dict) else None
231+
usage = (
232+
msg.get("response_data", {}).get("usage")
233+
if isinstance(msg.get("response_data"), dict)
234+
else None
235+
)
222236
if usage:
223237
message_data["usage"] = usage
224238
messages.append(message_data)
225239

226-
return JSONResponse({
227-
"chat_id": conversation.get("response_id"),
228-
"title": conversation.get("title", ""),
229-
"created_at": conversation.get("created_at"),
230-
"last_activity": conversation.get("last_activity"),
231-
"messages": messages,
232-
})
240+
return JSONResponse(
241+
{
242+
"chat_id": conversation.get("response_id"),
243+
"title": conversation.get("title", ""),
244+
"created_at": conversation.get("created_at"),
245+
"last_activity": conversation.get("last_activity"),
246+
"messages": messages,
247+
}
248+
)
233249
except Exception as e:
234-
logger.error("Failed to get conversation", error=str(e), user_id=user.user_id, chat_id=chat_id)
250+
logger.error(
251+
"Failed to get conversation", error=str(e), user_id=user.user_id, chat_id=chat_id
252+
)
235253
return JSONResponse({"error": f"Failed to get conversation: {str(e)}"}, status_code=500)
236254

237255

@@ -243,6 +261,7 @@ async def chat_delete_endpoint(
243261
"""Delete a conversation. DELETE /v1/chat/{chat_id}"""
244262
try:
245263
from api.chat import _assert_owns
264+
246265
storage_user_id = _openrag_user_id(user)
247266
await _assert_owns(chat_id, storage_user_id)
248267
result = await chat_service.delete_session(storage_user_id, chat_id)
@@ -258,5 +277,7 @@ async def chat_delete_endpoint(
258277
except HTTPException:
259278
raise
260279
except Exception as e:
261-
logger.error("Failed to delete conversation", error=str(e), user_id=user.user_id, chat_id=chat_id)
280+
logger.error(
281+
"Failed to delete conversation", error=str(e), user_id=user.user_id, chat_id=chat_id
282+
)
262283
return JSONResponse({"error": f"Failed to delete conversation: {str(e)}"}, status_code=500)

src/api/v1/knowledge_filters.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Uses API key authentication — delegates to the main api/knowledge_filter.py handlers
66
but overrides the user dependency to use API keys.
77
"""
8+
89
from fastapi import Depends
910

1011
from api import knowledge_filter

src/api/v1/models.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Lists available LLM and embedding models per provider.
55
Uses API key authentication. Uses stored credentials from config.
66
"""
7+
78
from fastapi import Depends
89
from fastapi.responses import JSONResponse
910

@@ -22,21 +23,27 @@ async def _fetch_models(provider, config, models_service):
2223
if provider == "openai":
2324
api_key = config.providers.openai.api_key
2425
if not api_key:
25-
return None, JSONResponse({"error": "OpenAI API key not configured. Set it in Settings."}, status_code=400)
26+
return None, JSONResponse(
27+
{"error": "OpenAI API key not configured. Set it in Settings."}, status_code=400
28+
)
2629
models = await models_service.get_openai_models(api_key=api_key)
2730
return models, None
2831

2932
if provider == "anthropic":
3033
api_key = config.providers.anthropic.api_key
3134
if not api_key:
32-
return None, JSONResponse({"error": "Anthropic API key not configured. Set it in Settings."}, status_code=400)
35+
return None, JSONResponse(
36+
{"error": "Anthropic API key not configured. Set it in Settings."}, status_code=400
37+
)
3338
models = await models_service.get_anthropic_models(api_key=api_key)
3439
return models, None
3540

3641
if provider == "ollama":
3742
endpoint = config.providers.ollama.endpoint
3843
if not endpoint:
39-
return None, JSONResponse({"error": "Ollama endpoint not configured. Set it in Settings."}, status_code=400)
44+
return None, JSONResponse(
45+
{"error": "Ollama endpoint not configured. Set it in Settings."}, status_code=400
46+
)
4047
models = await models_service.get_ollama_models(endpoint=endpoint)
4148
return models, None
4249

@@ -45,12 +52,20 @@ async def _fetch_models(provider, config, models_service):
4552
endpoint = config.providers.watsonx.endpoint
4653
project_id = config.providers.watsonx.project_id
4754
if not api_key:
48-
return None, JSONResponse({"error": "WatsonX API key not configured. Set it in Settings."}, status_code=400)
55+
return None, JSONResponse(
56+
{"error": "WatsonX API key not configured. Set it in Settings."}, status_code=400
57+
)
4958
if not endpoint:
50-
return None, JSONResponse({"error": "WatsonX endpoint not configured. Set it in Settings."}, status_code=400)
59+
return None, JSONResponse(
60+
{"error": "WatsonX endpoint not configured. Set it in Settings."}, status_code=400
61+
)
5162
if not project_id:
52-
return None, JSONResponse({"error": "WatsonX project ID not configured. Set it in Settings."}, status_code=400)
53-
models = await models_service.get_ibm_models(endpoint=endpoint, api_key=api_key, project_id=project_id)
63+
return None, JSONResponse(
64+
{"error": "WatsonX project ID not configured. Set it in Settings."}, status_code=400
65+
)
66+
models = await models_service.get_ibm_models(
67+
endpoint=endpoint, api_key=api_key, project_id=project_id
68+
)
5469
return models, None
5570

5671

src/api/v1/search.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Provides semantic search functionality.
55
Uses API key authentication.
66
"""
7+
78
from typing import Any
89

910
from fastapi import Depends

src/api/v1/settings.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222

2323
logger = get_logger(__name__)
2424

25+
2526
class AgentSettings(BaseModel):
2627
llm_provider: str | None = None
2728
llm_model: str | None = None
2829
system_prompt: str | None = None
2930

31+
3032
class KnowledgeSettings(BaseModel):
3133
embedding_provider: str | None = None
3234
embedding_model: str | None = None
@@ -36,10 +38,12 @@ class KnowledgeSettings(BaseModel):
3638
ocr: bool | None = None
3739
picture_descriptions: bool | None = None
3840

41+
3942
class SettingsResponse(BaseModel):
4043
agent: AgentSettings
4144
knowledge: KnowledgeSettings
4245

46+
4347
async def get_settings_endpoint(
4448
user: User = Depends(get_api_key_user_async),
4549
) -> SettingsResponse:
@@ -76,4 +80,6 @@ async def update_settings_endpoint(
7680
"""Update OpenRAG configuration settings. POST /v1/settings"""
7781
from api.settings import update_settings
7882

79-
return await update_settings(body=body, session_manager=session_manager, user=user, models_service=models_service)
83+
return await update_settings(
84+
body=body, session_manager=session_manager, user=user, models_service=models_service
85+
)

0 commit comments

Comments
 (0)