kevinkal commited on
Commit
17a1d3a
·
verified ·
1 Parent(s): 919b64e

inshallah3

Browse files
Files changed (1) hide show
  1. routers/mistral.py +21 -20
routers/mistral.py CHANGED
@@ -6,6 +6,7 @@ from mistralai.client import Mistral
6
  from auth import verify_token
7
  import os
8
  from schemas.common import APIResponse, ChatMessage, ChatMemoryRequest # Import common schemas
 
9
  import asyncio
10
  import time
11
  from functools import wraps
@@ -134,32 +135,32 @@ async def mistral_chat(request: LLMRequest, token: Annotated[str, Depends(verify
134
  print(f"Error calling Mistral chat completion: {e}")
135
  raise HTTPException(status_code=500, detail=f"Mistral API error: {str(e)}")
136
 
137
- @router.post("/chat-with-memory", response_model=APIResponse)
138
- async def mistral_chat_with_memory(request: ChatMemoryRequest, token: Annotated[str, Depends(verify_token)]):
 
 
 
 
 
 
 
139
  try:
140
- async def chat_with_memory_call(client, request):
141
- # Convert Pydantic models to dicts for the Mistral API call
142
- messages_dict = [msg.model_dump() for msg in request.messages]
143
-
144
- return await client.chat.complete_async(
145
- model=request.model,
146
- messages=messages_dict,
147
- )
148
-
149
- response = await mistral_manager.rate_limited_call(chat_with_memory_call, request)
150
-
151
- if response.choices and response.choices[0].message:
152
- content = response.choices[0].message.content
153
- return APIResponse(success=True, data={"response": content})
154
  else:
155
- return APIResponse(success=False, error="No response content received from Mistral.", data=response.dict())
156
-
 
 
 
 
157
  except HTTPException:
158
  # Re-raise HTTP exceptions (like timeout)
159
  raise
160
  except Exception as e:
161
- print(f"Error calling Mistral chat completion with memory: {e}")
162
- raise HTTPException(status_code=500, detail=f"Mistral API error: {str(e)}")
163
 
164
  @router.get("/health", response_model=APIResponse)
165
  async def mistral_health(token: Annotated[str, Depends(verify_token)]):
 
6
  from auth import verify_token
7
  import os
8
  from schemas.common import APIResponse, ChatMessage, ChatMemoryRequest # Import common schemas
9
+ from routers.gemini import generate_gemini_chat_response, GEMINI_DEFAULT_CHAT_MODEL, ALLOWED_CHAT_MODELS
10
  import asyncio
11
  import time
12
  from functools import wraps
 
135
  print(f"Error calling Mistral chat completion: {e}")
136
  raise HTTPException(status_code=500, detail=f"Mistral API error: {str(e)}")
137
 
138
+ @router.post("/chat-with-memory", response_model=APIResponse, deprecated=True)
139
+ async def mistral_chat_with_memory(request: ChatMemoryRequest, token: Annotated[str, Depends(verify_token)]):
140
+ """Backward-compatible alias rerouted to Gemini (Mistral is rate-limited).
141
+
142
+ Keeps the same path/schema so existing clients don't change.
143
+ New clients should use POST /gemini/chat-with-memory directly.
144
+ Mistral model names are mapped to GEMINI_CHAT_MODEL (default gemini-flash-latest);
145
+ a request model matching one of ALLOWED_CHAT_MODELS is passed through.
146
+ """
147
  try:
148
+ requested = (request.model or "").strip()
149
+ if requested in ALLOWED_CHAT_MODELS:
150
+ model = requested
 
 
 
 
 
 
 
 
 
 
 
151
  else:
152
+ if requested:
153
+ print(f"Mistral model '{requested}' rerouted to Gemini model '{GEMINI_DEFAULT_CHAT_MODEL}'")
154
+ model = GEMINI_DEFAULT_CHAT_MODEL
155
+ text = await asyncio.to_thread(generate_gemini_chat_response, model, request.messages)
156
+ return APIResponse(success=True, data={"response": text})
157
+
158
  except HTTPException:
159
  # Re-raise HTTP exceptions (like timeout)
160
  raise
161
  except Exception as e:
162
+ print(f"Error calling rerouted (Gemini) chat completion with memory: {e}")
163
+ raise HTTPException(status_code=500, detail=f"Chat API error: {str(e)}")
164
 
165
  @router.get("/health", response_model=APIResponse)
166
  async def mistral_health(token: Annotated[str, Depends(verify_token)]):