Skip to content

Commit e6600f6

Browse files
Fix ollama support for Kodu when muxing (#1022)
Muixing was failing for 2 reasons: 1. Sometimes we return OpenAI format from ollama provider. Before we were assuming that everything that was returned from ollama provider had ollama format. 2. The OpenAI format returned from ollama provider had an invalid `created` field.
1 parent 58512fd commit e6600f6

File tree

2 files changed

+9
-23
lines changed

2 files changed

+9
-23
lines changed

src/codegate/muxing/adapter.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,12 @@ def _format_ollama(self, chunk: str) -> str:
158158
ollama_chunk = ChatResponse(**chunk_dict)
159159
open_ai_chunk = OLlamaToModel.normalize_chat_chunk(ollama_chunk)
160160
return open_ai_chunk.model_dump_json(exclude_none=True, exclude_unset=True)
161-
except Exception:
161+
except Exception as e:
162+
# Sometimes we receive an OpenAI formatted chunk from ollama. Specifically when
163+
# talking to Cline or Kodu. If that's the case we use the format_openai function.
164+
if "data:" in chunk:
165+
return self._format_openai(chunk)
166+
logger.warning(f"Error formatting Ollama chunk: {chunk}. Error: {e}")
162167
return chunk
163168

164169
def _format_antropic(self, chunk: str) -> str:

src/codegate/providers/ollama/completion_handler.py

+3-22
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from codegate.clients.clients import ClientType
1010
from codegate.providers.base import BaseCompletionHandler
11+
from codegate.providers.ollama.adapter import OLlamaToModel
1112

1213
logger = structlog.get_logger("codegate")
1314

@@ -24,29 +25,9 @@ async def ollama_stream_generator( # noqa: C901
2425
# the correct format and start to handle multiple clients
2526
# in a more robust way.
2627
if client_type in [ClientType.CLINE, ClientType.KODU]:
27-
# First get the raw dict from the chunk
2828
chunk_dict = chunk.model_dump()
29-
# Create response dictionary in OpenAI-like format
30-
response = {
31-
"id": f"chatcmpl-{chunk_dict.get('created_at', '')}",
32-
"object": "chat.completion.chunk",
33-
"created": chunk_dict.get("created_at"),
34-
"model": chunk_dict.get("model"),
35-
"choices": [
36-
{
37-
"index": 0,
38-
"delta": {
39-
"content": chunk_dict.get("message", {}).get("content", ""),
40-
"role": chunk_dict.get("message", {}).get("role", "assistant"),
41-
},
42-
"finish_reason": (
43-
chunk_dict.get("done_reason")
44-
if chunk_dict.get("done", False)
45-
else None
46-
),
47-
}
48-
],
49-
}
29+
model_response = OLlamaToModel.normalize_chat_chunk(chunk)
30+
response = model_response.model_dump()
5031
# Preserve existing type or add default if missing
5132
response["type"] = chunk_dict.get("type", "stream")
5233

0 commit comments

Comments
 (0)