Skip to content

fix: correct problems with ollama and aider #596

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
Jan 16, 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
24 changes: 14 additions & 10 deletions src/codegate/providers/ollama/completion_handler.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from typing import AsyncIterator, Optional, Union

import structlog
Expand All @@ -11,18 +12,21 @@


async def ollama_stream_generator(
stream: AsyncIterator[ChatResponse],
stream: AsyncIterator[ChatResponse]
) -> AsyncIterator[str]:
"""OpenAI-style SSE format"""
try:
async for chunk in stream:
print(chunk)
try:
yield f"{chunk.model_dump_json()}\n\n"
content = chunk.model_dump_json()
if content:
yield f"{chunk.model_dump_json()}\n"
except Exception as e:
yield f"{str(e)}\n\n"
if str(e):
yield f"{str(e)}\n"
except Exception as e:
yield f"{str(e)}\n\n"
if str(e):
yield f"{str(e)}\n"


class OllamaShim(BaseCompletionHandler):
Expand All @@ -39,17 +43,17 @@ async def execute_completion(
) -> Union[ChatResponse, GenerateResponse]:
"""Stream response directly from Ollama API."""
if is_fim_request:
prompt = request["messages"][0]["content"]
prompt = request["messages"][0].get("content", "")
response = await self.client.generate(
model=request["model"], prompt=prompt, stream=stream, options=request["options"]
model=request["model"], prompt=prompt, stream=stream, options=request["options"] # type: ignore
)
else:
response = await self.client.chat(
model=request["model"],
messages=request["messages"],
stream=stream,
options=request["options"],
)
stream=stream, # type: ignore
options=request["options"], # type: ignore
) # type: ignore
return response

def _create_streaming_response(self, stream: AsyncIterator[ChatResponse]) -> StreamingResponse:
Expand Down
3 changes: 3 additions & 0 deletions src/codegate/providers/ollama/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ async def show_model(request: Request):
https://github.com/ollama/ollama/blob/main/docs/api.md#show-model-information
"""
body = await request.body()
body_json = json.loads(body)
if "name" not in body_json:
raise HTTPException(status_code=400, detail="model is required in the request body")
async with httpx.AsyncClient() as client:
response = await client.post(
f"{self.base_url}/api/show",
Expand Down
Loading