Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Commit df06cf1

Browse files
committed
Changed default values for base urls to just hostnames.
Also, moar fixes.
1 parent 17eae23 commit df06cf1

File tree

9 files changed

+40
-219
lines changed

9 files changed

+40
-219
lines changed

src/codegate/config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
# Default provider URLs
1818
DEFAULT_PROVIDER_URLS = {
19-
"openai": "https://api.openai.com/v1",
20-
"openrouter": "https://openrouter.ai/api/v1",
21-
"anthropic": "https://api.anthropic.com/v1",
19+
"openai": "https://api.openai.com",
20+
"openrouter": "https://openrouter.ai/api",
21+
"anthropic": "https://api.anthropic.com",
2222
"vllm": "http://localhost:8000", # Base URL without /v1 path
2323
"ollama": "http://localhost:11434", # Default Ollama server URL
2424
"lm_studio": "http://localhost:1234",

src/codegate/providers/anthropic/provider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __init__(
2828
if self._get_base_url() != "":
2929
self.base_url = self._get_base_url()
3030
else:
31-
self.base_url = "https://api.anthropic.com"
31+
self.base_url = "https://api.anthropic.com/v1"
3232

3333
completion_handler = AnthropicCompletion(stream_generator=stream_generator)
3434
super().__init__(

src/codegate/providers/ollama/adapter.py

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,12 @@
55

66
from codegate.providers.normalizer.base import ModelInputNormalizer, ModelOutputNormalizer
77
from codegate.types.common import (
8-
ChatCompletionRequest,
98
Delta,
109
ModelResponse,
1110
StreamingChoices,
1211
)
1312

1413

15-
class OllamaInputNormalizer(ModelInputNormalizer):
16-
17-
def normalize(self, data: Dict) -> ChatCompletionRequest:
18-
"""
19-
Normalize the input data to the format expected by Ollama.
20-
"""
21-
# Make a copy of the data to avoid modifying the original and normalize the message content
22-
normalized_data = self._normalize_content_messages(data)
23-
normalized_data["model"] = data.get("model", "").strip()
24-
normalized_data["options"] = data.get("options", {})
25-
26-
if "prompt" in normalized_data:
27-
normalized_data["messages"] = [
28-
{"content": normalized_data.pop("prompt"), "role": "user"}
29-
]
30-
31-
# if we have the stream flag in data we set it, otherwise defaults to true
32-
normalized_data["stream"] = data.get("stream", True)
33-
34-
# This would normally be the required to get the token usage.
35-
# However Ollama python client doesn't support it. We would be able to get the response
36-
# with a direct HTTP request. Since Ollama is local this is not critical.
37-
# if normalized_data.get("stream", False):
38-
# normalized_data["stream_options"] = {"include_usage": True}
39-
return ChatCompletionRequest(**normalized_data)
40-
41-
def denormalize(self, data: ChatCompletionRequest) -> Dict:
42-
"""
43-
Convert back to raw format for the API request
44-
"""
45-
return data
46-
47-
4814
class OLlamaToModel(AsyncIterator[ModelResponse]):
4915
def __init__(self, ollama_response: AsyncIterator[ChatResponse]):
5016
self.ollama_response = ollama_response
@@ -106,7 +72,7 @@ def normalize_chat_chunk(cls, chunk: ChatResponse) -> ModelResponse:
10672
return model_response
10773

10874
@classmethod
109-
def normalize_fim_chunk(cls, chunk: GenerateResponse) -> Dict:
75+
def normalize_fim_chunk(cls, chunk) -> Dict:
11076
"""
11177
Transform an ollama generation chunk to an OpenAI one
11278
"""
@@ -174,37 +140,3 @@ async def __anext__(self) -> Union[ChatResponse]:
174140
return ollama_response
175141
except StopAsyncIteration:
176142
raise StopAsyncIteration
177-
178-
179-
class OllamaOutputNormalizer(ModelOutputNormalizer):
180-
def __init__(self):
181-
super().__init__()
182-
183-
def normalize_streaming(
184-
self,
185-
model_reply: AsyncIterator[ChatResponse],
186-
) -> AsyncIterator[ModelResponse]:
187-
"""
188-
Pass through Ollama response
189-
"""
190-
return OLlamaToModel(model_reply)
191-
192-
def normalize(self, model_reply: Any) -> Any:
193-
"""
194-
Pass through Ollama response
195-
"""
196-
return model_reply
197-
198-
def denormalize(self, normalized_reply: Any) -> Any:
199-
"""
200-
Pass through Ollama response
201-
"""
202-
return normalized_reply
203-
204-
def denormalize_streaming(
205-
self, normalized_reply: AsyncIterator[ModelResponse]
206-
) -> AsyncIterator[ChatResponse]:
207-
"""
208-
Pass through Ollama response
209-
"""
210-
return ModelToOllama(normalized_reply)

src/codegate/providers/ollama/completion_handler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ async def execute_completion(
7979
) -> Union[ChatResponse, GenerateResponse]:
8080
"""Stream response directly from Ollama API."""
8181
if isinstance(request, ChatCompletionRequest): # case for OpenAI-style requests
82-
return await completions_streaming(request, api_key, base_url)
82+
return completions_streaming(request, api_key, base_url)
8383
if is_fim_request:
84-
return await generate_streaming(request, api_key, base_url)
85-
return await chat_streaming(request, api_key, base_url)
84+
return generate_streaming(request, api_key, base_url)
85+
return chat_streaming(request, api_key, base_url)
8686

8787
def _create_streaming_response(
8888
self,

src/codegate/providers/ollama/provider.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from codegate.pipeline.factory import PipelineFactory
1212
from codegate.providers.base import BaseProvider, ModelFetchError
1313
from codegate.providers.fim_analyzer import FIMAnalyzer
14-
from codegate.providers.ollama.adapter import OllamaInputNormalizer, OllamaOutputNormalizer
1514
from codegate.providers.ollama.completion_handler import OllamaShim
1615
from codegate.types.openai import ChatCompletionRequest
1716
from codegate.types.ollama import ChatRequest, GenerateRequest

src/codegate/providers/openrouter/provider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def __init__(self, pipeline_factory: PipelineFactory):
1919
if self._get_base_url() != "":
2020
self.base_url = self._get_base_url()
2121
else:
22-
self.base_url = "https://openrouter.ai/api/v1"
22+
self.base_url = "https://openrouter.ai/api"
2323

2424
@property
2525
def provider_route_name(self) -> str:

src/codegate/types/ollama/_generators.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@ async def stream_generator(
4242
async def chat_streaming(request, api_key, base_url):
4343
if base_url is None:
4444
base_url = "http://localhost:11434"
45-
return streaming(request, api_key, f"{base_url}/api/chat", StreamingChatCompletion)
45+
async for item in streaming(request, api_key, f"{base_url}/api/chat", StreamingChatCompletion):
46+
yield item
4647

4748

4849
async def generate_streaming(request, api_key, base_url):
4950
if base_url is None:
5051
base_url = "http://localhost:11434"
51-
return streaming(request, api_key, f"{base_url}/api/generate", StreamingGenerateCompletion)
52+
async for item in streaming(request, api_key, f"{base_url}/api/generate", StreamingGenerateCompletion):
53+
yield item
5254

5355

5456
async def streaming(request, api_key, url, cls):
@@ -90,7 +92,18 @@ async def get_data_lines(lines):
9092
async def message_wrapper(cls, lines):
9193
messages = get_data_lines(lines)
9294
async for payload in messages:
93-
item = cls.model_validate_json(payload)
94-
yield item
95-
if item.done:
96-
break
95+
try:
96+
item = cls.model_validate_json(payload)
97+
yield item
98+
if item.done:
99+
break
100+
except Exception as e:
101+
logger.warn("HTTP error while consuming SSE stream", payload=payload, exc_info=e)
102+
err = MessageError(
103+
error=ErrorDetails(
104+
message=str(e),
105+
code=500,
106+
),
107+
)
108+
item = MessageError.model_validate_json(payload)
109+
yield item

src/codegate/types/openai/_generators.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ async def stream_generator(stream: AsyncIterator[StreamingChatCompletion]) -> As
5050
async def completions_streaming(request, api_key, base_url):
5151
if base_url is None:
5252
base_url = "https://api.openai.com"
53-
async for item in streaming(request, api_key, f"{base_url}/chat/completions"):
53+
async for item in streaming(request, api_key, f"{base_url}/v1/chat/completions"):
5454
yield item
5555

5656

@@ -91,6 +91,8 @@ async def get_data_lines(lines):
9191
while True:
9292
# Get the `data: <type>` line.
9393
data_line = await anext(lines)
94+
# Get the empty line.
95+
_ = await anext(lines)
9496

9597
# As per standard, we ignore comment lines
9698
# https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation
@@ -103,9 +105,6 @@ async def get_data_lines(lines):
103105
break
104106

105107
yield data_line[6:]
106-
107-
# Get the empty line.
108-
_ = await anext(lines)
109108
logger.debug(f"Consumed {count} messages", provider="openai", count=count)
110109

111110

@@ -116,6 +115,12 @@ async def message_wrapper(lines):
116115
item = StreamingChatCompletion.model_validate_json(payload)
117116
yield item
118117
except Exception as e:
119-
logger.warn("HTTP error while consuming SSE stream", exc_info=e)
120-
item = MessageError.model_validate_json(payload)
121-
yield item
118+
print(f"WAAAGH {payload}")
119+
logger.warn("HTTP error while consuming SSE stream", payload=payload, exc_info=e)
120+
err = MessageError(
121+
error=ErrorDetails(
122+
message=str(e),
123+
code=500,
124+
),
125+
)
126+
yield err

tests/providers/ollama/test_ollama_adapter.py

Lines changed: 0 additions & 128 deletions
This file was deleted.

0 commit comments

Comments
 (0)