Skip to content

Fix codegate version and similar commands. #1392

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions src/codegate/providers/anthropic/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ async def process_request(
client_type: ClientType,
completion_handler: Callable | None = None,
stream_generator: Callable | None = None,
short_circuiter: Callable | None = None,
):
try:
stream = await self.complete(
Expand All @@ -86,6 +87,7 @@ async def process_request(
is_fim_request,
client_type,
completion_handler=completion_handler,
short_circuiter=short_circuiter,
)
except Exception as e:
# check if we have an status code there
Expand Down
5 changes: 5 additions & 0 deletions src/codegate/providers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ async def complete(
is_fim_request: bool,
client_type: ClientType,
completion_handler: Callable | None = None,
short_circuiter: Callable | None = None,
) -> Union[Any, AsyncIterator[Any]]:
"""
Main completion flow with pipeline integration
Expand Down Expand Up @@ -287,6 +288,10 @@ async def complete(
is_fim_request,
)

if input_pipeline_result.response and input_pipeline_result.context:
if short_circuiter: # this if should be removed eventually
return short_circuiter(input_pipeline_result)

provider_request = normalized_request # default value
if input_pipeline_result.request:
provider_request = self._input_normalizer.denormalize(input_pipeline_result.request)
Expand Down
4 changes: 4 additions & 0 deletions src/codegate/providers/openai/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from codegate.types.openai import (
ChatCompletionRequest,
completions_streaming,
short_circuiter,
stream_generator,
)

Expand Down Expand Up @@ -72,6 +73,7 @@ async def process_request(
client_type: ClientType,
completion_handler: Callable | None = None,
stream_generator: Callable | None = None,
short_circuiter: Callable | None = None,
):
try:
stream = await self.complete(
Expand All @@ -81,6 +83,7 @@ async def process_request(
is_fim_request=is_fim_request,
client_type=client_type,
completion_handler=completion_handler,
short_circuiter=short_circuiter,
)
except Exception as e:
# Check if we have an status code there
Expand Down Expand Up @@ -130,4 +133,5 @@ async def create_completion(
self.base_url,
is_fim_request,
request.state.detected_client,
short_circuiter=short_circuiter,
)
2 changes: 2 additions & 0 deletions src/codegate/types/openai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from ._generators import (
completions_streaming,
message_wrapper,
short_circuiter,
single_response_generator,
stream_generator,
streaming,
Expand Down Expand Up @@ -74,6 +75,7 @@
"completions_streaming",
"message_wrapper",
"single_response_generator",
"short_circuiter",
"stream_generator",
"streaming",
"LegacyCompletion",
Expand Down
51 changes: 51 additions & 0 deletions src/codegate/types/openai/_generators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
import time
from typing import (
Any,
AsyncIterator,
)

Expand All @@ -9,9 +11,16 @@
from ._legacy_models import (
LegacyCompletionRequest,
)
from ._request_models import (
ChatCompletionRequest,
)
from ._response_models import (
ChatCompletion,
Choice,
ChoiceDelta,
ErrorDetails,
Message,
MessageDelta,
MessageError,
StreamingChatCompletion,
VllmMessageError,
Expand All @@ -20,6 +29,48 @@
logger = structlog.get_logger("codegate")


async def short_circuiter(pipeline_result) -> AsyncIterator[Any]:
# NOTE: This routine MUST be called only when we short-circuit the
# request.
assert pipeline_result.context.shortcut_response # nosec

match pipeline_result.context.input_request.request:
case ChatCompletionRequest(stream=True):
yield StreamingChatCompletion(
id="codegate",
model=pipeline_result.response.model,
created=int(time.time()),
choices=[
ChoiceDelta(
finish_reason="stop",
index=0,
delta=MessageDelta(
content=pipeline_result.response.content,
),
),
],
)
case ChatCompletionRequest(stream=False):
yield ChatCompletion(
id="codegate",
model=pipeline_result.response.model,
created=int(time.time()),
choices=[
Choice(
finish_reason="stop",
index=0,
message=Message(
content=pipeline_result.response.content,
),
),
],
)
case _:
raise ValueError(
f"invalid input request: {pipeline_result.context.input_request.request}"
)


async def stream_generator(stream: AsyncIterator[StreamingChatCompletion]) -> AsyncIterator[str]:
"""OpenAI-style SSE format"""
try:
Expand Down
2 changes: 1 addition & 1 deletion src/codegate/updates/client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
from enum import Enum

import requests
import structlog
import os

logger = structlog.get_logger("codegate")

Expand Down
Loading