Skip to content

Fix ollama support for Kodu when muxing #1022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/codegate/muxing/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,12 @@ def _format_ollama(self, chunk: str) -> str:
ollama_chunk = ChatResponse(**chunk_dict)
open_ai_chunk = OLlamaToModel.normalize_chat_chunk(ollama_chunk)
return open_ai_chunk.model_dump_json(exclude_none=True, exclude_unset=True)
except Exception:
except Exception as e:
# Sometimes we receive an OpenAI formatted chunk from ollama. Specifically when
# talking to Cline or Kodu. If that's the case we use the format_openai function.
if "data:" in chunk:
return self._format_openai(chunk)
logger.warning(f"Error formatting Ollama chunk: {chunk}. Error: {e}")
return chunk

def _format_antropic(self, chunk: str) -> str:
Expand Down
25 changes: 3 additions & 22 deletions src/codegate/providers/ollama/completion_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from codegate.clients.clients import ClientType
from codegate.providers.base import BaseCompletionHandler
from codegate.providers.ollama.adapter import OLlamaToModel

logger = structlog.get_logger("codegate")

Expand All @@ -24,29 +25,9 @@ async def ollama_stream_generator( # noqa: C901
# the correct format and start to handle multiple clients
# in a more robust way.
if client_type in [ClientType.CLINE, ClientType.KODU]:
# First get the raw dict from the chunk
chunk_dict = chunk.model_dump()
# Create response dictionary in OpenAI-like format
response = {
"id": f"chatcmpl-{chunk_dict.get('created_at', '')}",
"object": "chat.completion.chunk",
"created": chunk_dict.get("created_at"),
"model": chunk_dict.get("model"),
"choices": [
{
"index": 0,
"delta": {
"content": chunk_dict.get("message", {}).get("content", ""),
"role": chunk_dict.get("message", {}).get("role", "assistant"),
},
"finish_reason": (
chunk_dict.get("done_reason")
if chunk_dict.get("done", False)
else None
),
}
],
}
model_response = OLlamaToModel.normalize_chat_chunk(chunk)
response = model_response.model_dump()
# Preserve existing type or add default if missing
response["type"] = chunk_dict.get("type", "stream")

Expand Down