Skip to content

Commit 4f68583

Browse files
committed
fix: do not assume streaming for ollama
Aider is sending a mix of requests that need stream or sync processing and we were hardcoding stream to True. Instead, pick if from the request and also fix the formatting of the json response Closes: #624
1 parent dceeef8 commit 4f68583

File tree

3 files changed

+7
-8
lines changed

3 files changed

+7
-8
lines changed

src/codegate/providers/ollama/adapter.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ def normalize(self, data: Dict) -> ChatCompletionRequest:
2525
{"content": normalized_data.pop("prompt"), "role": "user"}
2626
]
2727

28-
# In Ollama force the stream to be True. Continue is not setting this parameter and
29-
# most of our functionality is for streaming completions.
30-
normalized_data["stream"] = True
31-
28+
# if we have the stream flag in data we set it, otherwise defaults to true
29+
normalized_data["stream"] = data.get("stream", True)
3230
return ChatCompletionRequest(**normalized_data)
3331

3432
def denormalize(self, data: ChatCompletionRequest) -> Dict:

src/codegate/providers/ollama/completion_handler.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def _create_streaming_response(self, stream: AsyncIterator[ChatResponse]) -> Str
6060
"""
6161
return StreamingResponse(
6262
ollama_stream_generator(stream),
63-
media_type="application/x-ndjson",
63+
media_type="application/x-ndjson; charset=utf-8",
6464
headers={
6565
"Cache-Control": "no-cache",
6666
"Connection": "keep-alive",
@@ -70,4 +70,4 @@ def _create_streaming_response(self, stream: AsyncIterator[ChatResponse]) -> Str
7070
def _create_json_response(
7171
self, response: Union[GenerateResponse, ChatResponse]
7272
) -> JSONResponse:
73-
return JSONResponse(content=response.model_dump_json(), status_code=200)
73+
return JSONResponse(status_code=200, content=response.model_dump())

src/codegate/providers/ollama/provider.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ async def show_model(request: Request):
6565
response = await client.post(
6666
f"{self.base_url}/api/show",
6767
content=body,
68-
headers={"Content-Type": "application/json"},
68+
headers={"Content-Type": "application/json; charset=utf-8"},
6969
)
7070
return response.json()
7171

@@ -84,7 +84,7 @@ async def create_completion(request: Request):
8484

8585
is_fim_request = self._is_fim_request(request, data)
8686
try:
87-
stream = await self.complete(data, api_key=None, is_fim_request=is_fim_request)
87+
stream = await self.complete(data, api_key=None, is_fim_request=is_fim_request)
8888
except httpx.ConnectError as e:
8989
logger = structlog.get_logger("codegate")
9090
logger.error("Error in OllamaProvider completion", error=str(e))
@@ -99,4 +99,5 @@ async def create_completion(request: Request):
9999
else:
100100
# just continue raising the exception
101101
raise e
102+
102103
return self._completion_handler.create_response(stream)

0 commit comments

Comments
 (0)